Passed
Branch master (4e30dc)
by Michael
02:20
created

FaqHandler::updateCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
use XoopsModules\Smartfaq\Constants;
11
12
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
13
14
//require_once XOOPS_ROOT_PATH . '/modules/smartfaq/class/category.php';
15
16
17
/**
18
 * Q&A handler class.
19
 * This class is responsible for providing data access mechanisms to the data source
20
 * of Q&A class objects.
21
 *
22
 * @author  marcan <[email protected]>
23
 * @package SmartFAQ
24
 */
25
class FaqHandler extends \XoopsObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
{
27
    /**
28
     * @param  bool $isNew
29
     * @return Smartfaq\Faq
30
     */
31
    public function create($isNew = true)
32
    {
33
        $faq = new Smartfaq\Faq();
34
        if ($isNew) {
35
            $faq->setDefaultPermissions();
36
            $faq->setNew();
37
        }
38
39
        return $faq;
40
    }
41
42
    /**
43
     * retrieve an FAQ
44
     *
45
     * @param  int $id faqid of the user
46
     * @return mixed reference to the {@link Smartfaq\Faq} object, FALSE if failed
47
     */
48
    public function get($id)
49
    {
50
        if ((int)$id > 0) {
51
            $sql = 'SELECT * FROM ' . $this->db->prefix('smartfaq_faq') . ' WHERE faqid=' . $id;
52
            if (!$result = $this->db->query($sql)) {
53
                return false;
54
            }
55
56
            $numrows = $this->db->getRowsNum($result);
57
            if (1 == $numrows) {
58
                $faq = new Smartfaq\Faq();
59
                $faq->assignVars($this->db->fetchArray($result));
60
61
                return $faq;
62
            }
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * insert a new faq in the database
70
     *
71
     * @param \XoopsObject $faq reference to the {@link Smartfaq\Faq}
72
     *                          object
73
     * @param  bool        $force
74
     * @return bool        FALSE if failed, TRUE if already present and unchanged or successful
75
     */
76
    public function insert(\XoopsObject $faq, $force = false)
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
    {
78
        if ('xoopsmodules\smartfaq\faq' !== strtolower(get_class($faq))) {
79
            return false;
80
        }
81
82
        if (!$faq->isDirty()) {
83
            return true;
84
        }
85
86
        if (!$faq->cleanVars()) {
87
            return false;
88
        }
89
90
        foreach ($faq->cleanVars as $k => $v) {
91
            ${$k} = $v;
92
        }
93
94
        if ($faq->isNew()) {
95
            $sql = sprintf(
96
                '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)',
97
                           $this->db->prefix('smartfaq_faq'),
98
                $categoryid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $categoryid seems to be never defined.
Loading history...
99
                $this->db->quoteString($question),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $question seems to be never defined.
Loading history...
100
                $this->db->quoteString($howdoi),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $howdoi seems to be never defined.
Loading history...
101
                $this->db->quoteString($diduno),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $diduno seems to be never defined.
Loading history...
102
                $uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
103
                time(),
104
                $status,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $status seems to be never defined.
Loading history...
105
                $counter,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $counter seems to be never defined.
Loading history...
106
                $weight,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
107
                $html,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $html seems to be never defined.
Loading history...
108
                $smiley,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $smiley seems to be never defined.
Loading history...
109
                $xcodes,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xcodes seems to be never defined.
Loading history...
110
                $cancomment,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cancomment seems to be never defined.
Loading history...
111
                $comments,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $comments seems to be never defined.
Loading history...
112
                $notifypub,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notifypub seems to be never defined.
Loading history...
113
                $this->db->quoteString($modulelink),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modulelink seems to be never defined.
Loading history...
114
                $this->db->quoteString($contextpage),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $contextpage seems to be never defined.
Loading history...
115
                $exacturl,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $exacturl seems to be never defined.
Loading history...
116
                $partialview
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $partialview seems to be never defined.
Loading history...
117
            );
118
        } else {
119
            $sql = sprintf(
120
                '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',
121
122
                $this->db->prefix('smartfaq_faq'),
123
                $categoryid,
124
                $this->db->quoteString($question),
125
                $this->db->quoteString($howdoi),
126
                $this->db->quoteString($diduno),
127
                $uid,
128
                $datesub,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $datesub seems to be never defined.
Loading history...
129
                $status,
130
                $counter,
131
                $weight,
132
                $html,
133
                $smiley,
134
                $xcodes,
135
                $cancomment,
136
                $comments,
137
                $notifypub,
138
                $this->db->quoteString($modulelink),
139
                $this->db->quoteString($contextpage),
140
                $exacturl,
141
                $partialview,
142
                $faqid
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $faqid does not exist. Did you maybe mean $faq?
Loading history...
143
            );
144
        }
145
        if (false !== $force) {
146
            $result = $this->db->queryF($sql);
147
        } else {
148
            $result = $this->db->query($sql);
149
        }
150
151
        if (!$result) {
152
            $faq->setErrors('Could not store data in the database.<br />'.$this->db->error().' ('.$this->db->errno().')<br />'.$sql);
153
154
            $logger = \XoopsLogger::getInstance();
0 ignored issues
show
Bug introduced by
The type XoopsLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
155
            $logger->handleError(E_USER_WARNING, $sql, __FILE__, __LINE__);
156
            $logger->addExtra('Token Validation', 'No valid token found in request/session');
157
158
159
            /** @var Smartfaq\Helper $helper */
160
            $helper = Smartfaq\Helper::getInstance();
161
            $helper->addLog($this->db->error());
162
163
            /** @var \XoopsObject $faq */
164
//            $faq->setError($this->db->error());
165
166
167
            trigger_error('Class ' . $faq . ' could not be saved ' . __FILE__ . ' at line ' . __LINE__, E_USER_WARNING);
168
169
            return false;
170
        }
171
172
        if ($faq->isNew()) {
173
            $faq->assignVar('faqid', $this->db->getInsertId());
174
        }
175
176
        // Saving permissions
177
        Smartfaq\Utility::saveItemPermissions($faq->getGroups_read(), $faq->faqid());
178
179
        return true;
180
    }
181
182
    /**
183
     * delete an FAQ from the database
184
     *
185
     * @param \XoopsObject $faq reference to the FAQ to delete
186
     * @param  bool        $force
187
     * @return bool        FALSE if failed.
188
     */
189
    public function delete(\XoopsObject $faq, $force = false)
190
    {
191
        $smartModule = Smartfaq\Utility::getModuleInfo();
192
        $module_id   = $smartModule->getVar('mid');
193
194
        if ('xoopsmodules\smartfaq\faq' !== strtolower(get_class($faq))) {
195
            return false;
196
        }
197
198
        // Deleting the answers
199
        $answerHandler = new Smartfaq\AnswerHandler($this->db);
200
        if (!$answerHandler->deleteFaqAnswers($faq)) {
201
            // error msg...
202
            echo 'error while deleteing an answer';
203
        }
204
205
        $sql = sprintf('DELETE FROM %s WHERE faqid = %u', $this->db->prefix('smartfaq_faq'), $faq->getVar('faqid'));
206
207
        if (false !== $force) {
208
            $result = $this->db->queryF($sql);
209
        } else {
210
            $result = $this->db->query($sql);
211
        }
212
        if (!$result) {
213
            return false;
214
        }
215
216
        xoops_groupperm_deletebymoditem($module_id, 'item_read', $faq->faqid());
0 ignored issues
show
Bug introduced by
The function xoops_groupperm_deletebymoditem was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
        /** @scrutinizer ignore-call */ 
217
        xoops_groupperm_deletebymoditem($module_id, 'item_read', $faq->faqid());
Loading history...
217
218
        return true;
219
    }
220
221
    /**
222
     * retrieve FAQs from the database
223
     *
224
     * @param  \CriteriaElement $criteria  {@link CriteriaElement} conditions to be met
225
     * @param  bool            $id_as_key use the faqid as key for the array?
226
     * @param  string          $notNullFields
227
     * @return false|array  array of <a href='psi_element://Smartfaq\Faq'>Smartfaq\Faq</a> objects
228
     */
229
    public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $notNullFields = '')
0 ignored issues
show
Bug introduced by
The type CriteriaElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
230
    {
231
        $ret   = [];
232
        $limit = $start = 0;
233
        $sql   = 'SELECT * FROM ' . $this->db->prefix('smartfaq_faq');
234
235
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
236
            $whereClause = $criteria->renderWhere();
237
238
            if ('WHERE ()' !== $whereClause) {
239
                $sql .= ' ' . $criteria->renderWhere();
240
                if (!empty($notNullFields)) {
241
                    $sql .= $this->NotNullFieldClause($notNullFields, true);
242
                }
243
            } elseif (!empty($notNullFields)) {
244
                $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
245
            }
246
            if ('' != $criteria->getSort()) {
247
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
248
            }
249
            $limit = $criteria->getLimit();
250
            $start = $criteria->getStart();
251
        } elseif (!empty($notNullFields)) {
252
            $sql .= $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
253
        }
254
255
        //echo "<br>" . $sql . "<br>";
256
        $result = $this->db->query($sql, $limit, $start);
257
        if (!$result) {
258
            return false;
259
        }
260
261
        if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) {
262
            $temp = false;
263
            return $temp;
264
        }
265
266
        while (false !== ($myrow = $this->db->fetchArray($result))) {
267
            $faq = new Smartfaq\Faq();
268
            $faq->assignVars($myrow);
269
270
            if (!$id_as_key) {
271
                $ret[] =& $faq;
272
            } else {
273
                $ret[$myrow['faqid']] =& $faq;
274
            }
275
            unset($faq);
276
        }
277
278
        return $ret;
279
    }
