Index: trunk/core/units/general/cat_dbitem.php =================================================================== diff -u -r3179 -r3250 --- trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3179) +++ trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3250) @@ -207,15 +207,40 @@ $string = str_replace($not_allowed, '_', $string); $string = preg_replace('/(_+)/', '_', $string); + $string = $this->checkAutoFilename($string); - while( preg_match('/(.*)_([\d]+)$/', $string, $rets) ) - { - $string = $rets[1]; - } - return $string; } + function checkAutoFilename($filename) + { + if(!$filename || $this->GetID() == 0 ) return $filename; + + $sql = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE Filename = '.$this->Conn->qstr($filename); + $found_item_id = $this->Conn->GetOne($sql); + $has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets); + if( ($found_item_id != $this->GetID()) || $has_page ) // other category has same filename as ours OR we have filename, that ends with _number + { + $append = $found_item_id ? 'a' : ''; + if($has_page) + { + $filename = $rets[1].'_'.$rets[2]; + $append = $rets[3] ? $rets[3] : 'a'; + } + + $sql = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE (Filename = %s) AND ('.$this->IDField.' != '.$this->GetID().')'; + while ( $this->Conn->GetOne( sprintf($sql, $this->Conn->qstr($filename.$append)) ) > 0 ) + { + if (substr($append, -1) == 'z') $append .= 'a'; + $append = substr($append, 0, strlen($append) - 1) . chr( ord( substr($append, -1) ) + 1 ); + } + + return $filename.$append; + } + + return $filename; + } + /** * Generate item's filename based on it's title field value *