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() ); if ($this->useFilenames) { $this->checkFilename(); $this->generateFilename(); } $this->SetDBField('ParentId', $this->Application->GetVar('m_cat_id') ); $ret = parent::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) { if ($this->useFilenames) { $this->checkFilename(); $this->generateFilename(); } $ret = parent::Update($id, $system_update); return $ret; } function buildParentPath() { $parent_id = $this->GetDBField('ParentId'); $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) ); if(!$parent_path && $parent_id) $parent_path = '|'.$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; $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); $name = $this->stripDisallowed( $this->GetDBField($title_field) ); if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); } } ?>