280
281
    /**
282
     * @param  null|\CriteriaElement $criteria
283
     * @param  bool                 $id_as_key
284
     * @param  string               $notNullFields
285
     * @return array|bool
286
     */
287
    public function getObjectsAdminSide(\CriteriaElement $criteria = null, $id_as_key = false, $notNullFields = '')
288
    {
289
        $ret   = [];
290
        $limit = $start = 0;
291
        $sql   = 'SELECT
292
                            faq.faqid AS faqid,
293
                            faq.categoryid AS categoryid,
294
                            faq.question AS question,
295
                            faq.howdoi AS howdoi,
296
                            faq.diduno AS diduno,
297
                            faq.uid AS uid,
298
                            faq.datesub AS datesub,
299
                            faq.status AS status,
300
                            faq.counter AS counter,
301
                            faq.weight AS weight,
302
                            faq.html AS html,
303
                            faq.smiley AS smiley,
304
                            faq.image AS image,
305
                            faq.linebreak AS linebreak,
306
                            faq.xcodes AS xcodes,
307
                            faq.cancomment AS cancomment,
308
                            faq.comments AS comments,
309
                            faq.notifypub AS notifypub,
310
                            faq.modulelink AS modulelink,
311
                            faq.contextpage AS contextpage,
312
                            faq.exacturl AS exacturl
313
                FROM ' . $this->db->prefix('smartfaq_faq') . ' AS faq INNER JOIN ' . $this->db->prefix('smartfaq_categories') . ' AS category ON faq.categoryid = category.categoryid ';
314
315
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
316
            $whereClause = $criteria->renderWhere();
317
318
            if ('WHERE ()' !== $whereClause) {
319
                $sql .= ' ' . $criteria->renderWhere();
320
                if (!empty($notNullFields)) {
321
                    $sql .= $this->NotNullFieldClause($notNullFields, true);
322
                }
323
            } elseif (!empty($notNullFields)) {
324
                $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
325
            }
326
            if ('' != $criteria->getSort()) {
327
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
328
            }
329
            $limit = $criteria->getLimit();
330
            $start = $criteria->getStart();
331
        } elseif (!empty($notNullFields)) {
332
            $sql .= $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
333
        }
