|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace XoopsModules\Adslight; |
|
6
|
|
|
|
|
7
|
|
|
/* |
|
8
|
|
|
You may not change or alter any portion of this comment or credits |
|
9
|
|
|
of supporting developers from this source code or any supporting source code |
|
10
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Module: Adslight |
|
19
|
|
|
* |
|
20
|
|
|
* @category Module |
|
21
|
|
|
* @author XOOPS Development Team <https://xoops.org> |
|
22
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
|
23
|
|
|
* @license GPL 2.0 or later |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use Xmf\Module\Helper\Permission; |
|
27
|
|
|
use XoopsModules\Adslight; |
|
28
|
|
|
|
|
29
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
30
|
|
|
|
|
31
|
|
|
$permHelper = new Permission(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class CategoriesHandler |
|
35
|
|
|
*/ |
|
36
|
|
|
class CategoriesHandler extends \XoopsPersistableObjectHandler |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var Helper |
|
40
|
|
|
*/ |
|
41
|
|
|
public $helper; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
* @param null|\XoopsDatabase $db |
|
46
|
|
|
* @param null|\XoopsModules\Adslight\Helper $helper |
|
47
|
|
|
*/ |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(\XoopsDatabase $db = null, $helper = null) |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var \XoopsModules\Adslight\Helper $this- >helper */ |
|
52
|
|
|
$this->helper = $helper; |
|
53
|
|
|
parent::__construct($db, 'adslight_categories', Categories::class, 'cid', 'title'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param bool $isNew |
|
58
|
|
|
* |
|
59
|
|
|
* @return \XoopsObject |
|
60
|
|
|
*/ |
|
61
|
|
|
public function create($isNew = true): \XoopsObject |
|
62
|
|
|
{ |
|
63
|
|
|
$obj = parent::create($isNew); |
|
64
|
|
|
$obj->helper = $this->helper; |
|
65
|
|
|
|
|
66
|
|
|
return $obj; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
//==================================== |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $pid |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getCategoriesCount($pid = 0) |
|
78
|
|
|
{ |
|
79
|
|
|
if (-1 == $pid) { |
|
80
|
|
|
return $this->getCount(); |
|
81
|
|
|
} |
|
82
|
|
|
$helper = Helper::getInstance(); |
|
83
|
|
|
$criteria = new \CriteriaCompo(); |
|
84
|
|
|
if (isset($pid) && (-1 != $pid)) { |
|
85
|
|
|
$criteria->add(new \Criteria('pid', $pid)); |
|
86
|
|
|
if (!$helper->isUserAdmin()) { |
|
87
|
|
|
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read'); |
|
|
|
|
|
|
88
|
|
|
if (\count($categoriesGranted) > 0) { |
|
|
|
|
|
|
89
|
|
|
$criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
|
|
|
|
|
90
|
|
|
} else { |
|
91
|
|
|
return 0; |
|
92
|
|
|
} |
|
93
|
|
|
// if (\is_object($GLOBALS['xoopsUser'])) { |
|
94
|
|
|
// $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR'); |
|
95
|
|
|
// } |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->getCount($criteria); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get all subcats and put them in an array indexed by parent id |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $categories |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getSubCats($categories) |
|
110
|
|
|
{ |
|
111
|
|
|
$helper = Helper::getInstance(); |
|
112
|
|
|
$criteria = new \CriteriaCompo(new \Criteria('pid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN')); |
|
113
|
|
|
$ret = []; |
|
114
|
|
|
if (!$helper->isUserAdmin()) { |
|
115
|
|
|
$categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read'); |
|
116
|
|
|
if (\count($categoriesGranted) > 0) { |
|
|
|
|
|
|
117
|
|
|
$criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
|
|
|
|
|
118
|
|
|
} else { |
|
119
|
|
|
return $ret; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (\is_object($GLOBALS['xoopsUser'])) { |
|
123
|
|
|
$criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR'); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
$criteria->setSort('weight'); |
|
127
|
|
|
$criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method |
|
128
|
|
|
$subcats = $this->getObjects($criteria, true); |
|
129
|
|
|
/** @var Categories $subcat */ |
|
130
|
|
|
foreach ($subcats as $subcat) { |
|
131
|
|
|
$ret[$subcat->getVar('pid')][$subcat->getVar('cid')] = $subcat; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $ret; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|