Esempio n. 1
0
    public function getChildCategories($parentCategory = 0, $active = array())
    {
        $Data = array();
        if ($parentCategory == 0) {
            if (count($active) > 0) {
                $parentTree = $this->getParentCategoriesTreeById($active);
                foreach ($parentTree as $key => $val) {
                    $parentTree[$key] = (int) $val;
                }
                $sql = 'SELECT
							C.idcategory AS id,
							C.distinction,
							C.categoryid AS parent,
							CT.name AS categoryname,
							(SELECT COUNT( idcategory )
								FROM category
								WHERE categoryid = C.idcategory
							) AS haschildren
						FROM category C
						LEFT JOIN categorytranslation CT ON CT.categoryid = C.idcategory AND CT.languageid = :languageid
						LEFT JOIN viewcategory VC ON VC.categoryid = C.idcategory
						WHERE (C.categoryid IN (' . implode(',', $parentTree) . ') OR C.categoryid IS NULL)
				';
                if (Helper::getViewId() > 0) {
                    $sql .= ' AND VC.viewid IN (' . Helper::getViewIdsAsString() . ') ';
                }
                $sql .= 'GROUP BY C.idcategory ORDER BY C.distinction ASC';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('languageid', Helper::getLanguageId());
            } else {
                $sql = 'SELECT
							C.idcategory AS id,
							C.distinction,
							C.categoryid AS parent,
							CT.name AS categoryname,
							(SELECT COUNT( idcategory )
								FROM category
								WHERE categoryid = C.idcategory
							) AS haschildren
						FROM category C
						LEFT JOIN categorytranslation CT ON CT.categoryid = C.idcategory AND CT.languageid = :languageid
						LEFT JOIN viewcategory VC ON VC.categoryid = C.idcategory
						WHERE C.categoryid IS :parent
					';
                if (Helper::getViewId() > 0) {
                    $sql .= ' AND VC.viewid IN (' . Helper::getViewIdsAsString() . ') ';
                }
                $sql .= 'GROUP BY C.idcategory ORDER BY C.distinction ASC';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('languageid', Helper::getLanguageId());
                if ((!isset($parentTree) || empty($parentTree)) && empty($parentCategory)) {
                    $stmt->bindValue('parent', NULL);
                } else {
                    $stmt->bindValue('parent', $parentCategory);
                }
            }
        } else {
            $sql = 'SELECT
						C.idcategory AS id,
						C.distinction,
						C.categoryid AS parent,
						CT.name AS categoryname,
						(SELECT COUNT( idcategory )
							FROM category
							WHERE categoryid = C.idcategory
						) AS haschildren
						FROM category C
						LEFT JOIN categorytranslation CT ON CT.categoryid = C.idcategory AND CT.languageid = :languageid
						LEFT JOIN viewcategory VC ON VC.categoryid = C.idcategory
						WHERE C.categoryid = :parent
					';
            if (Helper::getViewId() > 0) {
                $sql .= ' AND VC.viewid IN (' . Helper::getViewIdsAsString() . ') ';
            }
            $sql .= 'GROUP BY C.idcategory ORDER BY C.distinction ASC';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('languageid', Helper::getLanguageId());
            if ((!isset($parentTree) || empty($parentTree)) && empty($parentCategory)) {
                $stmt->bindValue('parent', NULL);
            } else {
                $stmt->bindValue('parent', $parentCategory);
            }
        }
        $stmt->execute();
        $Data = array();
        while ($rs = $stmt->fetch()) {
            $Data[$rs['id']] = array('id' => $rs['id'], 'name' => $rs['categoryname'], 'hasChildren' => $rs['haschildren'] > 0 ? true : false, 'parent' => $rs['parent'] == 0 ? NULL : $rs['parent'], 'weight' => $rs['distinction']);
        }
        return $Data;
    }