334
335
        //echo "<br>" . $sql . "<br>";
336
        $result = $this->db->query($sql, $limit, $start);
337
        if (!$result) {
338
            return false;
339
        }
340
341
        if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) {
342
            return false;
343
        }
344
345
        while (false !== ($myrow = $this->db->fetchArray($result))) {
346
            $faq = new Smartfaq\Faq();
347
            $faq->assignVars($myrow);
348
349
            if (!$id_as_key) {
350
                $ret[] =& $faq;
351
            } else {
352
                $ret[$myrow['faqid']] =& $faq;
353
            }
354
            unset($faq);
355
        }
356
357
        return $ret;
358
359
        /*while ($myrow = $this->db->fetchArray($result)) {
360
            $faq = new Smartfaq\Faq($myrow['faqid']);
361
362
            if (!$id_as_key) {
363
                $ret[] =& $faq;
364
            } else {
365
                $ret[$myrow['faqid']] =& $faq;
366
            }
367
            unset($faq);
368
        }
369
370
        return $ret;*/
371
    }
372
373
    /**
374
     * count FAQs matching a condition
375
     *
376
     * @param  object $criteria {@link CriteriaElement} to match
377
     * @param  string $notNullFields
378
     * @return int    count of FAQs
379
     */
380
    public function getCount($criteria = null, $notNullFields = '')
381
    {
382
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('smartfaq_faq');
383
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
384
            $whereClause = $criteria->renderWhere();
385
            if ('WHERE ()' !== $whereClause) {
386
                $sql .= ' ' . $criteria->renderWhere();
387
                if (!empty($notNullFields)) {
388
                    $sql .= $this->NotNullFieldClause($notNullFields, true);
389
                }
390
            } elseif (!empty($notNullFields)) {
391
                $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
392
            }
393
        } elseif (!empty($notNullFields)) {
394
            $sql .= ' WHERE ' . $this->NotNullFieldClause($notNullFields);
395
        }
396
397
        //echo "<br>" . $sql . "<br>";
398
        $result = $this->db->query($sql);
399
        if (!$result) {
400
            return 0;
401
        }
402
        list($count) = $this->db->fetchRow($result);
403
404
        return $count;
405
    }
406
407
    /**
408
     * @param  int    $categoryid
409
     * @param  string $status
410
     * @param  string $notNullFields
411
     * @return int
412
     */
413
    public function getFaqsCount($categoryid = -1, $status = '', $notNullFields = '')
