Index: branches/5.2.x/units/coupons/coupons_event_handler.php =================================================================== diff -u -r14258 -r14569 --- branches/5.2.x/units/coupons/coupons_event_handler.php (.../coupons_event_handler.php) (revision 14258) +++ branches/5.2.x/units/coupons/coupons_event_handler.php (.../coupons_event_handler.php) (revision 14569) @@ -1,6 +1,6 @@ Array('self' => true), + 'OnApplyClone' => Array('self' => 'add'), + 'OnPrepareClone' => Array('self' => 'view'), ); $this->permMapping = array_merge($this->permMapping, $permissions); } - - function OnApplyClone(&$event) + /** + * Prepares coupon cloning + * + * @param kEvent $event + */ + function OnPrepareClone(&$event) { - $item = &$event->getObject(); - $clone_count = $this->Application->GetVar('clone_items_count'); - $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); - if ($clone_count && $clone_count>0){ - $this->Application->StoreVar('CoupLastCloneCount', $clone_count); - for ($i=0; $i<$clone_count; $i++) { - $this->SetNewCode($item); + $this->StoreSelectedIDs($event); - $item->SetField('Expiration_date', $this->Application->GetVar('clone_exp_date')); - $item->SetField('Expiration_time', $this->Application->GetVar('clone_exp_time')); - $item->NameCopy(); - $validated = $item->Create(); + $event->CallSubEvent('OnNew'); + $object =& $event->getObject(); + /* @var $object kDBItem */ - } + $this->setCloningRequired($object); + $clone_count = $this->Application->RecallVar('CoupLastCloneCount'); + + if ( is_numeric($clone_count) && $clone_count > 0 ) { + $object->SetDBField('CouponCount', $clone_count); } - if ($validated){ - $this->finalizePopup($event); - }else{ - $event->redirect = false; + $expire_days = $this->Application->ConfigValue('Comm_DefaultCouponDuration'); + $default_expiration = strtotime('+' . $expire_days . ' days'); + + $object->SetDBField('DefaultExpiration_date', $default_expiration); + $object->SetDBField('DefaultExpiration_time', $default_expiration); + } + + + function OnBeforeClone(&$event) + { + parent::OnBeforeClone($event); + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $this->SetNewCode($object); + + $object->SetDBField('LastUsedBy', NULL); + $object->SetDBField('LastUsedOn', NULL); + $object->SetDBField('NumberOfUses', NULL); + + $expiration = $this->Application->GetVar('clone_coupon_expiration'); + $object->SetDBField('Expiration_date', $expiration); + $object->SetDBField('Expiration_time', $expiration); + } + + function OnApplyClone(&$event) + { + if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + $event->status = erFAIL; + return; } + + $object =& $event->getObject( Array ('skip_autoload' => true) ); + /* @var $object kDBItem */ + + $this->setCloningRequired($object); + + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + list($id, $field_values) = each($items_info); + $object->SetFieldsFromHash($field_values); + $object->setID($id); + + if ( !$object->Validate() ) { + $event->status = erFAIL; + return ; + } + + $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + /* @var $temp kTempTablesHandler */ + + $original_coupon_ids = $this->getSelectedIDs($event, true); + $clone_count = $object->GetDBField('CouponCount'); + $this->Application->StoreVar('CoupLastCloneCount', $clone_count); + $this->Application->SetVar('clone_coupon_expiration', $object->GetDBField('DefaultExpiration')); + + for ($i = 0; $i < $clone_count; $i++) { + $temp->CloneItems($event->Prefix, $event->Special, $original_coupon_ids); + } + + $this->finalizePopup($event); } + function setCloningRequired(&$object) + { + $this->RemoveRequiredFields($object); + $object->setRequired('CouponCount'); + $object->setRequired('DefaultExpiration'); + } + function SetNewCode(&$item) { do{