Total Complexity | 169 |
Total Lines | 1025 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FaqHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FaqHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class FaqHandler extends \XoopsObjectHandler |
||
26 | { |
||
27 | protected $helper; |
||
28 | /** |
||
29 | * @param \XoopsDatabase $db |
||
30 | * @param null|\XoopsModules\Smartfaq\Helper $helper |
||
31 | */ |
||
32 | public function __construct(\XoopsDatabase $db = null, \XoopsModules\Smartfaq\Helper $helper = null) |
||
33 | { |
||
34 | /** @var \XoopsModules\Smartfaq\Helper $this->helper */ |
||
35 | if (null === $helper) { |
||
36 | $this->helper = \XoopsModules\Smartfaq\Helper::getInstance(); |
||
37 | } else { |
||
38 | $this->helper = $helper; |
||
39 | } |
||
40 | $smartfaqIsAdmin = $this->helper->isUserAdmin(); |
||
|
|||
41 | parent::__construct($db, 'smartfaq_faq', Faq::class, 'faqid', 'faqid'); |
||
42 | } |
||
43 | /** |
||
44 | * @param bool $isNew |
||
45 | * @return Smartfaq\Faq |
||
46 | */ |
||
47 | public function create($isNew = true) |
||
48 | { |
||
49 | $faq = new Smartfaq\Faq(); |
||
50 | if ($isNew) { |
||
51 | $faq->setDefaultPermissions(); |
||
52 | $faq->setNew(); |
||
53 | } |
||
54 | |||
55 | return $faq; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * retrieve an FAQ |
||
60 | * |
||
61 | * @param int $id faqid of the user |
||
62 | * @return mixed reference to the {@link Smartfaq\Faq} object, FALSE if failed |
||
63 | */ |
||
64 | public function get($id) |
||
65 | { |
||
66 | if ((int)$id > 0) { |
||
67 | $sql = 'SELECT * FROM ' . $this->db->prefix('smartfaq_faq') . ' WHERE faqid=' . $id; |
||
68 | if (!$result = $this->db->query($sql)) { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | $numrows = $this->db->getRowsNum($result); |
||
73 | if (1 == $numrows) { |
||
74 | $faq = new Smartfaq\Faq(); |
||
75 | $faq->assignVars($this->db->fetchArray($result)); |
||
76 | |||
77 | return $faq; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return false; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * insert a new faq in the database |
||
86 | * |
||
87 | * @param \XoopsObject $faq reference to the {@link Smartfaq\Faq} |
||
88 | * object |
||
89 | * @param bool $force |
||
90 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
91 | */ |
||
92 | public function insert(\XoopsObject $faq, $force = false) |
||
93 | { |
||
94 | if ('xoopsmodules\smartfaq\faq' !== mb_strtolower(get_class($faq))) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | if (!$faq->isDirty()) { |
||
99 | return true; |
||
100 | } |
||
101 | |||
102 | if (!$faq->cleanVars()) { |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | foreach ($faq->cleanVars as $k => $v) { |
||
107 | ${$k} = $v; |
||
108 | } |
||
109 | |||
110 | if ($faq->isNew()) { |
||
111 | $sql = sprintf('INSERT INTO `%s` (faqid, categoryid, question, howdoi, diduno, uid, datesub, status, counter, weight, html, smiley, xcodes, cancomment, comments, notifypub, modulelink, contextpage, exacturl, partialview) VALUES (NULL, %u, %s, %s, %s, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %s, %s, %u, %u)', |
||
112 | $this->db->prefix('smartfaq_faq'), $categoryid, $this->db->quoteString($question), $this->db->quoteString($howdoi), $this->db->quoteString($diduno), $uid, time(), $status, $counter, $weight, $html, $smiley, $xcodes, $cancomment, $comments, $notifypub, |
||
113 | $this->db->quoteString($modulelink), $this->db->quoteString($contextpage), $exacturl, $partialview); |
||
114 | } else { |
||
115 | $sql = sprintf('UPDATE `%s` SET categoryid = %u, question = %s, howdoi = %s, diduno = %s, uid = %u, datesub = %u, status = %u, counter = %u, weight = %u, html = %u, smiley = %u, xcodes = %u, cancomment = %u, comments = %u, notifypub = %u, modulelink = %s, contextpage = %s, exacturl = %u, partialview = %u WHERE faqid = %u', |
||
116 | |||
117 | $this->db->prefix('smartfaq_faq'), $categoryid, $this->db->quoteString($question), $this->db->quoteString($howdoi), $this->db->quoteString($diduno), $uid, $datesub, $status, $counter, $weight, $html, $smiley, $xcodes, $cancomment, $comments, $notifypub, |
||
118 | $this->db->quoteString($modulelink), $this->db->quoteString($contextpage), $exacturl, $partialview, $faqid); |
||
119 | } |
||
120 | if (false !== $force) { |
||
121 | $result = $this->db->queryF($sql); |
||
122 | } else { |
||
123 | $result = $this->db->query($sql); |
||
124 | } |
||
125 | |||
126 | if (!$result) { |
||
127 | $faq->setErrors('Could not store data in the database.<br />' . $this->db->error() . ' (' . $this->db->errno() . ')<br />' . $sql); |
||
128 | |||
129 | $logger = \XoopsLogger::getInstance(); |
||
130 | $logger->handleError(E_USER_WARNING, $sql, __FILE__, __LINE__); |
||
131 | $logger->addExtra('Token Validation', 'No valid token found in request/session'); |
||
132 | |||
133 | /** @var Smartfaq\Helper $helper */ |
||
134 | $helper = Smartfaq\Helper::getInstance(); |
||
135 | $helper->addLog($this->db->error()); |
||
136 | |||
137 | /** @var \XoopsObject $faq */ |
||
138 | // $faq->setError($this->db->error()); |
||
139 | |||
140 | trigger_error('Class ' . $faq . ' could not be saved ' . __FILE__ . ' at line ' . __LINE__, E_USER_WARNING); |
||
141 | |||
142 | return false; |
||
143 | } |
||
144 | |||
145 | if ($faq->isNew()) { |
||
146 | $faq->assignVar('faqid', $this->db->getInsertId()); |
||
147 | } |
||
148 | |||
149 | // Saving permissions |
||
150 | Smartfaq\Utility::saveItemPermissions($faq->getGroups_read(), $faq->faqid()); |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * delete an FAQ from the database |
||
157 | * |
||
158 | * @param \XoopsObject $faq reference to the FAQ to delete |
||
159 | * @param bool $force |
||
160 | * @return bool FALSE if failed. |
||
161 | */ |
||
162 | public function delete(\XoopsObject $faq, $force = false) |
||
163 | { |
||
164 | $smartModule = Smartfaq\Utility::getModuleInfo(); |
||
165 | $module_id = $smartModule->getVar('mid'); |
||
166 | |||
167 | // if ('XoopsModules\Smartfaq\Faq' !== mb_strtolower(get_class($faq))) { |
||
168 | if (Faq::class !== get_class($faq)) { |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | // Deleting the answers |
||
173 | $answerHandler = new Smartfaq\AnswerHandler($this->db); |
||
174 | if (!$answerHandler->deleteFaqAnswers($faq)) { |
||
175 | // error msg... |
||
176 | echo 'error while deleteing an answer'; |
||
177 | } |
||
178 | |||
179 | $sql = sprintf('DELETE FROM `%s` WHERE faqid = %u', $this->db->prefix('smartfaq_faq'), $faq->getVar('faqid')); |
||
180 | |||
181 | if (false !== $force) { |
||
182 | $result = $this->db->queryF($sql); |
||
183 | } else { |
||
184 | $result = $this->db->query($sql); |
||
185 | } |
||
186 | if (!$result) { |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | xoops_groupperm_deletebymoditem($module_id, 'item_read', $faq->faqid()); |
||
191 | |||
192 | return true; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * retrieve FAQs from the database |
||
197 | * |
||
198 | * @param \CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
199 | * @param bool $id_as_key use the faqid as key for the array? |
||
200 | * @param string $notNullFields |
||
201 | * @return false|array array of <a href='psi_element://Smartfaq\Faq'>Smartfaq\Faq</a> objects |
||
202 | */ |
||
203 | public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $notNullFields = '') |
||
204 | { |
||
205 | $ret = []; |
||
206 | $limit = $start = 0; |
||
207 | $sql = 'SELECT * FROM ' . $this->db->prefix('smartfaq_faq'); |
||
208 | |||
209 | if (null !== $criteria && $criteria instanceof \CriteriaElement) { |
||
210 | $whereClause = $criteria->renderWhere(); |
||
211 | |||
212 | if ('WHERE ()' !== $whereClause) { |
||
213 | $sql .= ' ' . $criteria->renderWhere(); |
||
214 | if (!empty($notNullFields)) { |
||
215 | $sql .= $this->NotNullFieldClause($notNullFields, true); |
||
216 | } |
||
217 | } elseif (!empty($notNullFields)) { |
||
218 | $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
219 | } |
||
220 | if ('' != $criteria->getSort()) { |
||
221 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
222 | } |
||
223 | $limit = $criteria->getLimit(); |
||
224 | $start = $criteria->getStart(); |
||
225 | } elseif (!empty($notNullFields)) { |
||
226 | $sql .= $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
227 | } |
||
228 | |||
229 | //echo "<br>" . $sql . "<br>"; |
||
230 | $result = $this->db->query($sql, $limit, $start); |
||
231 | if (!$result) { |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) { |
||
236 | $temp = false; |
||
237 | |||
238 | return $temp; |
||
239 | } |
||
240 | |||
241 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
242 | $faq = new Smartfaq\Faq(); |
||
243 | $faq->assignVars($myrow); |
||
244 | |||
245 | if (!$id_as_key) { |
||
246 | $ret[] = &$faq; |
||
247 | } else { |
||
248 | $ret[$myrow['faqid']] = &$faq; |
||
249 | } |
||
250 | unset($faq); |
||
251 | } |
||
252 | |||
253 | return $ret; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param null|\CriteriaElement $criteria |
||
258 | * @param bool $id_as_key |
||
259 | * @param string $notNullFields |
||
260 | * @return array|bool |
||
261 | */ |
||
262 | public function getObjectsAdminSide(\CriteriaElement $criteria = null, $id_as_key = false, $notNullFields = '') |
||
263 | { |
||
264 | $ret = []; |
||
265 | $limit = $start = 0; |
||
266 | $sql = 'SELECT |
||
267 | faq.faqid AS faqid, |
||
268 | faq.categoryid AS categoryid, |
||
269 | faq.question AS question, |
||
270 | faq.howdoi AS howdoi, |
||
271 | faq.diduno AS diduno, |
||
272 | faq.uid AS uid, |
||
273 | faq.datesub AS datesub, |
||
274 | faq.status AS status, |
||
275 | faq.counter AS counter, |
||
276 | faq.weight AS weight, |
||
277 | faq.html AS html, |
||
278 | faq.smiley AS smiley, |
||
279 | faq.image AS image, |
||
280 | faq.linebreak AS linebreak, |
||
281 | faq.xcodes AS xcodes, |
||
282 | faq.cancomment AS cancomment, |
||
283 | faq.comments AS comments, |
||
284 | faq.notifypub AS notifypub, |
||
285 | faq.modulelink AS modulelink, |
||
286 | faq.contextpage AS contextpage, |
||
287 | faq.exacturl AS exacturl |
||
288 | FROM ' . $this->db->prefix('smartfaq_faq') . ' AS faq INNER JOIN ' . $this->db->prefix('smartfaq_categories') . ' AS category ON faq.categoryid = category.categoryid '; |
||
289 | |||
290 | if (null !== $criteria && $criteria instanceof \CriteriaElement) { |
||
291 | $whereClause = $criteria->renderWhere(); |
||
292 | |||
293 | if ('WHERE ()' !== $whereClause) { |
||
294 | $sql .= ' ' . $criteria->renderWhere(); |
||
295 | if (!empty($notNullFields)) { |
||
296 | $sql .= $this->NotNullFieldClause($notNullFields, true); |
||
297 | } |
||
298 | } elseif (!empty($notNullFields)) { |
||
299 | $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
300 | } |
||
301 | if ('' != $criteria->getSort()) { |
||
302 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
303 | } |
||
304 | $limit = $criteria->getLimit(); |
||
305 | $start = $criteria->getStart(); |
||
306 | } elseif (!empty($notNullFields)) { |
||
307 | $sql .= $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
308 | } |
||
309 | |||
310 | //echo "<br>" . $sql . "<br>"; |
||
311 | $result = $this->db->query($sql, $limit, $start); |
||
312 | if (!$result) { |
||
313 | return false; |
||
314 | } |
||
315 | |||
316 | if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) { |
||
317 | return false; |
||
318 | } |
||
319 | |||
320 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
321 | $faq = new Smartfaq\Faq(); |
||
322 | $faq->assignVars($myrow); |
||
323 | |||
324 | if (!$id_as_key) { |
||
325 | $ret[] = &$faq; |
||
326 | } else { |
||
327 | $ret[$myrow['faqid']] = &$faq; |
||
328 | } |
||
329 | unset($faq); |
||
330 | } |
||
331 | |||
332 | return $ret; |
||
333 | /*while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
334 | $faq = new Smartfaq\Faq($myrow['faqid']); |
||
335 | |||
336 | if (!$id_as_key) { |
||
337 | $ret[] =& $faq; |
||
338 | } else { |
||
339 | $ret[$myrow['faqid']] =& $faq; |
||
340 | } |
||
341 | unset($faq); |
||
342 | } |
||
343 | |||
344 | return $ret;*/ |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * count FAQs matching a condition |
||
349 | * |
||
350 | * @param object $criteria {@link CriteriaElement} to match |
||
351 | * @param string $notNullFields |
||
352 | * @return int count of FAQs |
||
353 | */ |
||
354 | public function getCount($criteria = null, $notNullFields = '') |
||
355 | { |
||
356 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('smartfaq_faq'); |
||
357 | if (null !== $criteria && $criteria instanceof \CriteriaElement) { |
||
358 | $whereClause = $criteria->renderWhere(); |
||
359 | if ('WHERE ()' !== $whereClause) { |
||
360 | $sql .= ' ' . $criteria->renderWhere(); |
||
361 | if (!empty($notNullFields)) { |
||
362 | $sql .= $this->NotNullFieldClause($notNullFields, true); |
||
363 | } |
||
364 | } elseif (!empty($notNullFields)) { |
||
365 | $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
366 | } |
||
367 | } elseif (!empty($notNullFields)) { |
||
368 | $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields); |
||
369 | } |
||
370 | |||
371 | //echo "<br>" . $sql . "<br>"; |
||
372 | $result = $this->db->query($sql); |
||
373 | if (!$result) { |
||
374 | return 0; |
||
375 | } |
||
376 | list($count) = $this->db->fetchRow($result); |
||
377 | |||
378 | return $count; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param int $categoryid |
||
383 | * @param string $status |
||
384 | * @param string $notNullFields |
||
385 | * @return int |
||
386 | */ |
||
387 | public function getFaqsCount($categoryid = -1, $status = '', $notNullFields = '') |
||
388 | { |
||
389 | global $xoopsUser; |
||
390 | |||
391 | // if ( ($categoryid = -1) && (empty($status) || ($status == -1)) ) { |
||
392 | //return $this->getCount(); |
||
393 | //} |
||
394 | |||
395 | $criteriaCategory = null; |
||
396 | $userIsAdmin = Smartfaq\Utility::userIsAdmin(); |
||
397 | // Categories for which user has access |
||
398 | if (!$userIsAdmin) { |
||
399 | /** @var Smartfaq\PermissionHandler $smartPermHandler */ |
||
400 | $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission'); |
||
401 | |||
402 | $categoriesGranted = $smartPermHandler->getPermissions('category'); |
||
403 | $grantedCategories = new \Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'); |
||
404 | |||
405 | $faqsGranted = $smartPermHandler->getPermissions('item'); |
||
406 | $grantedFaq = new \CriteriaCompo(); |
||
407 | $grantedFaq->add(new \Criteria('faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR'); |
||
408 | // If user is anonymous, check if the FAQ allow partialview |
||
409 | if (!is_object($xoopsUser)) { |
||
410 | $grantedFaq->add(new \Criteria('partialview', '1'), 'OR'); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | if (isset($categoryid) && (-1 != $categoryid)) { |
||
415 | $criteriaCategory = new \Criteria('categoryid', $categoryid); |
||
416 | } |
||
417 | |||
418 | $criteriaStatus = new \CriteriaCompo(); |
||
419 | if (!empty($status) && is_array($status)) { |
||
420 | foreach ($status as $v) { |
||
421 | $criteriaStatus->add(new \Criteria('status', $v), 'OR'); |
||
422 | } |
||
423 | } elseif (!empty($status) && (-1 != $status)) { |
||
424 | $criteriaStatus->add(new \Criteria('status', $status), 'OR'); |
||
425 | } |
||
426 | |||
427 | $criteriaPermissions = new \CriteriaCompo(); |
||
428 | if (!$userIsAdmin) { |
||
429 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
430 | $criteriaPermissions->add($grantedFaq, 'AND'); |
||
431 | } |
||
432 | |||
433 | $criteria = new \CriteriaCompo(); |
||
434 | if (null !== $criteriaCategory) { |
||
435 | $criteria->add($criteriaCategory); |
||
436 | } |
||
437 | |||
438 | if (null !== $criteriaPermissions && (!$userIsAdmin)) { |
||
439 | $criteria->add($criteriaPermissions); |
||
440 | } |
||
441 | |||
442 | if (null !== $criteriaStatus) { |
||
443 | $criteria->add($criteriaStatus); |
||
444 | } |
||
445 | |||
446 | return $this->getCount($criteria, $notNullFields); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @return array |
||
451 | */ |
||
452 | public function getFaqsCountByStatus() |
||
453 | { |
||
454 | $sql = 'SELECT status, COUNT(*) FROM ' . $this->db->prefix('smartfaq_faq') . ' GROUP BY status'; |
||
455 | $result = $this->db->query($sql); |
||
456 | if (!$result) { |
||
457 | return []; |
||
458 | } |
||
459 | $ret = []; |
||
460 | while (list($status, $count) = $this->db->fetchRow($result)) { |
||
461 | $ret[$status] = $count; |
||
462 | } |
||
463 | |||
464 | return $ret; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @param int $limit |
||
469 | * @param int $start |
||
470 | * @param int $categoryid |
||
471 | * @param string $sort |
||
472 | * @param string $order |
||
473 | * @param bool $asobject |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getAllPublished( |
||
477 | $limit = 0, |
||
478 | $start = 0, |
||
479 | $categoryid = -1, |
||
480 | $sort = 'datesub', |
||
481 | $order = 'DESC', |
||
482 | $asobject = true) |
||
483 | { |
||
484 | return $this->getFaqs($limit, $start, [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], $categoryid, $sort, $order, null, $asobject, null); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * @param int $limit |
||
489 | * @param int $start |
||
490 | * @param string $status |
||
491 | * @param int $categoryid |
||
492 | * @param string $sort |
||
493 | * @param string $order |
||
494 | * @param string $notNullFields |
||
495 | * @param bool $asobject |
||
496 | * @param null $otherCriteria |
||
497 | * @return array |
||
498 | */ |
||
499 | public function getFaqs( |
||
500 | $limit = 0, |
||
501 | $start = 0, |
||
502 | $status = '', |
||
503 | $categoryid = -1, |
||
504 | $sort = 'datesub', |
||
505 | $order = 'DESC', |
||
506 | $notNullFields = '', |
||
507 | $asobject = true, |
||
508 | $otherCriteria = null) |
||
509 | { |
||
510 | global $xoopsUser; |
||
511 | // require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php'; |
||
512 | |||
513 | //if ( ($categoryid == -1) && (empty($status) || ($status == -1)) && ($limit == 0) && ($start ==0) ) { |
||
514 | // return $this->getObjects(); |
||
515 | //} |
||
516 | $ret = []; |
||
517 | $userIsAdmin = Smartfaq\Utility::userIsAdmin(); |
||
518 | $criteriaCategory = null; |
||
519 | // Categories for which user has access |
||
520 | if (!$userIsAdmin) { |
||
521 | /** @var Smartfaq\PermissionHandler $smartPermHandler */ |
||
522 | $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission'); |
||
523 | |||
524 | $categoriesGranted = $smartPermHandler->getPermissions('category'); |
||
525 | $grantedCategories = new \Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'); |
||
526 | |||
527 | $faqsGranted = $smartPermHandler->getPermissions('item'); |
||
528 | $grantedFaq = new \CriteriaCompo(); |
||
529 | $grantedFaq->add(new \Criteria('faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR'); |
||
530 | // If user is anonymous, check if the FAQ allow partialview |
||
531 | if (!is_object($xoopsUser)) { |
||
532 | $grantedFaq->add(new \Criteria('partialview', '1'), 'OR'); |
||
533 | } |
||
534 | } |
||
535 | |||
536 | if (isset($categoryid) && (-1 != $categoryid)) { |
||
537 | if (is_array($categoryid)) { |
||
538 | $criteriaCategory = new \Criteria('categoryid', '(' . implode(',', $categoryid) . ')', 'IN'); |
||
539 | } else { |
||
540 | $criteriaCategory = new \Criteria('categoryid', (int)$categoryid); |
||
541 | } |
||
542 | } |
||
543 | |||
544 | if (!empty($status) && is_array($status)) { |
||
545 | $criteriaStatus = new \CriteriaCompo(); |
||
546 | foreach ($status as $v) { |
||
547 | $criteriaStatus->add(new \Criteria('status', $v), 'OR'); |
||
548 | } |
||
549 | } elseif (!empty($status) && (-1 != $status)) { |
||
550 | $criteriaStatus = new \CriteriaCompo(); |
||
551 | $criteriaStatus->add(new \Criteria('status', $status), 'OR'); |
||
552 | } |
||
553 | |||
554 | $criteriaPermissions = new \CriteriaCompo(); |
||
555 | if (!$userIsAdmin) { |
||
556 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
557 | $criteriaPermissions->add($grantedFaq, 'AND'); |
||
558 | } |
||
559 | |||
560 | $criteria = new \CriteriaCompo(); |
||
561 | if (null !== $criteriaCategory) { |
||
562 | $criteria->add($criteriaCategory); |
||
563 | } |
||
564 | |||
565 | if ($criteriaPermissions !== null && (!$userIsAdmin)) { |
||
566 | $criteria->add($criteriaPermissions); |
||
567 | } |
||
568 | |||
569 | if ($criteriaStatus !== null) { |
||
570 | $criteria->add($criteriaStatus); |
||
571 | } |
||
572 | |||
573 | if (!empty($otherCriteria)) { |
||
574 | $criteria->add($otherCriteria); |
||
575 | } |
||
576 | |||
577 | $criteria->setLimit($limit); |
||
578 | $criteria->setStart($start); |
||
579 | $criteria->setSort($sort); |
||
580 | $criteria->setOrder($order); |
||
581 | $ret = $this->getObjects($criteria, false, $notNullFields); |
||
582 | |||
583 | return $ret; |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * @param int $limit |
||
588 | * @param int $start |
||
589 | * @param string $status |
||
590 | * @param int $categoryid |
||
591 | * @param string $sort |
||
592 | * @param string $order |
||
593 | * @param bool $asobject |
||
594 | * @param null $otherCriteria |
||
595 | * @return array|bool |
||
596 | */ |
||
597 | public function getFaqsAdminSide( |
||
598 | $limit = 0, |
||
599 | $start = 0, |
||
600 | $status = '', |
||
601 | $categoryid = -1, |
||
602 | $sort = 'datesub', |
||
603 | $order = 'DESC', |
||
604 | $asobject = true, |
||
605 | $otherCriteria = null) |
||
606 | { |
||
607 | // require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php'; |
||
608 | |||
609 | // $smartModule = Smartfaq\Utility::getModuleInfo(); |
||
610 | |||
611 | $ret = []; |
||
612 | $criteriaCategory = $criteriaStatus = null; |
||
613 | |||
614 | if (isset($categoryid) && (-1 != $categoryid)) { |
||
615 | $criteriaCategory = new \Criteria('faq.categoryid', $categoryid); |
||
616 | } |
||
617 | |||
618 | if (!empty($status) && is_array($status)) { |
||
619 | $criteriaStatus = new \CriteriaCompo(); |
||
620 | foreach ($status as $v) { |
||
621 | $criteriaStatus->add(new \Criteria('faq.status', $v), 'OR'); |
||
622 | } |
||
623 | } elseif (!empty($status) && (-1 != $status)) { |
||
624 | $criteriaStatus = new \CriteriaCompo(); |
||
625 | $criteriaStatus->add(new \Criteria('faq.status', $status), 'OR'); |
||
626 | } |
||
627 | |||
628 | $criteria = new \CriteriaCompo(); |
||
629 | if (null !== $criteriaCategory) { |
||
630 | $criteria->add($criteriaCategory); |
||
631 | } |
||
632 | |||
633 | if (null !== $criteriaStatus) { |
||
634 | $criteria->add($criteriaStatus); |
||
635 | } |
||
636 | |||
637 | if (!empty($otherCriteria)) { |
||
638 | $criteria->add($otherCriteria); |
||
639 | } |
||
640 | |||
641 | $criteria->setLimit($limit); |
||
642 | $criteria->setStart($start); |
||
643 | $criteria->setSort($sort); |
||
644 | $criteria->setOrder($order); |
||
645 | $ret = $this->getObjectsAdminSide($criteria, false); |
||
646 | |||
647 | return $ret; |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * @param string $field |
||
652 | * @param string $status |
||
653 | * @param int $category |
||
654 | * @return bool|mixed |
||
655 | */ |
||
656 | public function getRandomFaq($field = '', $status = '', $category = -1) |
||
657 | { |
||
658 | $ret = false; |
||
659 | |||
660 | $notNullFields = $field; |
||
661 | |||
662 | // Getting the number of published FAQ |
||
663 | $totalFaqs = $this->getFaqsCount(-1, $status, $notNullFields); |
||
664 | |||
665 | if ($totalFaqs > 0) { |
||
666 | --$totalFaqs; |
||
667 | $entrynumber = mt_rand(0, $totalFaqs); |
||
668 | $faq = $this->getFaqs(1, $entrynumber, $status, -1, 'datesub', 'DESC', $notNullFields); |
||
669 | if ($faq) { |
||
670 | $ret = &$faq[0]; |
||
671 | } |
||
672 | } |
||
673 | |||
674 | return $ret; |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * @param int $limit |
||
679 | * @return array|bool |
||
680 | */ |
||
681 | public function getContextualFaqs($limit = 0) |
||
682 | { |
||
683 | $ret = false; |
||
684 | |||
685 | $otherCriteria = new \CriteriaCompo(); |
||
686 | $otherCriteria->add(new \Criteria('modulelink', 'None', '<>')); |
||
687 | |||
688 | $faqsObj = $this->getFaqs(0, 0, [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], -1, 'datesub', 'DESC', '', true, $otherCriteria); |
||
689 | |||
690 | $totalfaqs = is_array($faqsObj) ? count($faqsObj) : 0; |
||
691 | $randomFaqs = []; |
||
692 | if ($faqsObj) { |
||
693 | foreach ($faqsObj as $i => $iValue) { |
||
694 | $display = false; |
||
695 | |||
696 | $http = (false === mb_strpos(XOOPS_URL, 'https://')) ? 'http://' : 'https://'; |
||
697 | $phpself = $_SERVER['PHP_SELF']; |
||
698 | $httphost = $_SERVER['HTTP_HOST']; |
||
699 | $querystring = $_SERVER['QUERY_STRING']; |
||
700 | if ('' != $querystring) { |
||
701 | $querystring = '?' . $querystring; |
||
702 | } |
||
703 | $currenturl = $http . $httphost . $phpself . $querystring; |
||
704 | $fullcontexturl = XOOPS_URL . '/' . $iValue->contextpage(); |
||
705 | switch ($iValue->modulelink()) { |
||
706 | case '': |
||
707 | $display = false; |
||
708 | break; |
||
709 | case 'None': |
||
710 | $display = false; |
||
711 | break; |
||
712 | case 'All': |
||
713 | $display = true; |
||
714 | break; |
||
715 | case 'url': |
||
716 | if ($iValue->exacturl()) { |
||
717 | $display = ($currenturl == $fullcontexturl); |
||
718 | } else { |
||
719 | $display = (false === mb_strpos($currenturl, $fullcontexturl)); |
||
720 | } |
||
721 | break; |
||
722 | default: |
||
723 | if (false === mb_strpos($currenturl, XOOPS_URL . '/modules/')) { |
||
724 | $display = false; |
||
725 | } else { |
||
726 | if (false === mb_strpos($currenturl, $iValue->modulelink())) { |
||
727 | $display = false; |
||
728 | } else { |
||
729 | $display = true; |
||
730 | } |
||
731 | } |
||
732 | break; |
||
733 | } |
||
734 | if ($display) { |
||
735 | $randomFaqs[] = &$faqsObj[$i]; |
||
736 | } |
||
737 | } |
||
738 | } |
||
739 | |||
740 | if (count($randomFaqs) > $limit) { |
||
741 | $rand_keys = array_rand($randomFaqs, $limit); |
||
742 | for ($j = 0, $jMax = count($rand_keys); $j < $jMax; ++$j) { |
||
743 | $ret[] = &$randomFaqs[$rand_keys[$j]]; |
||
744 | } |
||
745 | } else { |
||
746 | $ret = &$randomFaqs; |
||
747 | } |
||
748 | |||
749 | return $ret; |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * @param array $status |
||
754 | * @return array |
||
755 | */ |
||
756 | public function getLastPublishedByCat($status = [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER]) |
||
757 | { |
||
758 | $ret = []; |
||
759 | $faqclause = ''; |
||
760 | if (!Smartfaq\Utility::userIsAdmin()) { |
||
761 | /** @var Smartfaq\PermissionHandler $smartPermHandler */ |
||
762 | $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission'); |
||
763 | $items = $smartPermHandler->getPermissions('item'); |
||
764 | $faqclause = ' AND faqid IN (' . implode(',', $items) . ')'; |
||
765 | } |
||
766 | |||
767 | $sql = "CREATE TEMPORARY TABLE tmp (categoryid INT(8) UNSIGNED NOT NULL,datesub INT(11) DEFAULT '0' NOT NULL);"; |
||
768 | $sql2 = ' LOCK TABLES ' . $this->db->prefix('smartfaq_faq') . ' READ;'; |
||
769 | $sql3 = ' INSERT INTO tmp SELECT categoryid, MAX(datesub) FROM ' . $this->db->prefix('smartfaq_faq') . ' WHERE status IN (' . implode(',', $status) . ") $faqclause GROUP BY categoryid;"; |
||
770 | $sql4 = ' SELECT ' . $this->db->prefix('smartfaq_faq') . '.categoryid, faqid, question, uid, ' . $this->db->prefix('smartfaq_faq') . '.datesub FROM ' . $this->db->prefix('smartfaq_faq') . ', tmp |
||
771 | WHERE ' . $this->db->prefix('smartfaq_faq') . '.categoryid=tmp.categoryid AND ' . $this->db->prefix('smartfaq_faq') . '.datesub=tmp.datesub;'; |
||
772 | /* |
||
773 | //Old implementation |
||
774 | $sql = "SELECT categoryid, faqid, question, uid, MAX(datesub) AS datesub FROM ".$this->db->prefix("smartfaq_faq")." |
||
775 | WHERE status IN (". implode(',', $status).")"; |
||
776 | $sql .= " GROUP BY categoryid"; |
||
777 | */ |
||
778 | $this->db->queryF($sql); |
||
779 | $this->db->queryF($sql2); |
||
780 | $this->db->queryF($sql3); |
||
781 | $result = $this->db->query($sql4); |
||
782 | $error = $this->db->error(); |
||
783 | $this->db->queryF('UNLOCK TABLES;'); |
||
784 | $this->db->queryF('DROP TABLE tmp;'); |
||
785 | if (!$result) { |
||
786 | trigger_error('Error in getLastPublishedByCat SQL: ' . $error); |
||
787 | |||
788 | return $ret; |
||
789 | } |
||
790 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
791 | $faq = new Smartfaq\Faq(); |
||
792 | $faq->assignVars($row); |
||
793 | $ret[$row['categoryid']] = &$faq; |
||
794 | unset($faq); |
||
795 | } |
||
796 | |||
797 | return $ret; |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * delete FAQs matching a set of conditions |
||
802 | * |
||
803 | * @param object $criteria {@link CriteriaElement} |
||
804 | * @return bool FALSE if deletion failed |
||
805 | */ |
||
806 | public function deleteAll($criteria = null) |
||
807 | { |
||
808 | $sql = 'DELETE FROM ' . $this->db->prefix('smartfaq_faq'); |
||
809 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
810 | $sql .= ' ' . $criteria->renderWhere(); |
||
811 | } |
||
812 | if (!$this->db->query($sql)) { |
||
813 | return false; |
||
814 | // TODO : Also delete the permissions related to each FAQ |
||
815 | } |
||
816 | |||
817 | return true; |
||
818 | } |
||
819 | |||
820 | /** |
||
821 | * Change a value for FAQ with a certain criteria |
||
822 | * |
||
823 | * @param string $fieldname Name of the field |
||
824 | * @param string $fieldvalue Value to write |
||
825 | * @param object $criteria {@link CriteriaElement} |
||
826 | * |
||
827 | * @return bool |
||
828 | **/ |
||
829 | public function updateAll($fieldname, $fieldvalue, $criteria = null) |
||
830 | { |
||
831 | $set_clause = is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue); |
||
832 | $sql = 'UPDATE ' . $this->db->prefix('smartfaq_faq') . ' SET ' . $set_clause; |
||
833 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
834 | $sql .= ' ' . $criteria->renderWhere(); |
||
835 | } |
||
836 | if (!$this->db->queryF($sql)) { |
||
837 | return false; |
||
838 | } |
||
839 | |||
840 | return true; |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * @param $faqid |
||
845 | * @return bool |
||
846 | */ |
||
847 | public function updateCounter($faqid) |
||
848 | { |
||
849 | $sql = 'UPDATE ' . $this->db->prefix('smartfaq_faq') . ' SET counter=counter+1 WHERE faqid = ' . $faqid; |
||
850 | if ($this->db->queryF($sql)) { |
||
851 | return true; |
||
852 | } |
||
853 | |||
854 | return false; |
||
855 | } |
||
856 | |||
857 | /** |
||
858 | * @param string $notNullFields |
||
859 | * @param bool $withAnd |
||
860 | * @return string |
||
861 | */ |
||
862 | public function NotNullFieldClause($notNullFields = '', $withAnd = false) |
||
863 | { |
||
864 | $ret = ''; |
||
865 | if ($withAnd) { |
||
866 | $ret .= ' AND '; |
||
867 | } |
||
868 | if (!empty($notNullFields) && is_array($notNullFields)) { |
||
869 | foreach ($notNullFields as $v) { |
||
870 | $ret .= " ($v IS NOT NULL AND $v <> ' ' )"; |
||
871 | } |
||
872 | } elseif (!empty($notNullFields)) { |
||
873 | $ret .= " ($notNullFields IS NOT NULL AND $notNullFields <> ' ' )"; |
||
874 | } |
||
875 | |||
876 | return $ret; |
||
877 | } |
||
878 | |||
879 | /** |
||
880 | * @param array $queryarray |
||
881 | * @param string $andor |
||
882 | * @param int $limit |
||
883 | * @param int $offset |
||
884 | * @param int $userid |
||
885 | * @return array |
||
886 | */ |
||
887 | public function getFaqsFromSearch($queryarray = [], $andor = 'AND', $limit = 0, $offset = 0, $userid = 0) |
||
888 | { |
||
889 | global $xoopsUser; |
||
890 | |||
891 | $ret = []; |
||
892 | |||
893 | $userIsAdmin = Smartfaq\Utility::userIsAdmin(); |
||
894 | |||
895 | if (0 != $userid) { |
||
896 | $criteriaUser = new \CriteriaCompo(); |
||
897 | $criteriaUser->add(new \Criteria('faq.uid', $userid), 'OR'); |
||
898 | $criteriaUser->add(new \Criteria('answer.uid', $userid), 'OR'); |
||
899 | } |
||
900 | |||
901 | if (!empty($queryarray)) { |
||
902 | $criteriaKeywords = new \CriteriaCompo(); |
||
903 | foreach ($queryarray as $iValue) { |
||
904 | $criteriaKeyword = new \CriteriaCompo(); |
||
905 | $criteriaKeyword->add(new \Criteria('faq.question', '%' . $iValue . '%', 'LIKE'), 'OR'); |
||
906 | $criteriaKeyword->add(new \Criteria('answer.answer', '%' . $iValue . '%', 'LIKE'), 'OR'); |
||
907 | $criteriaKeywords->add($criteriaKeyword, $andor); |
||
908 | unset($criteriaKeyword); |
||
909 | } |
||
910 | } |
||
911 | |||
912 | // Categories for which user has access |
||
913 | if (!$userIsAdmin) { |
||
914 | /** @var Smartfaq\PermissionHandler $smartPermHandler */ |
||
915 | $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission'); |
||
916 | |||
917 | $categoriesGranted = $smartPermHandler->getPermissions('category'); |
||
918 | $faqsGranted = $smartPermHandler->getPermissions('item'); |
||
919 | if (empty($categoriesGranted)) { |
||
920 | return $ret; |
||
921 | } |
||
922 | if (empty($faqsGranted)) { |
||
923 | return $ret; |
||
924 | } |
||
925 | $grantedCategories = new \Criteria('faq.categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'); |
||
926 | $grantedFaq = new \CriteriaCompo(); |
||
927 | $grantedFaq->add(new \Criteria('faq.faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR'); |
||
928 | // If user is anonymous, check if the FAQ allow partialview |
||
929 | if (!is_object($xoopsUser)) { |
||
930 | $grantedFaq->add(new \Criteria('partialview', '1'), 'OR'); |
||
931 | } |
||
932 | } |
||
933 | |||
934 | $criteriaPermissions = new \CriteriaCompo(); |
||
935 | if (!$userIsAdmin) { |
||
936 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
937 | $criteriaPermissions->add($grantedFaq, 'AND'); |
||
938 | } |
||
939 | |||
940 | $criteriaAnswersStatus = new \CriteriaCompo(); |
||
941 | $criteriaAnswersStatus->add(new \Criteria('answer.status', Constants::SF_AN_STATUS_APPROVED)); |
||
942 | |||
943 | $criteriaFasStatus = new \CriteriaCompo(); |
||
944 | $criteriaFasStatus->add(new \Criteria('faq.status', Constants::SF_STATUS_OPENED), 'OR'); |
||
945 | $criteriaFasStatus->add(new \Criteria('faq.status', Constants::SF_STATUS_PUBLISHED), 'OR'); |
||
946 | |||
947 | $criteria = new \CriteriaCompo(); |
||
948 | if ($criteriaUser !== null) { |
||
949 | $criteria->add($criteriaUser, 'AND'); |
||
950 | } |
||
951 | |||
952 | if ($criteriaKeywords !== null) { |
||
953 | $criteria->add($criteriaKeywords, 'AND'); |
||
954 | } |
||
955 | |||
956 | if ($criteriaPermissions !== null && (!$userIsAdmin)) { |
||
957 | $criteria->add($criteriaPermissions); |
||
958 | } |
||
959 | |||
960 | if ($criteriaAnswersStatus !== null) { |
||
961 | $criteria->add($criteriaAnswersStatus, 'AND'); |
||
962 | } |
||
963 | |||
964 | if ($criteriaFasStatus !== null) { |
||
965 | $criteria->add($criteriaFasStatus, 'AND'); |
||
966 | } |
||
967 | |||
968 | $criteria->setLimit($limit); |
||
969 | $criteria->setStart($offset); |
||
970 | $criteria->setSort('faq.datesub'); |
||
971 | $criteria->setOrder('DESC'); |
||
972 | |||
973 | $sql = 'SELECT faq.faqid, faq.question, faq.datesub, faq.uid FROM ' . $this->db->prefix('smartfaq_faq') . ' AS faq INNER JOIN ' . $this->db->prefix('smartfaq_answers') . ' AS answer ON faq.faqid = answer.faqid'; |
||
974 | |||
975 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
976 | $whereClause = $criteria->renderWhere(); |
||
977 | |||
978 | if ('WHERE ()' !== $whereClause) { |
||
979 | $sql .= ' ' . $criteria->renderWhere(); |
||
980 | if ('' != $criteria->getSort()) { |
||
981 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
982 | } |
||
983 | $limit = $criteria->getLimit(); |
||
984 | $start = $criteria->getStart(); |
||
985 | } |
||
986 | } |
||
987 | |||
988 | //echo "<br>" . $sql . "<br>"; |
||
989 | |||
990 | $result = $this->db->query($sql, $limit, $start); |
||
991 | if (!$result) { |
||
992 | trigger_error('Query did not work in smartfaq', E_USER_WARNING); |
||
993 | |||
994 | return $ret; |
||
995 | } |
||
996 | |||
997 | if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) { |
||
998 | return $ret; |
||
999 | } |
||
1000 | |||
1001 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
1002 | $faq = new Smartfaq\Faq(); |
||
1003 | $faq->assignVars($myrow); |
||
1004 | $ret[] = &$faq; |
||
1005 | unset($faq); |
||
1006 | } |
||
1007 | |||
1008 | return $ret; |
||
1009 | } |
||
1010 | |||
1011 | /** |
||
1012 | * @param int $cat_id |
||
1013 | * @param $status |
||
1014 | * @return array |
||
1015 | */ |
||
1016 | public function getCountsByCat($cat_id, $status) |
||
1050 | } |
||
1051 | } |
||
1052 |