Index: branches/5.2.x/core/units/categories/categories_event_handler.php =================================================================== diff -u -N -r14602 -r14610 --- branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 14602) +++ branches/5.2.x/core/units/categories/categories_event_handler.php (.../categories_event_handler.php) (revision 14610) @@ -1,6 +1,6 @@ Application->recallObject($related_prefix . '.current', null, Array('skip_autoload' => true)); /* @var $p_item kCatDBItem */ - + $p_item->Load( (int)$id ); $p_resource_id = $p_item->GetDBField('ResourceId'); @@ -792,18 +792,12 @@ // 2. preset template $category_id = $this->Application->GetVar('m_cat_id'); $root_category = $this->Application->getBaseCategory(); - + if ( $category_id == $root_category ) { $object->SetDBField('Template', $this->_getDefaultDesign()); } - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - // 3. prepare priorities dropdown - $priority_helper->preparePriorities($event, true, 'ParentId = ' . $category_id); - - // 4. set default owner + // 3. set default owner $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); } @@ -830,16 +824,6 @@ return; } - // 1. update priorities - $tmp = $this->Application->RecallVar('priority_changes' . $this->Application->GetVar('m_wid')); - $changes = $tmp ? unserialize($tmp) : Array (); - $changed_ids = array_keys($changes); - - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - $priority_helper->updatePriorities($event, $changes, Array (0 => $event->getEventParam('ids'))); - if ( $this->Application->RecallVar('PermCache_UpdateRequired') ) { $this->Application->RemoveVar('IsRootCategory_' . $this->Application->GetVar('m_wid')); } @@ -848,7 +832,7 @@ $this->_resetMenuCache(); if ( $is_editing ) { - // 2. send email event to category owner, when it's status is changed (from admin) + // send email event to category owner, when it's status is changed (from admin) $object->SwitchToLive(); $new_statuses = $this->_getCategoryStatus($ids); $process_statuses = Array (STATUS_ACTIVE, STATUS_DISABLED); @@ -898,12 +882,6 @@ return; } - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - $category_id = $this->Application->GetVar('m_cat_id'); - $priority_helper->preparePriorities($event, true, 'ParentId = ' . $category_id); - parent::OnPreSaveCreated($event); } @@ -936,43 +914,26 @@ */ protected function customProcessing(&$event, $type) { - if ($event->Name == 'OnMassDelete' && $type == 'before') { + if ( $event->Name == 'OnMassDelete' && $type == 'before' ) { $ids = $event->getEventParam('ids'); - if (!$ids || $this->Application->ConfigValue('AllowDeleteRootCats')) { - return ; + if ( !$ids || $this->Application->ConfigValue('AllowDeleteRootCats') ) { + return; } + $root_categories = Array (); + // get module root categories and exclude them foreach ($this->Application->ModuleInfo as $module_info) { $root_categories[] = $module_info['RootCat']; } + $root_categories = array_unique($root_categories); - if ($root_categories && array_intersect($ids, $root_categories)) { + if ( $root_categories && array_intersect($ids, $root_categories) ) { $event->setEventParam('ids', array_diff($ids, $root_categories)); $this->Application->StoreVar('root_delete_error', 1); } } - - $change_events = Array ('OnPreSave', 'OnPreSaveCreated', 'OnUpdate', 'OnSave'); - if ($type == 'after' && in_array($event->Name, $change_events)) { - $object =& $event->getObject(); - /* @var $object kDBItem */ - - $tmp = $this->Application->RecallVar('priority_changes'.$this->Application->GetVar('m_wid')); - $changes = $tmp ? unserialize($tmp) : array(); - - if (!isset($changes[$object->GetID()])) { - $changes[$object->GetId()]['old'] = $object->GetID() == 0 ? 'new' : $object->GetDBField('OldPriority'); - } - - if ($changes[$object->GetId()]['old'] == $object->GetDBField('Priority')) return ; - - $changes[$object->GetId()]['new'] = $object->GetDBField('Priority'); - $changes[$object->GetId()]['parent'] = $object->GetDBField('ParentId'); - - $this->Application->StoreVar('priority_changes'.$this->Application->GetVar('m_wid'), serialize($changes)); - } } /** @@ -1068,14 +1029,6 @@ } $this->clearSelectedIDs($event); - // update priorities - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - // after deleting categories, all priorities should be recalculated - $parent_id = $this->Application->GetVar('m_cat_id'); - $priority_helper->recalculatePriorities($event, 'ParentId = ' . $parent_id); - $this->Application->StoreVar('RefreshStructureTree', 1); $this->_resetMenuCache(); } @@ -1208,13 +1161,21 @@ /* @var $priority_helper kPriorityHelper */ if ( $clipboard_data['cut'] ) { - $priority_helper->recalculatePriorities($event, 'ParentId = ' . $source_category_id); + $ids = $priority_helper->recalculatePriorities($event, 'ParentId = ' . $source_category_id); + + if ( $ids ) { + $priority_helper->massUpdateChanged($event->Prefix, $ids); + } } // recalculate priorities of newly pasted categories in destination category $parent_id = $this->Application->GetVar('m_cat_id'); - $priority_helper->recalculatePriorities($event, 'ParentId = ' . $parent_id); + $ids = $priority_helper->recalculatePriorities($event, 'ParentId = ' . $parent_id); + if ( $ids ) { + $priority_helper->massUpdateChanged($event->Prefix, $ids); + } + if ( $clipboard_data['cut'] || $clipboard_data['copy'] ) { // rebuild with progress bar if ( $this->Application->ConfigValue('QuickCategoryPermissionRebuild') ) { @@ -1570,7 +1531,7 @@ if ( $page->isLoaded() ) { $real_t = $page->GetDBField('CachedTemplate'); $this->Application->SetVar('m_cat_id', $page->GetDBField('CategoryId')); - + if ( $page->GetDBField('FormId') ) { $this->Application->SetVar('form_id', $page->GetDBField('FormId')); } @@ -2038,7 +1999,7 @@ if ( !$this->Application->isAdmin ) { // calculate priorities dropdown only for admin - return ; + return; } $object =& $event->getObject(); @@ -2050,42 +2011,14 @@ WHERE ParentPath LIKE "' . $object->GetDBField('ParentPath') . '%"'; $remove_categories = $this->Conn->GetCol($sql); - $field_options = $object->GetFieldOptions('ParentId'); + $options = $object->GetFieldOption('ParentId', 'options'); foreach ($remove_categories as $remove_category) { - unset($field_options['options'][$remove_category]); + unset($options[$remove_category]); } - $object->SetFieldOptions('ParentId', $field_options); - - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - $priority_helper->preparePriorities($event, false, 'ParentId = ' . $object->GetDBField('ParentId')); - - // storing priority right after load for comparing when updating - $object->SetDBField('OldPriority', $object->GetDBField('Priority')); + $object->SetFieldOption('ParentId', 'options', $options); } /** - * Builds list - * - * @param kEvent $event - * @access protected - */ - function OnListBuild(&$event) - { - parent::OnListBuild($event); - - if (!$this->Application->isAdminUser) { - return ; - } - - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - $priority_helper->preparePriorities($event, false, 'ParentId = '.$this->Application->GetVar('m_cat_id')); - } - - /** * Enter description here... * * @param kEvent $event @@ -2143,44 +2076,9 @@ */ function OnChangePriority(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { - $event->status = kEvent::erFAIL; - return; - } + $this->Application->SetVar('priority_prefix', $event->getPrefixSpecial()); + $event->CallSubEvent('priority:' . $event->Name); - $object =& $event->getObject( Array('skip_autoload' => true) ); - $ids = $this->StoreSelectedIDs($event); - - if ($ids) { - $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $parent_id = $this->Application->GetVar('m_cat_id'); - - $sql = 'SELECT Priority, '.$id_field.' - FROM '.$table_name.' - WHERE '.$id_field.' IN ('.implode(',', $ids).')'; - $priorities = $this->Conn->GetCol($sql, $id_field); - - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ - - foreach ($ids as $id) { - $new_priority = $priorities[$id] + ($event->Name == 'OnMassMoveUp' ? +1 : -1); - - $changes = Array ( - $id => Array ('old' => $priorities[$id], 'new' => $new_priority, 'parent' => $parent_id), - ); - - $sql = 'UPDATE '.$table_name.' - SET Priority = '.$new_priority.' - WHERE '.$id_field.' = '.$id; - $this->Conn->Query($sql); - - $priority_helper->updatePriorities($event, $changes, Array ($id => $id)); - } - } - - $this->clearSelectedIDs($event); $this->Application->StoreVar('RefreshStructureTree', 1); $this->_resetMenuCache(); } @@ -2197,11 +2095,9 @@ return; } - $priority_helper =& $this->Application->recallObject('PriorityHelper'); - /* @var $priority_helper kPriorityHelper */ + $this->Application->SetVar('priority_prefix', $event->getPrefixSpecial()); + $event->CallSubEvent('priority:' . $event->Name); - $parent_id = $this->Application->GetVar('m_cat_id'); - $priority_helper->recalculatePriorities($event, 'ParentId = ' . $parent_id); $this->_resetMenuCache(); } @@ -2238,7 +2134,7 @@ $query_object =& $this->Application->recallObject('HTTPQuery'); /* @var $query_object kHTTPQuery */ - + $sql = 'SHOW TABLES LIKE "'.$search_table.'"'; if ( !isset($query_object->Get['keywords']) && !isset($query_object->Post['keywords']) && $this->Conn->Query($sql) ) { @@ -2551,4 +2447,38 @@ $object->setID($id); } } + + /** + * Returns constrain for priority calculations + * + * @param kEvent $event + * @return void + * @see PriorityEventHandler + * @access protected + */ + protected function OnGetConstrainInfo(&$event) + { + $contarain = ''; // for OnSave + + $event_name = $event->getEventParam('original_event'); + $actual_event_name = $event->getEventParam('actual_event'); + + if ( $actual_event_name == 'OnSavePriorityChanges' || $event_name == 'OnAfterItemLoad' || $event_name == 'OnAfterItemDelete' ) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $contarain = 'ParentId = ' . $object->GetDBField('ParentId'); + } + elseif ( $actual_event_name == 'OnPreparePriorities' ) { + $contarain = 'ParentId = ' . $this->Application->GetVar('m_cat_id'); + } + elseif ( $event_name == 'OnSave' ) { + $contarain = ''; + } + else { + $contarain = 'ParentId = ' . $this->Application->GetVar('m_cat_id'); + } + + $event->setEventParam('constrain_info', Array ($contarain, '')); + } } \ No newline at end of file