<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 09.07.18
     * Time: 14:36
     */
    
    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\widgets\Form;
    use common\models\Partner;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\web\View;

    class PartnerController extends Controller
    {
        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'          => [
	                    'id' => [
		                    'type' => Index::NUMBER_COL,
	                    ],
                        'link'      => [
                            'type' => Index::ACTION_COL,
                        ],
                        'sort'       => [
                            'type' => Index::POSITION_COL,
                        ],
                        'status'       => [
                            'type' => Index::STATUS_COL,
                        ],

                    ],
                    'model'            => Partner::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
	                'defaultSort' => [
		                'id' => SORT_DESC
	                ]
                ],
                'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()),
                'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()),
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Partner::className(),
                    'hasAlias'       => false,
                    'fields'         => [
                        [
                            'name' => 'image_id',
                            'type' => Form::IMAGE,
                        ],
                        [
                            'name' => 'link',
                            'type' => Form::STRING,
                        ],
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
        public function findModel($id)
        {
            $model = Partner::find()
                          ->where([ 'id' => $id ])
                          ->one();
            if ($model !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    
        public function newModel()
        {
            return new Partner();
        }
    
        public function deleteModel($id)
        {
            $page = Partner::find()
                         ->where(
                             [
                                 'id' => $id,
                             ]
                         )
                         ->one();
            return $page->delete();
        }
    
        protected static function fieldsConfig()
        {
            return [
                'model'          => Partner::className(),
                'hasAlias'       => false,
                'fields'         => [
                
                    [
                        'name' => 'image_id',
                        'type' => Form::IMAGE,
                    ],
                    [
                        'name'              => 'link',
                        'type'              => Form::STRING,
                    ],
                    [
                        'name' => 'status',
                        'type' => Form::BOOL,
                    ],
                    [
                        'name' => 'sort',
                        'type' => Form::NUMBER,
                    ],
                ],
            ];
        }
    }