Index: branches/5.2.x/units/gift_certificates/gift_certificates_eh.php =================================================================== diff -u -r14569 -r14594 --- branches/5.2.x/units/gift_certificates/gift_certificates_eh.php (.../gift_certificates_eh.php) (revision 14569) +++ branches/5.2.x/units/gift_certificates/gift_certificates_eh.php (.../gift_certificates_eh.php) (revision 14594) @@ -1,6 +1,6 @@ getObject(); /* @var $object kDBItem */ - + $exp_date = adodb_mktime(); $default_duration = $this->Application->ConfigValue('Comm_DefaultCouponDuration'); - - if ($default_duration){ + + if ( $default_duration ) { $exp_date += (int)$default_duration * 86400; } $object->SetDBField('Expiration_date', $exp_date); } - function OnBeforeItemUpdate(&$event) + /** + * Occurs before updating item + * + * @param kEvent $event + * @return void + * @access protected + */ + 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); + $object =& $event->getObject(); /* @var $object kDBItem */ + + $object->SetDBField('Debit', $object->GetDBField('Amount')); + } + + /** + * Occurs before item is changed + * + * @param kEvent $event + */ + function itemChanged(&$event) + { + $object =& $event->getObject(); + /* @var $object kDBItem */ + $amount = abs($object->GetDBField('Amount')); $debit = abs($object->GetDBField('Debit')); - if ($debit > $amount) { + if ( $debit > $amount ) { $debit = $amount; } + $object->SetDBField('Amount', $amount); $object->SetDBField('Debit', $debit); - if ($object->GetDBField('SendVia') == 1) { + if ( $object->GetDBField('SendVia') == 1 ) { // by postal mail - if ($this->Application->GetVar('email_certificate') == 0) { + if ( $this->Application->GetVar('email_certificate') == 0 ) { $object->setRequired('RecipientEmail', false); } $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); /* @var $cs_helper kCountryStatesHelper */ - if (!$cs_helper->CountryHasStates($object->GetDBField('RecipientCountry'))) { + if ( !$cs_helper->CountryHasStates($object->GetDBField('RecipientCountry')) ) { $object->setRequired('RecipientState', false); } + $cs_helper->CheckStateField($event, 'RecipientState', 'RecipientCountry'); - } else { + } + else { $non_required_fields = Array ( - 'RecipientState', 'RecipientCity', 'RecipientCountry', - 'RecipientZipcode', 'RecipientAddress1', 'RecipientFirstname', - 'RecipientLastname', + 'RecipientState', 'RecipientCity', 'RecipientCountry', 'RecipientZipcode', + 'RecipientAddress1', 'RecipientFirstname', 'RecipientLastname' ); - foreach ($non_required_fields as $non_required_field) { - $object->setRequired($non_required_field, false); - } + $object->setRequired($non_required_fields, false); } } - function OnBeforeItemCreate(&$event) - { - $this->OnBeforeItemUpdate($event); - - $object =& $event->getObject(); - /* @var $object kDBItem */ - $object->SetDBField('Debit', $object->GetDBField('Amount')); - } - /** - * Print current gift certiicate + * Print current gift certificate * * @param kEvent $event + * @return void + * @access protected */ - function OnSave(&$event) + protected function OnSave(&$event) { parent::OnSave($event); - if ($event->status == kEvent::erSUCCESS) { - $object =& $event->getObject(); - /* @var $object kDBItem */ + if ( $event->status != kEvent::erSUCCESS || !$this->Application->GetVar('print_certificate') ) { + return ; + } - if ($this->Application->GetVar('print_certificate') == 1) { - // get object id by unique field Code - $sql = 'SELECT '.$object->IDField.' - FROM '.$this->Application->GetLiveName($object->TableName).' - WHERE Code = '.$this->Conn->qstr($object->GetDBField('Code')); - $id = $this->Conn->GetOne($sql); + $object =& $event->getObject(); + /* @var $object kDBItem */ - $this->Application->StoreVar('print_certificate_id', $id); + // get object id by unique field Code + $sql = 'SELECT ' . $object->IDField . ' + FROM ' . $this->Application->GetLiveName($object->TableName) . ' + WHERE Code = ' . $this->Conn->qstr( $object->GetDBField('Code') ); + $id = $this->Conn->GetOne($sql); - } - } + $this->Application->StoreVar('print_certificate_id', $id); } /**