VisitController.php 6.03 KB
<?php
    
    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\interfaces\ControllerInterface;
    use artbox\core\admin\widgets\Form;
    use common\models\Feedback;
    use common\models\Visit;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    
    /**
     * FeedbackController implements the CRUD actions for Feedback model.
     */
    class VisitController 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'          => [
                        'name'       => [
                            'type' => Index::ACTION_COL,
                        ],
                        'email'      => [
                            'type' => Index::STRING_COL
                        ],
                        'phone'      => [
                            'type' => Index::STRING_COL
                        ],
                        'status'     => [
                            'type' => Index::STATUS_COL,
                        ],
                        'created_at' => [
                            'type' => Index::DATETIME_COL,
                        ]
                    ],
                    'model'            => Visit::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'defaultSort'      => [
                        'created_at' => 'DESC',
                    ],
                    'create' =>         false,
                ],
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Visit::className(),
                    'hasAlias'       => false,
                    'hasGallery'     => false,
                   
                    'languageFields' => [
                    ],
                    'fields'         => [
                        [
                            'name' => 'name',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'email',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'phone',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'message',
                            'type' => Form::TEXTAREA,
                        ],
                        [
                            'name' => 'created_at',
                            'type' => Form::STRING,
                        ],
                        
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
        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 = Visit::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 Visit();
        }
    
        public function deleteModel($id)
        {
            $category = Visit::find()
                                ->where(
                                    [
                                        'id' => $id,
                                    ]
                                )
                                ->one();
        
            return $category->delete();
        }
    
        protected static function fieldsConfig()
        {
            return [
                'model'          => Visit::className(),
                'hasAlias'       => false,
                'hasGallery'     => false,
                'languageFields' => [
                ],
                [
                    'name' => 'name',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'email',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'phone',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'message',
                    'type' => Form::TEXTAREA,
                ],
                [
                    'name' => 'updated_at',
                    'type' => Form::STRING,
                ],
            ];
        }
    
    
    }