Index: branches/5.2.x/core/units/helpers/category_helper.php =================================================================== diff -u -N -r15226 -r15274 --- branches/5.2.x/core/units/helpers/category_helper.php (.../category_helper.php) (revision 15226) +++ branches/5.2.x/core/units/helpers/category_helper.php (.../category_helper.php) (revision 15274) @@ -1,6 +1,6 @@ Application->GetVar('m_cat_id'); - - if (array_key_exists('shift', $params) && $params['shift']) { - $home_element = ''; - $params['shift']--; - } - else { - $home_element = $this->getHomeCategoryPath($params, $main_category_id); - unset($params['is_first']); - } - - if (!getArrayValue($params, 'titles') && !getArrayValue($params, 'templates')) { - // no static templates given, show only category path - return $home_element . $this->getCategoryPath($main_category_id, $params); - } - - $navigation_parts = $this->getNavigationParts($params['titles'], $params['templates']); - - $ret = ''; - $block_params = Array (); //$params; // sort of TagProcessor:prepareTagParams - $block_params['no_editing'] = 1; - $block_params['category'] = 0; - $block_params['separator'] = $params['separator']; - $show_category = getArrayValue($params, 'show_category'); - - $current_template = $this->Application->GetVar('t'); - $physical_template = $this->Application->getPhysicalTemplate($current_template); - - if ($physical_template !== false) { - // replace menu template name with it's actual template name on disk - list ($current_template) = explode(':', $physical_template, 2); - } - - foreach ($navigation_parts as $template => $title) { - $block_params['template'] = $template; - - if ($title == '__item__') { - if ($show_category) { - $ret .= $this->getCategoryPath($main_category_id, $params); - $show_category = false; - } - - $category_path = $this->getCategoryParentPath($main_category_id); - $module_info = $this->getCategoryModule($params, array_keys($category_path)); - if (!$module_info) { - continue; - } - - $module_prefix = $module_info['Var']; - $object = $this->Application->recallObject($module_prefix); - /* @var $object kCatDBItem */ - - $title_field = $this->Application->getUnitOption($module_prefix, 'TitleField'); - $block_params['title'] = $object->GetField($title_field); - $block_params['prefix'] = $module_prefix; - $block_params['current'] = 0; - - $block_params['name'] = $this->SelectParam($params, 'module_item_render_as,render_as'); - } - else { - $block_params['current'] = ($template == $current_template); - - if ($title == '__page__') { - $block_params['title'] = $this->Application->ProcessParsedTag('st', 'PageInfo', Array ('type' => 'title')); - } - else { - $block_params['title'] = $this->Application->Phrase($title); - } - - - $block_params['name'] = $template == $current_template ? $params['current_render_as'] : $params['render_as']; - } - - $ret .= $this->Application->ParseBlock($block_params); - } - - if ($show_category) { - $params['no_current'] = true; - return $home_element . ($show_category ? $this->getCategoryPath($main_category_id, $params) : '') . $ret; - } - - return $home_element . $ret; - } - - /** - * Get navigation parts - * - * @param Array $titles - * @param Array $templates - * @return Array - */ - function getNavigationParts($titles, $templates) - { - $titles = explode(',', $titles); - $templates = explode(',', $templates); - - $ret = Array (); - foreach ($templates as $template_pos => $template) { - $ret[$template] = $titles[$template_pos]; - } - - 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); - if (!$category_path) { - // in "Home" category - return ''; - } - - if (array_key_exists('shift', $params) && $params['shift']) { - array_splice($category_path, 0, $params['shift']); - } - - $module_info = $this->getCategoryModule($params, array_keys($category_path)); - - $module_category_id = $module_info['RootCat']; - $module_item_id = $this->Application->GetVar($module_info['Var'].'_id'); - - $ret = ''; - $block_params['category'] = 1; - $block_params['no_editing'] = 1; - - if (array_key_exists('is_first', $params)) { - $block_params['is_first'] = $params['is_first']; - } - - $block_params['separator'] = $params['separator']; - $no_current = isset($params['no_current']) && $params['no_current']; - - $backup_category_id = $this->Application->GetVar('c_id'); - foreach ($category_path as $category_id => $category_name) { - $block_params['cat_id'] = $category_id; - $block_params['cat_name'] = $block_params['title'] = $category_name; - - if ($no_current) { - $block_params['current'] = 0; - } - else { - $block_params['current'] = ($main_category_id == $category_id) && !$module_item_id ? 1 : 0; - } - - $block_params['is_module_root'] = $category_id == $module_category_id ? 1 : 0; - $block_params['name'] = $this->SelectParam($params, 'render_as,block'); - - // which block to parse as current ? - if ($block_params['is_module_root']) { - $block_params['name'] = $this->SelectParam($params, 'module_root_render_as,render_as'); - $block_params['module_index'] = $module_info['TemplatePath'].'index'; - } - - if ($block_params['current']) { - $block_params['name'] = $this->SelectParam($params, 'current_render_as,render_as'); - } - - $this->Application->SetVar('c_id', $category_id); - $ret .= $this->Application->ParseBlock($block_params); - - if (array_key_exists('is_first', $block_params)) { - unset($block_params['is_first']); - } - } - - $this->Application->SetVar('c_id', $backup_category_id); - - return $ret; - } - - /** * Returns module information based on given module name or current category (relative to module root categories) * * @param Array $params * @param Array $category_ids category parent path (already as array) * @return Array + * @access public */ - function getCategoryModule($params, $category_ids) + public function getCategoryModule($params, $category_ids) { - if (isset($params['module'])) { + $module_info = Array (); + + if ( isset($params['module']) ) { // get module by name specified $module_info = $this->Application->findModule('Name', $params['module']); } - elseif ($category_ids) { + elseif ( $category_ids ) { // get module by category path $module_root_categories = $this->getModuleRootCategories(); $common_categories = array_intersect($category_ids, $module_root_categories); @@ -242,37 +59,18 @@ } /** - * 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->getBaseCategory(); - $block_params['no_editing'] = 1; - $block_params['current'] = $current_category == $block_params['cat_id'] ? 1 : 0; - $block_params['separator'] = $params['separator']; - $block_params['is_first'] = $params['is_first']; - $block_params['cat_name'] = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name'); - $block_params['name'] = $this->SelectParam($params, 'root_cat_render_as,render_as'); - - return $this->Application->ParseBlock($block_params); - } - - /** * Returns root categories from all modules * * @return Array + * @access protected */ - function getModuleRootCategories() + protected function getModuleRootCategories() { static $root_categories = null; - if (!isset($root_categories)) { + if ( !isset($root_categories) ) { $root_categories = Array (); - foreach ($this->Application->ModuleInfo as $module_name => $module_info) { + foreach ($this->Application->ModuleInfo as $module_info) { array_push($root_categories, $module_info['RootCat']); } @@ -283,106 +81,27 @@ } /** - * Returns given category's parent path as array of id=>name elements - * - * @param int $main_category_id - * @return Array - */ - function getCategoryParentPath($main_category_id) - { - if ( $main_category_id == 0 ) { - // don't query path for "Home" category - return Array (); - } - - $cache_key = 'parent_paths_named[%CIDSerial:' . $main_category_id . '%]'; - $cached_path = $this->Application->getCache($cache_key); - - if ( $cached_path === false ) { - $ml_formatter = $this->Application->recallObject('kMultiLanguage'); - /* @var $ml_formatter kMultiLanguage */ - - $navbar_field = $ml_formatter->LangFieldName('CachedNavBar'); - - $id_field = $this->Application->getUnitOption('c', 'IDField'); - $table_name = $this->Application->getUnitOption('c', 'TableName'); - - $this->Conn->nextQueryCachable = true; - $sql = 'SELECT ' . $navbar_field . ', ParentPath - FROM ' . $table_name . ' - WHERE ' . $id_field . ' = ' . $main_category_id; - $category_data = $this->Conn->GetRow($sql); - - $cached_path = Array (); - $skip_category = $this->Application->getBaseCategory(); - - if ( $category_data ) { - $category_names = explode('&|&', $category_data[$navbar_field]); - $category_ids = explode('|', substr($category_data['ParentPath'], 1, -1)); - - foreach ($category_ids as $category_index => $category_id) { - if ( $category_id == $skip_category ) { - continue; - } - - $cached_path[$category_id] = $category_names[$category_index]; - } - } - - $this->Application->setCache($cache_key, $cached_path); - } - - return $cached_path; - } - - /** - * Not tag. Method for parameter selection from list in this TagProcessor - * - * @param Array $params - * @param Array $possible_names - * - * @return string - * @access protected - */ - protected function SelectParam($params, $possible_names) - { - if ( !is_array($params) ) { - return ''; - } - if ( !is_array($possible_names) ) { - $possible_names = explode(',', $possible_names); - } - - foreach ($possible_names as $name) { - if ( isset($params[$name]) ) { - return $params[$name]; - } - } - - return ''; - } - - /** * 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 $language_id * @param int $theme_id * @param int $level * @return Array + * @access protected */ - function _printChildren(&$data, $parent_category_id, $language_id, $theme_id, $level = 0) + protected function _printChildren(&$data, $parent_category_id, $language_id, $theme_id, $level = 0) { - if ($data['ThemeId'] != $theme_id && $data['ThemeId'] != 0) { + if ( $data['ThemeId'] != $theme_id && $data['ThemeId'] != 0 ) { // don't show system templates from different themes return Array (); } $category_language = $data['l' . $language_id . '_Name'] ? $language_id : $this->_primaryLanguageId; - $ret = Array($parent_category_id => str_repeat('—', $level).' '.$data['l' . $category_language . '_Name']); + $ret = Array ($parent_category_id => str_repeat('—', $level) . ' ' . $data['l' . $category_language . '_Name']); - if ($data['children']) { + if ( $data['children'] ) { $level++; foreach ($data['children'] as $category_id => $category_data) { // numeric keys @@ -399,8 +118,9 @@ * @param int $parent_category_id * @param Array $languages * @return Array + * @access protected */ - function _getChildren($parent_category_id, $languages) + protected function _getChildren($parent_category_id, $languages) { static $items_by_parent = null, $parent_mapping = null; @@ -433,18 +153,18 @@ $index_category = $this->findIndexCategoryId($items_by_parent[$base_category]); // rename "Content" into "Home" keeping it's ID - $items_by_parent[ $parent_mapping[$base_category] ][$base_category]['l1_Name'] = $this->Application->Phrase('la_rootcategory_name'); + $items_by_parent[$parent_mapping[$base_category]][$base_category]['l1_Name'] = $this->Application->Phrase('la_rootcategory_name'); // remove category of "index.tpl" template unset($items_by_parent[$base_category][$index_category]); unset($parent_mapping[$index_category]); } - $data = $items_by_parent[ $parent_mapping[$parent_category_id] ][$parent_category_id]; + $data = $items_by_parent[$parent_mapping[$parent_category_id]][$parent_category_id]; $categories = array_key_exists($parent_category_id, $items_by_parent) ? $items_by_parent[$parent_category_id] : Array (); foreach ($categories as $category_id => $category_data) { - if ($category_id == $parent_category_id) { + if ( $category_id == $parent_category_id ) { // don't process myself - prevents recursion continue; } @@ -455,7 +175,14 @@ return $data; } - function findIndexCategoryId($top_categories) + /** + * Finds "Home" category among given top level categories + * + * @param Array $top_categories + * @return bool|int + * @access protected + */ + protected function findIndexCategoryId($top_categories) { foreach ($top_categories as $category_id => $category_info) { if ($category_info['Template'] == 'index') { @@ -470,8 +197,9 @@ * Generates OR retrieves from cache structure tree * * @return Array + * @access protected */ - function &_getStructureTree() + protected function &_getStructureTree() { // get cached version of structure tree if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { @@ -481,7 +209,7 @@ $data = $this->Application->getDBCache('StructureTree', CacheSettings::$structureTreeRebuildTime); } - if ($data) { + if ( $data ) { $data = unserialize($data); return $data; @@ -495,7 +223,7 @@ $root_category = $this->Application->getBaseCategory(); $data = $this->_getChildren($root_category, $languages); - if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) { + if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { $this->Application->setCache('master:StructureTree', serialize($data)); } else { @@ -505,7 +233,13 @@ return $data; } - function getTemplateMapping() + /** + * Returns template mapping (between physical and virtual pages) + * + * @return Array + * @access public + */ + public function getTemplateMapping() { if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { $data = $this->Application->getCache('master:template_mapping', false, CacheSettings::$templateMappingRebuildTime); @@ -537,10 +271,10 @@ foreach ($pages as $src_template => $page) { // process external url, before placing in cache - if ($page['UseExternalUrl']) { + if ( $page['UseExternalUrl'] ) { $external_url = $page['ExternalUrl']; - if (!preg_match('/^(.*?):\/\/(.*)$/', $external_url)) { + if ( !preg_match('/^(.*?):\/\/(.*)$/', $external_url) ) { // url without protocol will be relative url to our site $external_url = $base_url . $external_url; } @@ -554,7 +288,7 @@ $mapping[$src_template] = $dst_template; } - if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) { + if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { $data = $this->Application->setCache('master:template_mapping', serialize($mapping)); } else { @@ -568,16 +302,17 @@ * Returns category structure as field option list * * @return Array + * @access public */ - function getStructureTreeAsOptions() + public function getStructureTreeAsOptions() { - if ((defined('IS_INSTALL') && IS_INSTALL) || !$this->Application->isAdmin) { + if ( (defined('IS_INSTALL') && IS_INSTALL) || !$this->Application->isAdmin ) { // no need to create category structure during install // OR on Front-End, because it's not used there return Array (); } - if (isset($this->_structureTree)) { + if ( isset($this->_structureTree) ) { return $this->_structureTree; } @@ -599,10 +334,11 @@ * * @param string $text * @return string + * @access public */ - function replacePageIds($text) + public function replacePageIds($text) { - if (!preg_match_all('/@@(\\d+)@@/', $text, $regs)) { + if ( !preg_match_all('/@@(\\d+)@@/', $text, $regs) ) { return $text; }