RequestController.php 3.47 KB
<?php
    
    namespace backend\controllers;
    
    use artbox\core\admin\interfaces\ControllerInterface;
    use backend\actions\Index;
    use common\models\Customer;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    
    class RequestController extends Controller implements ControllerInterface
    {
        public function actions()
        {
            return [
                'index' => [
                    'class'            => Index::class,
                    'columns'          => [
                        'id'   => [
                            'type' => Index::NUMBER_COL,
                        
                        ],
                        'name' => [
                            'type'         => Index::ACTION_COL,
                            'columnConfig' => [
                                #'buttonsTemplate' => '{edit}{delete}',
                                'buttonsTemplate' => '{edit}',
                            ],
                        ],
                        
                        'organization' => [
                            'type' => Index::STRING_COL,
                        ],
                    ],
                    'model'            => Customer::class,
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'create'           => false,
                    'defaultSort'      => [ 'id' => SORT_DESC ],
                ],
            ];
        }
        
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            
            $model->scenario = Customer::ADMIN_SCENARIO;
            
            $request = \Yii::$app->request;
            
            $wasNew = (strval($model->status) === '2');
            
            if ($request->isPost) {
                if ($model->load($request->post()) && $model->save()) {
                    
                    if ($wasNew && (strval($model->status) === '1' )) {
                        $this->mail($model->email);
                    }
                    
                    return $this->redirect([ 'index' ]);
                }
            }
            
            return $this->render(
                'update',
                [
                    'model' => $model,
                ]
            );
        }
        
        /**
         * @param $id
         *
         * @return \common\models\Customer|mixed|null
         * @throws \yii\web\NotFoundHttpException
         */
        public function findModel($id)
        {
            $model = Customer::findOne($id);
            if ($model) {
                return $model;
            } else {
                throw new NotFoundHttpException();
            }
        }
        /**
         * Create new model
         *
         * @return mixed
         */
        public function newModel()
        {
            return new Customer();
        }
        /**
         * @param $id
         *
         * @return bool|mixed
         * @throws \Throwable
         * @throws \yii\db\StaleObjectException
         * @throws \yii\web\NotFoundHttpException
         */
        public function deleteModel($id)
        {
            $model = $this->findModel($id);
            
            if ($model->delete()) {
                return true;
            } else {
                return false;
            }
        }
        
        public function mail($email) {
        
        }
    }