1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cFrontendHelper {
25: 26: 27: 28:
29: static private $_instance = NULL;
30:
31: 32: 33: 34:
35: public static function getInstance() {
36: if (self::$_instance === NULL) {
37: self::$_instance = new self();
38: }
39:
40: return self::$_instance;
41: }
42:
43: 44: 45: 46:
47: protected function __construct() {
48: }
49:
50: 51: 52: 53: 54: 55: 56:
57: protected function _fetchCategoryTree($baseCategoryId, $depth, $currentCategoryId) {
58: if ((int) $baseCategoryId == 0) {
59: throw new cUnexpectedValueException("Expect category ID greater than 0.");
60: }
61:
62: $categoryHelper = cCategoryHelper::getInstance();
63: $categoryHelper->setAuth(cRegistry::getAuth());
64:
65: $categoryTree = $categoryHelper->getSubCategories($baseCategoryId, $depth);
66:
67: $tree = array();
68:
69: $parentCategories = $categoryHelper->getParentCategoryIds($currentCategoryId);
70:
71: foreach ($categoryTree as $treeData) {
72: $catId = $treeData['idcat'];
73:
74: $firstChildId = $lastChildId = 0;
75: if (count($treeData['subcats']) > 0) {
76: $lastIndex = count($treeData['subcats']) - 1;
77:
78: $firstChildId = $treeData['subcats'][0]['idcat'];
79: $lastChildId = $treeData['subcats'][$lastIndex]['idcat'];
80: }
81:
82: $markActive = ($currentCategoryId == $catId);
83: if ($markActive == false && in_array($catId, $parentCategories)) {
84: $markActive = true;
85: }
86:
87: $treeItem['first_child_id'] = $firstChildId;
88: $treeItem['last_child_id'] = $lastChildId;
89: $treeItem['tree_data'] = $treeData;
90: $treeItem['active'] = $markActive;
91: $tree[] = $treeItem;
92: }
93:
94: return $tree;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function renderNavigation($baseCategoryId, $depth, $currentCategoryId) {
104: $tree = $this->_fetchCategoryTree($baseCategoryId, $depth, $currentCategoryId);
105:
106: return $tree;
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: public function renderSitemap($baseCategoryId, $depth, cTemplate &$tpl) {
117: $tree = $this->_fetchCategoryTree($baseCategoryId, $depth, 0);
118:
119: foreach ($tree as $treeItem) {
120: $treeData = $treeItem['tree_data'];
121: $catId = $treeData['idcat'];
122:
123: $firstChildId = $treeItem['first_child_id'];
124:
125: $tpl->set('d', 'name', $treeData['item']->getField('name'));
126: $tpl->set('d', 'css_level', $treeData['level']);
127: $tpl->set('d', 'url', $treeData['item']->getLink());
128: $tpl->next();
129:
130: if ($firstChildId != 0) {
131: $this->renderSitemap($catId, $depth, $tpl);
132: }
133: }
134: }
135: }