helper.php
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* @package Joomla.Administrator
* @subpackage mod_latest
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_content/models', 'ContentModel');
/**
* Helper for mod_latest
*
* @since 1.5
*/
abstract class ModLatestHelper
{
/**
* Get a list of articles.
*
* @param \Joomla\Registry\Registry &$params The module parameters.
*
* @return mixed An array of articles, or false on error.
*/
public static function getList(&$params)
{
$user = JFactory::getuser();
// Get an instance of the generic articles model
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
// Set List SELECT
$model->setState('list.select', 'a.id, a.title, a.checked_out, a.checked_out_time, ' .
' a.access, a.created, a.created_by, a.created_by_alias, a.featured, a.state, a.publish_up, a.publish_down');
// Set Ordering filter
switch ($params->get('ordering', 'c_dsc'))
{
case 'm_dsc':
$model->setState('list.ordering', 'modified DESC, created');
$model->setState('list.direction', 'DESC');
break;
case 'c_dsc':
default:
$model->setState('list.ordering', 'created');
$model->setState('list.direction', 'DESC');
break;
}
// Set Category Filter
$categoryId = $params->get('catid', null);
if (is_numeric($categoryId))
{
$model->setState('filter.category_id', $categoryId);
}
// Set User Filter.
$userId = $user->get('id');
switch ($params->get('user_id', '0'))
{
case 'by_me':
$model->setState('filter.author_id', $userId);
break;
case 'not_me':
$model->setState('filter.author_id', $userId);
$model->setState('filter.author_id.include', false);
break;
}
// Set the Start and Limit
$model->setState('list.start', 0);
$model->setState('list.limit', $params->get('count', 5));
$items = $model->getItems();
if ($error = $model->getError())
{
JError::raiseError(500, $error);
return false;
}
// Set the links
foreach ($items as &$item)
{
if ($user->authorise('core.edit', 'com_content.article.' . $item->id))
{
$item->link = JRoute::_('index.php?option=com_content&task=article.edit&id=' . $item->id);
}
else
{
$item->link = '';
}
}
return $items;
}
/**
* Get the alternate title for the module.
*
* @param \Joomla\Registry\Registry $params The module parameters.
*
* @return string The alternate title for the module.
*/
public static function getTitle($params)
{
$who = $params->get('user_id', '0');
$catid = (int) $params->get('catid', null);
$type = $params->get('ordering', 'c_dsc') == 'c_dsc' ? '_CREATED' : '_MODIFIED';
if ($catid)
{
$category = JCategories::getInstance('Content')->get($catid);
if ($category)
{
$title = $category->title;
}
else
{
$title = JText::_('MOD_POPULAR_UNEXISTING');
}
}
else
{
$title = '';
}
return JText::plural(
'MOD_LATEST_TITLE' . $type . ($catid ? '_CATEGORY' : '') . ($who != '0' ? "_$who" : ''),
(int) $params->get('count', 5),
$title
);
}
}