|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Publisher; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits |
|
7
|
|
|
of supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use Xoops; |
|
16
|
|
|
use Xoops\Core\Database\Connection; |
|
17
|
|
|
use Xoops\Core\Kernel\Criteria; |
|
18
|
|
|
use Xoops\Core\Kernel\CriteriaCompo; |
|
19
|
|
|
use Xoops\Core\Kernel\XoopsObject; |
|
20
|
|
|
use Xoops\Core\Kernel\XoopsPersistableObjectHandler; |
|
21
|
|
|
use XoopsModules\Publisher; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @copyright The XUUPS Project http://sourceforge.net/projects/xuups/ |
|
25
|
|
|
* @license GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
26
|
|
|
* @package Publisher |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
* @author trabis <[email protected]> |
|
29
|
|
|
* @author The SmartFactory <www.smartfactory.ca> |
|
30
|
|
|
* @version $Id$ |
|
31
|
|
|
*/ |
|
32
|
|
|
require_once \dirname(__DIR__) . '/include/common.php'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Categories handler class. |
|
36
|
|
|
* This class is responsible for providing data access mechanisms to the data source |
|
37
|
|
|
* of Category class objects. |
|
38
|
|
|
* |
|
39
|
|
|
* @author marcan <[email protected]> |
|
40
|
|
|
* @package Publisher |
|
41
|
|
|
*/ |
|
42
|
|
|
class CategoryHandler extends XoopsPersistableObjectHandler |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var Helper |
|
46
|
|
|
* @access public |
|
47
|
|
|
*/ |
|
48
|
|
|
public $helper = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Xoops\Core\Database\Connection $db |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Connection $db) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->helper = Helper::getInstance(); |
|
56
|
|
|
parent::__construct($db, 'publisher_categories', Category::class, 'categoryid', 'name'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param null $id |
|
|
|
|
|
|
61
|
|
|
* @param null $fields |
|
|
|
|
|
|
62
|
|
|
* |
|
63
|
|
|
* @return null|Publisher\Category |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get($id = null, $fields = null) |
|
66
|
|
|
{ |
|
67
|
|
|
static $cats; |
|
68
|
|
|
if (null === $fields && isset($cats[$id])) { |
|
69
|
|
|
return $cats[$id]; |
|
70
|
|
|
} |
|
71
|
|
|
$obj = parent::get($id, $fields); |
|
72
|
|
|
if (null === $fields) { |
|
|
|
|
|
|
73
|
|
|
$cats[$id] = $obj; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $obj; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* insert a new category in the database |
|
81
|
|
|
* |
|
82
|
|
|
* @param XoopsObject $category reference to the {@link Publisher\Category} object |
|
83
|
|
|
* @param bool $force |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
|
86
|
|
|
*/ |
|
87
|
|
|
public function insert(XoopsObject $category, $force = true) |
|
88
|
|
|
{ |
|
89
|
|
|
// Auto create meta tags if empty |
|
90
|
|
|
if (!$category->getVar('meta_keywords') || !$category->getVar('meta_description')) { |
|
91
|
|
|
$publisher_metagen = new Publisher\Metagen($category->getVar('name'), $category->getVar('meta_keywords'), $category->getVar('description')); |
|
|
|
|
|
|
92
|
|
|
if (!$category->getVar('meta_keywords')) { |
|
93
|
|
|
$category->setVar('meta_keywords', $publisher_metagen->_keywords); |
|
94
|
|
|
} |
|
95
|
|
|
if (!$category->getVar('meta_description')) { |
|
96
|
|
|
$category->setVar('meta_description', $publisher_metagen->_description); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
// Auto create short_url if empty |
|
100
|
|
|
if (!$category->getVar('short_url')) { |
|
101
|
|
|
$category->setVar('short_url', Publisher\Metagen::generateSeoTitle($category->getVar('name', 'n'), false)); |
|
102
|
|
|
} |
|
103
|
|
|
$ret = parent::insert($category, $force); |
|
104
|
|
|
|
|
105
|
|
|
return $ret; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* delete a category from the database |
|
110
|
|
|
* |
|
111
|
|
|
* @param XoopsObject $category reference to the category to delete |
|
112
|
|
|
* @param bool $force |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool FALSE if failed. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function delete(XoopsObject $category, $force = false) |
|
117
|
|
|
{ |
|
118
|
|
|
$xoops = Xoops::getInstance(); |
|
119
|
|
|
// Deleting this category ITEMs |
|
120
|
|
|
$criteria = new Criteria('categoryid', $category->getVar('categoryid')); |
|
|
|
|
|
|
121
|
|
|
$this->helper->getItemHandler()->deleteAll($criteria, true, true); |
|
122
|
|
|
unset($criteria); |
|
123
|
|
|
// Deleting the sub categories |
|
124
|
|
|
$subcats = $this->getCategories(0, 0, $category->getVar('categoryid')); |
|
125
|
|
|
foreach ($subcats as $subcat) { |
|
126
|
|
|
$this->delete($subcat); |
|
127
|
|
|
} |
|
128
|
|
|
if (!parent::delete($category, $force)) { |
|
129
|
|
|
$category->setErrors('An error while deleting.'); |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
$module_id = $this->helper->getModule()->getVar('mid'); |
|
134
|
|
|
|
|
135
|
|
|
$xoops->getHandlerGroupPermission()->deleteByModule($module_id, 'category_read', $category->getVar('categoryid')); |
|
136
|
|
|
$xoops->getHandlerGroupPermission()->deleteByModule($module_id, 'item_submit', $category->getVar('categoryid')); |
|
137
|
|
|
$xoops->getHandlerGroupPermission()->deleteByModule($module_id, 'category_moderation', $category->getVar('categoryid')); |
|
138
|
|
|
|
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $limit |
|
144
|
|
|
* @param int $start |
|
145
|
|
|
* @param int $parentid |
|
146
|
|
|
* @param string $sort |
|
147
|
|
|
* @param string $order |
|
148
|
|
|
* @param bool $id_as_key |
|
149
|
|
|
*/ |
|
150
|
|
|
public function &getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true): array |
|
151
|
|
|
{ |
|
152
|
|
|
$xoops = Xoops::getInstance(); |
|
153
|
|
|
$ret = []; |
|
154
|
|
|
$criteria = new CriteriaCompo(); |
|
155
|
|
|
$criteria->setSort($sort); |
|
156
|
|
|
$criteria->setOrder($order); |
|
157
|
|
|
if (-1 != $parentid) { |
|
158
|
|
|
$criteria->add(new Criteria('parentid', $parentid)); |
|
159
|
|
|
} |
|
160
|
|
|
if (!Publisher\Utils::IsUserAdmin()) { |
|
161
|
|
|
$categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
|
162
|
|
|
if (\count($categoriesGranted) > 0) { |
|
163
|
|
|
$criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
164
|
|
|
} else { |
|
165
|
|
|
return $ret; |
|
166
|
|
|
} |
|
167
|
|
|
if ($xoops->isUser()) { |
|
168
|
|
|
$criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
$criteria->setStart($start); |
|
172
|
|
|
$criteria->setLimit($limit); |
|
173
|
|
|
$ret = $this->getObjects($criteria, $id_as_key); |
|
174
|
|
|
|
|
175
|
|
|
return $ret; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* getSubCatArray |
|
180
|
|
|
* |
|
181
|
|
|
* @param array $category |
|
182
|
|
|
* @param int $level |
|
183
|
|
|
* @param array $cat_array |
|
184
|
|
|
* @param array $cat_result |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getSubCatArray($category, $level, $cat_array, $cat_result): void |
|
187
|
|
|
{ |
|
188
|
|
|
global $theresult; |
|
189
|
|
|
$spaces = ''; |
|
190
|
|
|
for ($j = 0; $j < $level; ++$j) { |
|
191
|
|
|
$spaces .= '--'; |
|
192
|
|
|
} |
|
193
|
|
|
$theresult[$category['categoryid']] = $spaces . $category['name']; |
|
194
|
|
|
if (isset($cat_array[$category['categoryid']])) { |
|
195
|
|
|
$level = $level + 1; |
|
196
|
|
|
foreach ($cat_array[$category['categoryid']] as $cat) { |
|
197
|
|
|
$this->getSubCatArray($cat, $level, $cat_array, $cat_result); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function &getCategoriesForSubmit(): array |
|
203
|
|
|
{ |
|
204
|
|
|
global $theresult; |
|
205
|
|
|
$xoops = Xoops::getInstance(); |
|
206
|
|
|
$ret = []; |
|
207
|
|
|
$criteria = new CriteriaCompo(); |
|
208
|
|
|
$criteria->setSort('name'); |
|
209
|
|
|
$criteria->setOrder('ASC'); |
|
210
|
|
|
if (!Publisher\Utils::IsUserAdmin()) { |
|
211
|
|
|
$categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('item_submit'); |
|
212
|
|
|
if (\count($categoriesGranted) > 0) { |
|
213
|
|
|
$criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
214
|
|
|
} else { |
|
215
|
|
|
return $ret; |
|
216
|
|
|
} |
|
217
|
|
|
if ($xoops->isUser()) { |
|
218
|
|
|
$criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
$categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false); |
|
222
|
|
|
if (0 == \count($categories)) { |
|
223
|
|
|
return $ret; |
|
224
|
|
|
} |
|
225
|
|
|
$cat_array = []; |
|
226
|
|
|
foreach ($categories as $cat) { |
|
227
|
|
|
$cat_array[$cat['parentid']][$cat['categoryid']] = $cat; |
|
228
|
|
|
} |
|
229
|
|
|
// Needs to have permission on at least 1 top level category |
|
230
|
|
|
if (!isset($cat_array[0])) { |
|
231
|
|
|
return $ret; |
|
232
|
|
|
} |
|
233
|
|
|
$cat_result = []; |
|
234
|
|
|
foreach ($cat_array[0] as $thecat) { |
|
235
|
|
|
$level = 0; |
|
236
|
|
|
$this->getSubCatArray($thecat, $level, $cat_array, $cat_result); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $theresult; //this is a global |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function &getCategoriesForSearch(): array |
|
243
|
|
|
{ |
|
244
|
|
|
global $theresult; |
|
245
|
|
|
$xoops = Xoops::getInstance(); |
|
246
|
|
|
$ret = []; |
|
247
|
|
|
$criteria = new CriteriaCompo(); |
|
248
|
|
|
$criteria->setSort('name'); |
|
249
|
|
|
$criteria->setOrder('ASC'); |
|
250
|
|
|
if (!Publisher\Utils::IsUserAdmin()) { |
|
251
|
|
|
$categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
|
252
|
|
|
if (\count($categoriesGranted) > 0) { |
|
253
|
|
|
$criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
254
|
|
|
} else { |
|
255
|
|
|
return $ret; |
|
256
|
|
|
} |
|
257
|
|
|
if ($xoops->isUser()) { |
|
258
|
|
|
$criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
$categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false); |
|
262
|
|
|
if (0 == \count($categories)) { |
|
263
|
|
|
return $ret; |
|
264
|
|
|
} |
|
265
|
|
|
$cat_array = []; |
|
266
|
|
|
foreach ($categories as $cat) { |
|
267
|
|
|
$cat_array[$cat['parentid']][$cat['categoryid']] = $cat; |
|
268
|
|
|
} |
|
269
|
|
|
// Needs to have permission on at least 1 top level category |
|
270
|
|
|
if (!isset($cat_array[0])) { |
|
271
|
|
|
return $ret; |
|
272
|
|
|
} |
|
273
|
|
|
$cat_result = []; |
|
274
|
|
|
foreach ($cat_array[0] as $thecat) { |
|
275
|
|
|
$level = 0; |
|
276
|
|
|
$this->getSubCatArray($thecat, $level, $cat_array, $cat_result); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return $theresult; //this is a global |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param int $parentid |
|
284
|
|
|
*/ |
|
285
|
|
|
public function getCategoriesCount($parentid = 0): int |
|
286
|
|
|
{ |
|
287
|
|
|
$xoops = Xoops::getInstance(); |
|
288
|
|
|
if (-1 == $parentid) { |
|
289
|
|
|
return $this->getCount(); |
|
290
|
|
|
} |
|
291
|
|
|
$criteria = new CriteriaCompo(); |
|
292
|
|
|
if (isset($parentid) && (-1 != $parentid)) { |
|
293
|
|
|
$criteria->add(new criteria('parentid', $parentid)); |
|
294
|
|
|
if (!Publisher\Utils::IsUserAdmin()) { |
|
295
|
|
|
$categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
|
296
|
|
|
if (\count($categoriesGranted) > 0) { |
|
297
|
|
|
$criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
298
|
|
|
} else { |
|
299
|
|
|
return 0; |
|
300
|
|
|
} |
|
301
|
|
|
if ($xoops->isUser()) { |
|
302
|
|
|
$criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
|
|
|
|
|
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
return $this->getCount($criteria); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Get all subcats and put them in an array indexed by parent id |
|
312
|
|
|
* |
|
313
|
|
|
* @param array $categories |
|
314
|
|
|
*/ |
|
315
|
|
|
public function &getSubCats($categories): array |
|
316
|
|
|
{ |
|
317
|
|
|
$xoops = Xoops::getInstance(); |
|
318
|
|
|
$criteria = new CriteriaCompo(new Criteria('parentid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN')); |
|
319
|
|
|
$ret = []; |
|
320
|
|
|
if (!Publisher\Utils::IsUserAdmin()) { |
|
321
|
|
|
$categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
|
322
|
|
|
if (\count($categoriesGranted) > 0) { |
|
323
|
|
|
$criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
|
324
|
|
|
} else { |
|
325
|
|
|
return $ret; |
|
326
|
|
|
} |
|
327
|
|
|
if ($xoops->isUser()) { |
|
328
|
|
|
$criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
|
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
$criteria->setSort('weight'); |
|
332
|
|
|
$criteria->setOrder('ASC'); |
|
333
|
|
|
$subcats = $this->getObjects($criteria, true); |
|
334
|
|
|
/* @var Publisher\Category $subcat */ |
|
335
|
|
|
foreach ($subcats as $subcat) { |
|
336
|
|
|
$ret[$subcat->getVar('parentid')][$subcat->getVar('categoryid')] = $subcat; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
return $ret; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @param int $cat_id |
|
344
|
|
|
* |
|
345
|
|
|
* @return mixed |
|
346
|
|
|
*/ |
|
347
|
|
|
public function publishedItemsCount($cat_id = 0) |
|
348
|
|
|
{ |
|
349
|
|
|
return $this->itemsCount($cat_id, $status = [\_PUBLISHER_STATUS_PUBLISHED]); |
|
|
|
|
|
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* @param int $cat_id |
|
354
|
|
|
* @param string $status |
|
355
|
|
|
* |
|
356
|
|
|
* @return mixed |
|
357
|
|
|
*/ |
|
358
|
|
|
public function itemsCount($cat_id = 0, $status = '') |
|
359
|
|
|
{ |
|
360
|
|
|
return $this->helper->getItemHandler()->getCountsByCat($cat_id, $status); |
|
|
|
|
|
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|