_initDFRelations(); return $this; } public function isSubmitted() { $method = $this->getMethod(); if (0 == strcasecmp($_SERVER['REQUEST_METHOD'], $method)) { $data = \Qs_Request::get($method); $itemIdMatched = true; if (($itemId = $this->getCmsItemId())) { $itemIdMatched = (isset($data['__idItem']) && $data['__idItem'] == $itemId); } return ($itemIdMatched && 0 === strpos($data['action'], 'insert')); } return false; } protected function _getRequestPrefix() { return "attendee[{$this->getIndex()}]"; } protected function _getSubData($field = null, $default = null) { $result = null; if ($this->isSubmitted()) { $request = \Qs_Array::get(\Qs_Request::get($this->getMethod()), $this->_getRequestPrefix()); if (false === $field) { $result = $request; } else { $result = \Qs_Array::get($request, $field); } } if (null === $result) { if (null === $field) { $result = $this->_defaults; } else { $result = \Qs_Array::get($this->_defaults, $field); } } return (null === $result) ? $default : $result; } protected function _initElements() { $mode = $this->getMode(); $this->addElement('hidden', 'mode', array('value' => $mode)); if (self::MODE_CURRENT_USER === $mode) { $this->_addCurrentUserElements(); } else { $this->_addDefaultElements(); } return $this; } protected function _addCurrentUserElements() { if ($this->getCurrentUserId()) { $this->addElement('hidden', 'type', array('value' => AttendeeForm::TYPE_MEMBER)); $this->addElement('hidden', 'id', array('value' => $this->getCurrentUserId())); } else { $this->addElement('hidden', 'type', array('value' => AttendeeForm::TYPE_NONMEMBER)); $attendType = $this->_getData('attendType'); $required = ($this->isSubmitted() && in_array($attendType, array(NewForm::ATTEND_MYSELF, NewForm::ATTEND_MYSELF_OTHER))); $this->_addNonMemberFields($required); } $this->_addWaiverElement(); return $this; } protected function _addDefaultElements() { $isSubmitted = $this->isSubmitted(); $allowMembers = (bool) $this->getAllowMembers(); $allowNonMembers = (bool) $this->getAllowNonMembers(); if ($allowMembers && $allowNonMembers) { $type = $this->_getSubData('type'); $this->addElement( 'radio', 'type', array( 'label' => 'Attendee Type', 'separator' => ' ', 'multiOptions' => array( AttendeeForm::TYPE_MEMBER => 'Member', AttendeeForm::TYPE_NONMEMBER => 'Nonmember' ), 'required' => true ) ); } else { $type = ($allowMembers && !$allowNonMembers) ? AttendeeForm::TYPE_MEMBER : AttendeeForm::TYPE_NONMEMBER; $this->addElement('hidden', 'type', array('value' => $type)); } $attendType = $this->_getData('attendType'); if ($allowMembers) { $required = ($isSubmitted && (NewForm::ATTEND_MYSELF_OTHER === $attendType || NewForm::ATTEND_OTHER === $attendType) && AttendeeForm::TYPE_MEMBER == $this->_getSubData('type') ); if ($this->getCurrentUserId()) { $this->addElement( 'autocomplete', 'id', array( 'label' => 'Member\'s Name or Email', 'dataUrl' => $this->getUserAutocompleteUrl(), 'required' => $required ) ); $this->getElement('id')->getDecorator('label')->setOption('class', 'required'); if (($userId = $this->_getSubData('id')) && false !== ($userName = App_User_Abstract_Obj::getAutocompleteItemTitle($userId))) { $this->getElement('id')->setTitle($userName); } } else { $this->addElement('text', 'memberEmail', [ 'label' => 'Member\'s Email', 'required' => $required ]); $this->getElement('memberEmail')->addValidator('emailAddress'); $userObj = new \App_User_Obj(); $this->getElement('memberEmail')->addValidator('inArray', false, [$userObj->getEmails()]); /** @var \Zend_Validate_InArray $inArray */ $inArray = $this->getElement('memberEmail')->getValidator('inArray'); $inArray->setMessage( 'The email entered doesn\'t match the existing ones',\Zend_Validate_InArray::NOT_IN_ARRAY ); } } if ($allowNonMembers) { $required = ($isSubmitted && (NewForm::ATTEND_MYSELF_OTHER === $attendType || NewForm::ATTEND_OTHER === $attendType) && AttendeeForm::TYPE_NONMEMBER === $type); $this->_addNonMemberFields($required); } $this->_addWaiverElement(); if ($allowMembers || $allowNonMembers) { $this->addElement( 'button', 'delete', array( 'label' => 'Delete', 'class' => 'btn btn-del', 'data-action' => 'delete', 'ignore' => true ) ); } return $this; } protected function _addWaiverElement() { if ($this->getShowWaiver()) { $this->addElement( 'checkbox', 'waiverAgreed', array( 'label' => 'I agree with this ' . Html::renderLink('javascript: void(0)', 'Waiver Paragraph', array('data-waiver-btn' => '')), 'decoration' => 'simple', ) ); $this->getElement('waiverAgreed')->getDecorator('Label')->setOption('escape', false); } return $this; } protected function _addNonMemberFields($required) { $this->addElement('text', 'firstName', array('label' => 'First Name', 'required' => $required)); $this->addElement('text', 'lastName', array('label' => 'Last Name', 'required' => $required)); $this->addElement('text', 'email', array('label' => 'Email', 'required' => $required)); $this->getElement('email')->addValidator('emailAddress'); foreach (array('firstName', 'lastName', 'email') as $name) { $this->getElement($name)->getDecorator('label')->setOption('class', 'required'); } return $this; } public function initRender() { $attr = (self::MODE_CURRENT_USER === $this->getMode()) ? 'data-current-attendee' : 'data-attendee'; $this->getDecorator('Fieldset')->setOption($attr, $this->getIndex()); return parent::initRender(); } public function loadDefaultDecorators() { if (false == $this->loadDefaultDecoratorsIsDisabled() && null == $this->getDecorators()) { $this->addDecorator('FormElements') ->addDecorator('HtmlTag', array('tag' => 'dl')) ->addDecorator('Fieldset'); } return $this; } public function setMode($mode) { $this->_mode = $mode; return $this; } public function getMode() { return $this->_mode; } public function setCurrentUserId($currentUserId) { $this->_currentUserId = $currentUserId; return $this; } public function getCurrentUserId() { return $this->_currentUserId; } public function setAllowMembers($allowMembers) { $this->_allowMembers = $allowMembers; return $this; } public function getAllowMembers() { return $this->_allowMembers; } public function setAllowNonMembers($allowNonMembers) { $this->_allowNonMembers = $allowNonMembers; return $this; } public function getAllowNonMembers() { return $this->_allowNonMembers; } public function setUserAutocompleteUrl($userAutocompleteUrl) { $this->_userAutocompleteUrl = $userAutocompleteUrl; return $this; } public function getUserAutocompleteUrl() { if (null === $this->_userAutocompleteUrl) { throw new Qs_Exception_EmptyPropertyException($this, '_userAutocompleteUrl'); } return $this->_userAutocompleteUrl; } public function getShowWaiver() { return $this->_showWaiver; } public function setShowWaiver($showWaiver) { $this->_showWaiver = $showWaiver; } public function setIndex($index) { $this->_index = $index; return $this; } public function getIndex() { if (null === $this->_index) { throw new Qs_Exception_EmptyPropertyException($this, '_index'); } return $this->_index; } protected function _initDFRelations() { if (self::MODE_CHOOSE_TYPE !== $this->getMode() || $this->getElement('type') instanceof Zend_Form_Element_Hidden) { return $this; } $idx = $this->getIndex(); $namePrefix = 'attendee[' . $idx . ']'; $idPrefix = '#attendee-' . $idx . '-'; $this->_dfRelations = array( array( 'node' => '[name="' . $namePrefix . '[type]"]:checked', 'group' => '[name="' . $namePrefix . '[type]"]', 'event' => 'change', 'rules' => array( array( 'value' => AttendeeForm::TYPE_MEMBER, 'nodes' => array( $idPrefix . 'id-label', $idPrefix . 'id-element', $idPrefix . 'memberEmail-label', $idPrefix . 'memberEmail-element' ), ), array( 'value' => AttendeeForm::TYPE_NONMEMBER, 'nodes' => array( $idPrefix . 'firstName-label', $idPrefix . 'firstName-element', $idPrefix . 'lastName-label', $idPrefix . 'lastName-element', $idPrefix . 'email-label', $idPrefix . 'email-element', ), ), ) ), ); return $this; } protected function _addResources() { if ($this->_dfRelations) { $dynamicFormOptions = array( 'relations' => $this->dfConvertRelations() ); /** @var $doc \Qs_Doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/jquery.dynamicForm.js'); $doc->addInitFunction('$("#attendee-fieldset-' . $this->getIndex() . '").dynamicForm', array($dynamicFormOptions)); } return parent::_addResources(); } /** * @param string $name element name * @return string element label id (e.g. foo-label) */ protected function _dfGetElementLabelId($name) { return null; } /** * @param string $name element name * @return string element container id (e.g. foo-element) */ protected function _dfGetElementContainerId($name) { return null; } }