AliasController.php 7.7 KB
<?php
    
    namespace artbox\core\controllers;

    use artbox\catalog\models\Filter;
    use artbox\core\helpers\SlugHelper;
    use artbox\core\models\Language;
    use Yii;
    use artbox\core\models\Alias;
    use artbox\core\models\AliasSearch;
    use yii\filters\AccessControl;
    use yii\helpers\Json;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;

    /**
     * AliasController implements the CRUD actions for Alias model.
     */
    class AliasController extends Controller
    {
        /**
         * @return bool|string
         */
        public function getViewPath()
        {
            return \Yii::getAlias('@artbox/core/views/alias');
        }
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => [
                                'login',
                                'error',
                            ],
                            'allow'   => true,
                        ],
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [//                        'change-password' => [ 'post' ],
                    ],
                ],
            ];
        }
    
        /**
         * Lists all Alias models.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $languages = Language::find()
                                 ->select(
                                     [
                                         'name',
                                         'id',
                                     ]
                                 )
                                 ->where([ 'status' => 1 ])
                                 ->asArray()
                                 ->indexBy('id')
                                 ->column();
            $entities = Alias::find()
                ->select('entity')
                ->where(
                    [
                        'not',
                        [ 'entity' => null ],
                    ]
                )
                ->distinct()
                ->indexBy('entity')
                ->column();
            $searchModel = new AliasSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        
            return $this->render(
                'index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                    'languages'    => $languages,
                    'entities'     => $entities,
                ]
            );
        }
    
        /**
         * Displays a single Alias model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            return $this->render(
                'view',
                [
                    'model' => $this->findModel($id),
                ]
            );
        }
    
        /**
         * Creates a new Alias model.
         * If creation is successful, the browser will be redirected to the 'view' alias.
         *
         * @param string $url
         *
*@return mixed
         */
        public function actionCreate(string $url = null)
        {
            $languages = Language::find()
                                 ->select(
                                     [
                                         'name',
                                         'id',
                                     ]
                                 )
                                 ->where([ 'status' => 1 ])
                                 ->asArray()
                                 ->indexBy('id')
                                 ->column();
            $model = new Alias(
                [
                    'scenario' => Alias::SCENARIO_CREATE,
                ]
            );
            if ($model->load(Yii::$app->request->post())) {
                if ($model->save()) {
                    return $this->redirect(
                        [
                            'view',
                            'id' => $model->id,
                        ]
                    );
                }
            }
        
            if (!empty($url)) {
                $urlArray = explode('/', trim($url, '/'));
            
                $route = Json::encode(
                    [
                        'category/view',
                        'filter'   => $urlArray[ 2 ],
                        'category' => $urlArray[ 1 ],
                    ]
                );
            
                $model->route = $route;
                $model->entity = Filter::className();
                $model->fields = $url;
            }
        
            return $this->render(
                'create',
                [
                    'model'     => $model,
                    'languages' => $languages,
                ]
            );
        }
    
        /**
         * Updates an existing Alias model.
         * If update is successful, the browser will be redirected to the 'view' alias.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            $model->scenario = Alias::SCENARIO_UPDATE;
        
            if ($model->load(Yii::$app->request->post())) {
                if ($model->save()) {
                    return $this->redirect(
                        [
                            'view',
                            'id' => $model->id,
                        ]
                    );
                }
            }
        
            return $this->render(
                'update',
                [
                    'model' => $model,
                ]
            );
        }
    
        /**
         * Deletes an existing Alias model.
         * If deletion is successful, the browser will be redirected to the 'index' alias.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionDelete($id)
        {
            $this->findModel($id)
                 ->delete();
        
            return $this->redirect([ 'index' ]);
        }
    
        /**
         * Get unique slug from $text
         *
         * @param          $text
         * @param int|null $languageId
         *
         * @return string
         */
        public function actionSlugify($text, int $languageId = null)
        {
            $response = \Yii::$app->response;
            $response->format = $response::FORMAT_JSON;
            $slug = SlugHelper::slugify($text);
            return SlugHelper::unify($slug, null, $languageId);
        }
    
        /**
         * Finds the Alias model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return Alias the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($id)
        {
            if (( $model = Alias::findOne($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested alias does not exist.');
            }
        }
    }