_dfRelations = $this->getConfigArray('dynamicFormRelations'); if (empty($this->_defaults)) { $this->_defaults = $this->getConfigArray('defaults'); } parent::loadDefaultDecorators(); return parent::init(); } protected function _initElements() { $this->addElement('hidden', 'sourceId', ['value' => Qs_Request::getRequestValue('sourceId')]); $this->addElement('text', 'title', ['label' => 'Event Name', 'required' => true]); $this->addSeoGroup('Event', 'title'); $this->addElement('extendedImage', 'image', ['label' => 'Image', 'required' => true, 'resize' => $this->getImageSize()]); $this->addElement( 'radio', 'type', [ 'label' => 'Type', 'required' => true, 'separator' => '   ', 'multiOptions' => (new Qs_Db_Table('EventType'))->get4Select(), ] ); if ($this instanceof EditForm && ($type = $this->_getData('type'))) { $multiOptions = $this->getElement('type')->getMultiOptions(); unset($multiOptions[$type]); $this->getElement('type')->setAttrib('disable', array_keys($multiOptions)); } $this->addElement( 'select', 'committeeId', [ 'label' => 'Committee', 'multiOptions' => $this->getCommitteeOptions(), 'required' => !$this->isSubmitted() || $this->isVisible('committeeId'), ] ); $this->addEventTime(); $this->addDateTimeRange( 'registration', [ 'label' => 'Registration Time', 'required' => !$this->isSubmitted() || $this->isVisible('registrationStartDate'), ] ); $this->addElement('text', 'location', ['label' => 'Location', 'required' => true]); $this->addElement( 'text', 'mapUrl', [ 'label' => 'Google Maps Location URL', 'description' => $this->getConfig('urlDescription'), 'validators' => ['Url'], ] ); $this->addElement('htmlEditor', 'description', ['label' => 'Description']); $this->addPricing(); $this->addElement( 'numeric', 'registrationLimit', [ 'label' => 'Registration Limit', 'precision' => 0, 'negative' => false, ] ); $this->addElement('checkbox', 'enabled', ['label' => 'Show on user end', 'decoration' => 'simple']); return $this; } protected function addPricing() { $this->addPriceElement('memberPrice', 'Member Price'); $this->addPriceElement('nonmemberPrice', 'Nonmember Price'); $this->addDisplayGroup(['memberPrice', 'nonmemberPrice'], 'pricing', ['legend' => 'Pricing']); return $this; } protected function addEventTime() { $this->addElement( 'radio', 'durationType', [ 'label' => 'Event Duration', 'required' => true, 'separator' => '   ', 'multiOptions' => $this->getConfigArray('durationTypeOptions'), ] ); $this->addDateRange(); $this->addMultipleDaysTime(); $this->getDecorator('FormElements')->setOption('wrappers', [ [ 'decorators' => [ ['HTMLTag', ['tag' => 'dl']], ['Fieldset', ['legend' => 'Event Time', 'name' => 'eventTime']], ['DtDdWrapper', ['name' => 'eventTime']], ], 'elements' => ['durationType', 'startDate', 'endDate', 'timeRanges'], ], ]); return $this; } protected function addMultipleDaysTime() { if (!($defaults = Qs_Array::get($this->_defaults, 'timeRanges'))) { $defaults = [[]]; } $defaults = array_values($defaults); if (false === ($days = $this->_getDaysCount())) { $days = 1; } $defaults = Qs_Array::mergeAssoc(array_fill_keys(range(0, $days - 1), []), array_slice($defaults, 0, $days)); $startDate = Qs_Array::get($this->_defaults, 'startDate'); $this->addSubForm(new MultipleDaysTime\Items(compact('defaults', 'startDate')), 'timeRanges'); return $this; } protected function _getDaysCount() { $data = &$this->_defaults; try { $start = new DateTime($data['startDate']); $end = new DateTime(empty($data['endDate']) ? $data['startDate'] : $data['endDate']); return (int) $end->diff($start)->format('%a') + 1; } catch (\Exception $e) { return false; } } protected function addPriceElement($name, $label) { $this->addElement( 'numeric', $name, [ 'label' => $label, 'prepend' => '$', 'negative' => false, 'required' => !$this->isSubmitted() || $this->isVisible($name), ] ); return $this; } protected function addDateRange() { $this->addElement('date', 'startDate', ['label' => 'Start Date', 'required' => true]); $this->addElement( 'date', 'endDate', [ 'label' => 'End Date', 'required' => !$this->isSubmitted() || $this->isVisible('endDate'), ] ); $startDate = $this->getElement('startDate'); $endDate = $this->getElement('endDate'); $endDate->addValidator( 'Compare', true, [ 'callback' => [$startDate, 'getValue'], 'operator' => '>', 'messages' => $endDate->getLabel() . ' should be greater than ' . $startDate->getLabel(), ] ); return $this; } public function isValid($data) { $isValid = $this->isValidTrait($data); $isValidRegistrationTime = $this->isValidRegistrationEndTime(); return $isValid && $isValidRegistrationTime; } public function isValidPartial(array $data) { $isValid = $this->isValidPartialTrait($data); $isValidRegistrationTime = $this->isValidRegistrationEndTime(); return $isValid && $isValidRegistrationTime; } protected function isValidRegistrationEndTime() { $startDate = null; $registrationEndDate = null; $registrationEndTime = null; if ($this->getElement('type')->getValue() != EventEntity::TYPE_REGISTRATION) { // skip validation return true; } foreach (['startDate', 'registrationEndDate', 'registrationEndTime'] as $element) { $$element = $this->getElement($element)->getValue(); if (empty($$element)) { // skip validation return true; } } if (false === ($registrationEnd = strtotime($registrationEndDate . ' ' . $registrationEndTime))) { // skip validation return true; } $endDate = $this->getElement('endDate')->getValue(); if (empty($endDate)) { $endDate = $startDate; } $timeRanges = $this->getSubForm('timeRanges')->getValues(); if (key($timeRanges) == 'timeRanges') { $timeRanges = current($timeRanges); } if (empty($timeRanges)) { // skip validation return true; } $firstDayTimeRange = current($timeRanges); if (empty($firstDayTimeRange['startTime'])) { // skip validation return true; } if (false === ($end = strtotime($endDate . ' ' . $firstDayTimeRange['startTime']))) { // skip validation return true; } if ($registrationEnd > $end) { if ($registrationEndDate > $endDate) { $this->getElement('registrationEndDate')->addError( 'Registration End Date shouldn\'t be higher than Event Start Date' ); return false; } elseif ($registrationEndDate = $endDate) { $this->getElement('registrationEndTime')->addError( 'Registration End Time shouldn\'t be higher than Event Start Time' ); return false; } } return true; } protected function getImageSize() { $size = $this->getConfigArray('image'); return $size['width'] . 'x' . $size['height'] . $size['resizeMethodAlias']; } protected function getCommitteeOptions() { return ['' => 'Select One'] + (array) (new Qs_Db_Table('Committee'))->get4Select(['id', 'name'], ['enabled' => 'y']); } protected function _addResources() { $subForm = $this->getSubForm('timeRanges'); $options = [ 'id' => $this->getId(), 'partName' => $subForm->getName(), 'idMap' => [ 'time' => '#' . $subForm->getId() . '-element', ], 'startDateLabels' => [ EventEntity::DURATION_TYPE_SINGLE_DAY => 'Date', EventEntity::DURATION_TYPE_MULTIPLE_DAYS => 'Start Date', ], ]; $this->getDoc()->addScript('js/app/event/admin/Form.js'); $this->getDoc()->addInitObject('app.event.admin.Form', [$options], 'appEventAdminForm'); return self::_addResourcesTrait(); } public function getValues($suppressArrayNotation = false) { $values = parent::getValues($suppressArrayNotation); $this->dfClearUnusedValues($values); return $values; } }