Skip to content

Commit

Permalink
Merge pull request #6 from bobonov/master
Browse files Browse the repository at this point in the history
Step reordering and model validation
  • Loading branch information
Matej-ch authored May 3, 2022
2 parents e95b82b + 253de96 commit 0e65bef
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 53 deletions.
52 changes: 10 additions & 42 deletions src/controllers/PageGuideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ['@'],
]
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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,
]);
}

Expand All @@ -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();
}

Expand All @@ -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'));
}
}
4 changes: 3 additions & 1 deletion src/messages/en/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
return [
'ID' => 'Id',
'Url' => 'Url page for guide',
'Rules' => 'Rules'
'Rules' => 'Rules',
'Rules not set' => 'At least one rule step must be set',
'Rules not valid' => 'Rule is not valid',
];
1 change: 0 additions & 1 deletion src/messages/en/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
'next' => 'Next',
'skip' => 'Skip',
'done' => 'Done',
'Rules not set' => 'At least one rule step must be set'
];
4 changes: 3 additions & 1 deletion src/messages/it/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
return [
'ID' => 'Id',
'Url' => 'Url della pagina per la guida',
'Rules' => 'Regole'
'Rules' => 'Regole',
'Rules not set' => 'Regola non impostata',
'Rules not valid' => 'Il formato della regola non è valido',
];
1 change: 0 additions & 1 deletion src/messages/it/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
'next' => 'Successivo',
'skip' => 'Salta',
'done' => 'Fine',
'Rules not set' => 'Rules not set'
];
4 changes: 3 additions & 1 deletion src/messages/sk/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
return [
'ID' => 'Id',
'Url' => 'Stránka návodu',
'Rules' => 'Pravidlá'
'Rules' => 'Pravidlá',
'Rules not set' => 'Musíte nastaviť aspoň jeden krok',
'Rules not valid' => 'Rule is not valid',
];
1 change: 0 additions & 1 deletion src/messages/sk/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
'next' => 'Ďalej',
'skip' => 'Preskočiť',
'done' => 'Ukončiť',
'Rules not set' => 'Musíte nastaviť aspoň jeden krok'
];
61 changes: 56 additions & 5 deletions src/models/PageGuide.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -29,22 +32,70 @@ 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);
}],
];
}

/**
* validator: validate rules data
*/
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', Yii::t('pageGuide/model', 'Rules not valid'));
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/model', '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/model', 'Rules not valid'));
break;
}
}
$this->rules = Json::encode($this->rules);
}

/**
* filter: order rules on step value
*/
public function stepOrder(string $value): string
{
$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'),
];
}
}

0 comments on commit 0e65bef

Please sign in to comment.