Index: branches/5.1.x/core/kernel/db/cat_dbitem.php =================================================================== diff -u -N -r14241 -r14394 --- branches/5.1.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14241) +++ branches/5.1.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14394) @@ -1,6 +1,6 @@ useFilenames) { - $this->checkFilename(); - $this->generateFilename(); - } - $ret = parent::Create($force_id, $system_create); if ($ret) { // TODO: move to OnAfterItemCreate method $this->assignPrimaryCategory(); } + return $ret; } @@ -104,7 +99,7 @@ $ret = parent::Update($id, $system_update); if ($ret) { - $filename = $this->useFilenames ? $this->GetDBField('Filename') : ''; + $filename = $this->useFilenames ? (string)$this->GetDBField('Filename') : ''; $sql = 'UPDATE '.$this->CategoryItemsTable().' SET Filename = '.$this->Conn->qstr($filename).' WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); @@ -403,7 +398,7 @@ 'ItemResourceId' => $this->GetField('ResourceId'), 'PrimaryCat' => $is_primary ? 1 : 0, 'ItemPrefix' => $this->Prefix, - 'Filename' => $this->useFilenames ? $this->GetDBField('Filename') : '', // because some prefixes does not use filenames, + 'Filename' => $this->useFilenames ? (string)$this->GetDBField('Filename') : '', // because some prefixes does not use filenames, ); $this->Conn->doInsert($fields_hash, $table); } Index: branches/5.1.x/core/units/helpers/filenames_helper.php =================================================================== diff -u -N -r14339 -r14394 --- branches/5.1.x/core/units/helpers/filenames_helper.php (.../filenames_helper.php) (revision 14339) +++ branches/5.1.x/core/units/helpers/filenames_helper.php (.../filenames_helper.php) (revision 14394) @@ -1,6 +1,6 @@ Conn->GetCol('SELECT CategoryId FROM '.$table.' WHERE ItemResourceId = '.$item_id); - $item_categories_live = $this->Application->IsTempTable($table) ? $this->Conn->GetCol('SELECT CategoryId FROM '.$this->Application->GetLiveName($table).' WHERE ItemResourceId = '.$item_id) : array(); + if ( $this->Application->GetLiveName($table) == TABLE_PREFIX.'CategoryItems' ) { + $sql_mask = ' SELECT CategoryId + FROM %1$s + WHERE ItemResourceId = %2$s'; + $item_categories_cur = $this->Conn->GetCol( sprintf($sql_mask, $table, $item_id) ); + + if ( $this->Application->IsTempTable($table) ) { + $item_categories_live = $this->Conn->GetCol( sprintf($sql_mask, $this->Application->GetLiveName($table), $item_id) ); + } + else { + $item_categories_live = Array (); + } + $item_categories = array_unique(array_merge($item_categories_cur, $item_categories_live)); + if (!$item_categories) { - $item_categories = array($this->Application->GetVar('m_cat_id')); // this may happen when creating new item + $item_categories = Array( $this->Application->GetVar('m_cat_id') ); // this may happen when creating new item } - $cat_filter = ' AND CategoryId IN ('.implode(',', $item_categories).')'; + + $cat_filter = ' AND CategoryId IN (' . implode(',', $item_categories) . ')'; } else { $cat_filter = ''; } // check current table (temp or live) - $sql_temp = 'SELECT '.$id_field.' FROM '.$table.' WHERE Filename = '.$this->Conn->qstr($filename).$cat_filter; - $found_temp_ids = $this->Conn->GetCol($sql_temp); + $sql_mask = ' SELECT ' . $id_field . ' + FROM %1$s + WHERE Filename = ' . $this->Conn->qstr($filename) . $cat_filter; + $found_temp_ids = $this->Conn->GetCol( sprintf($sql_mask, $table) ); // check live table if current is temp if ( $this->Application->IsTempTable($table) ) { - $sql_live = 'SELECT '.$id_field.' FROM '.$this->Application->GetLiveName($table).' WHERE Filename = '.$this->Conn->qstr($filename).$cat_filter; - $found_live_ids = $this->Conn->GetCol($sql_live); + $found_live_ids = $this->Conn->GetCol( sprintf($sql_mask, $this->Application->GetLiveName($table)) ); } else { - $found_live_ids = array(); + $found_live_ids = Array (); } $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 - { - $append = $duplicates_found ? '_a' : ''; - if($has_page) - { + if ($duplicates_found || $has_page) {// other category has same filename as ours OR we have filename, that ends with _number + $append = $duplicates_found ? $this->_escapeChar . 'a' : ''; + + if ($has_page) { $filename = $rets[1].'_'.$rets[2]; - $append = $rets[3] ? $rets[3] : '_a'; + $append = $rets[3] ? $rets[3] : $this->_escapeChar . 'a'; } // check live & temp table - $sql_cur = 'SELECT '.$id_field.' FROM '.$table.' WHERE (Filename = %s) AND ('.$id_field.' != '.$item_id.')'.$cat_filter; - $sql_live = $this->Application->IsTempTable($table) ? 'SELECT '.$id_field.' FROM '.$this->Application->GetLiveName($table).' WHERE (Filename = %s) AND ('.$id_field.' != '.$item_id.')'.$cat_filter : false; + $sql_cur = 'SELECT ' . $id_field . ' + FROM ' . $table . ' + WHERE (Filename = %s) AND (' . $id_field . ' != ' . $item_id . ')' . $cat_filter; + + if ( $this->Application->IsTempTable($table) ) { + $sql_live = ' SELECT ' . $id_field . ' + FROM ' . $this->Application->GetLiveName($table) . ' + WHERE (Filename = %s) AND (' . $id_field . ' != ' . $item_id . ')' . $cat_filter; + } + else { + $sql_live = false; + } + while ( $this->Conn->GetOne( sprintf($sql_cur, $this->Conn->qstr($filename.$append)) ) > 0 || ( $sql_live && Index: branches/5.1.x/core/units/categories/categories_item.php =================================================================== diff -u -N -r14241 -r14394 --- branches/5.1.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 14241) +++ branches/5.1.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 14394) @@ -1,6 +1,6 @@ GetDBField('ParentId') > 0 ? $this->GetDBField('ParentId') : $this->Application->GetVar('m_cat_id'); + $this->SetDBField('ParentId', $parent_category); + $this->checkFilename(); $this->generateFilename(); @@ -33,9 +37,6 @@ $this->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); } - $parent_category = $this->GetDBField('ParentId') > 0 ? $this->GetDBField('ParentId') : $this->Application->GetVar('m_cat_id'); - $this->SetDBField('ParentId', $parent_category); - $ret = parent::Create($force_id, $system_create); if ($ret) { @@ -131,26 +132,25 @@ $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') : ''; + $unique_clause = '(Filename = %s) AND (ThemeId = ' . $item_theme . ' OR ThemeId = 0) AND (ParentId = ' . $this->GetDBField('ParentId') . ')'; + $sql_mask = ' SELECT ' . $this->IDField . ' + FROM %s + WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename)); + // check temp table - $sql_temp = ' SELECT ' . $this->IDField . ' - FROM ' . $this->TableName . ' - WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename)) . $check_in_parent_cat_only; + $sql_temp = sprintf($sql_mask, $this->TableName); $found_temp_ids = $this->Conn->GetCol($sql_temp); // check live table - $sql_live = ' SELECT ' . $this->IDField . ' - FROM ' . $this->Application->GetLiveName($this->TableName) . ' - WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename)) . $check_in_parent_cat_only; + $sql_live = sprintf($sql_mask, $this->Application->GetLiveName($this->TableName)); $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 $append = $duplicates_found ? $escape_char . 'a' : ''; if ($has_page) { @@ -162,6 +162,7 @@ $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 . ')';