BrandController.php 5.61 KB
<?php
    
    namespace artbox\catalog\catalog\controllers;
    
    use common\models\catalog\Brand;
    use artbox\catalog\models\BrandLang;
    use artbox\core\admin\actions\Create;
    use artbox\core\admin\actions\Delete;
    use artbox\core\admin\actions\Index;
    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\web\Controller;
    use yii\web\NotFoundHttpException;
    
    /**
     * Class BrandController
     *
     * @property \artbox\catalog\catalog\Module $module
     * @package artbox\catalog\catalog\controllers
     */
    class BrandController extends Controller implements ControllerInterface
    {
        public function actions()
        {
            return [
                'index'  => [
                    'class'            => Index::class,
                    'columns'          => [
                        'title' => [
                            'type'         => Index::ACTION_COL,
                            'columnConfig' => [
                                'buttonsTemplate' => '{edit}{delete}',
                            ],
                        ],
                        'sort'  => [
                            'type' => Index::POSITION_COL,
                        ],
                    ],
                    'model'            => Brand::class,
                    'hasLanguage'      => true,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                ],
                'create' => array_merge(
                    [
                        'class'           => Create::class,
                        'overwriteEntity' => BrandLang::class,
                    ],
                    $this->fieldsConfig()
                ),
                'update' => array_merge([ 'class' => Update::class ], $this->fieldsConfig()),
                'view'   => [
                    'class'          => View::class,
                    'model'          => Brand::class,
                    'hasAlias'       => true,
                    'hasGallery'     => false,
                    'languageFields' => [
                        [
                            'name' => 'title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'description',
                            'type' => Form::WYSIWYG,
                        ],
                    ],
                    'fields'         => [
                        [
                            'name' => 'image',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'sort',
                            'type' => Form::NUMBER,
                        ],
                        [
                            'name' => 'status',
                            'type' => Form::BOOL,
                        ],
                    ],
                ],
                'delete' => [
                    'class' => Delete::class,
                ],
            ];
        }
        
        public function findModel($id)
        {
            $model = Brand::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 Brand();
        }
        
        public function deleteModel($id)
        {
            $category = Brand::find()
                             ->with('languages')
                             ->where(
                                 [
                                     'id' => $id,
                                 ]
                             )
                             ->one();
            
            return $category->delete();
        }
        
        protected function fieldsConfig()
        {
            return [
                'model'          => Brand::class,
                'hasAlias'       => false,
                'hasGallery'     => false,
                'languageFields' => array_merge(
                    [
                        [
                            'name' => 'title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'alias',
                            'type' => Form::STRING,
                            'slugify' => 'title'
                        ],
                        [
                            'name' => 'description',
                            'type' => Form::WYSIWYG,
                        ],
                    
                    ],
                    $this->module->brandLanguageFields
                ),
                'fields'         => array_merge(
                    [
                        [
                            'name' => 'image',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'status',
                            'type' => Form::BOOL,
                        ],
                        [
                            'name' => 'sort',
                            'type' => Form::NUMBER,
                        ],
                    ],
                    $this->module->brandFields
                ),
            ];
        }
    }