forked from joomla/joomla-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoryModel.php
More file actions
544 lines (452 loc) · 17.1 KB
/
CategoryModel.php
File metadata and controls
544 lines (452 loc) · 17.1 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
/**
* @package Joomla.Site
* @subpackage com_content
*
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Content\Site\Model;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Categories\CategoryNode;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Table\Table;
use Joomla\Component\Content\Site\Helper\QueryHelper;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* This models supports retrieving a category, the articles associated with the category,
* sibling, child and parent categories.
*
* @since 1.5
*/
class CategoryModel extends ListModel
{
/**
* Category items data
*
* @var array
*/
protected $_item = null;
/**
* Array of articles in the category
*
* @var \stdClass[]
*/
protected $_articles = null;
/**
* Category left and right of this one
*
* @var CategoryNode[]|null
*/
protected $_siblings = null;
/**
* Array of child-categories
*
* @var CategoryNode[]|null
*/
protected $_children = null;
/**
* Parent category of the current one
*
* @var CategoryNode|null
*/
protected $_parent = null;
/**
* Model context string.
*
* @var string
*/
protected $_context = 'com_content.category';
/**
* The category that applies.
*
* @var object
*/
protected $_category = null;
/**
* The list of categories.
*
* @var array
*/
protected $_categories = null;
/**
* @param array $config An optional associative array of configuration settings.
* @param ?MVCFactoryInterface $factory The factory.
*
* @since 1.6
*/
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = [
'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',
'modified', 'a.modified',
'ordering', 'a.ordering',
'featured', 'a.featured',
'language', 'a.language',
'hits', 'a.hits',
'publish_up', 'a.publish_up',
'publish_down', 'a.publish_down',
'author', 'a.author',
'filter_tag',
];
}
parent::__construct($config, $factory);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering The field to order on.
* @param string $direction The direction to order on.
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = null, $direction = null)
{
$app = Factory::getApplication();
$pk = $app->getInput()->getInt('id');
$this->setState('category.id', $pk);
// Load the parameters. Merge Global and Menu Item params into new object
$params = $app->getParams();
$this->setState('params', $params);
$user = $this->getCurrentUser();
$asset = 'com_content';
if ($pk) {
$asset .= '.category.' . $pk;
}
if ((!$user->authorise('core.edit.state', $asset)) && (!$user->authorise('core.edit', $asset))) {
// Limit to published for people who can't edit or edit.state.
$this->setState('filter.published', 1);
} else {
$this->setState('filter.published', [0, 1]);
}
// Process show_noauth parameter
if (!$params->get('show_noauth')) {
$this->setState('filter.access', true);
} else {
$this->setState('filter.access', false);
}
$itemid = $app->getInput()->get('id', 0, 'int') . ':' . $app->getInput()->get('Itemid', 0, 'int');
$value = $this->getUserStateFromRequest('com_content.category.filter.' . $itemid . '.tag', 'filter_tag', 0, 'int', false);
$this->setState('filter.tag', $value);
// Optional filter text
$search = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter-search', 'filter-search', '', 'string');
$this->setState('list.filter', $search);
// Filter.order
$orderCol = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
if (!\in_array($orderCol, $this->filter_fields)) {
$orderCol = 'a.ordering';
}
$this->setState('list.ordering', $orderCol);
$listOrder = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
if (!\in_array(strtoupper($listOrder), ['ASC', 'DESC', ''])) {
$listOrder = 'ASC';
}
$this->setState('list.direction', $listOrder);
$this->setState('list.start', $app->getInput()->get('limitstart', 0, 'uint'));
// Set limit for query. If list, use parameter. If blog, add blog parameters for limit.
if (($app->getInput()->get('layout') === 'blog') || $params->get('layout_type') === 'blog') {
$limit = $params->get('num_leading_articles') + $params->get('num_intro_articles') + $params->get('num_links');
$this->setState('list.links', $params->get('num_links'));
} else {
$limit = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.limit', 'limit', $params->get('display_num'), 'uint');
}
$this->setState('list.limit', $limit);
// Set the depth of the category query based on parameter
$showSubcategories = $params->get('show_subcategory_content', '0');
if ($showSubcategories) {
$this->setState('filter.max_category_levels', $params->get('show_subcategory_content', '1'));
$this->setState('filter.subcategories', true);
}
$this->setState('filter.language', Multilanguage::isEnabled());
$this->setState('layout', $app->getInput()->getString('layout'));
// Set the featured articles state
$this->setState('filter.featured', $params->get('show_featured'));
}
/**
* Get the articles in the category
*
* @return array|bool An array of articles or false if an error occurs.
*
* @since 1.5
*/
public function getItems()
{
$limit = $this->getState('list.limit');
if ($this->_articles === null && $category = $this->getCategory()) {
/**
* Special case for blog layout with limit 0 - don't load articles for performance reasons. We also need to
* create an empty pagination object to avoid fatal errors in the view.
*/
if ($limit == 0 && $this->getState('view.layout') === 'blog') {
$this->_articles = [];
$this->_pagination = new Pagination(0, 0, 0);
return $this->_articles;
}
$model = $this->bootComponent('com_content')->getMVCFactory()
->createModel('Articles', 'Site', ['ignore_request' => true]);
$model->setState('params', Factory::getApplication()->getParams());
$model->setState('filter.category_id', $category->id);
$model->setState('filter.published', $this->getState('filter.published'));
$model->setState('filter.access', $this->getState('filter.access'));
$model->setState('filter.language', $this->getState('filter.language'));
$model->setState('filter.featured', $this->getState('filter.featured'));
$model->setState('list.ordering', $this->_buildContentOrderBy());
$model->setState('list.start', $this->getState('list.start'));
$model->setState('list.limit', $limit);
$model->setState('list.direction', $this->getState('list.direction'));
$model->setState('list.filter', $this->getState('list.filter'));
$model->setState('filter.tag', $this->getState('filter.tag'));
// Filter.subcategories indicates whether to include articles from subcategories in the list or blog
$model->setState('filter.subcategories', $this->getState('filter.subcategories'));
$model->setState('filter.max_category_levels', $this->getState('filter.max_category_levels'));
$model->setState('list.links', $this->getState('list.links'));
if ($limit >= 0) {
$this->_articles = $model->getItems();
// Adjust alphabetical ordering for specific locales when needed.
$this->applyLocaleAwareOrdering();
if ($this->_articles === false) {
$this->setError($model->getError());
}
} else {
$this->_articles = [];
}
$this->_pagination = $model->getPagination();
}
return $this->_articles;
}
/**
* Apply locale-aware ordering to the loaded articles when using alphabetical
* ordering and a locale which requires special collation (e.g. Danish).
*
* @return void
*
* @since 5.4.0
*/
protected function applyLocaleAwareOrdering(): void
{
if (empty($this->_articles) || !\class_exists('\\Collator')) {
return;
}
$app = Factory::getApplication();
$language = $app->getLanguage()->getTag();
// Only apply locale-aware sorting for Danish at this stage.
if (\stripos($language, 'da-') !== 0 && $language !== 'da') {
return;
}
$params = $this->state->get('params');
$articleOrderby = $params->get('orderby_sec', 'rdate');
if (!\in_array($articleOrderby, ['alpha', 'ralpha'], true)) {
return;
}
$collator = new \Collator('da_DK');
if (!$collator) {
return;
}
$ascending = $articleOrderby === 'alpha';
\usort(
$this->_articles,
static function ($a, $b) use ($collator, $ascending) {
$titleA = $a->title ?? '';
$titleB = $b->title ?? '';
$result = $collator->compare($titleA, $titleB);
if (!$ascending) {
$result = -$result;
}
return $result;
}
);
}
/**
* Build the orderby for the query
*
* @return string $orderby portion of query
*
* @since 1.5
*/
protected function _buildContentOrderBy()
{
$app = Factory::getApplication();
$db = $this->getDatabase();
$params = $this->state->get('params');
$itemid = $app->getInput()->get('id', 0, 'int') . ':' . $app->getInput()->get('Itemid', 0, 'int');
$orderCol = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
$orderDirn = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
$orderby = ' ';
if (!\in_array($orderCol, $this->filter_fields)) {
$orderCol = null;
}
if (!\in_array(strtoupper($orderDirn), ['ASC', 'DESC', ''])) {
$orderDirn = 'ASC';
}
if ($orderCol && $orderDirn) {
$orderby .= $db->escape($orderCol) . ' ' . $db->escape($orderDirn) . ', ';
}
$articleOrderby = $params->get('orderby_sec', 'rdate');
$articleOrderDate = $params->get('order_date');
$categoryOrderby = $params->def('orderby_pri', '');
$secondary = QueryHelper::orderbySecondary($articleOrderby, $articleOrderDate, $this->getDatabase()) . ', ';
$primary = QueryHelper::orderbyPrimary($categoryOrderby);
$orderby .= $primary . ' ' . $secondary . ' a.created ';
return $orderby;
}
/**
* Method to get a JPagination object for the data set.
*
* @return \Joomla\CMS\Pagination\Pagination A JPagination object for the data set.
*
* @since 3.0.1
*/
public function getPagination()
{
if (empty($this->_pagination)) {
return null;
}
return $this->_pagination;
}
/**
* Method to get category data for the current category
*
* @return object
*
* @since 1.5
*/
public function getCategory()
{
if (!\is_object($this->_item)) {
if (isset($this->state) && !empty($this->state->get('params'))) {
$params = $this->state->get('params');
$options = [];
$options['countItems'] = $params->get('show_cat_num_articles', 1) || !$params->get('show_empty_categories_cat', 0);
$options['access'] = $params->get('check_access_rights', 1);
} else {
$options['countItems'] = 0;
}
$categories = Categories::getInstance('Content', $options);
$this->_item = $categories->get($this->getState('category.id', 'root'));
// Compute selected asset permissions.
if (\is_object($this->_item)) {
$user = $this->getCurrentUser();
$asset = 'com_content.category.' . $this->_item->id;
// Check general create permission.
if ($user->authorise('core.create', $asset)) {
$this->_item->getParams()->set('access-create', true);
}
// @todo: Why aren't we lazy loading the children and siblings?
$this->_children = $this->_item->getChildren();
$this->_parent = false;
if ($this->_item->getParent()) {
$this->_parent = $this->_item->getParent();
}
$this->_rightsibling = $this->_item->getSibling();
$this->_leftsibling = $this->_item->getSibling(false);
} else {
$this->_children = false;
$this->_parent = false;
}
}
return $this->_item;
}
/**
* Get the parent category.
*
* @return mixed An array of categories or false if an error occurs.
*
* @since 1.6
*/
public function getParent()
{
if (!\is_object($this->_item)) {
$this->getCategory();
}
return $this->_parent;
}
/**
* Get the left sibling (adjacent) categories.
*
* @return mixed An array of categories or false if an error occurs.
*
* @since 1.6
*/
public function &getLeftSibling()
{
if (!\is_object($this->_item)) {
$this->getCategory();
}
return $this->_leftsibling;
}
/**
* Get the right sibling (adjacent) categories.
*
* @return mixed An array of categories or false if an error occurs.
*
* @since 1.6
*/
public function &getRightSibling()
{
if (!\is_object($this->_item)) {
$this->getCategory();
}
return $this->_rightsibling;
}
/**
* Get the child categories.
*
* @return mixed An array of categories or false if an error occurs.
*
* @since 1.6
*/
public function &getChildren()
{
if (!\is_object($this->_item)) {
$this->getCategory();
}
// Order subcategories
if ($this->_children) {
$params = $this->getState()->get('params');
$orderByPri = $params->get('orderby_pri');
if ($orderByPri === 'alpha' || $orderByPri === 'ralpha') {
$this->_children = ArrayHelper::sortObjects($this->_children, 'title', ($orderByPri === 'alpha') ? 1 : (-1));
}
}
return $this->_children;
}
/**
* Increment the hit counter for the category.
*
* @param int $pk Optional primary key of the category to increment.
*
* @return boolean True if successful; false otherwise and internal error set.
*/
public function hit($pk = 0)
{
$input = Factory::getApplication()->getInput();
$hitcount = $input->getInt('hitcount', 1);
if ($hitcount) {
$pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id');
$table = Table::getInstance('Category', '\\Joomla\\CMS\\Table\\');
$table->hit($pk);
}
return true;
}
}