CategoryController.php 11.4 KB
<?php
    
    namespace artbox\catalog\catalog\controllers;
    
    use common\models\catalog\Category;
    use artbox\core\admin\actions\Create;
    use artbox\core\admin\actions\Delete;
    use artbox\core\admin\actions\Update;
    use artbox\core\admin\actions\View;
    use artbox\core\admin\interfaces\ControllerInterface;
    use artbox\core\admin\widgets\Form;
    use yii\db\ActiveQuery;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\web\Response;
    use yii\web\ServerErrorHttpException;
    
    /**
     * Class CategoryController
     *
     * @property \artbox\catalog\catalog\Module $module
     * @package artbox\catalog\catalog\controllers
     */
    class CategoryController extends Controller implements ControllerInterface
    {
        public function actions()
        {
            return [
                'create' => array_merge([ 'class' => Create::class ], $this->fieldsConfig()),
                'update' => array_merge([ 'class' => Update::class ], $this->fieldsConfig()),
                'view'   => [
                    'class'          => View::class,
                    'model'          => Category::class,
                    'hasAlias'       => true,
                    'hasGallery'     => false,
                    'languageFields' => [
                        [
                            'name' => 'title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'description',
                            'type' => Form::WYSIWYG,
                        ],
                    ],
                    'fields'         => [
                        [
                            'name' => 'image',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'thumb',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'sort',
                            'type' => Form::NUMBER,
                        ],
                        [
                            'name' => 'status',
                            'type' => Form::BOOL,
                        ],
                    ],
                ],
                'delete' => [
                    'class' => Delete::class,
                ],
            ];
        }
        
        public function actionIndex()
        {
            return $this->render('index');
        }
        
        public function actionCategories($sort)
        {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            
            if ($sort === 'asc') {
                $sortKey = SORT_ASC;
            } elseif ($sort === 'desc') {
                $sortKey = SORT_DESC;
            } else {
                $sortKey = SORT_ASC;
            }
            
            $categories = Category::find()
                                  ->asArray()
                                  ->with('language')
                                  ->with(
                                      [
                                          'categories' => function (ActiveQuery $query) use ($sortKey) {
                                              $query->with('language')
                                                    ->with(
                                                        [
                                                            'categories' => function (ActiveQuery $query) use ($sortKey
                                                            ) {
                                                                $query->with('language')
                                                                      ->orderBy([ 'sort' => $sortKey ]);
                                                            },
                                                        ]
                                                    )
                                                    ->orderBy([ 'sort' => $sortKey ]);
                                          },
                                      ]
                                  )
                                  ->orderBy([ 'sort' => $sortKey ])
                                  ->where(
                                      [
                                          'parent_id' => null,
                                      ]
                                  )
                                  ->all();
            
            return $categories;
        }
        
        public function findModel($id)
        {
            $model = Category::find()
                             ->with('languages')
                             ->where([ 'id' => $id ])
                             ->one();
            if ($model !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
        public function newModel()
        {
            return new Category();
        }
        
        public function deleteModel($id)
        {
            $category = Category::find()
//                                ->with('languages.alias')
                                ->where(
                                    [
                                        'id' => $id,
                                    ]
                                )
                                ->one();
//            $langs = call_user_func(
//                [
//                    $category,
//                    'getVariationModels',
//                ]
//            );
//            foreach ($langs as $lang) {
//                if ($lang->alias !== null) {
//                    $lang->alias->delete();
//                }
//            }
            
            return $category->delete();
        }
        
        public function actionSort($id, $sort)
        {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            
            $category = Category::findOne($id);
            
            if ($category) {
                $category->sort = $sort;
                
                if ($category->save()) {
                    $categories = Category::find()
                                          ->asArray()
                                          ->with('language')
                                          ->with(
                                              [
                                                  'categories' => function (ActiveQuery $query) {
                                                      $query->with('language')
                                                            ->with(
                                                                [
                                                                    'categories' => function (ActiveQuery $query) {
                                                                        $query->with('language')
                                                                              ->orderBy('sort');
                                                                    },
                                                                ]
                                                            )
                                                            ->orderBy('sort');
                                                  },
                                              ]
                                          )
                                          ->orderBy('sort')
                                          ->where(
                                              [
                                                  'parent_id' => null,
                                              ]
                                          )
                                          ->all();
                    return $categories;
                } else {
                    throw new ServerErrorHttpException();
                }
            } else {
                throw new NotFoundHttpException('Category not found');
            }
        }
        
        protected function fieldsConfig()
        {
            return [
                'model'          => Category::class,
                'hasAlias'       => false,
                'hasGallery'     => false,
                'languageFields' => array_merge(
                    [
                        [
                            'name' => 'title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'description',
                            'type' => Form::WYSIWYG,
                        ],
                        [
                            'name'    => 'alias',
                            'type'    => Form::STRING,
                            'slugify' => 'title',
                        ],
                        [
                            'name' => 'seo_title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'product_title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'seo_h1',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'seo_description',
                            'type' => Form::TEXTAREA,
                        ],
                        [
                            'name' => 'seo_text',
                            'type' => Form::WYSIWYG,
                        ],
                    ],
                    $this->module->categoryLanguageFields
                ),
                'fields'         => array_merge(
                    [
                        [
                            'name' => 'thumb',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'status',
                            'type' => Form::BOOL,
                        ],
                        [
                            'name' => 'sort',
                            'type' => Form::NUMBER,
                        ],
                        [
                            'name'              => 'parent_id',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'title',
                            'relationName'      => 'parent',
                            'multiple'          => false,
                        ],
                        [
                            'name'              => 'articleIds',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'title',
                            'relationName'      => 'articles',
                            'multiple'          => true,
                        ],
                        [
                            'name'              => 'groupIds',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'title',
                            'relationName'      => 'groups',
                            'multiple'          => true,
                        ],
                    
                    ],
                    $this->module->categoryFields
                ),
            ];
        }
    }