Index: branches/5.1.x/core/units/categories/categories_item.php =================================================================== diff -u -r12657 -r13086 --- branches/5.1.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 12657) +++ branches/5.1.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 13086) @@ -1,6 +1,6 @@ Application->IsAdmin(); + $is_admin = $this->Application->isAdminUser; if ((!$this->IsTempTable() && !$is_admin) || ($is_admin && !$this->GetDBField('CreatedById'))) { $this->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); @@ -108,46 +108,72 @@ function checkAutoFilename($filename) { - if(!$filename) return $filename; + static $current_theme = null; + if (!$filename) { + return $filename; + } + + if (!isset($current_theme)) { + $themes_helper =& $this->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;