Commit 1cbe0d9fc52e06d881691c183473ea645e92b5b6

Authored by Eugeny Galkovskiy
1 parent c05d37a5

Для новой формы

common/models/Order.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace artbox\core\models;
  4 +
  5 +use Yii;
  6 +use yii\behaviors\AttributeBehavior;
  7 +use yii\behaviors\TimestampBehavior;
  8 +use yii\db\ActiveRecord;
  9 +
  10 +/**
  11 + * This is the model class for table "feedback".
  12 + *
  13 + * @property integer $id
  14 + * @property string $name
  15 + * @property string $email
  16 + * @property string $phone
  17 + * @property string $message
  18 + * @property integer $created_at
  19 + * @property string $ip
  20 + * @property string $url
  21 + * @property bool $status
  22 + */
  23 +class Order extends ActiveRecord
  24 +{
  25 +
  26 + const SCENARIO_FEEDBACK = 'feedback';
  27 + const SCENARIO_CALLBACK = 'callback';
  28 +
  29 + public $returnUrl;
  30 +
  31 + /**
  32 + * @inheritdoc
  33 + */
  34 + public static function tableName()
  35 + {
  36 + return 'feedback';
  37 + }
  38 +
  39 + /**
  40 + * @inheritdoc
  41 + */
  42 + public function scenarios()
  43 + {
  44 + $scenarios = parent::scenarios();
  45 + $scenarios = array_merge(
  46 + $scenarios,
  47 + [
  48 + self::SCENARIO_FEEDBACK => [
  49 + 'product',
  50 + 'name',
  51 + 'email',
  52 + 'message',
  53 + ],
  54 + self::SCENARIO_CALLBACK => [
  55 + 'product',
  56 + 'name',
  57 + 'phone',
  58 + 'message',
  59 + 'returnUrl',
  60 + ],
  61 + ]
  62 + );
  63 + return $scenarios;
  64 + }
  65 +
  66 + /**
  67 + * @inheritdoc
  68 + */
  69 + public function behaviors()
  70 + {
  71 + return [
  72 + [
  73 + 'class' => TimestampBehavior::className(),
  74 + 'updatedAtAttribute' => false,
  75 + ],
  76 + [
  77 + 'class' => AttributeBehavior::className(),
  78 + 'attributes' => [
  79 + ActiveRecord::EVENT_BEFORE_INSERT => 'ip',
  80 + ],
  81 + 'value' => function ($event) {
  82 + return \Yii::$app->request->userIP;
  83 + },
  84 + ],
  85 + [
  86 + 'class' => AttributeBehavior::className(),
  87 + 'attributes' => [
  88 + ActiveRecord::EVENT_BEFORE_INSERT => 'url',
  89 + ],
  90 + 'value' => function ($event) {
  91 + return \Yii::$app->request->referrer;
  92 + },
  93 + ],
  94 + ];
  95 + }
  96 +
  97 + /**
  98 + * @inheritdoc
  99 + */
  100 + public function rules()
  101 + {
  102 + return [
  103 + [
  104 + [
  105 + 'product',
  106 + 'phone',
  107 + 'name',
  108 + 'email',
  109 + ],
  110 + 'required',
  111 + ],
  112 + [
  113 + [ 'email' ],
  114 + 'email',
  115 + ],
  116 + // [
  117 + // [ 'phone' ],
  118 + // 'match',
  119 + // 'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
  120 + // ],
  121 + [
  122 + [
  123 + 'product',
  124 + 'name',
  125 + 'phone',
  126 + 'email',
  127 + ],
  128 + 'string',
  129 + 'max' => 255,
  130 + ],
  131 + [
  132 + [
  133 + 'message',
  134 + 'returnUrl,',
  135 + ],
  136 + 'string',
  137 + ],
  138 + [
  139 + [
  140 + 'status',
  141 + ],
  142 + 'boolean',
  143 + ],
  144 + ];
  145 + }
  146 +
  147 + /**
  148 + * @inheritdoc
  149 + */
  150 + public function attributeLabels()
  151 + {
  152 + return [
  153 + 'product' => Yii::t('core', 'product'),
  154 + 'id' => Yii::t('core', 'id'),
  155 + 'name' => Yii::t('core', 'name'),
  156 + 'phone' => Yii::t('core', 'phone'),
  157 + 'created_at' => Yii::t('core', 'created_at'),
  158 + 'ip' => Yii::t('core', 'ip'),
  159 + 'url' => Yii::t('core', 'url'),
  160 + 'status' => Yii::t('core', 'status'),
  161 + 'message' => Yii::t('core', 'message'),
  162 + 'email' => Yii::t('core', 'email'),
  163 + ];
  164 + }
  165 +}
... ...
frontend/controllers/SiteController.php
... ... @@ -124,4 +124,31 @@
124 124 }
125 125 }
126 126 }
  127 +
  128 + public function actionOrder()
  129 + {
  130 + Yii::$app->response->format = Response::FORMAT_JSON;
  131 + if (empty( Yii::$app->request->post() )) {
  132 + throw new BadRequestHttpException();
  133 + } else {
  134 + $model = new Order();
  135 + if ($model->load(Yii::$app->request->post()) && $model->save()) {
  136 + return [
  137 + 'success' => true,
  138 + 'message' => 'Success message',
  139 + 'alert' => '<div class="alert alert-success">
  140 + <h3>Success</h3>
  141 + <p>
  142 + Success text
  143 + </p>
  144 + </div>',
  145 + ];
  146 + } else {
  147 + return [
  148 + 'success' => false,
  149 + 'error' => $model->errors,
  150 + ];
  151 + }
  152 + }
  153 + }
