Index: branches/5.0.x/core/units/helpers/themes_helper.php =================================================================== diff -u -r12306 -r12634 --- branches/5.0.x/core/units/helpers/themes_helper.php (.../themes_helper.php) (revision 12306) +++ branches/5.0.x/core/units/helpers/themes_helper.php (.../themes_helper.php) (revision 12634) @@ -1,6 +1,6 @@ getCurrentThemeId(); + } + $sql = 'SELECT ' . $this->Application->getUnitOption('c', 'IDField') . ' FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' WHERE ( (NamedParentPath = ' . $this->Conn->qstr('Content/' . $template) . ') OR (IsSystem = 1 AND CachedTemplate = ' . $this->Conn->qstr($template) . ') ) - AND (ThemeId = ' . $this->getCurrentThemeId() . ' OR ThemeId = 0)'; + AND (ThemeId = ' . $theme_id . ($theme_id > 0 ? ' OR ThemeId = 0' : '') . ')'; + return $this->Conn->GetOne($sql); } } \ No newline at end of file Index: branches/5.0.x/core/units/categories/categories_item.php =================================================================== diff -u -r12535 -r12634 --- branches/5.0.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 12535) +++ branches/5.0.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 12634) @@ -1,6 +1,6 @@ Application->recallObject('ThemesHelper'); + /* @var $themes_helper kThemesHelper */ + + $current_theme = (int)$themes_helper->getCurrentThemeId(); + } + $escape_char = $this->Application->ConfigValue('FilenameSpecialCharReplacement'); $item_id = !$this->GetID() ? 0 : $this->GetID(); - $check_in_parent_cat_only = $item_id ? ' AND ParentId = '.$this->GetDBField('ParentId') : ''; + $item_theme = $this->GetDBField('ThemeId'); + if (!$item_theme) { + // user creates category manually, that's why ThemeId = 0 -> use current admin theme instead + $item_theme = $current_theme; + } + + $unique_clause = '(Filename = %s) AND (ThemeId = ' . $item_theme . ' OR ThemeId = 0)'; + $check_in_parent_cat_only = $item_id ? ' AND ParentId = ' . $this->GetDBField('ParentId') : ''; + // check temp table - $sql_temp = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE Filename = '.$this->Conn->qstr($filename).$check_in_parent_cat_only; + $sql_temp = ' SELECT ' . $this->IDField . ' + FROM ' . $this->TableName . ' + WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename)) . $check_in_parent_cat_only; $found_temp_ids = $this->Conn->GetCol($sql_temp); // check live table - $sql_live = 'SELECT '.$this->IDField.' FROM '.$this->Application->GetLiveName($this->TableName).' WHERE Filename = '.$this->Conn->qstr($filename).$check_in_parent_cat_only; + $sql_live = ' SELECT ' . $this->IDField . ' + FROM ' . $this->Application->GetLiveName($this->TableName) . ' + WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename)) . $check_in_parent_cat_only; $found_live_ids = $this->Conn->GetCol($sql_live); $found_item_ids = array_unique( array_merge($found_temp_ids, $found_live_ids) ); $has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets); $duplicates_found = (count($found_item_ids) > 1) || ($found_item_ids && $found_item_ids[0] != $item_id); - if ($duplicates_found || $has_page) // other category has same filename as ours OR we have filename, that ends with _number - { + if ($duplicates_found || $has_page) {// other category has same filename as ours OR we have filename, that ends with _number $append = $duplicates_found ? $escape_char . 'a' : ''; - if($has_page) - { + if ($has_page) { $filename = $rets[1].'_'.$rets[2]; $append = $rets[3] ? $rets[3] : $escape_char . 'a'; } // check live & temp table - $sql_temp = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE (Filename = %s) AND ('.$this->IDField.' != '.$item_id.')'; - $sql_live = 'SELECT '.$this->IDField.' FROM '.$this->Application->GetLiveName($this->TableName).' WHERE (Filename = %s) AND ('.$this->IDField.' != '.$item_id.')'; + $sql_temp = ' SELECT ' . $this->IDField . ' + FROM ' . $this->TableName . ' + WHERE ' . $unique_clause . ' AND (' . $this->IDField . ' != ' . $item_id . ')'; + $sql_live = ' SELECT ' . $this->IDField . ' + FROM ' . $this->Application->GetLiveName($this->TableName) . ' + WHERE ' . $unique_clause . ' AND (' . $this->IDField . ' != ' . $item_id . ')'; + while ( $this->Conn->GetOne( sprintf($sql_temp, $this->Conn->qstr($filename.$append)) ) > 0 || $this->Conn->GetOne( sprintf($sql_live, $this->Conn->qstr($filename.$append)) ) > 0 ) { if (mb_substr($append, -1) == 'z') $append .= 'a'; $append = mb_substr($append, 0, mb_strlen($append) - 1) . chr( ord( mb_substr($append, -1) ) + 1 ); } - return $filename.$append; + return $filename . $append; } return $filename; Index: branches/5.0.x/core/units/helpers/mod_rewrite_helper.php =================================================================== diff -u -r12511 -r12634 --- branches/5.0.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 12511) +++ branches/5.0.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 12634) @@ -1,6 +1,6 @@ Array ('m')); - $url_parts = $url ? explode('/', trim($url, '/')) : Array (); + $url_parts = $url ? explode('/', trim(mb_strtolower($url), '/')) : Array (); if (($this->HTTPQuery->Get('rewrite') == 'on') || !$url_parts) { $this->_setDefaultValues($vars); @@ -175,7 +175,7 @@ $themes_helper =& $this->Application->recallObject('ThemesHelper'); /* @var $themes_helper kThemesHelper */ - $vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t']); + $vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t'], $vars['m_theme']); header('HTTP/1.0 404 Not Found'); } @@ -234,7 +234,7 @@ $sql = 'SELECT FileId FROM ' . TABLE_PREFIX . 'ThemeFiles - WHERE (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')'; + WHERE (ThemeId = ' . $vars['m_theme'] . ') AND (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')'; $template_found = $this->Conn->GetOne($sql); if (!$template_found) { @@ -244,6 +244,12 @@ if ($template_found) { $vars['t'] = $template_path; + + $themes_helper =& $this->Application->recallObject('ThemesHelper'); + /* @var $themes_helper kThemesHelper */ + + $vars['m_cat_id'] = $themes_helper->getPageByTemplate($template_path, $vars['m_theme']); + return true; } @@ -404,21 +410,28 @@ return false; } - $url_part = array_shift($url_parts); + $url_part = reset($url_parts); - $sql = 'SELECT LanguageId + $sql = 'SELECT LanguageId, IF(LOWER(PackName) = ' . $this->Conn->qstr($url_part) . ', 2, PrimaryLang) AS SortKey FROM ' . TABLE_PREFIX . 'Language - WHERE LOWER(PackName) = ' . $this->Conn->qstr($url_part) . ' AND Enabled = 1'; - $language_id = $this->Conn->GetOne($sql); - + WHERE Enabled = 1 + ORDER BY SortKey DESC'; + $language_info = $this->Conn->GetRow($sql); $this->Application->Phrases = new PhrasesCache(); - if ($language_id) { - $vars['m_lang'] = $language_id; + + if ($language_info && $language_info['LanguageId'] && $language_info['SortKey']) { + // primary language will be selected in case, when $url_part doesn't match to other's language pack name + // don't use next enabled language, when primary language is disabled + $vars['m_lang'] = $language_info['LanguageId']; + + if ($language_info['SortKey'] == 2) { + // language was found by pack name + array_shift($url_parts); + } + return true; } - array_unshift($url_parts, $url_part); - return false; } @@ -435,18 +448,28 @@ return false; } - $url_part = array_shift($url_parts); + $url_part = reset($url_parts); - $sql = 'SELECT ThemeId + $sql = 'SELECT ThemeId, IF(LOWER(Name) = ' . $this->Conn->qstr($url_part) . ', 2, PrimaryTheme) AS SortKey FROM ' . TABLE_PREFIX . 'Theme - WHERE LOWER(Name) = ' . $this->Conn->qstr($url_part) . ' AND Enabled = 1'; - $theme_id = $this->Conn->GetOne($sql); - if ($theme_id) { - $vars['m_theme'] = $theme_id; + WHERE Enabled = 1 + ORDER BY SortKey DESC'; + $theme_info = $this->Conn->GetRow($sql); + + if ($theme_info && $theme_info['ThemeId'] && $theme_info['SortKey']) { + // primary theme will be selected in case, when $url_part doesn't match to other's theme name + // don't use next enabled theme, when primary theme is disabled + $vars['m_theme'] = $theme_info['ThemeId']; + + if ($theme_info['SortKey'] == 2) { + // theme was found by name + array_shift($url_parts); + } + return true; } - array_unshift($url_parts, $url_part); + $vars['m_theme'] = 0; // required, because used later for category/template detection return false; } @@ -563,7 +586,7 @@ $sql = 'SELECT CategoryId, IsIndex, NamedParentPath FROM ' . TABLE_PREFIX . 'Category - WHERE Status IN (1,4) AND (LOWER(NamedParentPath) = ' . $this->Conn->qstr($category_path) . ')'; + WHERE Status IN (1,4) AND (LOWER(NamedParentPath) = ' . $this->Conn->qstr($category_path) . ') AND (ThemeId = ' . $vars['m_theme'] . ' OR ThemeId = 0)'; $category_info = $this->Conn->GetRow($sql); if ($category_info !== false) { @@ -578,7 +601,7 @@ if ($last_category_info['IsIndex'] == 2) { $sql = 'SELECT CategoryId, NamedParentPath FROM ' . TABLE_PREFIX . 'Category - WHERE ParentId = ' . $last_category_info['CategoryId'] . ' AND IsIndex = 1'; + WHERE (ParentId = ' . $last_category_info['CategoryId'] . ') AND (IsIndex = 1) AND (ThemeId = ' . $vars['m_theme'] . ' OR ThemeId = 0)'; $category_info = $this->Conn->GetRow($sql); if ($category_info) {