Completed
Branch master (1b2f30)
by Michael
06:29 queued 03:22
created

class/category.php (14 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
9
10
class sfCategory extends XoopsObject
11
{
12
    /**
13
     * @var array
14
     * @access private
15
     */
16
    private $groups_read = null;
17
18
    /**
19
     * @var array
20
     * @access private
21
     */
22
    private $groups_admin = null;
0 ignored issues
show
The property $groups_admin is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24
    /**
25
     * constructor
26
     * @param null $id
27
     */
28
    public function __construct($id = null)
29
    {
30
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
31
        $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false);
32
        $this->initVar('parentid', XOBJ_DTYPE_INT, null, false);
33
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
34
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255);
35
        $this->initVar('total', XOBJ_DTYPE_INT, 1, false);
36
        $this->initVar('weight', XOBJ_DTYPE_INT, 1, false);
37
        $this->initVar('created', XOBJ_DTYPE_INT, null, false);
38
        $this->initVar('last_faq', XOBJ_DTYPE_INT);
39
40
        //not persistent values
41
        $this->initVar('faqcount', XOBJ_DTYPE_INT, 0, false);
42
        $this->initVar('last_faqid', XOBJ_DTYPE_INT);
43
        $this->initVar('last_question_link', XOBJ_DTYPE_TXTBOX);
44
45
        if (isset($id)) {
46
            if (is_array($id)) {
47
                $this->assignVars($id);
48
            } else {
49
                $categoryHandler = new sfCategoryHandler($this->db);
50
                $category        =& $categoryHandler->get($id);
51
                foreach ($category->vars as $k => $v) {
52
                    $this->assignVar($k, $v['value']);
53
                }
54
                $this->assignOtherProperties();
55
            }
56
        }
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function notLoaded()
63
    {
64
        return ($this->getVar('categoryid') == -1);
65
    }
66
67
    public function assignOtherProperties()
68
    {
69
        global $xoopsUser;
70
        $smartModule = sf_getModuleInfo();
71
        $module_id   = $smartModule->getVar('mid');
72
73
        $gpermHandler = xoops_getHandler('groupperm');
74
75
        $this->groups_read = $gpermHandler->getGroupIds('category_read', $this->categoryid(), $module_id);
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 View Code Duplication
    public function checkPermission()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
84
85
        $userIsAdmin = sf_userIsAdmin();
86
        if ($userIsAdmin) {
87
            return true;
88
        }
89
90
        $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
91
92
        $categoriesGranted = $smartPermHandler->getPermissions('category');
93
        if (in_array($this->categoryid(), $categoriesGranted)) {
94
            $ret = true;
95
        }
96
97
        return $ret;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function categoryid()
104
    {
105
        return $this->getVar('categoryid');
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function parentid()
112
    {
113
        return $this->getVar('parentid');
114
    }
115
116
    /**
117
     * @param  string $format
118
     * @return mixed
119
     */
120 View Code Duplication
    public function name($format = 'S')
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $ret = $this->getVar('name', $format);
123
        if (($format === 's') || ($format === 'S') || ($format === 'show')) {
124
            $myts = MyTextSanitizer::getInstance();
125
            $ret  = $myts->displayTarea($ret);
126
        }
127
128
        return $ret;
129
    }
130
131
    /**
132
     * @param  string $format
133
     * @return mixed
134
     */
135
    public function description($format = 'S')
136
    {
137
        return $this->getVar('description', $format);
138
    }
139
140
    /**
141
     * @return mixed
142
     */
143
    public function weight()
144
    {
145
        return $this->getVar('weight');
146
    }
147
148
    /**
149
     * @param  bool $withAllLink
150
     * @param  bool $open
151
     * @return mixed|string
152
     */
153
    public function getCategoryPath($withAllLink = false, $open = false)
154
    {
155
        $filename = 'category.php';
156
        if ($open != false) {
157
            $filename = 'open_category.php';
158
        }
159
        if ($withAllLink) {
160
            $ret = "<a href='" . XOOPS_URL . '/modules/smartfaq/' . $filename . '?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
161
        } else {
162
            $ret = $this->name();
163
        }
164
        $parentid        = $this->parentid();
165
        $categoryHandler = sf_gethandler('category');
166
        if ($parentid != 0) {
167
            $parentObj = $categoryHandler->get($parentid);
168
            if ($parentObj->notLoaded()) {
169
                exit;
170
            }
171
            $parentid = $parentObj->parentid();
172
            $ret      = $parentObj->getCategoryPath(true, $open) . ' > ' . $ret;
173
        }
174
175
        return $ret;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getGroups_read()
182
    {
183
        if (count($this->groups_read) < 1) {
184
            $this->assignOtherProperties();
185
        }
186
187
        return $this->groups_read;
188
    }
189
190
    /**
191
     * @param array $groups_read
192
     */
193
    public function setGroups_read($groups_read = array('0'))
194
    {
195
        $this->groups_read = $groups_read;
196
    }
197
198
    /**
199
     * @param  bool $sendNotifications
200
     * @param  bool $force
201
     * @return bool
202
     */
203
    public function store($sendNotifications = true, $force = true)
204
    {
205
        $categoryHandler = new sfCategoryHandler($this->db);
206
207
        $ret = $categoryHandler->insert($this, $force);
208
        if ($sendNotifications && $ret && $this->isNew()) {
209
            $this->sendNotifications();
210
        }
211
        $this->unsetNew();
212
213
        return $ret;
214
    }
215
216
    public function sendNotifications()
217
    {
218
        $smartModule = sf_getModuleInfo();
219
220
        $myts                = MyTextSanitizer::getInstance();
221
        $notificationHandler = xoops_getHandler('notification');
0 ignored issues
show
$notificationHandler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
222
223
        $tags                  = array();
224
        $tags['MODULE_NAME']   = $myts->htmlSpecialChars($smartModule->getVar('name'));
225
        $tags['CATEGORY_NAME'] = $this->name();
226
        $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid();
227
228
        $notificationHandler = xoops_getHandler('notification');
229
        $notificationHandler->triggerEvent('global_faq', 0, 'category_created', $tags);
230
    }
231
232
    /**
233
     * @param  array $category
234
     * @param  bool  $open
235
     * @return array
236
     */
237
    public function toArray($category = array(), $open = false)
238
    {
239
        $category['categoryid'] = $this->categoryid();
240
        $category['name']       = $this->name();
241
        if ($open != false) {
242
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/open_category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
243
        } else {
244
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
245
        }
246
        $category['total']       = $this->getVar('faqcount');
247
        $category['description'] = $this->description();
248
249
        if ($this->getVar('last_faqid') > 0) {
250
            $category['last_faqid']         = $this->getVar('last_faqid', 'n');
251
            $category['last_question_link'] = $this->getVar('last_question_link', 'n');
252
        }
253
254
        return $category;
255
    }
256
}
257
258
/**
259
 * Categories handler class.
260
 * This class is responsible for providing data access mechanisms to the data source
261
 * of Category class objects.
262
 *
263
 * @author  marcan <[email protected]>
264
 * @package SmartFAQ
265
 */
266
class sfCategoryHandler extends XoopsObjectHandler
267
{
268
    /**
269
     * create a new category
270
     *
271
     * @param  bool $isNew flag the new objects as "new"?
272
     * @return object sfCategory
273
     */
274
    public function &create($isNew = true)
275
    {
276
        $category = new sfCategory();
277
        if ($isNew) {
278
            $category->setNew();
279
        }
280
281
        return $category;
282
    }
283
284
    /**
285
     * retrieve a category
286
     *
287
     * @param  int $id categoryid of the category
288
     * @return mixed reference to the {@link sfCategory} object, FALSE if failed
289
     */
290
    public function &get($id)
291
    {
292
        $false = false;
293
        if ((int)$id > 0) {
294
            $sql = 'SELECT * FROM ' . $this->db->prefix('smartfaq_categories') . ' WHERE categoryid=' . $id;
295
            if (!$result = $this->db->query($sql)) {
296
                return $false;
297
            }
298
299
            $numrows = $this->db->getRowsNum($result);
300
            if ($numrows == 1) {
301
                $category = new sfCategory();
302
                $category->assignVars($this->db->fetchArray($result));
303
304
                return $category;
305
            }
306
        }
307
308
        return $false;
309
    }
310
311
    /**
312
     * insert a new category in the database
313
     *
314
     * @param  XoopsObject $category reference to the {@link sfCategory} object
315
     * @param  bool        $force
316
     * @return bool        FALSE if failed, TRUE if already present and unchanged or successful
317
     */
318
    public function insert(XoopsObject $category, $force = false)
319
    {
320
        if (strtolower(get_class($category)) !== 'sfcategory') {
321
            return false;
322
        }
323
        if (!$category->isDirty()) {
324
            return true;
325
        }
326
        if (!$category->cleanVars()) {
327
            return false;
328
        }
329
330
        foreach ($category->cleanVars as $k => $v) {
331
            ${$k} = $v;
332
        }
333
334
        if ($category->isNew()) {
335
            $sql = sprintf('INSERT INTO %s (categoryid, parentid, name, description, total, weight, created) VALUES (NULL, %u, %s, %s, %u, %u, %u)', $this->db->prefix('smartfaq_categories'), $parentid, $this->db->quoteString($name), $this->db->quoteString($description), $total, $weight, time());
336
        } else {
337
            $sql = sprintf('UPDATE %s SET parentid = %u, name = %s, description = %s, total = %s, weight = %u, created = %u WHERE categoryid = %u', $this->db->prefix('smartfaq_categories'), $parentid, $this->db->quoteString($name), $this->db->quoteString($description), $total, $weight, $created, $categoryid);
338
        }
339 View Code Duplication
        if (false != $force) {
340
            $result = $this->db->queryF($sql);
341
        } else {
342
            $result = $this->db->query($sql);
343
        }
344
        if (!$result) {
345
            return false;
346
        }
347
        if ($category->isNew()) {
348
            $category->assignVar('categoryid', $this->db->getInsertId());
349
        } else {
350
            $category->assignVar('categoryid', $categoryid);
351
        }
352
353
        return true;
354
    }
355
356
    /**
357
     * delete a category from the database
358
     *
359
     * @param  XoopsObject $category reference to the category to delete
360
     * @param  bool        $force
361
     * @return bool        FALSE if failed.
362
     */
363
    public function delete(XoopsObject $category, $force = false)
364
    {
365
        if (strtolower(get_class($category)) !== 'sfcategory') {
366
            return false;
367
        }
368
369
        // Deleting the FAQs
370
        $faqHandler = new sfFaqHandler($this->db);
371
        if (!$faqHandler->deleteAll(new Criteria('categoryid', $category->categoryid()))) {
372
            return false;
373
        }
374
375
        // Deleteing the sub categories
376
        $subcats =& $this->getCategories(0, 0, $category->categoryid());
377
        foreach ($subcats as $subcat) {
378
            $this->delete($subcat);
379
        }
380
381
        $sql = sprintf('DELETE FROM %s WHERE categoryid = %u', $this->db->prefix('smartfaq_categories'), $category->getVar('categoryid'));
382
383
        $smartModule = sf_getModuleInfo();
384
        $module_id   = $smartModule->getVar('mid');
385
386 View Code Duplication
        if (false != $force) {
387
            $result = $this->db->queryF($sql);
388
        } else {
389
            $result = $this->db->query($sql);
390
        }
391
392
        xoops_groupperm_deletebymoditem($module_id, 'category_read', $category->categoryid());
393
        //xoops_groupperm_deletebymoditem ($module_id, "category_admin", $categoryObj->categoryid());
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
394
395
        if (!$result) {
396
            return false;
397
        }
398
399
        return true;
400
    }
401
402
    /**
403
     * retrieve categories from the database
404
     *
405
     * @param  object $criteria  {@link CriteriaElement} conditions to be met
406
     * @param  bool   $id_as_key use the categoryid as key for the array?
407
     * @return array  array of {@link XoopsFaq} objects
408
     */
409
    public function getObjects($criteria = null, $id_as_key = false)
410
    {
411
        $ret   = array();
412
        $limit = $start = 0;
413
        $sql   = 'SELECT * FROM ' . $this->db->prefix('smartfaq_categories');
414 View Code Duplication
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
415
            $sql .= ' ' . $criteria->renderWhere();
416
            if ($criteria->getSort() != '') {
417
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
418
            }
419
            $limit = $criteria->getLimit();
420
            $start = $criteria->getStart();
421
        }
422
        //echo "<br />" . $sql . "<br />";
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
423
        $result = $this->db->query($sql, $limit, $start);
424
        if (!$result) {
425
            return $ret;
426
        }
427
428 View Code Duplication
        while ($myrow = $this->db->fetchArray($result)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
429
            $category = new sfCategory();
430
            $category->assignVars($myrow);
431
            if (!$id_as_key) {
432
                $ret[] = $category;
433
            } else {
434
                $ret[$myrow['categoryid']] = $category;
435
            }
436
            unset($category);
437
        }
438
439
        return $ret;
440
    }
441
442
    /**
443
     * @param  int    $limit
444
     * @param  int    $start
445
     * @param  int    $parentid
446
     * @param  string $sort
447
     * @param  string $order
448
     * @param  bool   $id_as_key
449
     * @return array
450
     */
451
    public function &getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true)
452
    {
453
        include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
454
455
        $criteria = new CriteriaCompo();
456
457
        $criteria->setSort($sort);
458
        $criteria->setOrder($order);
459
460
        if ($parentid != -1) {
461
            $criteria->add(new Criteria('parentid', $parentid));
462
        }
463
        if (!sf_userIsAdmin()) {
464
            $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
465
466
            $categoriesGranted = $smartPermHandler->getPermissions('category');
467
            $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
468
        }
469
        $criteria->setStart($start);
470
        $criteria->setLimit($limit);
471
        $ret = $this->getObjects($criteria, $id_as_key);
472
473
        return $ret;
474
    }
475
476
    /**
477
     * @param  int    $limit
478
     * @param  int    $start
479
     * @param  int    $parentid
480
     * @param  string $sort
481
     * @param  string $order
482
     * @return array
483
     */
484
    public function &getCategoriesWithOpenQuestion($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC')
485
    {
486
        include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
487
488
        $criteria = new CriteriaCompo();
489
490
        $criteria->setSort($sort);
491
        $criteria->setOrder($order);
492
493
        if ($parentid != -1) {
494
            $criteria->add(new Criteria('c.parentid', $parentid));
495
        }
496
        if (!sf_userIsAdmin()) {
497
            $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
498
499
            $categoriesGranted = $smartPermHandler->getPermissions('category');
500
            $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
501
        }
502
503
        $criteria->add(new Criteria('f.status', _SF_STATUS_OPENED));
504
        $criteria->setStart($start);
505
        $criteria->setLimit($limit);
506
507
        $ret   = array();
508
        $limit = $start = 0;
509
        $sql   = 'SELECT DISTINCT c.categoryid, c.parentid, c.name, c.description, c.total, c.weight, c.created FROM ' . $this->db->prefix('smartfaq_categories') . ' AS c INNER JOIN ' . $this->db->prefix('smartfaq_faq') . ' AS f ON c.categoryid = f.categoryid';
510 View Code Duplication
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
511
            $sql .= ' ' . $criteria->renderWhere();
512
            if ($criteria->getSort() != '') {
513
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
514
            }
515
            $limit = $criteria->getLimit();
516
            $start = $criteria->getStart();
517
        }
518
        //echo "<br />" . $sql . "<br />";
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
519
        $result = $this->db->query($sql, $limit, $start);
520
        if (!$result) {
521
            return $ret;
522
        }
523
524
        while ($myrow = $this->db->fetchArray($result)) {
525
            $category = new sfCategory();
526
            $category->assignVars($myrow);
527
            $ret[] = $category;
528
            unset($category);
529
        }
530
531
        return $ret;
532
    }
533
534
    /**
535
     * count Categories matching a condition
536
     *
537
     * @param  object $criteria {@link CriteriaElement} to match
538
     * @return int    count of categories
539
     */
540 View Code Duplication
    public function getCount($criteria = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
541
    {
542
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('smartfaq_categories');
543
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
544
            $sql .= ' ' . $criteria->renderWhere();
545
        }
546
        $result = $this->db->query($sql);
547
        if (!$result) {
548
            return 0;
549
        }
550
        list($count) = $this->db->fetchRow($result);
551
552
        return $count;
553
    }
554
555
    /**
556
     * @param  int $parentid
557
     * @return int
558
     */
559
    public function getCategoriesCount($parentid = 0)
560
    {
561
        if ($parentid == -1) {
562
            return $this->getCount();
563
        }
564
        $criteria = new CriteriaCompo();
565 View Code Duplication
        if (isset($parentid) && ($parentid != -1)) {
566
            $criteria->add(new criteria('parentid', $parentid));
567
            if (!sf_userIsAdmin()) {
568
                $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
569
570
                $categoriesGranted = $smartPermHandler->getPermissions('category');
571
                $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
572
            }
573
        }
574
575
        return $this->getCount($criteria);
576
    }
577
578
    /**
579
     * @param  int $parentid
580
     * @return int
581
     */
582
    public function getCategoriesWithOpenQuestionsCount($parentid = 0)
583
    {
584
        if ($parentid == -1) {
585
            return $this->getCount();
586
        }
587
        $criteria = new CriteriaCompo();
588 View Code Duplication
        if (isset($parentid) && ($parentid != -1)) {
589
            $criteria->add(new criteria('parentid', $parentid));
590
            if (!sf_userIsAdmin()) {
591
                $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
592
593
                $categoriesGranted = $smartPermHandler->getPermissions('category');
594
                $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
595
            }
596
        }
597
598
        $criteria->add(new Criteria('f.status', _SF_STATUS_OPENED));
599
600
        $sql = 'SELECT COUNT(c.categoryid) FROM ' . $this->db->prefix('smartfaq_categories') . ' AS c INNER JOIN ' . $this->db->prefix('smartfaq_faq') . ' AS f ON c.categoryid = f.categoryid';
601
602
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
603
            $sql .= ' ' . $criteria->renderWhere();
604
        }
605
606
        $result = $this->db->query($sql);
607
        if (!$result) {
608
            return 0;
609
        }
610
        list($count) = $this->db->fetchRow($result);
611
612
        return $count;
613
    }
