featured.php
8.27 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @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;
use Joomla\Utilities\ArrayHelper;
JLoader::register('ContentModelArticles', __DIR__ . '/articles.php');
/**
* Methods supporting a list of featured article records.
*
* @since 1.6
*/
class ContentModelFeatured extends ContentModelArticles
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JControllerLegacy
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id', 'a.id',
'title', 'a.title',
'alias', 'a.alias',
'checked_out', 'a.checked_out',
'checked_out_time', 'a.checked_out_time',
'catid', 'a.catid', 'category_title',
'state', 'a.state',
'access', 'a.access', 'access_level',
'created', 'a.created',
'created_by', 'a.created_by',
'created_by_alias', 'a.created_by_alias',
'ordering', 'a.ordering',
'featured', 'a.featured',
'language', 'a.language',
'hits', 'a.hits',
'publish_up', 'a.publish_up',
'publish_down', 'a.publish_down',
'fp.ordering',
'published', 'a.published',
'author_id',
'category_id',
'level',
'tag',
'rating_count', 'rating',
);
}
parent::__construct($config);
}
/**
* Build an SQL query to load the list data.
*
* @return JDatabaseQuery
*
* @since 1.6
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
$user = JFactory::getUser();
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid, a.state, a.access, a.created, a.hits,' .
'a.created_by, a.featured, a.language, a.created_by_alias, a.publish_up, a.publish_down, a.note'
)
);
$query->from('#__content AS a');
// Join over the language
$query->select('l.title AS language_title, l.image AS language_image')
->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
// Join over the content table.
$query->select('fp.ordering')
->join('INNER', '#__content_frontpage AS fp ON fp.content_id = a.id');
// Join over the users for the checked out user.
$query->select('uc.name AS editor')
->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
// Join over the asset groups.
$query->select('ag.title AS access_level')
->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
// Join over the categories.
$query->select('c.title AS category_title, c.created_user_id AS category_uid, c.level AS category_level')
->join('LEFT', '#__categories AS c ON c.id = a.catid');
// Join over the parent categories.
$query->select('parent.title AS parent_category_title, parent.id AS parent_category_id,
parent.created_user_id AS parent_category_uid, parent.level AS parent_category_level')
->join('LEFT', '#__categories AS parent ON parent.id = c.parent_id');
// Join over the users for the author.
$query->select('ua.name AS author_name')
->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
// Join on voting table
if (JPluginHelper::isEnabled('content', 'vote'))
{
$query->select('COALESCE(NULLIF(ROUND(v.rating_sum / v.rating_count, 0), 0), 0) AS rating,
COALESCE(NULLIF(v.rating_count, 0), 0) as rating_count')
->join('LEFT', '#__content_rating AS v ON a.id = v.content_id');
}
// Filter by access level.
$access = $this->getState('filter.access');
if (is_numeric($access))
{
$query->where('a.access = ' . (int) $access);
}
elseif (is_array($access))
{
$access = ArrayHelper::toInteger($access);
$access = implode(',', $access);
$query->where('a.access IN (' . $access . ')');
}
// Filter by access level on categories.
if (!$user->authorise('core.admin'))
{
$groups = implode(',', $user->getAuthorisedViewLevels());
$query->where('a.access IN (' . $groups . ')');
$query->where('c.access IN (' . $groups . ')');
}
// Filter by published state
$published = $this->getState('filter.published');
if (is_numeric($published))
{
$query->where('a.state = ' . (int) $published);
}
elseif ($published === '')
{
$query->where('(a.state = 0 OR a.state = 1)');
}
// Filter by a single or group of categories.
$baselevel = 1;
$categoryId = $this->getState('filter.category_id');
if (is_array($categoryId) && count($categoryId) === 1)
{
$cat_tbl = JTable::getInstance('Category', 'JTable');
$cat_tbl->load($categoryId[0]);
$rgt = $cat_tbl->rgt;
$lft = $cat_tbl->lft;
$baselevel = (int) $cat_tbl->level;
$query->where('c.lft >= ' . (int) $lft)
->where('c.rgt <= ' . (int) $rgt);
}
elseif (is_array($categoryId))
{
$categoryId = implode(',', ArrayHelper::toInteger($categoryId));
$query->where('a.catid IN (' . $categoryId . ')');
}
// Filter on the level.
if ($level = $this->getState('filter.level'))
{
$query->where('c.level <= ' . ((int) $level + (int) $baselevel - 1));
}
// Filter by author
$authorId = $this->getState('filter.author_id');
if (is_numeric($authorId))
{
$type = $this->getState('filter.author_id.include', true) ? '= ' : '<>';
$query->where('a.created_by ' . $type . (int) $authorId);
}
elseif (is_array($authorId))
{
$authorId = ArrayHelper::toInteger($authorId);
$authorId = implode(',', $authorId);
$query->where('a.created_by IN (' . $authorId . ')');
}
// Filter by search in title.
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
elseif (stripos($search, 'author:') === 0)
{
$search = $db->quote('%' . $db->escape(substr($search, 7), true) . '%');
$query->where('(ua.name LIKE ' . $search . ' OR ua.username LIKE ' . $search . ')');
}
elseif (stripos($search, 'content:') === 0)
{
$search = $db->quote('%' . $db->escape(substr($search, 8), true) . '%');
$query->where('(a.introtext LIKE ' . $search . ' OR a.fulltext LIKE ' . $search . ')');
}
else
{
$search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
$query->where('(a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ' OR a.note LIKE ' . $search . ')');
}
}
// Filter on the language.
if ($language = $this->getState('filter.language'))
{
$query->where('a.language = ' . $db->quote($language));
}
// Filter by a single or group of tags.
$tagId = $this->getState('filter.tag');
if (is_array($tagId) && count($tagId) === 1)
{
$tagId = current($tagId);
}
if (is_array($tagId))
{
$tagId = implode(',', ArrayHelper::toInteger($tagId));
if ($tagId)
{
$subQuery = $db->getQuery(true)
->select('DISTINCT content_item_id')
->from($db->quoteName('#__contentitem_tag_map'))
->where('tag_id IN (' . $tagId . ')')
->where('type_alias = ' . $db->quote('com_content.article'));
$query->join('INNER', '(' . (string) $subQuery . ') AS tagmap ON tagmap.content_item_id = a.id');
}
}
elseif ($tagId)
{
$query->join(
'INNER',
$db->quoteName('#__contentitem_tag_map', 'tagmap')
. ' ON tagmap.tag_id = ' . (int) $tagId
. ' AND tagmap.content_item_id = a.id'
. ' AND tagmap.type_alias = ' . $db->quote('com_content.article')
);
}
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering', 'a.title');
$orderDirn = $this->state->get('list.direction', 'ASC');
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
return $query;
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 3.5
*/
protected function populateState($ordering = 'a.title', $direction = 'asc')
{
parent::populateState($ordering, $direction);
}
}