CommentController.php 4.78 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 23.05.18
     * Time: 12:55
     */
    
    namespace backend\controllers;
    
    use artbox\core\admin\actions\Delete;
    use artbox\core\admin\actions\Index;
    use artbox\core\admin\actions\View;
    use artbox\core\admin\widgets\Form;
    use common\models\Comment;
    use common\models\Service;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\helpers\ArrayHelper;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;

    class CommentController 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,
                        ],
                        'email'      => [
                            'type' => Index::STRING_COL
                        ],
                        'status'     => [
                            'type' => Index::STATUS_COL,
                        ],
                        'created_at' => [
                            'type' => Index::DATETIME_COL,
                        ]
                    ],
                    'model'            => Comment::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'defaultSort'      => [
                        'created_at' => 'DESC',
                    ],
                    'create' => false
                ],
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Comment::className(),
                    'hasAlias'       => false,
                    'hasGallery'     => false,
                    'languageFields' => [
                    ],
                    'fields'         => [
                        [
                            'name' => 'name',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'email',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'comment',
                            'type' => Form::TEXTAREA,
                        ],
                        [
                            'name' => 'created_at',
                            'type' => Form::STRING,
                        ],
                
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
    
        public function findModel($id)
        {
            
            $model = Comment::find()
                             ->where([ 'id' => $id ])
                             ->one();
            if ($model !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    
        public function deleteModel($id)
        {
            $category = Comment::find()
                                ->where(
                                    [
                                        'id' => $id,
                                    ]
                                )
                                ->one();
            
        
            return $category->delete();
        }
    
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            $services = Service::find()->all();
            $data = ArrayHelper::map($services, 'id', 'title');
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect('index');
            } else {
                return $this->render(
                    'update',
                    [
                        'model' => $model,
                        'services' => $data
                    ]
                );
            }
        }
    }