-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCategoryHandler.php
437 lines (399 loc) · 14.7 KB
/
CategoryHandler.php
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
<?php
declare(strict_types=1);
namespace XoopsModules\Publisher;
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright The XUUPS Project http://sourceforge.net/projects/xuups/
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @since 1.0
* @author trabis <lusopoemas@gmail.com>
* @author The SmartFactory <www.smartfactory.ca>
*/
require_once \dirname(__DIR__) . '/include/common.php';
/**
* Categories handler class.
* This class is responsible for providing data access mechanisms to the data source
* of Category class objects.
*
* @author marcan <marcan@notrevie.ca>
*/
class CategoryHandler extends \XoopsPersistableObjectHandler
{
private const TABLE = 'publisher_categories';
private const ENTITY = Category::class;
private const ENTITYNAME = 'Category';
private const KEYNAME = 'categoryid';
private const IDENTIFIER = 'name';
/**
* @var Helper
*/
public $helper;
public $publisherIsAdmin;
public function __construct(\XoopsDatabase $db = null, Helper $helper = null)
{
/** @var Helper $this->helper */
$this->helper = $helper ?? Helper::getInstance();
$this->db = $db;
$this->publisherIsAdmin = $this->helper->isUserAdmin();
parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
}
/**
* @param bool $isNew
*
* @return \XoopsObject
*/
public function create($isNew = true)
{
$obj = parent::create($isNew);
// if ($isNew) {
// $obj->setDefaultPermissions();
// }
$obj->helper = $this->helper;
return $obj;
}
/**
* retrieve an item
*
* @param int|null $id itemid of the user
*
* @param null $fields
* @return mixed reference to the <a href='psi_element://Category'>Category</a> object, FALSE if failed
* object, FALSE if failed
*/
public function get($id = null, $fields = null)
{
static $cats;
if (isset($cats[$id])) {
return $cats[$id];
}
$obj = parent::get($id);
$cats[$id] = $obj;
return $obj;
}
/**
* insert a new category in the database
*
* @param \XoopsObject $category reference to the {@link Category}
* @param bool $force
* @return bool FALSE if failed, TRUE if already present and unchanged or successful
*/
public function insert(\XoopsObject $category, $force = false) //insert(&$category, $force = false)
{
// Auto create meta tags if empty
/** @var Category $category */
if (!$category->meta_keywords || !$category->meta_description) {
$publisherMetagen = new Metagen($category->name, $category->getVar('meta_keywords'), $category->getVar('description'));
if (!$category->meta_keywords) {
$category->setVar('meta_keywords', $publisherMetagen->keywords);
}
if (!$category->meta_description) {
$category->setVar('meta_description', $publisherMetagen->description);
}
}
// Auto create short_url if empty
if (!$category->short_url) {
$category->setVar('short_url', Metagen::generateSeoTitle($category->name('n'), false));
}
$ret = parent::insert($category, $force);
return $ret;
}
/**
* delete a category from the database
*
* @param \XoopsObject $category reference to the category to delete
* @param bool $force
*
* @return bool FALSE if failed.
*/
public function delete(\XoopsObject $category, $force = false) //delete(&$category, $force = false)
{
/** @var Category $category */
// Deleting this category ITEMs
$criteria = new \Criteria('categoryid', $category->categoryid());
$this->helper->getHandler('Item')->deleteAll($criteria);
unset($criteria);
// Deleting the sub categories
$subcats = &$this->getCategories(0, 0, $category->categoryid());
foreach ($subcats as $subcat) {
$this->delete($subcat);
}
if (!parent::delete($category, $force)) {
$category->setErrors('An error while deleting.');
return false;
}
$moduleId = $this->helper->getModule()->getVar('mid');
\xoops_groupperm_deletebymoditem($moduleId, 'category_read', $category->categoryid());
\xoops_groupperm_deletebymoditem($moduleId, 'item_submit', $category->categoryid());
\xoops_groupperm_deletebymoditem($moduleId, 'category_moderation', $category->categoryid());
return true;
}
/**
* retrieve categories from the database
*
* @param \CriteriaElement|null $criteria {@link CriteriaElement} conditions to be met
* @param bool $idAsKey use the categoryid as key for the array?
*
* @param bool $as_object
* @return array array of <a href='psi_element://XoopsItem'>XoopsItem</a> objects
*/
public function &getObjects(\CriteriaElement $criteria = null, $idAsKey = false, $as_object = true) //&getObjects($criteria = null, $idAsKey = false)
{
$ret = [];
$theObjects = parent::getObjects($criteria, true);
foreach ($theObjects as $theObject) {
if ($idAsKey) {
$ret[$theObject->categoryid()] = $theObject;
} else {
$ret[] = $theObject;
}
unset($theObject);
}
return $ret;
}
/**
* @param int $limit
* @param int $start
* @param int $parentid
* @param string $sort
* @param string $order
* @param bool $idAsKey
*
* @return array
*/
public function &getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $idAsKey = true)
{
$ret = [];
$criteria = new \CriteriaCompo();
$criteria->setSort($sort);
$criteria->order = $order; // used to fix bug in setOrder() for XOOPS < 2.5.10
if (-1 != $parentid) {
$criteria->add(new \Criteria('parentid', (int)$parentid));
}
if (!$this->publisherIsAdmin) {
$criteria2 = new \CriteriaCompo();
/** @var PermissionHandler $permissionHandler */
$permissionHandler = $this->helper->getHandler('Permission');
$categoriesGranted = $permissionHandler->getGrantedItems('category_read');
if (\count($categoriesGranted) > 0) {
$criteria2->add(new \Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
} else {
return $ret;
}
if (\is_object($GLOBALS['xoopsUser'])) {
$criteria2->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
}
$criteria->add($criteria2);
}
$criteria->setStart($start);
$criteria->setLimit($limit);
$ret = $this->getObjects($criteria, $idAsKey);
return $ret;
}
/**
* @param $category
* @param $level
* @param $catArray
* @param $catResult
*/
public function getSubCatArray($category, $level, $catArray, $catResult)
{
global $theresult;
$spaces = '';
for ($j = 0; $j < $level; ++$j) {
$spaces .= '--';
}
$theresult[$category['categoryid']] = $spaces . $category['name'];
if (isset($catArray[$category['categoryid']])) {
++$level;
foreach ($catArray[$category['categoryid']] as $parentid => $cat) {
$this->getSubCatArray($cat, $level, $catArray, $catResult);
}
}
}
/**
* @return array
*/
public function &getCategoriesForSubmit()
{
global $theresult;
$ret = [];
$criteria = new \CriteriaCompo();
$criteria->setSort('name');
$criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
if (!$this->publisherIsAdmin) {
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('item_submit');
if (\count($categoriesGranted) > 0) {
$criteria->add(new \Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
} else {
return $ret;
}
if (\is_object($GLOBALS['xoopsUser'])) {
$criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
}
}
$categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false);
if (0 == \count($categories)) {
return $ret;
}
$catArray = [];
foreach ($categories as $cat) {
$catArray[$cat['parentid']][$cat['categoryid']] = $cat;
}
// Needs to have permission on at least 1 top level category
if (!isset($catArray[0])) {
return $ret;
}
$catResult = [];
foreach ($catArray[0] as $thecat) {
$level = 0;
$this->getSubCatArray($thecat, $level, $catArray, $catResult);
}
return $theresult; //this is a global
}
/**
* @return array
*/
public function getCategoriesForSearch()
{
global $theresult;
$ret = [];
$criteria = new \CriteriaCompo();
$criteria->setSort('name');
$criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
if (!$this->publisherIsAdmin) {
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
if (\count($categoriesGranted) > 0) {
$criteria->add(new \Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
} else {
return $ret;
}
if (\is_object($GLOBALS['xoopsUser'])) {
$criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
}
}
$categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false);
if (0 == \count($categories)) {
return $ret;
}
$catArray = [];
foreach ($categories as $cat) {
$catArray[$cat['parentid']][$cat['categoryid']] = $cat;
}
// Needs to have permission on at least 1 top level category
if (!isset($catArray[0])) {
return $ret;
}
$catResult = [];
foreach ($catArray[0] as $thecat) {
$level = 0;
$this->getSubCatArray($thecat, $level, $catArray, $catResult);
}
return $theresult; //this is a global
}
/**
* @param int $parentid
*
* @return int
*/
public function getCategoriesCount($parentid = 0)
{
if (-1 == $parentid) {
return $this->getCount();
}
$criteria = new \CriteriaCompo();
if (isset($parentid) && (-1 != $parentid)) {
$criteria->add(new \Criteria('parentid', $parentid));
if (!$this->publisherIsAdmin) {
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
if (\count($categoriesGranted) > 0) {
$criteria->add(new \Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
} else {
return 0;
}
if (\is_object($GLOBALS['xoopsUser'])) {
$criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
}
}
}
return $this->getCount($criteria);
}
/**
* Get all subcats and put them in an array indexed by parent id
*
* @param array $categories
*
* @return array
*/
public function getSubCats($categories)
{
$criteria = new \CriteriaCompo(new \Criteria('parentid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN'));
$ret = [];
if (!$this->publisherIsAdmin) {
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
if (\count($categoriesGranted) > 0) {
$criteria->add(new \Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
} else {
return $ret;
}
if (\is_object($GLOBALS['xoopsUser'])) {
$criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
}
}
$criteria->setSort('weight');
$criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
$subcats = $this->getObjects($criteria, true);
/** @var Category $subcat */
foreach ($subcats as $subcat) {
$ret[$subcat->getVar('parentid')][$subcat->getVar('categoryid')] = $subcat;
}
return $ret;
}
/**
* delete categories matching a set of conditions
*
* @param \CriteriaElement|null $criteria {@link CriteriaElement}
*
* @param bool $force
* @param bool $asObject
* @return bool FALSE if deletion failed
*/
public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false) //deleteAll($criteria = null)
{
$categories = $this->getObjects($criteria);
foreach ($categories as $category) {
if (!$this->delete($category)) {
return false;
}
}
return true;
}
/**
* @param int $catId
*
* @return mixed
*/
public function publishedItemsCount($catId = 0)
{
$status = [Constants::PUBLISHER_STATUS_PUBLISHED];
return $this->itemsCount($catId, $status);
}
/**
* @param int $catId
* @param array $status
*
* @return mixed
*/
public function itemsCount($catId = 0, $status = '')
{
/** @var ItemHandler $itemHandler */
$itemHandler = $this->helper->getHandler('Item');
return $itemHandler->getCountsByCat($catId, $status);
}
}