array('asBilling' => 'y')); protected $_states4Select; protected $_hasSeoFields = false; protected function _initElements() { $this->_initPersonalInfoFields(); $this->_initCheckoutFields(); return $this; } /** * @return AdminObj */ protected function _getDataObj() { if (null === $this->_dataObj) { $this->_dataObj = new AdminObj(); } return $this->_dataObj; } /** * @return CartObj */ protected function _getCart() { return CartObj::getInstance(); } /** * "Personal" fields for "simple" mode * @return $this */ protected function _initPersonalInfoFields() { $this->addElement('text', 'firstName', array('label' => 'First Name', 'required' => true)); $this->addElement('text', 'lastName', array('label' => 'Last Name', 'required' => true)); $this->addElement('internationalTel', 'directPhone', array('label' => 'Phone Number', 'required' => true)); $this->addElement( 'email', 'email', array('label' => 'Email Address', 'required' => true, 'attribs' => array('autocomplete' => 'off')) ); $uniqueValidator = new Qs_Validate_Unique(new Qs_Db_Table('User'), 'email', $this->_getData('id')); $this->getElement('email')->addValidator($uniqueValidator); $this->_initPasswordFields(); $this->addDisplayGroup( array('firstName', 'lastName', 'directPhone', 'email', 'password', 'confirmPassword'), 'personalInfo', array('legend' => 'Personal Information') ); return $this; } protected function _initPasswordFields() { $this->addElement( 'password', 'password', array( 'label' => 'Password', 'required' => true, 'attribs' => array('autocomplete' => 'off') ) ); $this->getElement('password')->addValidator('Callback', true, array(array($this, 'validatePassword'))); $this->addElement( 'password', 'confirmPassword', array( 'label' => 'Confirm Password', 'required' => true, 'attribs' => array('autocomplete' => 'off') ) ); $this->getElement('confirmPassword')->addValidator('ConfirmPassword', true, array('password')); return $this; } protected function _initCheckoutFields($isRequired = true) { $this->_initAddressFields(Entity::ADDRESS_BILLING, $isRequired); $this->_initAddressFields(Entity::ADDRESS_SHIPPING, $isRequired); return $this; } /** * "Address" fields for "simple" mode * @param string $type * @param bool $isRequired * @return $this */ protected function _initAddressFields($type, $isRequired = true) { $form = new Qs_Form_SubForm(array('legend' => ucfirst($type) . ' Address')); if ($type != Entity::ADDRESS_BILLING) { $form->addElement( 'checkbox', 'asBilling', array('label' => 'My shipping address is the same as my billing address', 'decoration' => 'simple') ); } $form->addElement('text', 'firstName', array('label' => 'First Name')); $form->addElement('text', 'lastName', array('label' => 'Last Name')); $form->addElement('text', 'address', array('label' => 'Address')); $form->addElement('text', 'address2', array('label' => 'Address 2')); $form->addElement('text', 'city', array('label' => 'City', 'class' => 'city')); $form->addElement( 'select', 'state', array( 'label' => 'State', 'class' => 'state', 'multiOptions' => array('' => 'Select One') + $this->_getStates4Select() ) ); $form->addElement('zip', 'zip', array('label' => 'Zip', 'class' => 'zip')); if ($isRequired) { /** @var $element \Zend_Form_Element */ $element = null; if ($type == Entity::ADDRESS_BILLING) { foreach ($form as $element) { if ($element->getName() != 'address2') { $element->setRequired(); } } } else { $asBilling = $this->_getData($type . '[asBilling]'); foreach ($form as $element) { if (in_array($element->getName(), ['asBilling', 'address2'])) { continue; } if ($asBilling && 'n' == $asBilling) { $element->setRequired(); } else { $element->getDecorator('Label')->setOption('class', 'required'); } } } } $this->addSubForm($form, $type); return $this; } protected function _getScriptOptions() { $options['formId'] = $this->getId(); $options['addressTypes'] = [Entity::ADDRESS_SHIPPING]; if ($this->getSubForm(Entity::ADDRESS_BILLING)) { $options['addressFields'] = array_keys($this->getSubForm(Entity::ADDRESS_BILLING)->getElements()); } $options['inheritedAddress'] = Entity::ADDRESS_BILLING; return $options; } public function render(Zend_View_Interface $view = null) { /** @var $doc \App_Doc_Admin */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/app/user/form.js'); $doc->addInitObject('app.user.Form', [$this->_getScriptOptions()], 'appUserForm'); return parent::render($view); } public function validatePassword($value, $context = null) { if (!empty($value) && empty($context['confirmPassword'])) { $this->getElement('confirmPassword')->setRequired()->isValid(''); $this->getElement('confirmPassword')->setErrors(array('Password confirmation does not match')); } return true; } protected function _getStates4Select() { if (null === $this->_states4Select) { $this->_states4Select = $this->_getDataObj()->getDState4Select(); } return $this->_states4Select; } }