414
    {
415
        global $xoopsUser;
416
417
        //  if ( ($categoryid = -1) && (empty($status) || ($status == -1)) ) {
418
        //return $this->getCount();
419
        //}
420
421
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
422
        // Categories for which user has access
423
        if (!$userIsAdmin) {
424
            /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
425
            $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
426
427
            $categoriesGranted = $smartPermHandler->getPermissions('category');
428
            $grantedCategories = new \Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN');
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
429
430
            $faqsGranted = $smartPermHandler->getPermissions('item');
431
            $grantedFaq  = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
432
            $grantedFaq->add(new \Criteria('faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR');
433
            // If user is anonymous, check if the FAQ allow partialview
434
            if (!is_object($xoopsUser)) {
435
                $grantedFaq->add(new \Criteria('partialview', '1'), 'OR');
436
            }
437
        }
438
439
        if (isset($categoryid) && (-1 != $categoryid)) {
440
            $criteriaCategory = new \Criteria('categoryid', $categoryid);
441
        }
442
443
        $criteriaStatus = new \CriteriaCompo();
444
        if (!empty($status) && is_array($status)) {
0 ignored issues
show
introduced by
The condition is_array($status) is always false.
Loading history...
445
            foreach ($status as $v) {
446
                $criteriaStatus->add(new \Criteria('status', $v), 'OR');
447
            }
448
        } elseif (!empty($status) && (-1 != $status)) {
449
            $criteriaStatus->add(new \Criteria('status', $status), 'OR');
450
        }
451
452
        $criteriaPermissions = new \CriteriaCompo();
453
        if (!$userIsAdmin) {
454
            $criteriaPermissions->add($grantedCategories, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedCategories does not seem to be defined for all execution paths leading up to this point.
Loading history...
455
            $criteriaPermissions->add($grantedFaq, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedFaq does not seem to be defined for all execution paths leading up to this point.
Loading history...
456
        }
457
458
        $criteria = new \CriteriaCompo();
459
        if (!empty($criteriaCategory)) {
460
            $criteria->add($criteriaCategory);
461
        }
462
463
        if (!empty($criteriaPermissions) && (!$userIsAdmin)) {
464
            $criteria->add($criteriaPermissions);
465
        }
466
467
        if (!empty($criteriaStatus)) {
468
            $criteria->add($criteriaStatus);
469
        }
470
471
        return $this->getCount($criteria, $notNullFields);
472
    }
473
474
    /**
475
     * @return array
476
     */
477
    public function getFaqsCountByStatus()
478
    {
479
        $sql    = 'SELECT status, COUNT(*) FROM ' . $this->db->prefix('smartfaq_faq') . ' GROUP BY status';
480
        $result = $this->db->query($sql);
481
        if (!$result) {
482
            return [];
483
        }
484
        $ret = [];
485
        while (list($status, $count) = $this->db->fetchRow($result)) {
486
            $ret[$status] = $count;
487
        }
488
489
        return $ret;
490
    }
491
492
    /**
493
     * @param  int    $limit
494
     * @param  int    $start
495
     * @param  int    $categoryid
496
     * @param  string $sort
497
     * @param  string $order
498
     * @param  bool   $asobject
499
     * @return array
500
     */
501
    public function getAllPublished(
502
        $limit = 0,
503
        $start = 0,
504
        $categoryid = -1,
505
        $sort = 'datesub',
506
        $order = 'DESC',
507
        $asobject = true
508
    ) {
509
        return $this->getFaqs($limit, $start, [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], $categoryid, $sort, $order, null, $asobject, null);
0 ignored issues
show
Bug introduced by
array(XoopsModules\Smart...::SF_STATUS_NEW_ANSWER) of type array<integer,integer> is incompatible with the type string expected by parameter $status of XoopsModules\Smartfaq\FaqHandler::getFaqs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

509
        return $this->getFaqs($limit, $start, /** @scrutinizer ignore-type */ [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], $categoryid, $sort, $order, null, $asobject, null);
Loading history...
510
    }
511
512
    /**
513
     * @param  int    $limit
514
     * @param  int    $start
515
     * @param  string $status
516
     * @param  int    $categoryid
517
     * @param  string $sort
518
     * @param  string $order
519
     * @param  string $notNullFields
520
     * @param  bool   $asobject
521
     * @param  null   $otherCriteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $otherCriteria is correct as it would always require null to be passed?
Loading history...
522
     * @return array
523
     */
524
    public function getFaqs(
525
        $limit = 0,
526
        $start = 0,
527
        $status = '',
528
        $categoryid = -1,
529
        $sort = 'datesub',
530
        $order = 'DESC',
531
        $notNullFields = '',
532
        $asobject = true,
533
        $otherCriteria = null
534
    ) {
535
        global $xoopsUser;
536
//        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
537
538
        //if ( ($categoryid == -1) && (empty($status) || ($status == -1)) && ($limit == 0) && ($start ==0) ) {
539
        //  return $this->getObjects();
540
        //}
541
        $ret         = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
542
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
543
        // Categories for which user has access
544
        if (!$userIsAdmin) {
545
            /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
546
            $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
547
548
            $categoriesGranted = $smartPermHandler->getPermissions('category');
549
            $grantedCategories = new \Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN');
550
551
            $faqsGranted = $smartPermHandler->getPermissions('item');
552
            $grantedFaq  = new \CriteriaCompo();
553
            $grantedFaq->add(new \Criteria('faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR');
554
            // If user is anonymous, check if the FAQ allow partialview
555
            if (!is_object($xoopsUser)) {
556
                $grantedFaq->add(new \Criteria('partialview', '1'), 'OR');
557
            }
558
        }
559
560
        if (isset($categoryid) && (-1 != $categoryid)) {
561
            if (is_array($categoryid)) {
562
                $criteriaCategory = new \Criteria('categoryid', '(' . implode(',', $categoryid) . ')', 'IN');
563
            } else {
564
                $criteriaCategory = new \Criteria('categoryid', (int)$categoryid);
565
            }
566
        }
567
568
        if (!empty($status) && is_array($status)) {
0 ignored issues
show
introduced by
The condition is_array($status) is always false.
Loading history...
569
            $criteriaStatus = new \CriteriaCompo();
570
            foreach ($status as $v) {
571
                $criteriaStatus->add(new \Criteria('status', $v), 'OR');
572
            }
573
        } elseif (!empty($status) && (-1 != $status)) {
574
            $criteriaStatus = new \CriteriaCompo();
575
            $criteriaStatus->add(new \Criteria('status', $status), 'OR');
576
        }
577
578
        $criteriaPermissions = new \CriteriaCompo();
579
        if (!$userIsAdmin) {
580
            $criteriaPermissions->add($grantedCategories, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedCategories does not seem to be defined for all execution paths leading up to this point.
Loading history...
581
            $criteriaPermissions->add($grantedFaq, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedFaq does not seem to be defined for all execution paths leading up to this point.
Loading history...
582
        }
583
584
        $criteria = new \CriteriaCompo();
585
        if (!empty($criteriaCategory)) {
586
            $criteria->add($criteriaCategory);
587
        }
588
589
        if (!empty($criteriaPermissions) && (!$userIsAdmin)) {
590
            $criteria->add($criteriaPermissions);
591
        }
592
593
        if (!empty($criteriaStatus)) {
594
            $criteria->add($criteriaStatus);
595
        }
596
597
        if (!empty($otherCriteria)) {
598
            $criteria->add($otherCriteria);
599
        }
600
601
        $criteria->setLimit($limit);
602
        $criteria->setStart($start);
603
        $criteria->setSort($sort);
604
        $criteria->setOrder($order);
605
        $ret =& $this->getObjects($criteria, false, $notNullFields);
606
607
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type false which is incompatible with the documented return type array.
Loading history...
608
    }
609
610
    /**
611
     * @param  int    $limit
612
     * @param  int    $start
613
     * @param  string $status
614
     * @param  int    $categoryid
615
     * @param  string $sort
616
     * @param  string $order
617
     * @param  bool   $asobject
618
     * @param  null   $otherCriteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $otherCriteria is correct as it would always require null to be passed?
Loading history...
619
     * @return array|bool
620
     */
621
    public function getFaqsAdminSide(
622
        $limit = 0,
623
        $start = 0,
624
        $status = '',
625
        $categoryid = -1,
626
        $sort = 'datesub',
627
        $order = 'DESC',
628
        $asobject = true,
629
        $otherCriteria = null
630
    ) {
631
//        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
632
633
        $smartModule = Smartfaq\Utility::getModuleInfo();
0 ignored issues
show
Unused Code introduced by
The assignment to $smartModule is dead and can be removed.
Loading history...
634
635
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
636
637
        if (isset($categoryid) && (-1 != $categoryid)) {
638
            $criteriaCategory = new \Criteria('faq.categoryid', $categoryid);
639
        }
640
641
        if (!empty($status) && is_array($status)) {
0 ignored issues
show
introduced by
The condition is_array($status) is always false.
Loading history...
642
            $criteriaStatus = new \CriteriaCompo();
643
            foreach ($status as $v) {
644
                $criteriaStatus->add(new \Criteria('faq.status', $v), 'OR');
645
            }
646
        } elseif (!empty($status) && (-1 != $status)) {
647
            $criteriaStatus = new \CriteriaCompo();
648
            $criteriaStatus->add(new \Criteria('faq.status', $status), 'OR');
649
        }
650
651
        $criteria = new \CriteriaCompo();
652
        if (!empty($criteriaCategory)) {
653
            $criteria->add($criteriaCategory);
654
        }
655
656
        if (!empty($criteriaStatus)) {
657
            $criteria->add($criteriaStatus);
658
        }
659
660
        if (!empty($otherCriteria)) {
661
            $criteria->add($otherCriteria);
662
        }
663
664
        $criteria->setLimit($limit);
665
        $criteria->setStart($start);
666
        $criteria->setSort($sort);
667
        $criteria->setOrder($order);
668
        $ret = $this->getObjectsAdminSide($criteria, false);
669
670
        return $ret;
671
    }
672
673
    /**
674
     * @param  string $field
675
     * @param  string $status
676
     * @param  int    $category
677
     * @return bool|mixed
678
     */
679
    public function getRandomFaq($field = '', $status = '', $category = -1)
680
    {
681
        $ret = false;
682
683
        $notNullFields = $field;
684
685
        // Getting the number of published FAQ
686
        $totalFaqs = $this->getFaqsCount(-1, $status, $notNullFields);
687
688
        if ($totalFaqs > 0) {
689
            --$totalFaqs;
690
            mt_srand((double)microtime() * 1000000);
0 ignored issues
show
Bug introduced by
(double)microtime() * 1000000 of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

690
            mt_srand(/** @scrutinizer ignore-type */ (double)microtime() * 1000000);
Loading history...
691
            $entrynumber = mt_rand(0, $totalFaqs);
692
            $faq         = $this->getFaqs(1, $entrynumber, $status, -1, 'datesub', 'DESC', $notNullFields);
693
            if ($faq) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $faq of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
694
                $ret =& $faq[0];
695
            }
696
        }
697
698
        return $ret;
699
    }
700
701
    /**
702
     * @param  int $limit
703
     * @return array|bool
704
     */
705
    public function getContextualFaqs($limit = 0)
706
    {
707
        $ret = false;
708
709
        $otherCriteria = new \CriteriaCompo();
710
        $otherCriteria->add(new \Criteria('modulelink', 'None', '<>'));
711
712
        $faqsObj = $this->getFaqs(0, 0, [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], -1, 'datesub', 'DESC', '', true, $otherCriteria);
0 ignored issues
show
Bug introduced by
array(XoopsModules\Smart...::SF_STATUS_NEW_ANSWER) of type array<integer,integer> is incompatible with the type string expected by parameter $status of XoopsModules\Smartfaq\FaqHandler::getFaqs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

712
        $faqsObj = $this->getFaqs(0, 0, /** @scrutinizer ignore-type */ [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER], -1, 'datesub', 'DESC', '', true, $otherCriteria);
Loading history...
713
714
        $totalfaqs  = count($faqsObj);
0 ignored issues
show
Unused Code introduced by
The assignment to $totalfaqs is dead and can be removed.
Loading history...
715
        $randomFaqs = [];
716
        if ($faqsObj) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $faqsObj of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
717
            foreach ($faqsObj as $i => $iValue) {
718
                $display = false;
719
720
                $http        = (false === strpos(XOOPS_URL, 'https://')) ? 'http://' : 'https://';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Smartfaq\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
721
                $phpself     = $_SERVER['PHP_SELF'];
722
                $httphost    = $_SERVER['HTTP_HOST'];
723
                $querystring = $_SERVER['QUERY_STRING'];
724
                if ('' != $querystring) {
725
                    $querystring = '?' . $querystring;
726
                }
727
                $currenturl     = $http . $httphost . $phpself . $querystring;
728
                $fullcontexturl = XOOPS_URL . '/' . $iValue->contextpage();
729
                switch ($iValue->modulelink()) {
730
                    case '':
731
                        $display = false;
732
                        break;
733
                    case 'None':
734
                        $display = false;
735
                        break;
736
                    case 'All':
737
                        $display = true;
738
                        break;
739
                    case 'url':
740
                        if ($iValue->exacturl()) {
741
                            $display = ($currenturl == $fullcontexturl);
742
                        } else {
743
                            $display = (false === strpos($currenturl, $fullcontexturl));
744
                        }
745
                        break;
746
                    default:
747
                        if (false === strpos($currenturl, XOOPS_URL . '/modules/')) {
748
                            $display = false;
749
                        } else {
750
                            if (false === strpos($currenturl, $iValue->modulelink())) {
751
                                $display = false;
752
                            } else {
753
                                $display = true;
754
                            }
755
                        }
756
                        break;
757
                }
758
                if ($display) {
759
                    $randomFaqs[] =& $faqsObj[$i];
760
                }
761
            }
762
        }
763
764
        if (count($randomFaqs) > $limit) {
765
            mt_srand((float)microtime() * 10000000);
0 ignored issues
show
Bug introduced by
(double)microtime() * 10000000 of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

765
            mt_srand(/** @scrutinizer ignore-type */ (float)microtime() * 10000000);
Loading history...
766
            $rand_keys = array_rand($randomFaqs, $limit);
767
            for ($j = 0, $jMax = count($rand_keys); $j < $jMax; ++$j) {
768
                $ret[] =& $randomFaqs[$rand_keys[$j]];
769
            }
770
        } else {
771
            $ret =& $randomFaqs;
772
        }
773
774
        return $ret;
775
    }
776
777
    /**
778
     * @param  array $status
779
     * @return array
780
     */
781
    public function getLastPublishedByCat($status = [Constants::SF_STATUS_PUBLISHED, Constants::SF_STATUS_NEW_ANSWER])
782
    {
783
        $ret       = [];
784
        $faqclause = '';
785
        if (!Smartfaq\Utility::userIsAdmin()) {
786
            /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
787
            $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
788
            $items            = $smartPermHandler->getPermissions('item');
789
            $faqclause        = ' AND faqid IN (' . implode(',', $items) . ')';
790
        }
791
792
        $sql  = "CREATE TEMPORARY TABLE tmp (categoryid INT(8) UNSIGNED NOT NULL,datesub INT(11) DEFAULT '0' NOT NULL);";
793
        $sql2 = ' LOCK TABLES ' . $this->db->prefix('smartfaq_faq') . ' READ;';
794
        $sql3 = ' INSERT INTO tmp SELECT categoryid, MAX(datesub) FROM ' . $this->db->prefix('smartfaq_faq') . ' WHERE status IN (' . implode(',', $status) . ") $faqclause GROUP BY categoryid;";
795
        $sql4 = ' SELECT ' . $this->db->prefix('smartfaq_faq') . '.categoryid, faqid, question, uid, ' . $this->db->prefix('smartfaq_faq') . '.datesub FROM ' . $this->db->prefix('smartfaq_faq') . ', tmp
796
                              WHERE ' . $this->db->prefix('smartfaq_faq') . '.categoryid=tmp.categoryid AND ' . $this->db->prefix('smartfaq_faq') . '.datesub=tmp.datesub;';
797
        /*
798
        //Old implementation
799
        $sql = "SELECT categoryid, faqid, question, uid, MAX(datesub) AS datesub FROM ".$this->db->prefix("smartfaq_faq")."
800
               WHERE status IN (". implode(',', $status).")";
801
        $sql .= " GROUP BY categoryid";
802
        */
803
        $this->db->queryF($sql);
804
        $this->db->queryF($sql2);
805
        $this->db->queryF($sql3);
806
        $result = $this->db->query($sql4);
807
        $error  = $this->db->error();
808
        $this->db->queryF('UNLOCK TABLES;');
809
        $this->db->queryF('DROP TABLE tmp;');
810
        if (!$result) {
811
            trigger_error('Error in getLastPublishedByCat SQL: ' . $error);
812
813
            return $ret;
814
        }
815
        while ($row = $this->db->fetchArray($result)) {
816
            $faq = new Smartfaq\Faq();
817
            $faq->assignVars($row);
818
            $ret[$row['categoryid']] =& $faq;
819
            unset($faq);
820
        }
821
822
        return $ret;
823
    }
824
825
    /**
826
     * delete FAQs matching a set of conditions
827
     *
828
     * @param  object $criteria {@link CriteriaElement}
829
     * @return bool   FALSE if deletion failed
830
     */
831
    public function deleteAll($criteria = null)
832
    {
833
        $sql = 'DELETE FROM ' . $this->db->prefix('smartfaq_faq');
834
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
835
            $sql .= ' ' . $criteria->renderWhere();
836
        }
837
        if (!$this->db->query($sql)) {
838
            return false;
839
            // TODO : Also delete the permissions related to each FAQ
840
        }
841
842
        return true;
843
    }
844
845
    /**
846
     * Change a value for FAQ with a certain criteria
847
     *
848
     * @param string $fieldname  Name of the field
849
     * @param string $fieldvalue Value to write
850
     * @param object $criteria   {@link CriteriaElement}
851
     *
852
     * @return bool
853
     **/
854
    public function updateAll($fieldname, $fieldvalue, $criteria = null)
855
    {
856
        $set_clause = is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue);
857
        $sql        = 'UPDATE ' . $this->db->prefix('smartfaq_faq') . ' SET ' . $set_clause;
858
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
859
            $sql .= ' ' . $criteria->renderWhere();
860
        }
861
        if (!$this->db->queryF($sql)) {
862
            return false;
863
        }
864
865
        return true;
866
    }
867
868
    /**
869
     * @param $faqid
870
     * @return bool
871
     */
872
    public function updateCounter($faqid)
873
    {
874
        $sql = 'UPDATE ' . $this->db->prefix('smartfaq_faq') . ' SET counter=counter+1 WHERE faqid = ' . $faqid;
875
        if ($this->db->queryF($sql)) {
876
            return true;
877
        } else {
878
            return false;
879
        }
880
    }
881
882
    /**
883
     * @param  string $notNullFields
884
     * @param  bool   $withAnd
885
     * @return string
886
     */
887
    public function NotNullFieldClause($notNullFields = '', $withAnd = false)
888
    {
889
        $ret = '';
890
        if ($withAnd) {
891
            $ret .= ' AND ';
892
        }
893
        if (!empty($notNullFields) && is_array($notNullFields)) {
0 ignored issues
show
introduced by
The condition is_array($notNullFields) is always false.
Loading history...
894
            foreach ($notNullFields as $v) {
895
                $ret .= " ($v IS NOT NULL AND $v <> ' ' )";
896
            }
897
        } elseif (!empty($notNullFields)) {
898
            $ret .= " ($notNullFields IS NOT NULL AND $notNullFields <> ' ' )";
899
        }
900
901
        return $ret;
902
    }
903
904
    /**
905
     * @param  array  $queryarray
906
     * @param  string $andor
907
     * @param  int    $limit
908
     * @param  int    $offset
909
     * @param  int    $userid
910
     * @return array
911
     */
912
    public function getFaqsFromSearch($queryarray = [], $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
913
    {
914
        global $xoopsUser;
915
916
        $ret = [];
917
918
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
919
920
        if (0 != $userid) {
921
            $criteriaUser = new \CriteriaCompo();
922
            $criteriaUser->add(new \Criteria('faq.uid', $userid), 'OR');
923
            $criteriaUser->add(new \Criteria('answer.uid', $userid), 'OR');
924
        }
925
926
        if ($queryarray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $queryarray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
927
            $criteriaKeywords = new \CriteriaCompo();
928
            foreach ($queryarray as $iValue) {
929
                $criteriaKeyword = new \CriteriaCompo();
930
                $criteriaKeyword->add(new \Criteria('faq.question', '%' . $iValue . '%', 'LIKE'), 'OR');
931
                $criteriaKeyword->add(new \Criteria('answer.answer', '%' . $iValue . '%', 'LIKE'), 'OR');
932
                $criteriaKeywords->add($criteriaKeyword, $andor);
933
                unset($criteriaKeyword);
934
            }
935
        }
936
937
        // Categories for which user has access
938
        if (!$userIsAdmin) {
939
            /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
940
            $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
941
942
            $categoriesGranted = $smartPermHandler->getPermissions('category');
943
            $faqsGranted       = $smartPermHandler->getPermissions('item');
944
            if (!$categoriesGranted) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoriesGranted of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
945
                return $ret;
946
            }
947
            if (!$faqsGranted) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $faqsGranted of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
948
                return $ret;
949
            }
950
            $grantedCategories = new \Criteria('faq.categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN');
951
            $grantedFaq        = new \CriteriaCompo();
952
            $grantedFaq->add(new \Criteria('faq.faqid', '(' . implode(',', $faqsGranted) . ')', 'IN'), 'OR');
953
            // If user is anonymous, check if the FAQ allow partialview
954
            if (!is_object($xoopsUser)) {
955
                $grantedFaq->add(new \Criteria('partialview', '1'), 'OR');
956
            }
957
        }
958
959
        $criteriaPermissions = new \CriteriaCompo();
960
        if (!$userIsAdmin) {
961
            $criteriaPermissions->add($grantedCategories, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedCategories does not seem to be defined for all execution paths leading up to this point.
Loading history...
962
            $criteriaPermissions->add($grantedFaq, 'AND');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $grantedFaq does not seem to be defined for all execution paths leading up to this point.
Loading history...
963
        }
964
965
        $criteriaAnswersStatus = new \CriteriaCompo();
966
        $criteriaAnswersStatus->add(new \Criteria('answer.status', Constants::SF_AN_STATUS_APPROVED));
967
968
        $criteriaFasStatus = new \CriteriaCompo();
969
        $criteriaFasStatus->add(new \Criteria('faq.status', Constants::SF_STATUS_OPENED), 'OR');
970
        $criteriaFasStatus->add(new \Criteria('faq.status', Constants::SF_STATUS_PUBLISHED), 'OR');
971
972
        $criteria = new \CriteriaCompo();
973
        if (!empty($criteriaUser)) {
974
            $criteria->add($criteriaUser, 'AND');
975
        }
976
977
        if (!empty($criteriaKeywords)) {
978
            $criteria->add($criteriaKeywords, 'AND');
979
        }
980
981
        if (!empty($criteriaPermissions) && (!$userIsAdmin)) {
982
            $criteria->add($criteriaPermissions);
983
        }
984
985
        if (!empty($criteriaAnswersStatus)) {
986
            $criteria->add($criteriaAnswersStatus, 'AND');
987
        }
988
989
        if (!empty($criteriaFasStatus)) {
990
            $criteria->add($criteriaFasStatus, 'AND');
991
        }
992
993
        $criteria->setLimit($limit);
994
        $criteria->setStart($offset);
995
        $criteria->setSort('faq.datesub');
996
        $criteria->setOrder('DESC');
997
998
        $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';
999
1000
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
1001
            $whereClause = $criteria->renderWhere();
1002
1003
            if ('WHERE ()' !== $whereClause) {
1004
                $sql .= ' ' . $criteria->renderWhere();
1005
                if ('' != $criteria->getSort()) {
1006
                    $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
1007
                }
1008
                $limit = $criteria->getLimit();
1009
                $start = $criteria->getStart();
1010
            }
1011
        }
1012
1013
        //echo "<br>" . $sql . "<br>";
1014
1015
        $result = $this->db->query($sql, $limit, $start);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $start does not seem to be defined for all execution paths leading up to this point.
Loading history...
1016
        if (!$result) {
1017
            trigger_error('Query did not work in smartfaq', E_USER_WARNING);
1018
1019
            return $ret;
1020
        }
1021
1022
        if (0 == $GLOBALS['xoopsDB']->getRowsNum($result)) {
1023
            return $ret;
1024
        }
1025
1026
        while (false !== ($myrow = $this->db->fetchArray($result))) {
1027
            $faq = new Smartfaq\Faq();
1028
            $faq->assignVars($myrow);
1029
            $ret[] =& $faq;
1030
            unset($faq);
1031
        }
1032
1033
        return $ret;
1034
    }
