QuestionController.php 5.01 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 22.05.18
     * Time: 16:38
     */
    
    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\Doctor;
    use common\models\Question;
    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 QuestionController 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'            => Question::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'defaultSort'      => [
                        'created_at' => 'DESC',
                    ],
                    'create' => false
                ],
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Question::className(),
                    'hasAlias'       => false,
                    'hasGallery'     => false,
                    'languageFields' => [
                    ],
                    'fields'         => [
                        [
                            'name' => 'name',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'email',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'question',
                            'type' => Form::TEXTAREA,
                        ],
                        [
                            'name' => 'created_at',
                            'type' => Form::STRING,
                        ],
                
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
    
        public function findModel($id)
        {
            $model = Question::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 = Question::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');
    
            $doctors = Doctor::find()->all();
            $dataDoctors = ArrayHelper::map($doctors, 'id', 'name');
            $model->scenario = Question::SCENARIO_ANSWER;
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect('index');
            } else {
                return $this->render(
                    'update',
                    [
                        'model' => $model,
                        'services' => $data,
                        'doctors'  => $dataDoctors
                    ]
                );
            }
        }
    }