614
615
    /**
616
     * @param $categories
617
     * @return array
618
     */
619
    public function getSubCats($categories)
620
    {
621
        $criteria = new CriteriaCompo(new Criteria('parentid', '(' . implode(',', array_keys($categories)) . ')'), 'IN');
622
        $ret      = array();
623
        if (!sf_userIsAdmin()) {
624
            $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
625
626
            $categoriesGranted = $smartPermHandler->getPermissions('category');
627
            $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
628
        }
629
        $subcats = $this->getObjects($criteria, true);
630
        foreach ($subcats as $subcat_id => $subcat) {
631
            $ret[$subcat->getVar('parentid')][$subcat->getVar('categoryid')] = $subcat;
632
        }
633
634
        return $ret;
635
    }
636
637
    /**
638
     * delete categories matching a set of conditions
639
     *
640
     * @param  object $criteria {@link CriteriaElement}
641
     * @return bool   FALSE if deletion failed
642
     */
643 View Code Duplication
    public function deleteAll($criteria = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
644
    {
645
        $sql = 'DELETE FROM ' . $this->db->prefix('smartfaq_categories');
646
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
647
            $sql .= ' ' . $criteria->renderWhere();
648
        }
649
        if (!$this->db->query($sql)) {
650
            return false;
651
            // TODO : Also delete the permissions related to each FAQ
652
            // TODO : What about sub-categories???
653
        }
654
655
        return true;
656
    }
