/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = RuleAction::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'action_value' => $this->action_value, 'value_value' => $this->value_value, 'rule_id' => $this->rule_id, 'weight' => $this->weight, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'action', $this->action])->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
public static function execute($id) { echo '$id: ' . $id . '<br/>' . PHP_EOL; Yii::info('execute id: ' . $id, 'rule'); $model = Rule::findOne($id); // Rule Condition Yii::info('condition', 'rule'); $modelsRuleCondition = RuleCondition::findAll(['rule_id' => $id]); /*echo('$modelsRuleCondition: <pre>'); print_r($modelsRuleCondition); echo('</pre>');*/ // condition foreach ($modelsRuleCondition as $modelRuleCondition) { Yii::info('$modelRuleCondition->id: ' . $modelRuleCondition->id, 'rule'); Yii::info('$modelRuleCondition->condition: ' . $modelRuleCondition->condition, 'rule'); Yii::info('$modelRuleCondition->condition_value: ' . $modelRuleCondition->condition_value, 'rule'); Yii::info('$modelRuleCondition->equation: ' . $modelRuleCondition->equation, 'rule'); Yii::info('$modelRuleCondition->value: ' . $modelRuleCondition->value, 'rule'); Yii::info('$modelRuleCondition->value_value: ' . $modelRuleCondition->value_value, 'rule'); echo '$modelRuleCondition->id: ' . $modelRuleCondition->id . '<br/>' . PHP_EOL; echo '$modelRuleCondition->condition: ' . $modelRuleCondition->condition . '<br/>' . PHP_EOL; echo '$modelRuleCondition->condition_value: ' . $modelRuleCondition->condition_value . '<br/>' . PHP_EOL; echo '$modelRuleCondition->equation: ' . $modelRuleCondition->equation . '<br/>' . PHP_EOL; echo '$modelRuleCondition->value: ' . $modelRuleCondition->value . '<br/>' . PHP_EOL; echo '$modelRuleCondition->value_value: ' . $modelRuleCondition->value_value . '<br/>' . PHP_EOL; if (!class_exists('app\\models\\' . $modelRuleCondition->condition)) { Yii::info('!class_exists: ' . 'app\\models\\' . $modelRuleCondition->condition, 'rule'); return false; } if (!class_exists('app\\models\\' . $modelRuleCondition->value)) { Yii::info('!class_exists: ' . 'app\\models\\' . $modelRuleCondition->value, 'rule'); return false; } $conditions = call_user_func(array('app\\models\\' . ucfirst($modelRuleCondition->condition), 'ruleCondition'), $modelRuleCondition->condition_value); Yii::info('$conditions: ' . json_encode($conditions), 'rule'); echo '$conditions: <pre>'; print_r($conditions); echo '</pre>'; $conditions_values = call_user_func(array('app\\models\\' . ucfirst($modelRuleCondition->value), 'ruleCondition'), $modelRuleCondition->value_value); Yii::info('$conditions_values: ' . json_encode($conditions_values), 'rule'); echo '$conditions_values: <pre>'; print_r($conditions_values); echo '</pre>'; // if one of the $values is true relative to the $condition $equation = false; Yii::info('$equation: ' . json_encode($equation), 'rule'); // json_encode prints true or false foreach ($conditions_values as $value) { Yii::info('$value: ' . $value, 'rule'); foreach ($conditions as $condition) { echo '$value: ' . $value . '<br/>' . PHP_EOL; echo '$condition: ' . $condition . '<br/>' . PHP_EOL; // $equal = version_compare($condition, $value, $modelRuleCondition->equation); Yii::info('$equal: ' . json_encode($equal), 'rule'); // json_encode prints true or false if ($equal) { $equation = true; } } } echo '$equation: ' . $equation . '<br/>' . PHP_EOL; var_dump($equation); //exit(); if (!$equation) { Yii::info('!$equation', 'rule'); return false; } } echo 'condition is: ' . $equation . '<br/>' . PHP_EOL; // if nothing has returned something, the condition must be gone good Yii::info('action', 'rule'); $modelsRuleAction = RuleAction::findAll(['rule_id' => $id]); /*echo('$modelsRuleCondition: <pre>'); print_r($modelsRuleCondition); echo('</pre>');*/ foreach ($modelsRuleAction as $modelRuleAction) { Yii::info('$modelRuleAction->id: ' . $modelRuleAction->id, 'rule'); Yii::info('$modelRuleAction->action: ' . $modelRuleAction->action, 'rule'); Yii::info('$modelRuleAction->action_value: ' . $modelRuleAction->action_value, 'rule'); Yii::info('$modelRuleAction->value: ' . $modelRuleAction->value, 'rule'); Yii::info('$modelRuleAction->value_value: ' . $modelRuleAction->value_value, 'rule'); echo '$modelRuleAction->id: ' . $modelRuleAction->id . '<br/>' . PHP_EOL; echo '$modelRuleAction->action: ' . $modelRuleAction->action . '<br/>' . PHP_EOL; echo '$modelRuleAction->action_value: ' . $modelRuleAction->action_value . '<br/>' . PHP_EOL; echo '$modelRuleAction->value: ' . $modelRuleAction->value . '<br/>' . PHP_EOL; echo '$modelRuleAction->value_value: ' . $modelRuleAction->value_value . '<br/>' . PHP_EOL; //exit(); if (!class_exists('app\\models\\' . $modelRuleAction->action)) { Yii::info('!class_exists: ' . 'app\\models\\' . $modelRuleAction->action, 'rule'); return false; } // only retrieve a value if the action is setting $value = ''; if ('setting' == $modelRuleAction->action) { if (!class_exists('app\\models\\' . $modelRuleAction->value)) { Yii::info('!class_exists: ' . 'app\\models\\' . $modelRuleAction->value, 'rule'); return false; } // get the value $values = call_user_func(array('app\\models\\' . ucfirst($modelRuleAction->value), 'ruleCondition'), $modelRuleAction->value_value); Yii::info('$value: ' . $value, 'rule'); echo '$values: <pre>'; print_r($values); echo '</pre>'; $value = HelperData::dataImplode($values); echo '$value: ' . $value . '<br/>' . PHP_EOL; } // use the value $return = call_user_func(array('app\\models\\' . ucfirst($modelRuleAction->action), 'ruleAction'), $modelRuleAction->action_value, $value); Yii::info('$return: ' . json_encode($return), 'rule'); //exit(); if (!$return) { return false; } } return true; }
/** * Finds the RuleAction model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return RuleAction the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = RuleAction::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing Rule model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); /* if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } */ //echo('$id: ' . $id) . '<br/>' . PHP_EOL; $modelsRuleCondition = RuleCondition::findAll(['rule_id' => $id]); //$modelsRuleCondition = RuleCondition::find()->where(['rule_id' => $id])->asArray()->all(); //echo('count: ' . count($modelsRuleCondition)) . '<br/>' . PHP_EOL; for ($i = count($modelsRuleCondition); $i <= 9; $i++) { //echo('$i: ' . $i) . '<br/>' . PHP_EOL; $modelsRuleCondition[$i] = new RuleCondition(); $modelsRuleCondition[$i]->rule_id = $id; $modelsRuleCondition[$i]->value_value = Yii::t('app', '- None -'); $modelsRuleCondition[$i]->weight = $i; } $modelsRuleAction = RuleAction::findAll(['rule_id' => $id]); //echo('count: ' . count($modelsRuleAction)) . '<br/>' . PHP_EOL; for ($i = count($modelsRuleAction); $i <= 4; $i++) { $modelsRuleAction[$i] = new RuleAction(); $modelsRuleAction[$i]->rule_id = $id; $modelsRuleAction[$i]->value_value = Yii::t('app', '- None -'); $modelsRuleAction[$i]->weight = $i; } //var_dump($modelsRuleCondition); //var_dump($modelsRuleAction); //exit(); if ($model->load(Yii::$app->request->post()) && RuleCondition::loadMultiple($modelsRuleCondition, Yii::$app->request->post()) && RuleAction::loadMultiple($modelsRuleAction, Yii::$app->request->post())) { 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); } else { $modelRuleCondition->delete(); } } // change the rule_id, and save foreach ($modelsRuleAction as $modelRuleAction) { if (Yii::t('app', '- None -') != $modelRuleAction->value_value) { $modelRuleAction->rule_id = $model->id; $modelRuleAction->save(false); } else { $modelRuleAction->delete(); } } return $this->redirect(['index']); } } return $this->render('update', ['model' => $model, 'modelsRuleCondition' => $modelsRuleCondition, 'modelsRuleAction' => $modelsRuleAction]); }