Example #1
0
 public function add()
 {
     // add common content menu
     $this->addContentMenu();
     // get available account types from DB
     $accountTypes = $this->questionService->findAllAccountTypes();
     $serviceLang = BOL_LanguageService::getInstance();
     $language = OW::getLanguage();
     $currentLanguageId = OW::getLanguage()->getCurrentId();
     $accounts = array(BOL_QuestionService::ALL_ACCOUNT_TYPES => $language->text('base', 'questions_account_type_all'));
     /* @var $value BOL_QuestionAccount */
     foreach ($accountTypes as $value) {
         $accounts[$value->name] = $language->text('base', 'questions_account_type_' . $value->name);
     }
     $sections = $this->questionService->findAllSections();
     // need to hide sections select box
     if (empty($sections)) {
         $this->assign('no_sections', true);
     }
     $sectionsArray = array();
     /* @var $section BOL_QuestionSection */
     foreach ($sections as $section) {
         $sectionsArray[$section->name] = $language->text('base', 'questions_section_' . $section->name . '_label');
     }
     $presentations2types = $this->questionService->getPresentations();
     unset($presentations2types[BOL_QuestionService::QUESTION_PRESENTATION_PASSWORD]);
     $presentationList = array_keys($presentations2types);
     $presentations = array();
     $presentationsLabel = array();
     foreach ($presentationList as $item) {
         $presentations[$item] = $item;
         $presentationsLabel[$item] = $language->text('base', 'questions_question_presentation_' . $item . '_label');
     }
     $presentation = $presentationList[0];
     if (OW::getSession()->isKeySet(self::ADD_QUESTION_SESSION_VAR)) {
         $session = OW::getSession()->get(self::ADD_QUESTION_SESSION_VAR);
         if (isset($presentations[$session['qst_answer_type']])) {
             $presentation = $presentations[$session['qst_answer_type']];
         }
     }
     if (isset($_POST['qst_answer_type']) && isset($presentations[$_POST['qst_answer_type']])) {
         $presentation = $presentations[$_POST['qst_answer_type']];
     }
     $displayPossibleValues = in_array($presentations2types[$presentation], array('select', 'multiselect')) ? true : false;
     // form creating
     $addForm = new Form('qst_add_form');
     $addForm->setId('qst_add_form');
     $qstName = new TextField('qst_name');
     $qstName->setLabel($language->text('admin', 'questions_question_name_label'));
     //$qstName->addValidator(new StringValidator(0, 24000));
     $qstName->setRequired();
     $addForm->addElement($qstName);
     $qstName = new TextField('qst_description');
     $qstName->setLabel($language->text('admin', 'questions_question_description_label'));
     //$qstName->addValidator(new StringValidator(0, 24000));
     $addForm->addElement($qstName);
     if (count($accountTypes) > 1) {
         $qstAccountType = new Selectbox('qst_account_type');
         $qstAccountType->setLabel($language->text('admin', 'questions_for_account_type_label'));
         $qstAccountType->setRequired();
         $qstAccountType->setDescription($language->text('admin', 'questions_for_account_type_description'));
         $qstAccountType->setOptions($accounts);
         $qstAccountType->setValue(0);
         $qstAccountType->setHasInvitation(false);
         $addForm->addElement($qstAccountType);
     }
     if (!empty($sectionsArray)) {
         $qstSection = new Selectbox('qst_section');
         $qstSection->setLabel($language->text('admin', 'questions_question_section_label'));
         $qstSection->setOptions($sectionsArray);
         $qstSection->setHasInvitation(false);
         $addForm->addElement($qstSection);
     }
     $qstAnswerType = new Selectbox('qst_answer_type');
     $qstAnswerType->setLabel($language->text('admin', 'questions_answer_type_label'));
     $qstAnswerType->addAttribute('class', $qstAnswerType->getName());
     $qstAnswerType->setOptions($presentationsLabel);
     $qstAnswerType->setRequired();
     $qstAnswerType->setHasInvitation(false);
     $qstAnswerType->setValue($presentation);
     $addForm->addElement($qstAnswerType);
     if ($displayPossibleValues) {
         $qstPossibleValues = new Textarea('qst_possible_values');
         $qstPossibleValues->addAttribute('class', $qstPossibleValues->getName());
         $qstPossibleValues->setLabel($language->text('admin', 'questions_possible_values_label'));
         $qstPossibleValues->setDescription($language->text('admin', 'questions_possible_values_description'));
         $addForm->addElement($qstPossibleValues);
     }
     $presentationConfigList = BOL_QuestionService::getInstance()->getConfigList($presentation);
     foreach ($presentationConfigList as $config) {
         $className = $config->presentationClass;
         /* @var $qstConfig OW_FormElement */
         $qstConfig = new $className($config->name);
         $qstConfig->setLabel($language->text('admin', 'questions_config_' . $config->name . '_label'));
         if (!empty($config->description)) {
             $qstConfig->setDescription($config->description);
         }
         $addForm->addElement($qstConfig);
     }
     $columnCountPresentation = array(BOL_QuestionService::QUESTION_PRESENTATION_MULTICHECKBOX, BOL_QuestionService::QUESTION_PRESENTATION_RADIO);
     if (in_array($presentation, $columnCountPresentation)) {
         $qstColumnCount = new Selectbox('qst_column_count');
         $qstColumnCount->addAttribute('class', $qstColumnCount->getName());
         $qstColumnCount->setLabel($language->text('admin', 'questions_columns_count_label'));
         $qstColumnCount->setOptions($this->qstColumnCountValues);
         $qstColumnCount->setValue(1);
         $addForm->addElement($qstColumnCount);
     }
     $qstRequired = new CheckboxField('qst_required');
     $qstRequired->setLabel($language->text('admin', 'questions_required_label'));
     $qstRequired->setDescription($language->text('admin', 'questions_required_description'));
     $addForm->addElement($qstRequired);
     $qstOnSignUp = new CheckboxField('qst_on_sign_up');
     $qstOnSignUp->setLabel($language->text('admin', 'questions_on_sing_up_label'));
     $qstOnSignUp->setDescription($language->text('admin', 'questions_on_sing_up_description'));
     $addForm->addElement($qstOnSignUp);
     $qstOnEdit = new CheckboxField('qst_on_edit');
     $qstOnEdit->setLabel($language->text('admin', 'questions_on_edit_label'));
     $qstOnEdit->setDescription($language->text('admin', 'questions_on_edit_description'));
     $addForm->addElement($qstOnEdit);
     $qstOnView = new CheckboxField('qst_on_view');
     $qstOnView->setLabel($language->text('admin', 'questions_on_view_label'));
     $qstOnView->setDescription($language->text('admin', 'questions_on_view_description'));
     $addForm->addElement($qstOnView);
     $qstOnSearch = new CheckboxField('qst_on_search');
     $qstOnSearch->setLabel($language->text('admin', 'questions_on_search_label'));
     $qstOnSearch->setDescription($language->text('admin', 'questions_on_search_description'));
     $addForm->addElement($qstOnSearch);
     $qstSubmit = new Submit('qst_submit');
     $qstSubmit->setValue($language->text('admin', 'save_btn_label'));
     $qstSubmit->addAttribute('class', 'ow_button ow_ic_save');
     $addForm->addElement($qstSubmit);
     $qstSubmitAdd = new Submit('qst_submit_and_add');
     $qstSubmitAdd->addAttribute('class', 'ow_button ow_ic_save');
     $qstSubmitAdd->setValue($language->text('admin', 'questions_save_and_new_label'));
     $addForm->addElement($qstSubmitAdd);
     if (OW::getSession()->isKeySet(self::ADD_QUESTION_SESSION_VAR)) {
         $addForm->setValues(OW::getSession()->get(self::ADD_QUESTION_SESSION_VAR));
         OW::getSession()->delete(self::ADD_QUESTION_SESSION_VAR);
     }
     if (OW_Request::getInstance()->isPost()) {
         if ((isset($_POST['qst_submit_and_add']) || isset($_POST['qst_submit'])) && $addForm->isValid($_POST)) {
             OW::getSession()->delete(self::ADD_QUESTION_SESSION_VAR);
             $data = $addForm->getValues();
             if (!isset($data['qst_section'])) {
                 $data['qst_section'] = null;
             } else {
                 $data['qst_section'] = htmlspecialchars(trim($data['qst_section']));
             }
             $presentations = BOL_QuestionService::getInstance()->getPresentations();
             // insert question
             $question = new BOL_Question();
             $question->name = md5(uniqid());
             $question->required = (int) $data['qst_required'];
             $question->onJoin = (int) $data['qst_on_sign_up'];
             $question->onEdit = (int) $data['qst_on_edit'];
             $question->onSearch = (int) $data['qst_on_search'];
             $question->onView = (int) $data['qst_on_view'];
             $question->presentation = htmlspecialchars($data['qst_answer_type']);
             $question->type = htmlspecialchars($presentations[trim($data['qst_answer_type'])]);
             if ((int) $data['qst_column_count'] > 1) {
                 $question->columnCount = (int) $data['qst_column_count'];
             }
             if (isset($data['qst_account_type'])) {
                 $question->accountTypeName = htmlspecialchars(trim($data['qst_account_type']));
                 if ($question->accountTypeName === BOL_QuestionService::ALL_ACCOUNT_TYPES) {
                     $question->accountTypeName = null;
                 }
             }
             if ($data['qst_section'] !== null) {
                 $section = $this->questionService->findSectionBySectionName(htmlspecialchars(trim($data['qst_section'])));
                 if (isset($section)) {
                     $question->sectionName = $section->name;
                 } else {
                     $question->sectionName = null;
                 }
             }
             $question->sortOrder = (int) BOL_QuestionService::getInstance()->findLastQuestionOrder($question->sectionName) + 1;
             // save question configs
             $configs = array();
             foreach ($presentationConfigList as $config) {
                 if (isset($data[$config->name])) {
                     $configs[$config->name] = $data[$config->name];
                 }
             }
             $question->custom = json_encode($configs);
             //$this->questionService->saveOrUpdateQuestion($question);
             //$this->questionService->setQuestionDescription($question->name, htmlspecialchars(trim($data['qst_description'])));
             //$this->questionService->setQuestionLabel($question->name, htmlspecialchars(trim($data['qst_name'])));
             $questionValues = array();
             //add question values
             if (isset($data['qst_possible_values']) && mb_strlen(trim($data['qst_possible_values'])) > 0 && in_array($question->type, array('select', 'multiselect'))) {
                 $questionValues = preg_split('/\\n/', trim($data['qst_possible_values']));
             }
             $this->questionService->createQuestion($question, htmlspecialchars(trim($data['qst_name'])), htmlspecialchars(trim($data['qst_description'])), $questionValues);
             OW::getFeedback()->info($language->text('admin', 'questions_add_question_message'));
             if (isset($_POST['qst_submit'])) {
                 $this->redirect(OW::getRouter()->urlFor('ADMIN_CTRL_Questions', 'index'));
             }
             $this->redirect(OW::getRequest()->getRequestUri());
         }
         $addForm->setValues($_POST);
         OW::getSession()->set(self::ADD_QUESTION_SESSION_VAR, $_POST);
         $this->redirect();
     }
     //        $types = array();
     //        foreach ( $this->questionService->getPresentations() as $presentation => $type )
     //        {
     //            if ( $type === 'select' )
     //            {
     //                $types[] = $presentation;
     //            }
     //        }
     $this->addForm($addForm);
     $fields = array();
     foreach ($addForm->getElements() as $element) {
         if (!$element instanceof HiddenField) {
             $fields[$element->getName()] = $element->getName();
         }
     }
     $this->assign('formData', $fields);
     //        $script = '
     //                    var addQuest = new addQuestion(' . json_encode($types) . ');
     //                    addQuest.displayPossibleValues();
     //                    ';
     //
     //        OW::getDocument()->addOnloadScript($script);
     //
     //        $jsDir = OW::getPluginManager()->getPlugin("admin")->getStaticJsUrl();
     //
     //        OW::getDocument()->addScript($jsDir . "questions.js");
 }