array('asBilling' => 'y')); /** * @return App_User_Admin_Obj */ protected function _getDataObj() { if (null === $this->_dataObj) { $this->_dataObj = new App_User_Admin_Obj(); } return $this->_dataObj; } /** * @return App_ECommerce_Cart_Obj */ protected function _getCart() { if (null === $this->_cart) { $this->_cart = new App_ECommerce_Cart_Obj(); $this->_cart->setPrimaryKey($this->_cart->getCartId()); } return $this->_cart; } protected function _initElements() { $this->_initPersonalInfoFields(); foreach ($this->_getDataObj()->getAddressTypes() as $type) { $this->_initAddressFields($type); } 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('phone', 'phone', array('label' => 'Phone Number', 'required' => true)); $uniqueValidator = new Qs_Validate_Unique(new Qs_Db_Table('User'), 'email', $this->_getData('id')); $uniqueValidator->setMessage('Email must be unique', Qs_Validate_Unique::NOT_UNIQUE); $this->addElement( 'email', 'email', array('label' => 'Email Address', 'required' => true, 'attribs' => array('autocomplete' => 'off')) ); $this->getElement('email')->addValidator($uniqueValidator); $this->addElement( 'password', 'password', array('label' => 'Password', 'attribs' => array('autocomplete' => 'off')) ); $this->getElement('password')->addValidator('Callback', true, array(array($this, 'validatePassword'))); $this->addElement( 'password', 'confirmPassword', array('label' => 'Confirm Password', 'attribs' => array('autocomplete' => 'off')) ); $this->getElement('confirmPassword')->addValidator('ConfirmPassword', true, array('password')); $elements = array('firstName', 'lastName', 'phone', 'email', 'password', 'confirmPassword'); if ($this->_isAdminForm) { $this->addElement('text', 'clientType', array('label' => 'Client Type')); array_push($elements, 'clientType'); } $this->addDisplayGroup( $elements, 'personalInfo', array('legend' => 'Personal Information') ); return $this; } protected function _initAddressFields($type) { $form = new Qs_Form_SubForm(array('legend' => ucfirst($type) . ' Address')); if ($type != App_User_Abstract_Obj::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('phone', 'phone', array('label' => 'Phone Number')); $form->addElement('email', 'email', array('label' => 'Email Address')); $form->addElement('text', 'companySiteName', array('label' => 'Company / Site Name')); $form->addElement('text', 'address', array('label' => 'Address')); $form->addElement('text', 'city', array('label' => 'City', 'class' => 'city')); $form->addElement( 'select', 'state', array( 'label' => 'State', 'class' => 'state', 'multiOptions' => array('' => 'Select One') + $this->_getStateList($type) ) ); $form->addElement('tel', 'zip', array('label' => 'Zip Code', 'class' => 'zip')); /** @var $element Zend_Form_Element */ $element = null; if ($type == App_User_Admin_Obj::ADDRESS_BILLING) { foreach ($form as $element) { $element->setRequired(); } } else { $asBilling = $this->_getData($type . '[asBilling]'); foreach ($form as $element) { if ('asBilling' == $element->getName() || 'email' == $element->getName()) { continue; } if ($asBilling && 'n' == $asBilling) { $element->setRequired(); } else { $element->getDecorator('Label')->setOption('class', 'required'); } } } $this->addSubForm($form, $type); return $this; } protected function _getStateList($type) { if ($type != App_User_Admin_Obj::ADDRESS_BILLING && $excludedShippingStates = $this->_getExcludedShippingStates() ) { $states = $this->_getDataObj()->getDState4Select( ['id', 'title'], array('id NOT IN (?)' => $excludedShippingStates) ); } else { $states = $this->_getDataObj()->getDState4Select(); } return $states; } protected function _getExcludedShippingStates() { return Qs_Application::getConfig('ECommerce_Checkout')->get('excludedShippingStates')->toArray(); } 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'); $options = array(); $options['formId'] = $this->getId(); $options['addressTypes'] = $this->_getDataObj()->getAddressTypes(); if ($this->getSubForm(App_User_Abstract_Obj::ADDRESS_BILLING)) { $options['addressFields'] = array_keys($this->getSubForm(App_User_Abstract_Obj::ADDRESS_BILLING) ->getElements()); } $options['inheritedAddress'] = array_shift($options['addressTypes']); $options['excludedShippingStates'] = $this->_getExcludedShippingStates(); $doc->addInitObject('App_User_Form', array($options), '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; } }