Index: branches/5.2.x/core/units/categories/categories_event_handler.php =================================================================== diff -u -N -r14714 -r14719 --- branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 14714) +++ branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 14719) @@ -1,6 +1,6 @@ getObject(); + /* @var $object CategoriesItem */ + + if ( $object->GetDBField('ParentId') <= 0 ) { + // no parent category - use current (happens during import) + $object->SetDBField('ParentId', $this->Application->GetVar('m_cat_id')); + } + $this->_beforeItemChange($event); if ( $this->Application->isAdminUser || $event->Prefix == 'st' ) { @@ -1309,9 +1317,6 @@ } if ( $new_status ) { - $object =& $event->getObject(); - /* @var $object kDBItem */ - $object->SetDBField('Status', $new_status); // don't forget to set Priority for suggested from Front-End categories @@ -1350,14 +1355,14 @@ { parent::OnBeforeItemUpdate($event); + $this->_beforeItemChange($event); + $object =& $event->getObject(); /* @var $object kDBItem */ if ( $object->GetChangedFields() ) { $object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id')); } - - $this->_beforeItemChange($event); } /** @@ -1620,8 +1625,11 @@ function _beforeItemChange(&$event) { $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object CategoriesItem */ + $object->checkFilename(); + $object->generateFilename(); + $now = adodb_mktime(); if ( !$this->Application->isDebugMode() && strpos($event->Special, 'rebuild') === false ) { @@ -1636,6 +1644,12 @@ } } + $is_admin = $this->Application->isAdminUser; + + if ( (!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField('CreatedById')) ) { + $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); + } + if ($object->GetChangedFields()) { $object->SetDBField('Modified_date', $now); $object->SetDBField('Modified_time', $now); @@ -2071,6 +2085,31 @@ } /** + * Occurs after creating item + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnAfterItemCreate(&$event) + { + parent::OnAfterItemCreate($event); + + $object =& $event->getObject(); + /* @var $object CategoriesItem */ + + // need to update path after category is created, so category is included in that path + $parent_path = $object->buildParentPath(); + + $sql = 'UPDATE ' . $object->TableName . ' + SET ParentPath = ' . $this->Conn->qstr($parent_path) . ' + WHERE CategoryId = ' . $object->GetID(); + $this->Conn->Query($sql); + + $object->SetDBField('ParentPath', $parent_path); + } + + /** * Enter description here... * * @param kEvent $event @@ -2785,4 +2824,23 @@ return $res; } + + /** + * Set's new unique resource id to user + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnAfterItemValidate(kEvent &$event) + { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $resource_id = $object->GetDBField('ResourceId'); + + if ( !$resource_id ) { + $object->SetDBField('ResourceId', $this->Application->NextResourceId()); + } + } } \ No newline at end of file Index: branches/5.2.x/core/kernel/db/cat_dbitem.php =================================================================== diff -u -N -r14699 -r14719 --- branches/5.2.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14699) +++ branches/5.2.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14719) @@ -1,6 +1,6 @@ CategoryPath = Array(); @@ -58,31 +65,11 @@ } /** - * Creates a record in the database table with current item' values - * - * @param mixed $force_id Set to TRUE to force creating of item's own ID or to value to force creating of passed id. Do not pass 1 for true, pass exactly TRUE! - * @param bool $system_create - * @return bool - * @access public - */ - public function Create($force_id = false, $system_create = false) - { - $ret = parent::Create($force_id, $system_create); - - if ($ret) { - // TODO: move to OnAfterItemCreate method - $this->assignPrimaryCategory(); - } - - return $ret; - } - - /** * Assigns primary category for the item * * @access public */ - function assignPrimaryCategory() + public function assignPrimaryCategory() { if ( $this->GetDBField('CategoryId') <= 0 ) { // set primary category in item object @@ -103,15 +90,6 @@ */ public function Update($id = null, $system_update = false) { - $this->VirtualFields['ResourceId'] = Array (); - - if ( $this->GetChangedFields() ) { - $now = adodb_mktime(); - $this->SetDBField('Modified_date', $now); - $this->SetDBField('Modified_time', $now); - $this->SetDBField('ModifiedById', $this->Application->RecallVar('user_id')); - } - if ( $this->useFilenames ) { $this->checkFilename(); $this->generateFilename(); @@ -127,8 +105,6 @@ $this->Conn->Query($sql); } - unset($this->VirtualFields['ResourceId']); - return $ret; } Index: branches/5.2.x/core/units/users/users_event_handler.php =================================================================== diff -u -N -r14663 -r14719 --- branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14663) +++ branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14719) @@ -1,6 +1,6 @@ getObject(); /* @var $object kDBItem */ $resource_id = $object->GetDBField('ResourceId'); - if (!$resource_id) { + if ( !$resource_id ) { $object->SetDBField('ResourceId', $this->Application->NextResourceId()); } } - /** * Enter description here... * Index: branches/5.2.x/core/kernel/db/cat_event_handler.php =================================================================== diff -u -N -r14685 -r14719 --- branches/5.2.x/core/kernel/db/cat_event_handler.php (.../cat_event_handler.php) (revision 14685) +++ branches/5.2.x/core/kernel/db/cat_event_handler.php (.../cat_event_handler.php) (revision 14719) @@ -1,6 +1,6 @@ CallSubEvent('OnAfterItemDelete'); } } + + if ( $object->GetChangedFields() ) { + $now = adodb_mktime(); + $object->SetDBField('Modified_date', $now); + $object->SetDBField('Modified_time', $now); + $object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id')); + } } /** @@ -1012,14 +1019,16 @@ { parent::OnAfterItemCreate($event); + $object =& $event->getObject(); + /* @var $object kCatDBItem */ + if ( substr($event->Special, -6) == 'import' ) { $this->setCustomExportColumns($event); } - if ( !$this->Application->isAdmin ) { - $object =& $event->getObject(); - /* @var $object kCatDBItem */ + $object->assignPrimaryCategory(); + if ( !$this->Application->isAdmin ) { $image_helper =& $this->Application->recallObject('ImageHelper'); /* @var $image_helper ImageHelper */ @@ -2303,10 +2312,6 @@ $object->SetDBField($owner_field, $this->Application->RecallVar('user_id')); } - if ( $object->Validate() ) { - $object->SetDBField('ResourceId', $this->Application->NextResourceId()); - } - if ( !$this->Application->isAdmin ) { $this->setItemStatusByPermission($event); } @@ -2865,4 +2870,23 @@ $this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones); } } + + /** + * Set's new unique resource id to user + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnAfterItemValidate(kEvent &$event) + { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $resource_id = $object->GetDBField('ResourceId'); + + if ( !$resource_id ) { + $object->SetDBField('ResourceId', $this->Application->NextResourceId()); + } + } } \ No newline at end of file Index: branches/5.2.x/core/units/categories/categories_item.php =================================================================== diff -u -N -r14699 -r14719 --- branches/5.2.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 14699) +++ branches/5.2.x/core/units/categories/categories_item.php (.../categories_item.php) (revision 14719) @@ -1,6 +1,6 @@ GetDBField('ParentId') > 0 ? $this->GetDBField('ParentId') : $this->Application->GetVar('m_cat_id'); - $this->SetDBField('ParentId', $parent_category); - - $this->checkFilename(); - $this->generateFilename(); - - if ($this->Validate()) { - // TODO: such approach will not respect changes from CategoryEventHandler::OnBeforeItemCreate event - $this->SetDBField('ResourceId', $this->Application->NextResourceId()); - } - - // TODO: move to CategoryEventHandler::OnBeforeItemCreate - $is_admin = $this->Application->isAdminUser; - - if ((!$this->IsTempTable() && !$is_admin) || ($is_admin && !$this->GetDBField('CreatedById'))) { - $this->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); - } - - $ret = parent::Create($force_id, $system_create); - - if ($ret) { - // TODO: move to CategoryEventHandler::OnAfterItemCreate method - $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; - - } - - /** - * Updates previously loaded record with current item' values - * - * @access public - * @param int $id Primary Key Id to update - * @param bool $system_update - * @return bool - * @access public - */ - public function Update($id = null, $system_update = false) - { - $this->checkFilename(); - $this->generateFilename(); - - return parent::Update($id, $system_update); - } - - function buildParentPath() - { $parent_id = $this->GetDBField('ParentId'); - if ($parent_id == 0) { + 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) ); + $sql = 'SELECT ParentPath + FROM ' . $this->Application->getUnitOption($this->Prefix, 'TableName') . ' WHERE + CategoryId = ' . $parent_id; + $parent_path = $this->Conn->GetOne($sql); } - return $parent_path.$this->GetID().'|'; + return $parent_path . $this->GetID() . '|'; } /** * replace not allowed symbols with "_" chars + remove duplicate "_" chars in result * * @param string $string * @return string + * @access protected */ - function stripDisallowed($string) + protected function stripDisallowed($string) { $filenames_helper =& $this->Application->recallObject('FilenamesHelper'); /* @var $filenames_helper kFilenamesHelper */ @@ -110,22 +56,22 @@ return $this->checkAutoFilename($string); } - function checkFilename() + public function checkFilename() { - if ($this->GetDBField('AutomaticFilename')) { + if ( $this->GetDBField('AutomaticFilename') ) { // filename will be generated from scratch, don't check anything here - return ; + return; } - elseif ($this->GetDBField('Type') == PAGE_TYPE_TEMPLATE) { + elseif ( $this->GetDBField('Type') == PAGE_TYPE_TEMPLATE ) { // system page with AutomaticFilename checkbox unchecked -> compatibility with Proj-CMS <= 4.3.9 (when "/" were allowed in Filename) - return ; + return; } $filename = $this->GetDBField('Filename'); $this->SetDBField('Filename', $this->stripDisallowed($filename)); } - function checkAutoFilename($filename) + protected function checkAutoFilename($filename) { static $current_theme = null; @@ -202,9 +148,9 @@ * Generate item's filename based on it's title field value * * @return void - * @access protected + * @access public */ - protected function generateFilename() + public function generateFilename() { if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) { return ; @@ -224,25 +170,30 @@ * Allows to detect if root category being edited * * @return int + * @access public */ - function IsRoot() + public function IsRoot() { return $this->Application->RecallVar('IsRootCategory_'.$this->Application->GetVar('m_wid')); } + /** * Sets correct name to Home category while editing it * * @return bool + * @access public */ - function IsNewItem() + public function IsNewItem() { - if ($this->IsRoot() && $this->Prefix == 'c') { + if ( $this->IsRoot() && $this->Prefix == 'c' ) { $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); $category_name = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name'); $this->SetDBField($title_field, $category_name); + return false; } + return parent::IsNewItem(); }