Index: trunk/core/units/general/cat_dbitem.php =================================================================== diff -u -r2591 -r3150 --- trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 2591) +++ trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3150) @@ -17,9 +17,11 @@ { if (!$this->Validate()) return false; + $this->checkFilename(); $this->SetDBField('ResourceId', $this->Application->NextResourceId()); $this->SetDBField('Modified', mktime()); $this->SetDBField('CreatedById', $this->Application->GetVar('u_id')); + $this->generateFilename(); $ret = parent::Create(); if($ret) @@ -40,12 +42,24 @@ function Update($id=null) { + $this->checkFilename(); $this->VirtualFields['ResourceId'] = true; $this->SetDBField('Modified', mktime()); $this->SetDBField('ModifiedById', $this->Application->GetVar('u_id')); + $this->generateFilename(); + return parent::Update($id); } + function checkFilename() + { + if( !$this->GetDBField('AutomaticFilename') ) + { + $filename = $this->GetDBField('Filename'); + $this->SetDBField('Filename', $this->stripDisallowed($filename) ); + } + } + function Copy($cat_id=null) { if (!isset($cat_id)) $cat_id = $this->Application->GetVar('m_cat_id'); @@ -178,6 +192,34 @@ $this->Conn->Query( sprintf($sql, $data_id, $this->GetDBField('ResourceId'), $cf_id, $this->Conn->qstr($value) ) ); } + + /** + * 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); + return preg_replace('/(_+)/', '_', $string); + } + + /** + * 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); + } } ?> \ No newline at end of file