_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')); $this->getElement('code')->getDecorator('ViewHelper')->setAdditionalHtmlAfterElement($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( 'select', 'isRelated', array( 'label' => 'Related to a specific Product?', 'multiOptions' => array('n' => 'No', 'y' => 'Yes'), 'required' => true ) ); $this->addElement( 'autocomplete', 'productId', array( 'label' => 'What is the Product?', 'dataUrl' => BASE_URL_LANGUAGE . '/' . CURRENT_PAGE . '?action=productAutocomplete', 'validators' => array(array('Int', false)), ) ); if ($this->_getData('productId')) { $this->getElement('productId')->setTitle($dataObj->getProductTitleById($this->_getData('productId'))); } $this->getElement('productId')->setNotFoundMessage('"%value%" does not exist'); $this->addDisplayGroup(array('isRelated', 'productId'), 'productGroup'); $this->addElement( 'numeric', 'minOrderValue', array( 'label' => 'Minimum Order Value:', 'required' => true, ) ); /** @var $numeric Qs_Form_Decorator_Numeric */ $numeric = $this->getElement('minOrderValue')->getDecorator('Numeric'); $numeric->setAdditionalHtmlBeforeElement(''); $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['isRelated'] == 'y') { if (empty($data['productId'])) { $errors['productId'] = 'This is a required field'; } else { $productObj = new App_ECommerce_Product_Admin_Obj(); $productObj->setPrimaryKey($data['productId']); if (null == $productObj->getData()) { $errors['productId'] = 'Product is wrong'; } } } 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); } }