1035
1036
    /**
1037
     * @param  int   $cat_id
1038
     * @param        $status
1039
     * @return array
1040
     */
1041
    public function getCountsByCat($cat_id = 0, $status)
1042
    {
1043
        global $xoopsUser;
1044
        $ret = [];
1045
        $sql = 'SELECT categoryid, COUNT(*) AS count FROM ' . $this->db->prefix('smartfaq_faq');
1046
        if ((int)$cat_id > 0) {
1047
            $sql .= ' WHERE categoryid = ' . (int)$cat_id;
1048
            $sql .= ' AND status IN (' . implode(',', $status) . ')';
1049
        } else {
1050
            $sql .= ' WHERE status IN (' . implode(',', $status) . ')';
1051
            if (!Smartfaq\Utility::userIsAdmin()) {
1052
                /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
1053
                $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
1054
                $items            = $smartPermHandler->getPermissions('item');
1055
                if (is_object($xoopsUser)) {
1056
                    $sql .= ' AND faqid IN (' . implode(',', $items) . ')';
1057
                } else {
1058
                    $sql .= ' AND (faqid IN (' . implode(',', $items) . ') OR partialview = 1)';
1059
                }
1060
            }
1061
        }
1062
        $sql .= ' GROUP BY categoryid';
1063
1064
        //echo "<br>" . $sql . "<br>";
1065
1066
        $result = $this->db->query($sql);
1067
        if (!$result) {
1068
            return $ret;
1069
        }
1070
        while ($row = $this->db->fetchArray($result)) {
1071
            $ret[$row['categoryid']] = (int)$row['count'];
1072
        }
1073
1074
        return $ret;
1075
    }
1076
}
1077