_dataObj) { $this->_dataObj = new App_ECommerce_Promo_Admin_Obj(); if (!empty($this->_primaryKey)) { $this->_dataObj->setPrimaryKey($this->_primaryKey); } } return $this->_dataObj; } protected function _initElements() { parent::_initElements(); $dataObj = $this->_getDataObj(); $this->addElement('text', 'code', array('label' => 'Promo Code', 'required' => true)); $codeValidator = new Qs_Validate_Unique(new Qs_Db_Table('Promo'), 'code', $this->_getData('id')); $expirationWhere = '(`hasNoExpirationDate` = "y") OR (`hasNoExpirationDate` = "n" AND `endDate` < CURRENT_DATE())'; $codeValidator->setWhere($expirationWhere); $codeValidator->setMessage('Code must be unique', Qs_Validate_Unique::NOT_UNIQUE); $this->code->addValidator($codeValidator); $newButton = $this->getView()->formButton('newCardCode', 'Generate a Code', array('class' => 'btn btn-primary')); $this->getElement('code')->getDecorator('ViewHelper')->setHtmlAfterElement($newButton); $this->addElement( 'numeric', 'value', array('label' => 'Code Value', 'required' => true, 'precision' => 0) ); $this->addElement( 'select', 'type', array('required' => true, 'multiOptions' => $dataObj->getDPromoType4Select()) ); $this->addDisplayGroup(array('value', 'type'), 'valueGroup'); $this->addElement( 'numeric', 'minOrderValue', array( 'label' => 'Minimum Order Value:', 'required' => true, ) ); /** @var $numeric Qs_Form_Decorator_Numeric */ $numeric = $this->getElement('minOrderValue')->getDecorator('Numeric'); $numeric->setHtmlBeforeElement(''); $this->addElement('date', 'startDate', array('label' => 'Start Date', 'required' => true)); $this->addElement('date', 'endDate', array('label' => 'Exp. Date')); $this->getElement('endDate')->getDecorator('Label')->setOption('class', 'required'); $this->addElement( 'checkbox', 'hasNoExpirationDate', array('label' => 'Promo Code has no expiration date', 'decoration' => 'simple') ); $this->addFormRule(array($this, 'validateForm')); return $this; } public function validateForm($data) { $errors = array(); $value = str_replace(',', '', $data['value']); if ('' != $value) { if ($value <= 0) { $errors['value'] = 'Value should be greater than 0'; } else if ('percent' == $data['type'] && $value >= 100) { $errors['value'] = 'Value should be less than 100%'; } } if ($data['hasNoExpirationDate'] == 'n') { if (empty($data['endDate'])) { $errors['endDate'] = 'This is a required field'; } else if (!empty($data['startDate'])) { if (strtotime($data['startDate']) > strtotime($data['endDate'])) { $errors['endDate'] = 'Exp. Date should be greater than the Start Date'; } } } return (empty($errors)) ? true : $errors; } public function render(Zend_View_Interface $view = null) { /** @var $doc Qs_Doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/jquery.defaultHint.js'); $doc->addScript('js/app/ECommerce/promo/admin/form.js'); $options = array( 'formId' => $this->getId(), ); $doc->addInitObject('App_Promo_Admin_Form', array($options)); return parent::render($view); } }