657
658
    /**
659
     * Change a value for categories with a certain criteria
660
     *
661
     * @param string $fieldname  Name of the field
662
     * @param string $fieldvalue Value to write
663
     * @param object $criteria   {@link CriteriaElement}
664
     *
665
     * @return bool
666
     **/
667 View Code Duplication
    public function updateAll($fieldname, $fieldvalue, $criteria = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
668
    {
669
        $set_clause = is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue);
670
        $sql        = 'UPDATE ' . $this->db->prefix('smartfaq_categories') . ' SET ' . $set_clause;
671
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
672
            $sql .= ' ' . $criteria->renderWhere();
673
        }
674
        if (!$this->db->queryF($sql)) {
675
            return false;
676
        }
677
678
        return true;
679
    }
680
681
    /**
682
     * @param  int $cat_id
683
     * @return mixed
684
     */
685
    public function publishedFaqsCount($cat_id = 0)
686
    {
687
        return $this->faqsCount($cat_id, $status = array(_SF_STATUS_PUBLISHED, _SF_STATUS_NEW_ANSWER));
688
    }
689
690
    /**
691
     * @param  int    $cat_id
692
     * @param  string $status
693
     * @return mixed
694
     */
695
    public function faqsCount($cat_id = 0, $status = '')
696
    {
697
        global $xoopsUser;
698
        include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
699
700
        $faqHandler = sf_gethandler('faq');
701
702
        return $faqHandler->getCountsByCat($cat_id, $status);
703
    }
704
}
705