Пример #1
0
 public function validateAttribute($model, $attribute)
 {
     $attributeArray = explode('_', $attribute);
     if (count($attributeArray) > 1) {
         list($value, $id) = $attributeArray;
         $questionModel = MappingQuestions::findOne(['id' => $id]);
         $attributesModel = MembersAttributes::findOne(['id' => $id]);
         if ($questionModel && $questionModel->is_required && empty($model->{$attribute})) {
             $message = "'{$questionModel->title}' " . Yii::t('errors', 'can not be empty!');
             $this->addError($model, $attribute, $message);
         } else {
             if ($attributesModel && $attributesModel->is_required && empty($model->{$attribute})) {
                 $message = "'{$attributesModel->label}' " . Yii::t('errors', 'can not be empty!');
                 $this->addError($model, $attribute, $message);
             }
         }
     }
 }
Пример #2
0
 public function actionMapping_old_171115()
 {
     if (!Yii::$app->user->isGuest) {
         $user = Yii::$app->getUser()->getIdentity();
         //@TODO new logic to find out if there are answers
         /*$answers = MembersQuestionsAnswers::find(['member_id' => $user->id])->count();
         
                     if ($answers > 0) {
                         $user->status = User::STATUS_ACTIVE;
         
                         if ($user->save()) {
                             return Yii::$app->response->redirect(array('members/index'));
                         } else {
                             Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not update user status. Please contact system administrator'));
                         }
                     }*/
         $settingsModel = GeneralSettings::findOne(['name' => 'mapping_page_id']);
         $pageModel = CmsPages::findOne(['id' => $settingsModel->value]);
         $categoryModel = MappingCategories::findOne(['id' => $pageModel->mapping_id]);
         $attributesModel = MembersAttributes::findAll(['id' => $categoryModel->attributes_id]);
         $questionsModel = MappingQuestions::find(['category_id' => $categoryModel->id])->with('fieldsTypes')->all();
         $questionsAnswers = new MembersQuestionsAnswers($pageModel->category_id);
         $attributesAnswers = new MembersAttributesAnswers($categoryModel->attributes_id);
         if (Yii::$app->request->post()) {
             $mappingQuestionsSaved = false;
             $mappingAttributesSaved = false;
             if ($questionsAnswers->load(Yii::$app->request->post()) && $questionsAnswers->validate()) {
                 foreach ($questionsAnswers->attributes as $attribute_id => $attribute) {
                     $attributesArray = explode('_', $attribute_id);
                     if (count($attributesArray) > 1) {
                         list($name, $id) = $attributesArray;
                         if (!($model = MembersQuestionsAnswers::findOne(['question_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) {
                             $model = new MembersQuestionsAnswers();
                         }
                         $has_option = false;
                         if ($questionsModel = MappingQuestions::findOne($id)) {
                             $type_id = $questionsModel->type_id;
                             $has_option = FieldsTypes::findOne($type_id)->has_options;
                         }
                         if ($has_option) {
                             $model->value = '';
                             $model->option_id = '';
                             if ($optionsModel = MappingQuestionsToOptions::findOne($questionsAnswers->{$attribute_id})) {
                                 $model->value = $optionsModel->title;
                                 $model->option_id = $optionsModel->id;
                             }
                         } else {
                             $model->value = $questionsAnswers->{$attribute_id};
                         }
                         $model->question_id = $id;
                         $model->member_id = Yii::$app->getUser()->id;
                         $model->save();
                     }
                 }
                 $mappingQuestionsSaved = true;
             }
             if ($attributesAnswers->load(Yii::$app->request->post()) && $attributesAnswers->validate()) {
                 foreach ($attributesAnswers->attributes as $attribute_id => $attribute) {
                     $attributesArray = explode('_', $attribute_id);
                     if (count($attributesArray) > 1) {
                         list($name, $id) = $attributesArray;
                         if (!($model = MembersAttributesAnswers::findOne(['attribute_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) {
                             $model = new MembersAttributesAnswers();
                         }
                         $has_option = false;
                         if ($questionsModel = MembersAttributes::findOne($id)) {
                             $type_id = $questionsModel->type_id;
                             $has_option = FieldsTypes::findOne($type_id)->has_options;
                             $filedTypeTitle = FieldsTypes::findOne($type_id)->title;
                         }
                         if ($has_option) {
                             $model->value = '';
                             $model->option_id = '';
                             if ($optionsModel = MembersAttributesToOptions::findOne($attributesAnswers->{$attribute_id})) {
                                 $model->value = $optionsModel->title;
                                 $model->option_id = $optionsModel->id;
                             }
                         } else {
                             $model->value = $attributesAnswers->{$attribute_id};
                             if (isset($filedTypeTitle) && $filedTypeTitle == 'Date Picker') {
                                 //@TODO replace datepicker with some database constatnt value;
                                 $model->value = strtotime($attributesAnswers->{$attribute_id});
                             }
                         }
                         $model->attribute_id = $id;
                         $model->member_id = Yii::$app->getUser()->id;
                         $model->save();
                     }
                 }
                 $mappingAttributesSaved = true;
             }
             if ($mappingQuestionsSaved && $mappingAttributesSaved) {
                 $user = Yii::$app->getUser()->getIdentity();
                 $user->status = User::STATUS_ACTIVE;
                 if ($user->save()) {
                     return Yii::$app->response->redirect(array('members'));
                 } else {
                     Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not save mapping data. Please contact system administrator'));
                 }
             } else {
                 $categoryModel = MappingCategories::findOne(['id' => $pageModel->category_id]);
                 $attributesModel = MembersAttributes::find(['id in ' => $categoryModel->attributes_id])->with('fieldsTypes')->all();
                 $questionsModel = MappingQuestions::find(['category_id' => $categoryModel->id])->with('fieldsTypes')->all();
                 return $this->render('mapping', ['questionsAnswers' => $questionsAnswers, 'attributesAnswers' => $attributesAnswers, 'pageModel' => $pageModel, 'categoryModel' => $categoryModel, 'questionsModel' => $questionsModel, 'attributesModel' => $attributesModel]);
             }
         } else {
             return $this->render('mapping', ['questionsAnswers' => $questionsAnswers, 'attributesAnswers' => $attributesAnswers, 'pageModel' => $pageModel, 'categoryModel' => $categoryModel, 'questionsModel' => $questionsModel, 'attributesModel' => $attributesModel]);
         }
     } else {
         return Yii::$app->response->redirect(array('site/login'));
     }
 }