/**
  * Creates a new Rule model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Rule();
     // create 10 RuleCondition models
     $modelsRuleCondition[] = new RuleCondition();
     for ($i = 0; $i <= 9; $i++) {
         $modelsRuleCondition[$i] = new RuleCondition();
         // if it is not the first one, there must be always one condition
         if (0 < $i) {
             $modelsRuleCondition[$i]->value_value = Yii::t('app', '- None -');
             $modelsRuleCondition[$i]->weight = $i;
         }
     }
     // create 5 RuleAction models
     $modelsRuleAction[] = new RuleAction();
     for ($i = 0; $i <= 4; $i++) {
         $modelsRuleAction[$i] = new RuleAction();
         // if it is not the first one, there must be always one condition
         if (0 < $i) {
             $modelsRuleAction[$i]->value_value = Yii::t('app', '- None -');
             $modelsRuleAction[$i]->weight = $i;
         }
     }
     if ($model->load(Yii::$app->request->post()) && RuleCondition::loadMultiple($modelsRuleCondition, Yii::$app->request->post()) && RuleAction::loadMultiple($modelsRuleAction, Yii::$app->request->post())) {
         // set rule_id temporary on 0, for validation
         foreach ($modelsRuleCondition as $key => $modelRuleCondition) {
             $modelRuleCondition->rule_id = 0;
             $modelsRuleCondition[$key] = $modelRuleCondition;
         }
         // set rule_id temporary on 0, for validation
         foreach ($modelsRuleAction as $key => $modelRuleAction) {
             $modelRuleAction->rule_id = 0;
             $modelsRuleAction[$key] = $modelRuleAction;
         }
         if ($model->validate() && RuleCondition::validateMultiple($modelsRuleCondition) && RuleAction::validateMultiple($modelsRuleAction)) {
             $model->save(false);
             // change the rule_id, and save
             foreach ($modelsRuleCondition as $modelRuleCondition) {
                 if (Yii::t('app', '- None -') != $modelRuleCondition->value_value) {
                     $modelRuleCondition->rule_id = $model->id;
                     $modelRuleCondition->save(false);
                 }
             }
             // change the rule_id, and save
             foreach ($modelsRuleAction as $modelRuleAction) {
                 //echo('$modelRuleAction: ') . '<br/>' . PHP_EOL;
                 //var_dump($modelRuleAction);
                 if (Yii::t('app', '- None -') != $modelRuleAction->value_value) {
                     $modelRuleAction->rule_id = $model->id;
                     $modelRuleAction->save(false);
                 }
             }
             //exit();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model, 'modelsRuleCondition' => $modelsRuleCondition, 'modelsRuleAction' => $modelsRuleAction]);
 }
 /**
  * Creates a new Rule model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Rule();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 3
0
 public function saveRule(Request $request)
 {
     // [1] CHECK IF THE BLUEPRINT EXIST AND THE USER CAN CHANGE IT
     $user = Auth::user();
     $blueprint = Blueprint::find($request->input('blueprint_id'));
     if (!$blueprint) {
         if ($user->level == 3) {
             abort(404, 'El formulario no existe!');
         } else {
             abort(403, 'El formulario no pertenece al usuario');
         }
     }
     // [2] CREATE THE RULE OBJECT
     $rule = new Rule();
     $rule->blueprint_id = $blueprint->id;
     $rule->section_id = (int) $request->input("section_id");
     $rule->question_id = (int) $request->input("question_id");
     $rule->value = $request->input("value");
     $rule->save();
     // [4] GENERATE A NEW TOKEN TO PREVENT TOKEN MISSMATCH
     $rule->new_token = csrf_token();
     // [5] RETURN THE JSON
     return response()->json($rule)->header('Access-Control-Allow-Origin', '*');
 }