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 Notifications; |
16
|
|
|
use Xoops; |
17
|
|
|
use Xoops\Core\Kernel\XoopsObject; |
18
|
|
|
use XoopsModules\Publisher; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @copyright The XUUPS Project http://sourceforge.net/projects/xuups/ |
22
|
|
|
* @license GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
23
|
|
|
* @package Publisher |
24
|
|
|
* @since 1.0 |
25
|
|
|
* @author trabis <[email protected]> |
26
|
|
|
* @author The SmartFactory <www.smartfactory.ca> |
27
|
|
|
* @version $Id$ |
28
|
|
|
*/ |
29
|
|
|
require_once \dirname(__DIR__) . '/include/common.php'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Category |
33
|
|
|
* @package XoopsModules\Publisher |
34
|
|
|
*/ |
35
|
|
|
class Category extends XoopsObject |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Helper |
39
|
|
|
* @access public |
40
|
|
|
*/ |
41
|
|
|
public $helper = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $_categoryPath = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* constructor |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->helper = Helper::getInstance(); |
54
|
|
|
$this->initVar('categoryid', \XOBJ_DTYPE_INT, null, false); |
55
|
|
|
$this->initVar('parentid', \XOBJ_DTYPE_INT, null, false); |
56
|
|
|
$this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, true, 100); |
57
|
|
|
$this->initVar('description', \XOBJ_DTYPE_TXTAREA, null, false, 255); |
58
|
|
|
$this->initVar('image', \XOBJ_DTYPE_TXTBOX, null, false, 255); |
59
|
|
|
$this->initVar('total', \XOBJ_DTYPE_INT, 1, false); |
60
|
|
|
$this->initVar('weight', \XOBJ_DTYPE_INT, 1, false); |
61
|
|
|
$this->initVar('created', \XOBJ_DTYPE_INT, null, false); |
62
|
|
|
$this->initVar('template', \XOBJ_DTYPE_TXTBOX, null, false, 255); |
63
|
|
|
$this->initVar('header', \XOBJ_DTYPE_TXTAREA, null, false); |
64
|
|
|
$this->initVar('meta_keywords', \XOBJ_DTYPE_TXTAREA, null, false); |
65
|
|
|
$this->initVar('meta_description', \XOBJ_DTYPE_TXTAREA, null, false); |
66
|
|
|
$this->initVar('short_url', \XOBJ_DTYPE_TXTBOX, null, false, 255); |
67
|
|
|
$this->initVar('moderator', \XOBJ_DTYPE_INT, null, false, 0); |
68
|
|
|
//not persistent values |
69
|
|
|
$this->initVar('itemcount', \XOBJ_DTYPE_INT, 0, false); |
70
|
|
|
$this->initVar('last_itemid', \XOBJ_DTYPE_INT); |
71
|
|
|
$this->initVar('last_title_link', \XOBJ_DTYPE_TXTBOX); |
72
|
|
|
$this->initVar('dohtml', \XOBJ_DTYPE_INT, 1, false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function notLoaded(): bool |
76
|
|
|
{ |
77
|
|
|
return (-1 == $this->getVar('categoryid')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function checkPermission(): bool |
81
|
|
|
{ |
82
|
|
|
$xoops = Xoops::getInstance(); |
83
|
|
|
if ($this->helper->isUserAdmin()) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
if ($xoops->isUser() && $xoops->user->getVar('uid') == $this->getVar('moderator')) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->helper->getPermissionHandler()->isGranted('category_read', $this->getVar('categoryid')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $format |
95
|
|
|
* |
96
|
|
|
* @return mixed|string |
97
|
|
|
*/ |
98
|
|
|
public function image($format = 's') |
99
|
|
|
{ |
100
|
|
|
if ('' != $this->getVar('image')) { |
101
|
|
|
return $this->getVar('image', $format); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return 'blank.png'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $format |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function template($format = 'n') |
113
|
|
|
{ |
114
|
|
|
return $this->getVar('template', $format); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $withAllLink |
119
|
|
|
* |
120
|
|
|
* @return array|bool|string |
121
|
|
|
*/ |
122
|
|
|
public function getCategoryPath($withAllLink = true) |
123
|
|
|
{ |
124
|
|
|
if (!$this->_categoryPath) { |
|
|
|
|
125
|
|
|
if ($withAllLink) { |
126
|
|
|
$ret = $this->getCategoryLink(); |
127
|
|
|
} else { |
128
|
|
|
$ret = $this->getVar('name'); |
129
|
|
|
} |
130
|
|
|
$parentid = $this->getVar('parentid'); |
131
|
|
|
if (0 != $parentid) { |
132
|
|
|
$parentObj = $this->helper->getCategoryHandler()->get($parentid); |
133
|
|
|
if ($parentObj->notLoaded()) { |
134
|
|
|
exit; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
$ret = $parentObj->getCategoryPath($withAllLink) . ' > ' . $ret; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
$this->_categoryPath = $ret; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->_categoryPath; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return mixed|string |
146
|
|
|
*/ |
147
|
|
|
public function getCategoryPathForMetaTitle() |
148
|
|
|
{ |
149
|
|
|
$ret = ''; |
150
|
|
|
$parentid = $this->getVar('parentid'); |
151
|
|
|
if (0 != $parentid) { |
152
|
|
|
$parentObj = $this->helper->getCategoryHandler()->get($parentid); |
153
|
|
|
if ($parentObj->notLoaded()) { |
154
|
|
|
exit('NOT LOADED'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
$ret = $parentObj->getCategoryPath(false); |
157
|
|
|
$ret = \str_replace(' >', ' -', $ret); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $ret; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getGroups_read(): ?array |
164
|
|
|
{ |
165
|
|
|
return $this->helper->getPermissionHandler()->getGrantedGroupsById('category_read', $this->getVar('categoryid')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getGroups_submit(): ?array |
169
|
|
|
{ |
170
|
|
|
return $this->helper->getPermissionHandler()->getGrantedGroupsById('item_submit', $this->getVar('categoryid')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getGroups_moderation(): ?array |
174
|
|
|
{ |
175
|
|
|
return $this->helper->getPermissionHandler()->getGrantedGroupsById('category_moderation', $this->getVar('categoryid')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getCategoryUrl(): string |
179
|
|
|
{ |
180
|
|
|
return Publisher\Utils::seoGenUrl('category', $this->getVar('categoryid'), $this->getVar('short_url')); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param bool $class |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getCategoryLink($class = false): ?string |
189
|
|
|
{ |
190
|
|
|
if ($class) { |
191
|
|
|
return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . '</a>'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return "<a href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . '</a>'; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param bool $sendNotifications |
199
|
|
|
* @param bool $force |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function store($sendNotifications = true, $force = true) |
204
|
|
|
{ |
205
|
|
|
$ret = $this->helper->getCategoryHandler()->insert($this, $force); |
206
|
|
|
if ($sendNotifications && $ret && $this->isNew()) { |
207
|
|
|
$this->sendNotifications(); |
208
|
|
|
} |
209
|
|
|
$this->unsetNew(); |
210
|
|
|
|
211
|
|
|
return $ret; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Send notifications |
216
|
|
|
*/ |
217
|
|
|
public function sendNotifications(): void |
218
|
|
|
{ |
219
|
|
|
$xoops = Xoops::getInstance(); |
220
|
|
|
if ($xoops->isActiveModule('notifications')) { |
221
|
|
|
$tags = []; |
222
|
|
|
$tags['MODULE_NAME'] = $this->helper->getModule()->getVar('name'); |
223
|
|
|
$tags['CATEGORY_NAME'] = $this->getVar('name'); |
224
|
|
|
$tags['CATEGORY_URL'] = $this->getCategoryUrl(); |
225
|
|
|
$notificationHandler = Notifications::getInstance()->getHandlerNotification(); |
226
|
|
|
$notificationHandler->triggerEvent('global', 0, 'category_created', $tags); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $category |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
|
|
public function toArray($category = []) |
236
|
|
|
{ |
237
|
|
|
$category['categoryid'] = $this->getVar('categoryid'); |
238
|
|
|
$category['name'] = $this->getVar('name'); |
239
|
|
|
$category['categorylink'] = $this->getCategoryLink(); |
240
|
|
|
$category['categoryurl'] = $this->getCategoryUrl(); |
241
|
|
|
$category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : ''; |
242
|
|
|
$category['description'] = $this->getVar('description'); |
243
|
|
|
$category['header'] = $this->getVar('header'); |
244
|
|
|
$category['meta_keywords'] = $this->getVar('meta_keywords'); |
245
|
|
|
$category['meta_description'] = $this->getVar('meta_description'); |
246
|
|
|
$category['short_url'] = $this->getVar('short_url'); |
247
|
|
|
if ($this->getVar('last_itemid') > 0) { |
248
|
|
|
$category['last_itemid'] = $this->getVar('last_itemid', 'n'); |
249
|
|
|
$category['last_title_link'] = $this->getVar('last_title_link', 'n'); |
250
|
|
|
} |
251
|
|
|
if ('blank.png' !== $this->image()) { |
252
|
|
|
$category['image_path'] = Publisher\Utils::getImageDir('category', false) . $this->image(); |
253
|
|
|
} else { |
254
|
|
|
$category['image_path'] = ''; |
255
|
|
|
} |
256
|
|
|
$category['lang_subcategories'] = \sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name')); |
257
|
|
|
|
258
|
|
|
return $category; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param array $category |
263
|
|
|
*/ |
264
|
|
|
public function toArrayTable($category = []): array |
265
|
|
|
{ |
266
|
|
|
$category['categoryid'] = $this->getVar('categoryid'); |
267
|
|
|
$category['categorylink'] = $this->getCategoryLink(); |
268
|
|
|
$category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : ''; |
269
|
|
|
$category['description'] = $this->getVar('description'); |
270
|
|
|
if ($this->getVar('last_itemid') > 0) { |
271
|
|
|
$category['last_itemid'] = $this->getVar('last_itemid', 'n'); |
272
|
|
|
$category['last_title_link'] = $this->getVar('last_title_link', 'n'); |
273
|
|
|
} |
274
|
|
|
if ('blank.png' !== $this->image()) { |
275
|
|
|
$category['image_path'] = Publisher\Utils::getImageDir('category', false) . $this->image(); |
276
|
|
|
} else { |
277
|
|
|
$category['image_path'] = ''; |
278
|
|
|
} |
279
|
|
|
$category['lang_subcategories'] = \sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name')); |
280
|
|
|
|
281
|
|
|
return $category; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function createMetaTags(): void |
285
|
|
|
{ |
286
|
|
|
$publisher_metagen = new Publisher\Metagen($this->getVar('name'), $this->getVar('meta_keywords'), $this->getVar('meta_description')); |
|
|
|
|
287
|
|
|
$publisher_metagen->createMetaTags(); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.