Completed
Push — master ( 88111b...b3fce7 )
by Michael
05:58
created

CategoryHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D getAllCategoriesArray() 0 31 9
C getParentIds() 0 30 7
1
<?php namespace XoopsModules\Smartobject;
2
3
/**
4
 * Contains the basic classe for managing a category object based on SmartObject
5
 *
6
 * @license    GNU
7
 * @author     marcan <[email protected]>
8
 * @link       http://smartfactory.ca The SmartFactory
9
 * @package    SmartObject
10
 * @subpackage SmartObjectItems
11
 */
12
13
use XoopsModules\Smartobject;
14
15
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
16
//require_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartseoobject.php';
17
18
19
/**
20
 * Class SmartobjectCategoryHandler
21
 */
22
class CategoryHandler extends Smartobject\PersistableObjectHandler
23
{
24
    public $allCategoriesObj = false;
25
    public $_allCategoriesId = false;
26
27
    /**
28
     * SmartobjectCategoryHandler constructor.
29
     * @param \XoopsDatabase       $db
30
     * @param                      $modulename
31
     */
32
    public function __construct(\XoopsDatabase $db, $modulename)
33
    {
34
        parent::__construct($db, 'category', 'categoryid', 'name', 'description', $modulename);
35
    }
36
37
    /**
38
     * @param  int    $parentid
39
     * @param  bool   $perm_name
40
     * @param  string $sort
41
     * @param  string $order
42
     * @return array|bool
43
     */
44
    public function getAllCategoriesArray($parentid = 0, $perm_name = false, $sort = 'parentid', $order = 'ASC')
45
    {
46
        if (!$this->allCategoriesObj) {
47
            $criteria = new \CriteriaCompo();
48
            $criteria->setSort($sort);
49
            $criteria->setOrder($order);
50
            global $xoopsUser;
51
            $userIsAdmin = is_object($xoopsUser) && $xoopsUser->isAdmin();
52
53
            if ($perm_name && !$userIsAdmin) {
54
                if (!$this->setGrantedObjectsCriteria($criteria, $perm_name)) {
55
                    return false;
56
                }
57
            }
58
59
            $this->allCategoriesObj =& $this->getObjects($criteria, 'parentid');
60
        }
61
62
        $ret = [];
63
        if (isset($this->allCategoriesObj[$parentid])) {
64
            foreach ($this->allCategoriesObj[$parentid] as $categoryid => $categoryObj) {
65
                $ret[$categoryid]['self'] = $categoryObj->toArray();
66
                if (isset($this->allCategoriesObj[$categoryid])) {
67
                    $ret[$categoryid]['sub']          = $this->getAllCategoriesArray($categoryid);
68
                    $ret[$categoryid]['subcatscount'] = count($ret[$categoryid]['sub']);
69
                }
70
            }
71
        }
72
73
        return $ret;
74
    }
75
76
    /**
77
     * @param               $parentid
78
     * @param  bool         $asString
79
     * @return array|string
80
     */
81
    public function getParentIds($parentid, $asString = true)
82
    {
83
        if (!$this->allCategoriesId) {
84
            $ret = [];
85
            $sql = 'SELECT categoryid, parentid FROM ' . $this->table . ' AS ' . $this->_itemname . ' ORDER BY parentid';
86
87
            $result = $this->db->query($sql);
88
89
            if (!$result) {
90
                return $ret;
91
            }
92
93
            while (false !== ($myrow = $this->db->fetchArray($result))) {
94
                $this->allCategoriesId[$myrow['categoryid']] = $myrow['parentid'];
95
            }
96
        }
97
98
        $retArray = [$parentid];
99
        while (0 != $parentid) {
100
            $parentid = $this->allCategoriesId[$parentid];
101
            if (0 != $parentid) {
102
                $retArray[] = $parentid;
103
            }
104
        }
105
        if ($asString) {
106
            return implode(', ', $retArray);
107
        } else {
108
            return $retArray;
109
        }
110
    }
111
}
112