Index: units/priorites/priorites_config.php =================================================================== --- units/priorites/priorites_config.php (revision 0) +++ units/priorites/priorites_config.php (revision 0) @@ -0,0 +1,30 @@ + 'priority', + 'EventHandlerClass' => Array('class' => 'PriorityEventHandler', 'file' => 'priority_eh.php', 'build_event' => 'OnBuild'), + + 'QueryString' => Array( + 1 => 'prefix', + 2 => 'event', + ), + + 'Hooks' => Array( + Array( + 'Mode' => hAFTER, + 'Conditional' => false, + 'HookToPrefix' => 'adm', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnBeforeShutdown'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnBeforeShutdown', + 'Conditional' => false, + ), + ), + + 'PermSection' => Array ('main' => 'custom',), + + 'ProcessPrefixes' => Array() + ); +?> \ No newline at end of file Index: units/priorites/priority_eh.php =================================================================== --- units/priorites/priority_eh.php (revision 0) +++ units/priorites/priority_eh.php (revision 0) @@ -0,0 +1,316 @@ + Array('self' => true), + ); + $this->permMapping = array_merge($this->permMapping, $permissions); + } + + function mapEvents() + { + parent::mapEvents(); + + $events_map = Array ( + 'OnMassMoveUp' => 'OnChangePriority', + 'OnMassMoveDown' => 'OnChangePriority', + ); + + $this->eventMethods = array_merge($this->eventMethods, $events_map); + } + + /** + * Enter description here... + * + * @param kEvent $event + */ + function OnAfterConfigRead(&$event) + { + $hooks = Array( + Array( + 'Mode' => hAFTER, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnAfterItemLoad', 'OnPreCreate', 'OnListBuild'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnPreparePriorities', + 'Conditional' => false, + ), + Array( + 'Mode' => hBEFORE, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnPreSaveCreated'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnPreparePriorities', + 'Conditional' => false, + ), + Array( + 'Mode' => hAFTER, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnPreSave', 'OnPreSaveCreated', 'OnSave', 'OnUpdate'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnSavePriorityChanges', + 'Conditional' => false, + ), + Array( + 'Mode' => hAFTER, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnSave'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnSaveItems', + 'Conditional' => false, + ), + Array( + 'Mode' => hBEFORE, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnBeforeItemCreate'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnItemCreate', + 'Conditional' => false, + ), + Array( + 'Mode' => hBEFORE, + 'Conditional' => false, + 'HookToPrefix' => '', + 'HookToSpecial' => '*', + 'HookToEvent' => Array('OnAfterItemDelete'), + 'DoPrefix' => 'priority', + 'DoSpecial' => '*', + 'DoEvent' => 'OnItemDelete', + 'Conditional' => false, + ) + ); + + $prefixes = $this->Application->getUnitOption($event->Prefix, 'ProcessPrefixes'); + foreach ($prefixes as $prefix) { + foreach ($hooks as $hook) { + if (!is_array($hook['HookToEvent'])) { + $hook['HookToEvent'] = Array($hook['HookToEvent']); + } + foreach ($hook['HookToEvent'] as $hook_event) { + $this->Application->registerHook( + $prefix, + $hook['HookToSpecial'], + $hook_event, + $hook['Mode'], + $event->Prefix, + $hook['DoSpecial'], + $hook['DoEvent'], + $hook['Conditional'] + ); + } + } + } + } + + /** + * Should be hooked to OnAfterItemLoad, OnPreSaveCreated (why latter?) + * + * @param kEvent $event + */ + function OnPreparePriorities(&$event) + { + if (!$this->Application->IsAdmin()) return; + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + $is_new = $event->MasterEvent->Name == 'OnPreCreate' || $event->MasterEvent->Name == 'OnPreSaveCreated'; + $priority_helper->preparePriorities($event->MasterEvent, $is_new); + } + + /** + * Enter description here... + * + * @param kEvent $event + */ + function OnSavePriorityChanges(&$event) + { + $object =& $event->MasterEvent->getObject(); + + $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'); + + $this->Application->StoreVar('priority_changes'.$this->Application->GetVar('m_wid'), serialize($changes)); + } + + /** + * Enter description here... + * + * @param kEvent $event + */ + function OnItemDelete(&$event) + { + // just store the prefix in which the items were deleted + $del = $this->Application->RecallVar('priority_deleted'.$this->Application->GetVar('m_wid')); + $del = $del ? unserialize($del) : array(); + $del[] = $event->MasterEvent->Prefix; + $this->Application->StoreVar('priority_deleted'.$this->Application->GetVar('m_wid'), serialize(array_unique($del))); + } + + /** + * Called before script shut-down and recalculate all deleted prefixes, to avoid recalculation on each deleted item + * + * @param kEvent $event + */ + function OnBeforeShutDown(&$event) + { + $del = $this->Application->RecallVar('priority_deleted'.$this->Application->GetVar('m_wid')); + $del = $del ? unserialize($del) : array(); + + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + foreach ($del as $prefix) { + $dummy_event = new kEvent( array('prefix'=>$prefix, 'name'=>'Dummy' ) ); + + $ids = $priority_helper->recalculatePriorities($dummy_event); + if ($ids) { + $my_event = new kEvent('api:OnAddToQueue'); + $my_event->setEventParam('prefix', $prefix); + $my_event->setEventParam('ids', array_unique($ids)); + $this->Application->HandleEvent($my_event); + } + } + $this->Application->RemoveVar('priority_deleted'.$this->Application->GetVar('m_wid')); + } + + + /** + * Enter description here... + * + * @param kEvent $event + */ + function OnSaveItems(&$event) + { + $tmp = $this->Application->RecallVar('priority_changes'.$this->Application->GetVar('m_wid')); + $changes = $tmp ? unserialize($tmp) : array(); + + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + $ids = $priority_helper->updatePriorities($event->MasterEvent, $changes, $event->MasterEvent->getEventParam('ids')); + if ($ids) { + $my_event = new kEvent('api:OnAddToQueue'); + $my_event->setEventParam('prefix', $event->MasterEvent->Prefix); + $my_event->setEventParam('ids', array_unique($ids)); + $this->Application->HandleEvent($my_event); + } + } + + function OnItemCreate(&$event) + { + $obj =& $event->MasterEvent->getObject(); + if ($obj->GetDBField('Priority') == 0) { + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + $priority_helper->preparePriorities($event->MasterEvent, true); + } + } + + /** + * Processes OnMassMoveUp, OnMassMoveDown events + * + * @param kEvent $event + */ + function OnChangePriority(&$event) + { + $prefix = $this->Application->GetVar('priority_prefix'); + $dummy_event = new kEvent( array('prefix'=>$prefix, 'name'=>'Dummy' ) ); + + $ids = $this->StoreSelectedIDs($dummy_event); + + if ($ids) { + $id_field = $this->Application->getUnitOption($prefix, 'IDField'); + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); +// $parent_id = $this->Application->GetVar('m_cat_id'); + + $sql = 'SELECT Priority, '.$id_field.' + FROM '.$table_name.' + WHERE '.$id_field.' IN ('.implode(',', $ids).') ORDER BY Priority DESC'; + $priorities = $this->Conn->GetCol($sql, $id_field); + $min_priorirty = $this->Conn->GetOne('SELECT IFNULL(MIN(Priority),-1) FROM '.$table_name); + + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + foreach ($ids as $id) { + $new_priority = $priorities[$id] + ($event->Name == 'OnMassMoveUp' ? +1 : -1); + if ($new_priority > -1 || $new_priority < $min_priorirty) { + continue; + } + + $changes = Array ( + $id => Array ('old' => $priorities[$id], 'new' => $new_priority), + ); + + $sql = 'UPDATE '.$table_name.' + SET Priority = '.$new_priority.' + WHERE '.$id_field.' = '.$id; + $this->Conn->Query($sql); + + $ids = $priority_helper->updatePriorities($dummy_event, $changes, Array ($id => $id)); + if ($ids) { + $my_event = new kEvent('api:OnAddToQueue'); + $my_event->setEventParam('prefix', $prefix); + $my_event->setEventParam('ids', array_unique($ids)); + $this->Application->HandleEvent($my_event); + } + } + } + + $this->clearSelectedIDs($dummy_event); + } + + /** + * Completely recalculates priorities in current category + * + * @param kEvent $event + */ + function OnRecalculatePriorities(&$event) + { + $priority_helper =& $this->Application->recallObject('PriorityHelper'); + /* @var $priority_helper kPriorityHelper */ + + $prefix = $this->Application->GetVar('priority_prefix'); + $dummy_event = new kEvent( array('prefix'=>$prefix, 'name'=>'Dummy' ) ); + + $ids = $priority_helper->recalculatePriorities($dummy_event); + if ($ids) { + $my_event = new kEvent('api:OnAddToQueue'); + $my_event->setEventParam('prefix', $prefix); + $my_event->setEventParam('ids', array_unique($ids)); + $this->Application->HandleEvent($my_event); + } + } + +} \ No newline at end of file