<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 04.04.18
     * Time: 12:19
     */
    
    namespace backend\controllers;
    
    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 common\models\blog\Article;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;

    class BlogArticleController extends Controller implements ControllerInterface
    {
        public function behaviors()
        {
            return [
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
            ];
        }
        public function actions()
        {
            return [
                'index'  => [
                    'class'            => Index::className(),
                    'columns'          => [
                        'title'      => [
                            'type' => Index::ACTION_COL,
                        ],
                        'categories' => [
                            'type'         => Index::RELATION_COL,
                            'columnConfig' => [
                                'relationField' => 'title',
                            ],
                        ],
                        'tags' => [
                            'type'         => Index::RELATION_COL,
                            'columnConfig' => [
                                'relationField' => 'label',
                            ],
                        ],
                        'updated_at' => [
                            'type' => Index::DATETIME_COL,
                        ],
                        'sort'       => [
                            'type' => Index::POSITION_COL,
                        ],
                        'status'       => [
                            'type' => Index::STATUS_COL,
                        ],
                    ],
                    'model'            => Article::className(),
                    'hasLanguage'      => true,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                ],
                'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()),
                'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()),
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Article::className(),
                    'hasAlias'       => true,
                    'languageFields' => [
                        [
                            'name' => 'title',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'body',
                            'type' => Form::WYSIWYG,
                        ],
                        [
                            'name' => 'body_preview',
                            'type' => Form::TEXTAREA,
                        ],
                    ],
                    'fields'         => [
                        [
                            'name' => 'image_id',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name'              => 'categoryIds',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'title',
                            'relationName'      => 'categories',
                            'multiple'          => true,
                        ],
                        [
                            'name'              => 'tagIds',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'label',
                            'relationName'      => 'tags',
                            'multiple'          => true,
                        ],
                        [
                            'name'              => 'articleIds',
                            'type'              => Form::RELATION,
                            'relationAttribute' => 'title',
                            'relationName'      => 'articles',
                            'multiple'          => true,
                        ],
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
    
        public function findModel($id)
        {
            $model = Article::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 Article();
        }
    
        public function deleteModel($id)
        {
            $page = Article::find()
                        ->with('languages.alias')
                        ->where(
                            [
                                'id' => $id,
                            ]
                        )
                        ->one();
            $langs = call_user_func(
                [
                    $page,
                    'getVariationModels',
                ]
            );
            foreach ($langs as $lang) {
                if ($lang->alias !== null) {
                    $lang->alias->delete();
                }
            }
        
            return $page->delete();
        }
    
        protected static function fieldsConfig()
        {
            return [
                'model'          => Article::className(),
                'hasAlias'       => true,
                'languageFields' => [
                    [
                        'name' => 'title',
                        'type' => Form::STRING,
                    ],
                    [
                        'name' => 'body',
                        'type' => Form::WYSIWYG,
                    ],
                    [
                        'name' => 'body_preview',
                        'type' => Form::TEXTAREA,
                    ],
                ],
                'fields'         => [
                
                    [
                        'name' => 'image_id',
                        'type' => Form::IMAGE,
                    ],
                    [
                        'name'              => 'categoryIds',
                        'type'              => Form::RELATION,
                        'relationAttribute' => 'title',
                        'relationName'      => 'categories',
                        'multiple'          => true,
                    ],
                    [
                        'name'              => 'tagIds',
                        'type'              => Form::RELATION,
                        'relationAttribute' => 'label',
                        'relationName'      => 'tags',
                        'multiple'          => true,
                    ],
                    [
                        'name'              => 'articleIds',
                        'type'              => Form::RELATION,
                        'relationAttribute' => 'title',
                        'relationName'      => 'articles',
                        'multiple'          => true,
                    ],
                    [
                        'name' => 'status',
                        'type' => Form::BOOL,
                    ],
                    [
                        'name' => 'sort',
                        'type' => Form::NUMBER,
                    ],
                ],
            ];
        }
    }