AuthorController.php 3.11 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 07.06.18
     * Time: 15:48
     */
    
    namespace backend\controllers;
    
    use artbox\core\admin\actions\Index;
    use artbox\core\admin\interfaces\ControllerInterface;
    use common\models\Author;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;

    class AuthorController 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'          => [
                        'name'      => [
                            'type' => Index::ACTION_COL,
                        ],
                        'secondname' => [
                            'type'         => Index::STRING_COL,
                        ],
                        'email' => [
                            'type'         => Index::STRING_COL,
                        ],
                        'created_at' => [
                            'type' => Index::DATETIME_COL,
                        ],
                        'status'       => [
                            'type' => Index::STRING_COL,
                        ],
                    ],
                    'model'            => Author::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'create' => false,
                ],
            ];
        }
    
    
    
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
        
            $model->status = true;
            $model->save(false, [ 'status' ]);
        
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect('index');
            } else {
                return $this->render(
                    'update',
                    [
                        'model' => $model,
                    ]
                );
            }
        }
    
        public function findModel($id)
        {
            $model = Author::find()
                             ->where([ 'id' => $id ])
                             ->one();
            if ($model !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    }