_dataObj) { $this->_dataObj = new App_ECommerce_Promo_Admin_Obj(); if (!empty($this->_albumType)) { $this->_dataObj->setPrimaryKey($this->_albumType); } } return $this->_dataObj; } protected function _initElements() { parent::_initElements(); $dataObj = $this->_getDataObj(); $this->addElement('text', 'code', array('label' => 'Client 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 Product Categories?', 'multiOptions' => array('n' => 'No', 'y' => 'Yes'), 'required' => true ) ); $this->addElement('multiCheckbox', 'productCategoryIds', array( 'label' => 'What is the Category?', 'multiOptions' => $dataObj->getProductCategory4Select( array('id', 'title'), array('typeId' => App_ECommerce_Product_Category_Abstract_Obj::PRODUCT_IN_STOCK_TYPE_ID), array('title') ) )); $this->getElement('productCategoryIds')->getDecorator('Label')->setOption('class', 'required'); $this->addDisplayGroup(array('isRelated', 'productCategoryIds'), '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' => 'Client Code has no expiration date', 'decoration' => 'simple') ); $this->addFormRule(array($this, 'validateForm')); return $this; } public function validateForm($data) { $errors = array(); if ('' != $data['value']) { if ($data['value'] <= 0) { $errors['value'] = 'Value should be greater than 0'; } else if ('percent' == $data['type'] && $data['value'] > 100) { $errors['value'] = 'Value should be less than 100%'; } } if ($data['isRelated'] == 'y') { if (empty($data['productCategoryIds'])) { $errors['productCategoryIds'] = 'This is a required field'; } } 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/app/ECommerce/promo/admin/form.js'); $options = array( 'formId' => $this->getId(), ); $doc->addInitObject('App_Promo_Admin_Form', array($options)); return parent::render($view); } }