BackendController.php 3.72 KB
Newer Older
semenov committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @author Semenov Alexander <semenov@skeeks.com>
 * @link http://skeeks.com/
 * @copyright 2010 SkeekS (СкикС)
 * @date 15.03.2015
 */
namespace skeeks\modules\cms\form\controllers;
use skeeks\cms\base\Controller;
1  
semenov committed
10
use skeeks\modules\cms\form\models\Form;
semenov committed
11
use skeeks\modules\cms\form\models\FormField;
1  
semenov committed
12
use skeeks\modules\cms\form\models\FormSendMessage;
1  
semenov committed
13 14 15 16 17
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\Response;
use yii\widgets\ActiveForm;

semenov committed
18 19 20 21 22 23
/**
 * Class BackendController
 * @package skeeks\modules\cms\form\controllers
 */
class BackendController extends Controller
{
1  
semenov committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    /**
     * @return array
     */
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [

            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'validate' => ['post'],
                    'submit' => ['post'],
                ],
            ],
        ]);
    }


    /**
     * Процесс отправки формы
     * @return array
     */
semenov committed
46 47
    public function actionSubmit()
    {
1  
semenov committed
48 49 50
        if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax)
        {
            \Yii::$app->response->format = Response::FORMAT_JSON;
semenov committed
51

1  
semenov committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
            $response = [
                'success' => false,
                'message' => 'Произошла ошибка',
            ];

            if ($formId = \Yii::$app->request->post(Form::FROM_PARAM_ID_NAME))
            {
                /**
                 * @var $modelForm Form
                 */
                $modelForm = Form::find()->where(['id' => $formId])->one();

                $model = $modelForm->createValidateModel();

                if ($model->load(\Yii::$app->request->post()) && $model->validate())
                {
1  
semenov committed
68 69 70
                    //Все проверки прошли, формируем модель отправленного сообщения и сохраняем ее
                    $modelFormSendMessage = new FormSendMessage();

semenov committed
71 72
                    $modelFormSendMessage->data_values     = $model->attributeValues();
                    $modelFormSendMessage->data_labels     = $model->attributeLabels();
1  
semenov committed
73 74 75 76 77 78

                    $modelFormSendMessage->page_url = \Yii::$app->request->referrer;
                    $modelFormSendMessage->form_id  = $formId;

                    if ($modelFormSendMessage->save())
                    {
semenov committed
79 80
                        $modelFormSendMessage->notify();

1  
semenov committed
81 82 83 84 85 86
                        $response['success'] = true;
                        $response['message'] = 'Успешно отправлена';
                    } else
                    {
                        $response['message'] = 'Не удалось сохранить сообщение в базу';
                    }
1  
semenov committed
87 88 89
                } else
                {
                    $response['message'] = 'Форма заполнена неправильно';
1  
semenov committed
90 91 92 93 94
                }

                return $response;
            }
        }
semenov committed
95
    }
1  
semenov committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    /**
     * Валидация данных с формы
     * @return array
     */
    public function actionValidate()
    {
        if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax)
        {
            if ($formId = \Yii::$app->request->post(Form::FROM_PARAM_ID_NAME))
            {
                /**
                 * @var $modelForm Form
                 */
                $modelForm = Form::find()->where(['id' => $formId])->one();

                $model = $modelForm->createValidateModel();

                $model->load(\Yii::$app->request->post());
                \Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
            }
        }
    }
semenov committed
120
}