CategoryHandler::getParentIds()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 2
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
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');
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