Index: trunk/kernel/units/general/cat_dbitem.php =================================================================== diff -u -N -r5211 -r5505 --- trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 5211) +++ trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 5505) @@ -22,7 +22,7 @@ $this->SetDBField('ResourceId', $this->Application->NextResourceId()); $this->SetDBField('Modified', adodb_mktime() ); - + if ($this->mode != 't') { $this->SetDBField('CreatedById', $this->Application->GetVar('u_id')); } @@ -42,8 +42,8 @@ $table = TABLE_PREFIX.'CategoryItems'; } $cat_id = $this->Application->GetVar('m_cat_id'); - $query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat) - VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1)'; + $query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat,ItemPrefix,Filename) + VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1,'.$this->Conn->qstr($this->Prefix).','.$this->Conn->qstr($this->GetDBField('Filename')).')'; $this->Conn->Query($query); } return $ret; @@ -62,6 +62,11 @@ $ret = parent::Update($id, $system_update); + if ($ret) { + $table = $this->Application->IsTempTable($this->TableName) ? $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems') : TABLE_PREFIX.'CategoryItems'; + $this->Conn->Query('UPDATE '.$table.' SET Filename = '.$this->Conn->qstr($this->GetDBField('Filename')).' WHERE ItemResourceId = '.$this->GetDBField('ResourceId')); + } + unset($this->VirtualFields['ResourceId']); return $ret; } @@ -197,63 +202,20 @@ * @param string $string * @return string */ - function stripDisallowed($string) + function stripDisallowed($filename) { - $not_allowed = Array( ' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|', '`', - '~', '!', '@', '#', '$', '%', '^', '&', '(', ')', '~', - '+', '=', '-', '{', '}', ']', '[', "'", ';', '.', ','); - - $string = str_replace($not_allowed, '_', $string); - $string = preg_replace('/(_+)/', '_', $string); - $string = $this->checkAutoFilename($string); - - return $string; + $filenames_helper =& $this->Application->recallObject('FilenamesHelper'); + return $filenames_helper->stripDisallowed(TABLE_PREFIX.'CategoryItems', 'ItemResourceId', $this->GetDBField('ResourceId'), $filename); } + /* commented out because it's called only from stripDisallowed body, which is moved to helper + function checkAutoFilename($filename) { - if(!$filename) return $filename; + $filenames_helper =& $this->Application->recallObject('FilenamesHelper'); + return $filenames_helper->checkAutoFilename($this->TableName, $this->IDField, $this->GetID(), $filename); + }*/ - $item_id = !$this->GetID() ? 0 : $this->GetID(); - - // check temp table - $sql_temp = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE Filename = '.$this->Conn->qstr($filename); - $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); - $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 ? '_a' : ''; - if($has_page) - { - $filename = $rets[1].'_'.$rets[2]; - $append = $rets[3] ? $rets[3] : '_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.')'; - 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 (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 * @@ -297,7 +259,7 @@ */ function assignToCategory($category_id, $is_primary = false) { - $table = TABLE_PREFIX.'CategoryItems'; + $table = $this->mode == 't' ? $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems') : TABLE_PREFIX.'CategoryItems'; $key_clause = '(ItemResourceId = '.$this->GetDBField('ResourceId').')'; // get all cateories, where item is in @@ -335,9 +297,12 @@ $this->Conn->Query($sql); } else { - $sql = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat) VALUES (%s,%s,%s)'; - $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0) ); + $sql = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat,ItemPrefix,Filename) VALUES (%s,%s,%s,%s,%s)'; + $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0, $this->Conn->qstr($this->Prefix), $this->Conn->qstr($this->GetDBField('Filename'))) ); } + // to ensure filename update after adding to another category + // this is critical since there may be an item with same filename in newly added category! + $this->Update(); } /**