1
|
|
|
<?php namespace XoopsModules\Smartfaq; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Module: SmartFAQ |
5
|
|
|
* Author: The SmartFactory <www.smartfactory.ca> |
6
|
|
|
* Licence: GNU |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use \XoopsModules\Smartfaq; |
10
|
|
|
|
11
|
|
|
// defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Category |
15
|
|
|
* @package XoopsModules\Smartfaq |
16
|
|
|
*/ |
17
|
|
|
class Category extends \XoopsObject |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
* @access private |
22
|
|
|
*/ |
23
|
|
|
private $groups_read = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
* @access private |
28
|
|
|
*/ |
29
|
|
|
private $groups_admin = null; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* constructor |
33
|
|
|
* @param null $id |
34
|
|
|
*/ |
35
|
|
|
public function __construct($id = null) |
36
|
|
|
{ |
37
|
|
|
$this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
38
|
|
|
$this->initVar('categoryid', XOBJ_DTYPE_INT, null, false); |
39
|
|
|
$this->initVar('parentid', XOBJ_DTYPE_INT, null, false); |
40
|
|
|
$this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); |
41
|
|
|
$this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255); |
42
|
|
|
$this->initVar('total', XOBJ_DTYPE_INT, 1, false); |
43
|
|
|
$this->initVar('weight', XOBJ_DTYPE_INT, 1, false); |
44
|
|
|
$this->initVar('created', XOBJ_DTYPE_INT, null, false); |
45
|
|
|
$this->initVar('last_faq', XOBJ_DTYPE_INT); |
46
|
|
|
|
47
|
|
|
//not persistent values |
48
|
|
|
$this->initVar('faqcount', XOBJ_DTYPE_INT, 0, false); |
49
|
|
|
$this->initVar('last_faqid', XOBJ_DTYPE_INT); |
50
|
|
|
$this->initVar('last_question_link', XOBJ_DTYPE_TXTBOX); |
51
|
|
|
|
52
|
|
|
if (null !== $id) { |
53
|
|
|
if (is_array($id)) { |
54
|
|
|
$this->assignVars($id); |
55
|
|
|
} else { |
56
|
|
|
/** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */ |
57
|
|
|
$categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category'); |
58
|
|
|
$category = $categoryHandler->get($id); |
59
|
|
|
foreach ($category->vars as $k => $v) { |
60
|
|
|
$this->assignVar($k, $v['value']); |
61
|
|
|
} |
62
|
|
|
$this->assignOtherProperties(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function notLoaded() |
71
|
|
|
{ |
72
|
|
|
return (-1 == $this->getVar('categoryid')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function assignOtherProperties() |
76
|
|
|
{ |
77
|
|
|
global $xoopsUser; |
|
|
|
|
78
|
|
|
$smartModule = Smartfaq\Utility::getModuleInfo(); |
79
|
|
|
$module_id = $smartModule->getVar('mid'); |
80
|
|
|
|
81
|
|
|
$gpermHandler = xoops_getHandler('groupperm'); |
82
|
|
|
|
83
|
|
|
$this->groups_read = $gpermHandler->getGroupIds('category_read', $this->categoryid(), $module_id); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function checkPermission() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php'; |
92
|
|
|
|
93
|
|
|
$userIsAdmin = Smartfaq\Utility::userIsAdmin(); |
94
|
|
|
if ($userIsAdmin) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */ |
99
|
|
|
$smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission'); |
100
|
|
|
|
101
|
|
|
$categoriesGranted = $smartPermHandler->getPermissions('category'); |
102
|
|
|
if (in_array($this->categoryid(), $categoriesGranted)) { |
103
|
|
|
$ret = true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $ret; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function categoryid() |
113
|
|
|
{ |
114
|
|
|
return $this->getVar('categoryid'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function parentid() |
121
|
|
|
{ |
122
|
|
|
return $this->getVar('parentid'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $format |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function name($format = 'S') |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$ret = $this->getVar('name', $format); |
132
|
|
|
if (('s' === $format) || ('S' === $format) || ('show' === $format)) { |
133
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
134
|
|
|
$ret = $myts->displayTarea($ret); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $ret; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $format |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function description($format = 'S') |
145
|
|
|
{ |
146
|
|
|
return $this->getVar('description', $format); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function weight() |
153
|
|
|
{ |
154
|
|
|
return $this->getVar('weight'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param bool $withAllLink |
159
|
|
|
* @param bool $open |
160
|
|
|
* @return mixed|string |
161
|
|
|
*/ |
162
|
|
|
public function getCategoryPath($withAllLink = false, $open = false) |
163
|
|
|
{ |
164
|
|
|
$filename = 'category.php'; |
165
|
|
|
if (false !== $open) { |
166
|
|
|
$filename = 'open_category.php'; |
167
|
|
|
} |
168
|
|
|
if ($withAllLink) { |
169
|
|
|
$ret = "<a href='" . XOOPS_URL . '/modules/smartfaq/' . $filename . '?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>'; |
170
|
|
|
} else { |
171
|
|
|
$ret = $this->name(); |
172
|
|
|
} |
173
|
|
|
$parentid = $this->parentid(); |
174
|
|
|
/** @var Smartfaq\CategoryHandler $categoryHandler */ |
175
|
|
|
$categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category'); |
176
|
|
|
if (0 != $parentid) { |
177
|
|
|
$parentObj = $categoryHandler->get($parentid); |
178
|
|
|
if ($parentObj->notLoaded()) { |
179
|
|
|
exit; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
$parentid = $parentObj->parentid(); |
|
|
|
|
182
|
|
|
$ret = $parentObj->getCategoryPath(true, $open) . ' > ' . $ret; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $ret; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getGroups_read() |
192
|
|
|
{ |
193
|
|
|
// if(count($this->groups_read) < 1) { |
|
|
|
|
194
|
|
|
if (!is_array($this->groups_read)) { |
195
|
|
|
$this->assignOtherProperties(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->groups_read; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $groups_read |
203
|
|
|
*/ |
204
|
|
|
public function setGroups_read($groups_read = ['0']) |
205
|
|
|
{ |
206
|
|
|
$this->groups_read = $groups_read; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param bool $sendNotifications |
211
|
|
|
* @param bool $force |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function store($sendNotifications = true, $force = true) |
215
|
|
|
{ |
216
|
|
|
// $categoryHandler = new sfCategoryHandler($this->db); |
|
|
|
|
217
|
|
|
/** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */ |
218
|
|
|
$categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category'); |
219
|
|
|
|
220
|
|
|
$ret = $categoryHandler->insert($this, $force); |
221
|
|
|
if ($sendNotifications && $ret && $this->isNew()) { |
222
|
|
|
$this->sendNotifications(); |
223
|
|
|
} |
224
|
|
|
$this->unsetNew(); |
225
|
|
|
|
226
|
|
|
return $ret; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function sendNotifications() |
230
|
|
|
{ |
231
|
|
|
$smartModule = Smartfaq\Utility::getModuleInfo(); |
232
|
|
|
|
233
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
234
|
|
|
$notificationHandler = xoops_getHandler('notification'); |
|
|
|
|
235
|
|
|
|
236
|
|
|
$tags = []; |
237
|
|
|
$tags['MODULE_NAME'] = $myts->htmlSpecialChars($smartModule->getVar('name')); |
238
|
|
|
$tags['CATEGORY_NAME'] = $this->name(); |
239
|
|
|
$tags['CATEGORY_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid(); |
240
|
|
|
|
241
|
|
|
$notificationHandler = xoops_getHandler('notification'); |
242
|
|
|
$notificationHandler->triggerEvent('global_faq', 0, 'category_created', $tags); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param array $category |
247
|
|
|
* @param bool $open |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function toArray($category = [], $open = false) |
251
|
|
|
{ |
252
|
|
|
$category['categoryid'] = $this->categoryid(); |
253
|
|
|
$category['name'] = $this->name(); |
254
|
|
|
if (false !== $open) { |
255
|
|
|
$category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/open_category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>'; |
256
|
|
|
} else { |
257
|
|
|
$category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>'; |
258
|
|
|
} |
259
|
|
|
$category['total'] = $this->getVar('faqcount'); |
260
|
|
|
$category['description'] = $this->description(); |
261
|
|
|
|
262
|
|
|
if ($this->getVar('last_faqid') > 0) { |
263
|
|
|
$category['last_faqid'] = $this->getVar('last_faqid', 'n'); |
264
|
|
|
$category['last_question_link'] = $this->getVar('last_question_link', 'n'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $category; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.