RequestController.php 4.51 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
    {
        const LANGUAGES = [
            1 => 'en',
            2 => 'ru',
            3 => 'ua',
            4 => 'fr',
        ];
        
        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);
                    }
                    
                    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(Customer $model)
        {
            $subject = "CONFIRM__#" . $model->id . ': ' . $model->name;
            
            \Yii::$app->language = self::LANGUAGES[ intval($model->language_id) ];
            
            \Yii::$app->mailer->compose(
                'thanks_' . $model->language_id,
                [
                    'phone' => '+380732590821',
                    'email' => 'energyforum@euromediacompany.com',
                    'model' => $model,
                ]
            )
                              ->setFrom(
                                  [
                                      'artboxcore@gmail.com' => 'NINTH INTERNATIONAL FORUM',
                                  ]
                              )
                              ->setTo(
                                  $model->email
                              )
                              ->setSubject($subject)
                              ->send();
        }
    }