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