Index: trunk/kernel/include/item.php =================================================================== diff -u -r3179 -r3250 --- trunk/kernel/include/item.php (.../item.php) (revision 3179) +++ trunk/kernel/include/item.php (.../item.php) (revision 3250) @@ -1117,13 +1117,39 @@ $string = str_replace($not_allowed, '_', $string); $string = preg_replace('/(_+)/', '_', $string); + $string = $this->checkAutoFilename($string); - while( preg_match('/(.*)_([\d]+)$/', $string, $rets) ) + return $string; + } + + function checkAutoFilename($filename) + { + if(!$filename || $this->UniqueId() == 0 ) return $filename; + $db =& GetADODBConnection(); + + $sql = 'SELECT '.$this->IdField().' FROM '.$this->tablename.' WHERE Filename = '.$db->qstr($filename); + $found_item_id = $db->GetOne($sql); + $has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets); + if( ($found_item_id != $this->UniqueId()) || $has_page ) // other category has same filename as ours OR we have filename, that ends with _number { - $string = $rets[1]; + $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->UniqueId().')'; + while ( $db->GetOne( sprintf($sql, $db->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 $string; + + return $filename; } function GenerateFilename()