diff --git a/src/controllers/PageGuideController.php b/src/controllers/PageGuideController.php index 74b12b1..f2f450a 100644 --- a/src/controllers/PageGuideController.php +++ b/src/controllers/PageGuideController.php @@ -16,11 +16,11 @@ class PageGuideController extends Controller public function behaviors() { return [ - 'access' =>[ + 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ - 'actions' => ['index','update','create','delete'], + 'actions' => ['index', 'update', 'create', 'delete'], 'allow' => true, 'roles' => ['@'], ] @@ -51,28 +51,8 @@ public function actionCreate() $model = new PageGuide(); $post = Yii::$app->request->post(); - if(Yii::$app->request->isPost) { - - $origUrl = $post['PageGuide']['url']; - - $post['PageGuide']['url'] = parse_url($post['PageGuide']['url'],PHP_URL_PATH); - - $post['PageGuide']['rules'] = array_filter($post['PageGuide']['rules'], static function ($val) { - return !empty($val['element']); - }); - - if(empty($post['PageGuide']['rules'])) { - $model->url = $origUrl; - return $this->render('create', [ - 'model' => $model, - 'rulesError' => Yii::t('pageGuide/view','Rules not set') - ]); - } - - $post['PageGuide']['rules'] = Json::encode($post['PageGuide']['rules']); - + if (Yii::$app->request->isPost) { PageGuide::deleteAll(['url' => $post['PageGuide']['url']]); - if ($model->load($post) && $model->save()) { return $this->redirect(['index']); } @@ -86,30 +66,18 @@ public function actionCreate() public function actionUpdate($id) { $model = $this->findModel($id); - - if(Yii::$app->request->isPost) { + $rulesError = ''; + if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); - $post['PageGuide']['rules'] = array_filter($post['PageGuide']['rules'], static function ($val) { - return !empty($val['element']); - }); - - if(empty($post['PageGuide']['rules'])) { - return $this->render('update', [ - 'model' => $model, - 'rulesError' => Yii::t('pageGuide/view','Rules not set') - ]); - } - - $post['PageGuide']['rules'] = Json::encode($post['PageGuide']['rules']); - if ($model->load($post) && $model->save()) { return $this->redirect(['index']); } + $rulesError = $model->getFirstError('rules'); } - return $this->render('update', [ 'model' => $model, + 'rulesError' => $rulesError, ]); } @@ -118,11 +86,11 @@ public function actionDelete($id) try { $model = $this->findModel($id); } catch (NotFoundHttpException $e) { - Yii::$app->session->setFlash('warning',$e->getMessage()); + Yii::$app->session->setFlash('warning', $e->getMessage()); return $this->redirect(['index']); } - if($model) { + if ($model) { $model->delete(); } @@ -140,6 +108,6 @@ protected function findModel($id): ?PageGuide return $model; } - throw new NotFoundHttpException(Yii::t('pageGuide/view','Not found')); + throw new NotFoundHttpException(Yii::t('pageGuide/view', 'Not found')); } } \ No newline at end of file diff --git a/src/models/PageGuide.php b/src/models/PageGuide.php index 1620738..6975a4e 100644 --- a/src/models/PageGuide.php +++ b/src/models/PageGuide.php @@ -2,8 +2,11 @@ namespace matejch\pageGuide\models; +use Exception; use Yii; use yii\db\ActiveRecord; +use yii\helpers\Json; + /** * This is the model class for table "page_guide". @@ -29,22 +32,67 @@ public function rules() { return [ [['url'], 'required'], - [['rules'], 'string'], [['url'], 'string', 'max' => 1024], [['url'], 'trim'], - [['url'],'filter','filter'=>'strip_tags','skipOnArray' => true] + [['url'], 'filter', 'filter' => 'strip_tags', 'skipOnArray' => true], + [['rules'], 'validateRuleFormat'], + [['rules'], 'filter', 'filter' => [$this, 'stepOrder']], + [['url'], 'filter', 'filter' => function ($value) { + return parse_url($value, PHP_URL_PATH); + }], ]; } + public function validateRuleFormat($attribute, $params, $validator): void + { + if (!is_array($this->rules)) { + try { + $this->rules = Json::decode($this->rules); + } catch (Exception $e) { + $this->rules = '{}'; + $this->addError('rules', 'Invalid rule format'); + return; + } + } + + $this->rules = array_filter($this->rules, static function ($val) { + return !empty($val['element']); + }); + if (empty($this->rules)) { + $this->rules = '{}'; + $this->addError('rules', Yii::t('pageGuide/view', 'Rules not set')); + return; + } + + foreach ($this->rules as $rule) { + if (!is_array($rule) || !isset($rule['step'], $rule['element'], $rule['intro'])) { + $this->addError('rules', Yii::t('pageGuide/view', 'Invalid rule format')); + break; + } + } + Json::encode($this->rules); + } + + /** + * Order the rule on step value + */ + public function stepOrder(string $value): array + { + $value = Json::decode($value); + $keys = array_column($value, 'step'); + array_multisort($keys, SORT_ASC, $value); + return Json::encode($value); + } + /** * {@inheritdoc} */ public function attributeLabels() { return [ - 'id' => Yii::t('pageGuide/model','ID'), - 'url' => Yii::t('pageGuide/model','Url'), - 'rules' => Yii::t('pageGuide/model','Rules'), + 'id' => Yii::t('pageGuide/model', 'ID'), + 'url' => Yii::t('pageGuide/model', 'Url'), + 'rules' => Yii::t('pageGuide/model', 'Rules'), ]; } } \ No newline at end of file