Index: trunk/core/units/categories/categories_item.php =================================================================== diff -u -r7391 -r7635 --- trunk/core/units/categories/categories_item.php (.../categories_item.php) (revision 7391) +++ trunk/core/units/categories/categories_item.php (.../categories_item.php) (revision 7635) @@ -176,6 +176,58 @@ } return parent::IsNewItem(); } + + /** + * Sets new name for item in case if it is beeing copied + * in same table + * + * @param array $master Table data from TempHandler + * @param int $foreign_key ForeignKey value to filter name check query by + * @access private + */ + function NameCopy($master=null, $foreign_key=null) + { + $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); + if (!$title_field || isset($this->CalculatedFields[$title_field]) ) return; + + $new_name = $this->GetDBField($title_field); + $cat_id = $this->Application->GetVar('m_cat_id'); + $this->SetDBField('ParentId', $cat_id); + $original_checked = false; + do { + if ( preg_match('/Copy ([0-9]*) *of (.*)/', $new_name, $regs) ) { + $new_name = 'Copy '.($regs[1]+1).' of '.$regs[2]; + } + elseif ($original_checked) { + $new_name = 'Copy of '.$new_name; + } + + // if we are cloning in temp table this will look for names in temp table, + // since object' TableName contains correct TableName (for temp also!) + // if we are cloning live - look in live + $query = 'SELECT '.$title_field.' FROM '.$this->TableName.' + WHERE ParentId = '.$cat_id.' AND '.$title_field.' = '.$this->Conn->qstr($new_name); + + $foreign_key_field = getArrayValue($master, 'ForeignKey'); + $foreign_key_field = is_array($foreign_key_field) ? $foreign_key_field[ $master['ParentPrefix'] ] : $foreign_key_field; + + if ($foreign_key_field && isset($foreign_key)) { + $query .= ' AND '.$foreign_key_field.' = '.$foreign_key; + } + + $res = $this->Conn->GetOne($query); + + /*// if not found in live table, check in temp table if applicable + if ($res === false && $object->Special == 'temp') { + $query = 'SELECT '.$name_field.' FROM '.$this->GetTempName($master['TableName']).' + WHERE '.$name_field.' = '.$this->Conn->qstr($new_name); + $res = $this->Conn->GetOne($query); + }*/ + + $original_checked = true; + } while ($res !== false); + $this->SetDBField($title_field, $new_name); + } } ?> \ No newline at end of file