_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', ['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', ['class' => 'btn']); $this->getElement('code')->getDecorator('ViewHelper')->setHtmlAfterElement($newButton); $this->addElement( 'numeric', 'value', ['label' => 'Code Value', 'required' => true, 'precision' => 0] ); $this->addElement( 'select', 'type', ['required' => true, 'multiOptions' => $dataObj->getDPromoType4Select()] ); $this->addDisplayGroup(['value', 'type'], 'valueGroup'); $this->addElement( 'select', 'isRelated', [ 'label' => 'Related to a specific Product?', 'multiOptions' => ['n' => 'No', 'y' => 'Yes'], 'required' => true, ] ); $this->addElement( 'autocomplete', 'productId', [ 'label' => 'What is the Product?', 'dataUrl' => BASE_URL_LANGUAGE . '/' . CURRENT_PAGE . '?action=productAutocomplete', 'validators' => [['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(['isRelated', 'productId'], 'productGroup'); $this->addElement( 'numeric', 'minOrderValue', [ 'label' => 'Minimum Order Value:', 'required' => true, ] ); /** @var $numeric Qs_Form_Decorator_Numeric */ $numeric = $this->getElement('minOrderValue')->getDecorator('Numeric'); $numeric->setHtmlBeforeElement(''); $this->addElement('date', 'startDate', ['label' => 'Start Date', 'required' => true]); $this->addElement('date', 'endDate', ['label' => 'Exp. Date']); $this->getElement('endDate')->getDecorator('Label')->setOption('class', 'required'); $this->addElement( 'checkbox', 'hasNoExpirationDate', ['label' => 'Promo Code has no expiration date', 'decoration' => 'simple'] ); $this->addFormRule([$this, 'validateForm']); return $this; } public function validateForm($data) { $errors = []; $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 = [ 'formId' => $this->getId(), ]; $doc->addInitObject('App_Promo_Admin_Form', [$options]); return parent::render($view); } }