127 154 }
... ...
frontend/views/layouts/main.php
... ... @@ -336,56 +336,111 @@ _________________________________________________________ --&gt;
336 336 _________________________________________________________ -->
337 337  
338 338 <div class="modal fade" id="feedback-modal" tabindex="-1" role="dialog" aria-labelledby="Login" aria-hidden="true">
339   - <div class="modal-dialog">
340   -
341   - <div class="modal-content">
342   - <div class="modal-header">
343   - <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>
344   - <h3 class="modal-title" id="Login">Обратная связь</h3>
345   - </div>
346   - <div class="modal-body">
347   -
348   - <?php $form = ActiveForm::begin(
349   - [
350   - 'id' => 'feedback-form',
351   - 'method' => 'POST',
352   - 'action' => '/site/feedback',
353   - ]
354   - ); ?>
355   -
356   - <?= $form->field($feedback, 'name')
357   - ->textInput(); ?>
358   -
359   - <?= $form->field($feedback, 'email')
360   - ->textInput(); ?>
361   -
362   - <?= $form->field($feedback, 'phone')
363   - ->textInput(); ?>
364   -
365   - <?= $form->field($feedback, 'message')
366   - ->textarea(
367   - [
368   - 'rows' => 4,
369   - ]
370   - ); ?>
371   -
372   - <p class="text-center">
373   - <?= Html::submitButton(
374   - 'Отправить',
375   - [
376   - 'class' => 'send-form btn btn-lg btn-template-primary',
377   - ]
378   - ) ?>
379   - </p>
380   -
381   - <?php ActiveForm::end(); ?>
382   -
  339 + <div class="modal-dialog">
  340 +
  341 + <div class="modal-content">
  342 + <div class="modal-header">
  343 + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>
  344 + <h3 class="modal-title" id="Login">Обратная связь</h3>
  345 + </div>
  346 + <div class="modal-body">
  347 +
  348 + <?php $form = ActiveForm::begin(
  349 + [
  350 + 'id' => 'feedback-form',
  351 + 'method' => 'POST',
  352 + 'action' => '/site/feedback',
  353 + ]
  354 + ); ?>
  355 +
  356 + <?= $form->field($feedback, 'name')
  357 + ->textInput(); ?>
  358 +
  359 + <?= $form->field($feedback, 'email')
  360 + ->textInput(); ?>
  361 +
  362 + <?= $form->field($feedback, 'phone')
  363 + ->textInput(); ?>
  364 +
  365 + <?= $form->field($feedback, 'message')
  366 + ->textarea(
  367 + [
  368 + 'rows' => 4,
  369 + ]
  370 + ); ?>
  371 +
  372 + <p class="text-center">
  373 + <?= Html::submitButton(
  374 + 'Отправить',
  375 + [
  376 + 'class' => 'send-form btn btn-lg btn-template-primary',
  377 + ]
  378 + ) ?>
  379 + </p>
  380 +
  381 + <?php ActiveForm::end(); ?>
  382 +
  383 + </div>
  384 + </div>
383 385 </div>
384   - </div>
385 386 </div>
386   - </div>
387   -
  387 +
388 388 <!-- *** FeedBack MODAL END *** -->
  389 +
  390 + <!-- *** *** -->
  391 +
  392 + <div class="modal fade" id="order-modal" tabindex="-1" role="dialog" aria-labelledby="Login" aria-hidden="true">
  393 + <div class="modal-dialog">
  394 +
  395 + <div class="modal-content">
  396 + <div class="modal-header">
  397 + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>
  398 + <h3 class="modal-title" id="Login"></h3>
  399 + </div>
  400 + <div class="modal-body">
  401 +
  402 + <?php $form = ActiveForm::begin(
  403 + [
  404 + 'id' => 'order-form',
  405 + 'method' => 'POST',
  406 + 'action' => '/site/order',
  407 + ]
  408 + ); ?>
  409 +
  410 + <?= $form->field($feedback, 'product')
  411 + ->hiddenInput(); ?>
  412 +
  413 + <?= $form->field($feedback, 'name')
  414 + ->textInput(); ?>
  415 +
  416 + <?= $form->field($feedback, 'email')
  417 + ->textInput(); ?>
  418 +
  419 + <?= $form->field($feedback, 'phone')
  420 + ->textInput(); ?>
  421 +
  422 + <?= $form->field($feedback, 'message')
  423 + ->textarea(
  424 + [
  425 + 'rows' => 4,
  426 + ]
  427 + ); ?>
  428 +
  429 + <p class="text-center">
  430 + <?= Html::submitButton(
  431 + 'Отправить',
  432 + [
  433 + 'class' => 'send-form btn btn-lg btn-template-primary',
  434 + ]
  435 + ) ?>
  436 + </p>
  437 +
  438 + <?php ActiveForm::end(); ?>
  439 +
  440 + </div>
  441 + </div>
  442 + </div>
  443 + </div>
389 444  
390 445 <!-- *** FeedBack MODAL ***
391 446 _________________________________________________________ -->
... ...