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: 30:
31: private static $_instance = NULL;
32:
33: 34: 35: 36: 37:
38: public static function getInstance() {
39: if (self::$_instance === NULL) {
40: self::$_instance = new self();
41: }
42:
43: return self::$_instance;
44: }
45:
46: 47: 48: 49: 50:
51: protected function __construct() {
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
64: protected function _fetchCategoryTree($baseCategoryId, $depth, $currentCategoryId) {
65: if ((int) $baseCategoryId == 0) {
66: throw new cUnexpectedValueException("Expect category ID greater than 0.");
67: }
68:
69: $categoryHelper = cCategoryHelper::getInstance();
70: $categoryHelper->setAuth(cRegistry::getAuth());
71:
72: $categoryTree = $categoryHelper->getSubCategories($baseCategoryId, $depth);
73:
74: $tree = array();
75:
76: $parentCategories = $categoryHelper->getParentCategoryIds($currentCategoryId);
77:
78: foreach ($categoryTree as $treeData) {
79: $catId = $treeData['idcat'];
80:
81: $firstChildId = $lastChildId = 0;
82: if (count($treeData['subcats']) > 0) {
83: $lastIndex = count($treeData['subcats']) - 1;
84:
85: $firstChildId = $treeData['subcats'][0]['idcat'];
86: $lastChildId = $treeData['subcats'][$lastIndex]['idcat'];
87: }
88:
89: $markActive = ($currentCategoryId == $catId);
90: if ($markActive == false && in_array($catId, $parentCategories)) {
91: $markActive = true;
92: }
93:
94: $treeItem['first_child_id'] = $firstChildId;
95: $treeItem['last_child_id'] = $lastChildId;
96: $treeItem['tree_data'] = $treeData;
97: $treeItem['active'] = $markActive;
98: $tree[] = $treeItem;
99: }
100:
101: return $tree;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: public function renderNavigation($baseCategoryId, $depth, $currentCategoryId) {
113: $tree = $this->_fetchCategoryTree($baseCategoryId, $depth, $currentCategoryId);
114:
115: return $tree;
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125:
126: public function renderSitemap($baseCategoryId, $depth, cTemplate &$tpl) {
127: $tree = $this->_fetchCategoryTree($baseCategoryId, $depth, 0);
128:
129: foreach ($tree as $treeItem) {
130: $treeData = $treeItem['tree_data'];
131: $catId = $treeData['idcat'];
132:
133: $firstChildId = $treeItem['first_child_id'];
134:
135: $tpl->set('d', 'name', $treeData['item']->getField('name'));
136: $tpl->set('d', 'css_level', $treeData['level']);
137: $tpl->set('d', 'url', $treeData['item']->getLink());
138: $tpl->next();
139:
140: if ($firstChildId != 0) {
141: $this->renderSitemap($catId, $depth, $tpl);
142: }
143: }
144: }
145: }