Index: trunk/kernel/units/categories/categories_item.php =================================================================== diff -u -N --- trunk/kernel/units/categories/categories_item.php (revision 6093) +++ trunk/kernel/units/categories/categories_item.php (revision 0) @@ -1,179 +0,0 @@ -Validate()) return false; - - $this->SetDBField('ResourceId', $this->Application->NextResourceId()); - $this->SetDBField('CreatedById', $this->Application->GetVar('u_id') ); - $this->SetDBField('CreatedOn_date', adodb_mktime() ); - $this->SetDBField('CreatedOn_time', adodb_mktime() ); - - $this->checkFilename(); - $this->generateFilename(); - - $this->SetDBField('ParentId', $this->Application->GetVar('m_cat_id') ); - $ret = parent::Create($force_id, $system_create); - if ($ret) { - $sql = 'UPDATE %s SET ParentPath = %s WHERE CategoryId = %s'; - $parent_path = $this->buildParentPath(); - $this->Conn->Query( sprintf($sql, $this->TableName, $this->Conn->qstr($parent_path), $this->GetID() ) ); - - $this->SetDBField('ParentPath', $parent_path); - } - return $ret; - - } - - function Update($id=null, $system_update=false) - { - $this->checkFilename(); - $this->generateFilename(); - - $ret = parent::Update($id, $system_update); - return $ret; - } - - function buildParentPath() - { - $parent_id = $this->GetDBField('ParentId'); - - if ($parent_id == 0) { - $parent_path = '|'; - } - else { - $cat_table = $this->Application->getUnitOption($this->Prefix, 'TableName'); - $sql = 'SELECT ParentPath FROM '.$cat_table.' WHERE CategoryId = %s'; - $parent_path = $this->Conn->GetOne( sprintf($sql, $parent_id) ); - } - - return $parent_path.$this->GetID().'|'; - } - - /** - * replace not allowed symbols with "_" chars + remove duplicate "_" chars in result - * - * @param string $string - * @return string - */ - function stripDisallowed($string) - { - $not_allowed = Array( ' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|', '`', - '~', '!', '@', '#', '$', '%', '^', '&', '(', ')', '~', - '+', '=', '-', '{', '}', ']', '[', "'", ';', '.', ','); - - $string = str_replace($not_allowed, '_', $string); - $string = preg_replace('/(_+)/', '_', $string); - $string = $this->checkAutoFilename($string); - - return $string; - } - - function checkFilename() - { - if( !$this->GetDBField('AutomaticFilename') ) - { - $filename = $this->GetDBField('Filename'); - $this->SetDBField('Filename', $this->stripDisallowed($filename) ); - } - } - - function checkAutoFilename($filename) - { - if(!$filename) return $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 - * - * @return string - */ - function generateFilename() - { - if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) return false; - - $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); - $name = $this->stripDisallowed( $this->GetDBField( $ml_formatter->LangFieldName('Name', true) ) ); - - if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); - } - - /** - * Allows to detect if root category being edited - * - * @param Array $params - */ - function IsRoot() - { - $category_id = $this->Application->GetVar($this->getPrefixSpecial().'_id'); - if (is_numeric($category_id) && $category_id == 0) { - $sql = 'SELECT '.$this->IDField.' - FROM '.$this->TableName.' - WHERE '.$this->IDField.' = '.$category_id; - if ($this->Conn->GetOne($sql) === false) { - return true; - } - } - - return false; - } - - /** - * Sets correct name to Home category while editing it - * - * @return bool - */ - function IsNewItem() - { - if ($this->IsRoot()) { - $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); - $category_name = $this->Application->Phrase( $this->Application->ConfigValue('Root_Name') ); - $this->SetDBField($title_field, $category_name); - return false; - } - return parent::IsNewItem(); - } - } - -?> \ No newline at end of file