function usearch_set_presentation(OW_Event $event) { $params = $event->getParams(); if ($params['type'] != 'search' || !in_array($params['fieldName'], array('sex', 'birthdate'))) { return; } $lang = OW::getLanguage(); $sessionData = OW::getSession()->get(USEARCH_CLASS_QuickSearchForm::FORM_SESSEION_VAR); switch ($params['fieldName']) { case 'sex': $field = new Selectbox('sex'); $field->setLabel($lang->text('usearch', 'search_label_sex')); $field->setHasInvitation(false); if (!empty($sessionData['sex'])) { $field->setValue($sessionData['sex']); } break; case 'birthdate': $field = new USEARCH_CLASS_AgeRangeField('birthdate'); $field->setLabel($lang->text('usearch', 'age')); if (!empty($sessionData['birthdate']['from']) && !empty($sessionData['birthdate']['to'])) { $field->setValue($sessionData['birthdate']); } $configs = !empty($params['configs']) ? BOL_QuestionService::getInstance()->getQuestionConfig($params['configs'], 'year_range') : null; $max = !empty($configs['from']) ? date("Y") - (int) $configs['from'] : null; $min = !empty($configs['to']) ? date("Y") - (int) $configs['to'] : null; $field->setMaxAge($max); $field->setMinAge($min); $validator = new USEARCH_CLASS_AgeRangeValidator($min, $max); $errorMsg = $lang->text('usearch', 'age_range_incorrect_values', array('min' => $min, 'max' => $max)); $validator->setErrorMessage($errorMsg); $field->addValidator($validator); break; } if (!empty($field)) { $event->setData($field); } }
protected function getPresentationClass($presentation, $questionName, $configs = null) { $event = new OW_Event('base.questions_field_get_label', array('presentation' => $presentation, 'fieldName' => $questionName, 'configs' => $configs, 'type' => 'edit')); OW::getEventManager()->trigger($event); $label = $event->getData(); $class = null; $event = new OW_Event('base.questions_field_init', array('type' => 'search', 'presentation' => $presentation, 'fieldName' => $questionName, 'configs' => $configs)); OW::getEventManager()->trigger($event); $class = $event->getData(); if (empty($class)) { switch ($presentation) { case BOL_QuestionService::QUESTION_PRESENTATION_TEXT: case BOL_QuestionService::QUESTION_PRESENTATION_TEXTAREA: $class = new TextField($questionName); break; case BOL_QuestionService::QUESTION_PRESENTATION_CHECKBOX: $class = new CheckboxField($questionName); break; case BOL_QuestionService::QUESTION_PRESENTATION_RADIO: case BOL_QuestionService::QUESTION_PRESENTATION_SELECT: case BOL_QuestionService::QUESTION_PRESENTATION_MULTICHECKBOX: $class = new Selectbox($questionName); break; case BOL_QuestionService::QUESTION_PRESENTATION_BIRTHDATE: case BOL_QuestionService::QUESTION_PRESENTATION_AGE: $class = new USEARCH_CLASS_AgeRangeField($questionName); if (!empty($configs) && mb_strlen(trim($configs)) > 0) { $configsList = json_decode($configs, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinYear($value['from']); $class->setMaxYear($value['to']); } } } $class->addValidator(new USEARCH_CLASS_AgeRangeValidator($class->getMinAge(), $class->getMaxAge())); break; case self::QUESTION_PRESENTATION_RANGE: $class = new Range($fieldName); if (empty($this->birthdayConfig)) { $birthday = $this->findQuestionByName("birthdate"); if (!empty($birthday)) { $this->birthdayConfig = $birthday->custom; } } $rangeValidator = new RangeValidator(); if (!empty($this->birthdayConfig) && mb_strlen(trim($this->birthdayConfig)) > 0) { $configsList = json_decode($this->birthdayConfig, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinValue(date("Y") - $value['to']); $class->setMaxValue(date("Y") - $value['from']); $rangeValidator->setMinValue(date("Y") - $value['to']); $rangeValidator->setMaxValue(date("Y") - $value['from']); } } } $class->addValidator($rangeValidator); break; case self::QUESTION_PRESENTATION_DATE: $class = new DateRange($fieldName); if (!empty($configs) && mb_strlen(trim($configs)) > 0) { $configsList = json_decode($configs, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinYear($value['from']); $class->setMaxYear($value['to']); } } } $class->addValidator(new DateValidator($class->getMinYear(), $class->getMaxYear())); break; case BOL_QuestionService::QUESTION_PRESENTATION_URL: $class = new TextField($questionName); $class->addValidator(new UrlValidator()); break; } if (!empty($label)) { $class->setLabel($label); } if (empty($class)) { $class = BOL_QuestionService::getInstance()->getSearchPresentationClass($presentation, $questionName, $configs); } } return $class; }