Index: branches/5.2.x/units/affiliate_plans/affiliate_plans_event_handler.php =================================================================== diff -u -N -r14258 -r14625 --- branches/5.2.x/units/affiliate_plans/affiliate_plans_event_handler.php (.../affiliate_plans_event_handler.php) (revision 14258) +++ branches/5.2.x/units/affiliate_plans/affiliate_plans_event_handler.php (.../affiliate_plans_event_handler.php) (revision 14625) @@ -1,6 +1,6 @@ Special == 'active') { @@ -37,56 +45,80 @@ } /** - * Enter description here... + * Occurs before updating item * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeItemUpdate(&$event) + protected function OnBeforeItemUpdate(&$event) { + parent::OnBeforeItemUpdate($event); + + $this->itemChanged($event); + } + + /** + * Occurs before creating item + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnBeforeItemCreate(&$event) + { + parent::OnBeforeItemCreate($event); + + $this->itemChanged($event); + } + + /** + * Occurs before item is changed + * + * @param kEvent $event + */ + function itemChanged(&$event) + { $object =& $event->getObject(); + /* @var $object kDBItem */ $live_table = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $plans_count = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$live_table); - if(!$plans_count) $object->SetDBField('IsPrimary', 1); + $plans_count = $this->Conn->GetOne('SELECT COUNT(*) FROM ' . $live_table); + if ( !$plans_count ) { + $object->SetDBField('IsPrimary', 1); + } - if( $object->GetDBField('IsPrimary') && $object->Validate() ) - { - $sql = 'UPDATE '.$this->Application->getUnitOption($event->Prefix, 'TableName').' + if ( $object->GetDBField('IsPrimary') && $object->Validate() ) { + $sql = 'UPDATE ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' SET IsPrimary = 0'; $this->Conn->Query($sql); - $status_field = array_shift( $this->Application->getUnitOption($event->Prefix,'StatusField') ); + $status_field = array_shift($this->Application->getUnitOption($event->Prefix, 'StatusField')); $object->SetDBField($status_field, 1); } } - function OnBeforeItemCreate(&$event) + /** + * Don't allow to delete primary affiliate plan + * + * @param kEvent $event + * @param string $type + * @return void + * @access protected + */ + protected function customProcessing(&$event, $type) { - $this->OnBeforeItemUpdate($event); - } + if ( $event->Name == 'OnMassDelete' && $type == 'before' ) { + $ids = $event->getEventParam('ids'); - function OnMassDelete(&$event) - { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { - $event->status = kEvent::erFAIL; - return; - } + $sql = 'SELECT ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . ' + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE IsPrimary = 1'; + $primary_id = $this->Conn->GetOne($sql); - $ids = $this->StoreSelectedIDs($event); - $event->setEventParam('ids', $ids ); - $ids = $event->getEventParam('ids'); + $ids = array_diff($ids, Array ($primary_id)); - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - - $sql = 'SELECT AffiliatePlanId FROM '.$this->Application->getUnitOption('ap', 'TableName').' - WHERE IsPrimary = 1'; - $primary_id = $this->Conn->GetOne($sql); - $ids = array_diff($ids, Array($primary_id)); - - if($ids) - { - $temp->DeleteItems($event->Prefix, $event->Special, $ids); + $event->setEventParam('ids', $ids); } - $this->clearSelectedIDs($event); } } \ No newline at end of file