Index: branches/RC/core/units/general/helpers/category_helper.php =================================================================== diff -u -N -r11640 -r11724 --- branches/RC/core/units/general/helpers/category_helper.php (.../category_helper.php) (revision 11640) +++ branches/RC/core/units/general/helpers/category_helper.php (.../category_helper.php) (revision 11724) @@ -2,6 +2,19 @@ class CategoryHelper extends kHelper { + /** + * Structure tree for ParentId field in category or category items + * + * @var Array + */ + var $_structureTree = null; + + /** + * Prints category path using given blocks. Also supports used defined path elements at the end. + * + * @param Array $params + * @return string + */ function NavigationBar($params) { $main_category_id = isset($params['cat_id']) ? $params['cat_id'] : $this->Application->GetVar('m_cat_id'); @@ -86,6 +99,13 @@ return $ret; } + /** + * Renders path to given category using given blocks. + * + * @param int $main_category_id + * @param Array $params + * @return string + */ function getCategoryPath($main_category_id, $params) { $category_path = $this->getCategoryParentPath($main_category_id); @@ -162,6 +182,13 @@ return $module_info; } + /** + * Renders path to top catalog category + * + * @param Array $params + * @param int $current_category + * @return string + */ function getHomeCategoryPath($params, $current_category) { $block_params['cat_id'] = $this->Application->findModule('Name', 'Core', 'RootCat'); // 0; @@ -173,6 +200,11 @@ return $this->Application->ParseBlock($block_params); } + /** + * Returns root categories from all modules + * + * @return Array + */ function getModuleRootCategories() { static $root_categories = null; @@ -189,6 +221,12 @@ return $root_categories; } + /** + * Returns given category's parent path as array of id=>name elements + * + * @param int $main_category_id + * @return Array + */ function getCategoryParentPath($main_category_id) { static $cached_path = null; @@ -248,6 +286,143 @@ } return false; } + + /** + * Converts multi-dimensional category structure in one-dimensional option array (category_id=>category_name) + * + * @param Array $data + * @param int $parent_category_id + * @param int_type $language_id + * @param int $theme_id + * @param int $level + * @return Array + */ + function _printChildren(&$data, $parent_category_id, $language_id, $theme_id, $level = 0) + { + if ($data['ThemeId'] != $theme_id && $data['ThemeId'] != 0) { + // don't show system templates from different themes + return Array (); + } + + $ret = Array($parent_category_id => str_repeat('-', $level).' '.$data['l'.$language_id.'_Name']); + + if ($data['children']) { + $level++; + foreach ($data['children'] as $category_id => $category_data) { + $ret = array_merge_recursive2($ret, $this->_printChildren($data['children'][$category_id], $category_id, $language_id, $theme_id, $level)); + } + } + + return $ret; + } + + /** + * Returns information about children under parent path (recursive) + * + * @param int $parent_category_id + * @param int $language_count + * @return Array + */ + function _getChildren($parent_category_id, $language_count) + { + $id_field = $this->Application->getUnitOption('c', 'IDField'); + + // get category children + parent category + $sql = 'SELECT * + FROM '.$this->Application->getUnitOption('c', 'TableName').' + WHERE ParentId = '.$parent_category_id.' OR '.$id_field.' = '.$parent_category_id.' + ORDER BY Priority DESC'; + $categories = $this->Conn->Query($sql, $id_field); + + $parent_data = $categories[$parent_category_id]; + unset($categories[$parent_category_id]); + + // no children for this category + $data = Array ('id' => $parent_data[$id_field], 'children' => false, 'ThemeId' => $parent_data['ThemeId']); + for ($i = 1; $i <= $language_count; $i++) { + $data['l'.$i.'_Name'] = $parent_data['l'.$i.'_Name']; + } + + if (!$categories) { + // no children + return $data; + } + + // category has children + foreach ($categories as $category_id => $category_data) { + $data['children'][$category_id] = $this->_getChildren($category_id, $language_count); + } + + return $data; + } + + /** + * Generates OR retrieves from cache structure tree + * + * @return Array + */ + function &_getStructureTree() + { + // get cached version of structure tree + $sql = 'SELECT Data + FROM ' . TABLE_PREFIX . 'Cache + WHERE VarName = "StructureTree"'; + $data = $this->Conn->GetOne($sql); + + if ($data) { + $data = unserialize($data); + + return $data; + } + + // generate structure tree from scratch + $ml_helper =& $this->Application->recallObject('kMultiLanguageHelper'); + /* @var $ml_helper kMultiLanguageHelper */ + + $language_count = $ml_helper->getLanguageCount(); + + $root_category = $this->Application->findModule('Name', 'Core', 'RootCat'); + $data = $this->_getChildren($root_category, $language_count); + + $fields_hash = Array ( + 'VarName' => 'StructureTree', + 'Data' => serialize($data), + 'Cached' => adodb_mktime(), + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'Cache', 'REPLACE'); + + return $data; + } + + /** + * Returns category structure as field option list + * + * @return Array + */ + function getStructureTreeAsOptions() + { + if (defined('IS_INSTALL') && IS_INSTALL) { + // no need to create category structure during install, because it's not used there + return Array (); + } + + if (isset($this->_structureTree)) { + return $this->_structureTree; + } + + $themes_helper =& $this->Application->recallObject('ThemesHelper'); + /* @var $themes_helper kThemesHelper */ + + $data = $this->_getStructureTree(); + + $theme_id = (int)$themes_helper->getCurrentThemeId(); + $root_category = $this->Application->findModule('Name', 'Core', 'RootCat'); + + $this->_structureTree = $this->_printChildren($data, $root_category, $this->Application->GetVar('m_lang'), $theme_id); + + return $this->_structureTree; + } }