Commit 1fffbc2c8ff56128a85b3f463ee6db4eb6a63397
conflict
Showing
14 changed files
with
610 additions
and
37 deletions
Show diff stats
common/config/main.php
| @@ -26,6 +26,13 @@ | @@ -26,6 +26,13 @@ | ||
| 26 | ] | 26 | ] |
| 27 | ], | 27 | ], |
| 28 | 'components' => [ | 28 | 'components' => [ |
| 29 | + | ||
| 30 | + 'reCaptcha' => [ | ||
| 31 | + 'name' => 'reCaptcha', | ||
| 32 | + 'class' => 'himiklab\yii2\recaptcha\ReCaptcha', | ||
| 33 | + 'siteKey' => '6LcZcmQUAAAAANF1TashIYAvYtTBCscpTlPe1q6T', | ||
| 34 | + 'secret' => '6LcZcmQUAAAAAFH4Ru4S6QnmB3An3PJWglmV', | ||
| 35 | + ], | ||
| 29 | 'cache' => [ | 36 | 'cache' => [ |
| 30 | 'class' => 'yii\caching\FileCache', | 37 | 'class' => 'yii\caching\FileCache', |
| 31 | ], | 38 | ], |
console/migrations/m180709_111256_create_partner_table.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use yii\db\Migration; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * Handles the creation of table `partner`. | ||
| 7 | + */ | ||
| 8 | +class m180709_111256_create_partner_table extends Migration | ||
| 9 | +{ | ||
| 10 | + /** | ||
| 11 | + * {@inheritdoc} | ||
| 12 | + */ | ||
| 13 | + public function safeUp() | ||
| 14 | + { | ||
| 15 | + $this->createTable('partner', [ | ||
| 16 | + 'id' => $this->primaryKey(), | ||
| 17 | + 'image_id' => $this->integer(), | ||
| 18 | + 'status' => $this->boolean(), | ||
| 19 | + 'sort' => $this->integer(), | ||
| 20 | + 'link' => $this->string() | ||
| 21 | + ]); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * {@inheritdoc} | ||
| 26 | + */ | ||
| 27 | + public function safeDown() | ||
| 28 | + { | ||
| 29 | + $this->dropTable('partner'); | ||
| 30 | + } | ||
| 31 | +} |
frontend/config/main.php
| @@ -137,7 +137,8 @@ | @@ -137,7 +137,8 @@ | ||
| 137 | }', | 137 | }', |
| 138 | ], | 138 | ], |
| 139 | 'contact' => [ | 139 | 'contact' => [ |
| 140 | - 'class' => 'artbox\core\forms\Module', | 140 | + 'class' => 'frontend\modules\forms\Module', |
| 141 | + #'class' => 'artbox\core\forms\Module', | ||
| 141 | 'activeRecord' => "frontend\models\Feedback", | 142 | 'activeRecord' => "frontend\models\Feedback", |
| 142 | 'templateForm' => '<div class="row">{form}</div>', | 143 | 'templateForm' => '<div class="row">{form}</div>', |
| 143 | 'attributes' => [ | 144 | 'attributes' => [ |
| @@ -145,6 +146,7 @@ | @@ -145,6 +146,7 @@ | ||
| 145 | 'email', | 146 | 'email', |
| 146 | 'phone', | 147 | 'phone', |
| 147 | 'message', | 148 | 'message', |
| 149 | + | ||
| 148 | ], | 150 | ], |
| 149 | 'rules' => [ | 151 | 'rules' => [ |
| 150 | [ | 152 | [ |
| @@ -158,7 +160,8 @@ | @@ -158,7 +160,8 @@ | ||
| 158 | 'phone', | 160 | 'phone', |
| 159 | ], | 161 | ], |
| 160 | 'required', | 162 | 'required', |
| 161 | - ] | 163 | + ], |
| 164 | + | ||
| 162 | ], | 165 | ], |
| 163 | 'labels' => [ | 166 | 'labels' => [ |
| 164 | 'email' => \Yii::t('app','Email'), | 167 | 'email' => \Yii::t('app','Email'), |
frontend/controllers/SiteController.php
| 1 | <?php | 1 | <?php |
| 2 | namespace frontend\controllers; | 2 | namespace frontend\controllers; |
| 3 | 3 | ||
| 4 | - use frontend\models\Feedback; | 4 | + use frontend\models\Feedback as F; |
| 5 | use common\models\blog\Article; | 5 | use common\models\blog\Article; |
| 6 | use common\models\Settings; | 6 | use common\models\Settings; |
| 7 | use common\models\slider\Slide; | 7 | use common\models\slider\Slide; |
| @@ -72,11 +72,11 @@ | @@ -72,11 +72,11 @@ | ||
| 72 | */ | 72 | */ |
| 73 | public function actionContact() | 73 | public function actionContact() |
| 74 | { | 74 | { |
| 75 | - $contact = new Feedback(); | 75 | + $contact = new F(); |
| 76 | return $this->render( | 76 | return $this->render( |
| 77 | 'contact', | 77 | 'contact', |
| 78 | [ | 78 | [ |
| 79 | - 'contact' => $contact, | 79 | + 'model' => $contact, |
| 80 | ] | 80 | ] |
| 81 | ); | 81 | ); |
| 82 | } | 82 | } |
frontend/models/Feedback.php
| @@ -11,9 +11,14 @@ use artbox\core\models\Feedback as ArtboxCoreFeedback; | @@ -11,9 +11,14 @@ use artbox\core\models\Feedback as ArtboxCoreFeedback; | ||
| 11 | 11 | ||
| 12 | class Feedback extends ArtboxCoreFeedback | 12 | class Feedback extends ArtboxCoreFeedback |
| 13 | { | 13 | { |
| 14 | + public $reCaptcha; | ||
| 15 | + | ||
| 16 | + | ||
| 14 | public function rules() | 17 | public function rules() |
| 15 | { | 18 | { |
| 16 | - return [ | 19 | + |
| 20 | + | ||
| 21 | + return [ | ||
| 17 | [ | 22 | [ |
| 18 | [ | 23 | [ |
| 19 | 'name', | 24 | 'name', |
| @@ -24,7 +29,17 @@ class Feedback extends ArtboxCoreFeedback | @@ -24,7 +29,17 @@ class Feedback extends ArtboxCoreFeedback | ||
| 24 | 'on' => self::SCENARIO_DEFAULT, | 29 | 'on' => self::SCENARIO_DEFAULT, |
| 25 | 'message' => \Yii::t('app', 'Empty field'), | 30 | 'message' => \Yii::t('app', 'Empty field'), |
| 26 | ], | 31 | ], |
| 27 | - [ | 32 | + ['reCaptcha', \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), |
| 33 | + 'uncheckedMessage' => 'Please confirm that you are not a bot.', | ||
| 34 | + // add follow lines to prevent checking recaptcha when from has errors | ||
| 35 | + 'when' => function ($model) { | ||
| 36 | + return !$model->hasErrors(); | ||
| 37 | + }, | ||
| 38 | + ], | ||
| 39 | +// [ | ||
| 40 | +// 'reCaptcha','safe' | ||
| 41 | +// ], | ||
| 42 | + [ | ||
| 28 | [ | 43 | [ |
| 29 | 'name', | 44 | 'name', |
| 30 | 'email', | 45 | 'email', |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace frontend\modules\forms; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * Class DynamicModel | ||
| 7 | + * | ||
| 8 | + * @package artbox\core\forms | ||
| 9 | + */ | ||
| 10 | +class DynamicModel extends \yii\base\DynamicModel | ||
| 11 | +{ | ||
| 12 | + /** | ||
| 13 | + * @var mixed | ||
| 14 | + */ | ||
| 15 | + protected $_labels; | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * @param $labels | ||
| 19 | + */ | ||
| 20 | + public function setAttributeLabels($labels) | ||
| 21 | + { | ||
| 22 | + $this->_labels = $labels; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * @param string $name | ||
| 27 | + * | ||
| 28 | + * @return string | ||
| 29 | + */ | ||
| 30 | + public function getAttributeLabel($name) | ||
| 31 | + { | ||
| 32 | + return $this->_labels[$name] ?? $name; | ||
| 33 | + } | ||
| 34 | +} | ||
| 0 | \ No newline at end of file | 35 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace frontend\modules\forms; | ||
| 4 | + | ||
| 5 | +use yii\base\InvalidConfigException; | ||
| 6 | +use yii\helpers\Html; | ||
| 7 | +use yii\web\View; | ||
| 8 | +use yii\widgets\ActiveForm; | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * Module for generation forms adn saving data from frontend | ||
| 13 | + * | ||
| 14 | + * @package artbox\core\forms | ||
| 15 | + */ | ||
| 16 | +class Module extends \yii\base\Module | ||
| 17 | +{ | ||
| 18 | + /** | ||
| 19 | + * ActiveRecord for insert to db | ||
| 20 | + * | ||
| 21 | + * @var null | ||
| 22 | + */ | ||
| 23 | + public $templateForm = '{form}'; | ||
| 24 | + | ||
| 25 | + public $activeRecord = null; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * attributes which render in form | ||
| 29 | + * | ||
| 30 | + * @var array | ||
| 31 | + */ | ||
| 32 | + public $attributes = []; | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * validation rules for DynamicModel | ||
| 36 | + * | ||
| 37 | + * @var array | ||
| 38 | + */ | ||
| 39 | + public $rules = []; | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * scenario for ActiveRecord | ||
| 43 | + * | ||
| 44 | + * @var string | ||
| 45 | + */ | ||
| 46 | + public $scenario = 'default'; | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * Input types for attributes. textinput default | ||
| 50 | + * | ||
| 51 | + * @var array | ||
| 52 | + */ | ||
| 53 | + public $inputOptions = []; | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Labels by form (if ActiveRecord is null) | ||
| 57 | + * | ||
| 58 | + * @var array | ||
| 59 | + */ | ||
| 60 | + public $labels = []; | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Send or not email | ||
| 64 | + * | ||
| 65 | + * @var bool | ||
| 66 | + */ | ||
| 67 | + public $sendEmail = true; | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Email address | ||
| 71 | + * | ||
| 72 | + * @var string | ||
| 73 | + */ | ||
| 74 | + public $email = 'admin@example.com'; | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Email template | ||
| 78 | + * | ||
| 79 | + * @var string | ||
| 80 | + */ | ||
| 81 | + public $emailFile = 'mail.php'; | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Email subject | ||
| 85 | + * | ||
| 86 | + * @var string | ||
| 87 | + */ | ||
| 88 | + public $subject = ''; | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * mailer which send email | ||
| 92 | + * | ||
| 93 | + * @var null | ||
| 94 | + */ | ||
| 95 | + public $mailer = null; | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * @var \yii\db\ActiveRecord $model ; | ||
| 99 | + */ | ||
| 100 | + public $model = null; | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * send form with ajax | ||
| 104 | + * | ||
| 105 | + * @var bool | ||
| 106 | + */ | ||
| 107 | + public $ajax = false; | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * callback function for ajax script of send form | ||
| 111 | + * | ||
| 112 | + * @var string | ||
| 113 | + */ | ||
| 114 | + public $successCallback = 'function (data) { | ||
| 115 | + alert("ok"); | ||
| 116 | + }'; | ||
| 117 | + | ||
| 118 | + /** | ||
| 119 | + * error function for ajax script of send form | ||
| 120 | + * | ||
| 121 | + * @var string | ||
| 122 | + */ | ||
| 123 | + public $errorCallback = 'function (data) { | ||
| 124 | + alert("error"); | ||
| 125 | + }'; | ||
| 126 | + | ||
| 127 | + /** | ||
| 128 | + * id form | ||
| 129 | + * | ||
| 130 | + * @var string | ||
| 131 | + */ | ||
| 132 | + public $formId = 'dynamic-form'; | ||
| 133 | + | ||
| 134 | + /** | ||
| 135 | + * options for submit button | ||
| 136 | + * | ||
| 137 | + * @var array | ||
| 138 | + */ | ||
| 139 | + public $buttonOptions = ['class' => 'btn']; | ||
| 140 | + | ||
| 141 | + /** | ||
| 142 | + * content for submit button | ||
| 143 | + * | ||
| 144 | + * @var string | ||
| 145 | + */ | ||
| 146 | + public $buttonContent = 'Save'; | ||
| 147 | + | ||
| 148 | + /** | ||
| 149 | + * @var string | ||
| 150 | + */ | ||
| 151 | + public $classForm = 'form'; | ||
| 152 | + | ||
| 153 | + public $id = 'forms'; | ||
| 154 | + | ||
| 155 | + public $buttonTemplate = '{button}'; | ||
| 156 | + | ||
| 157 | + public $locale = 'app'; | ||
| 158 | + | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * @inheritdoc | ||
| 162 | + * @throws \yii\base\InvalidConfigException | ||
| 163 | + */ | ||
| 164 | + public function init() | ||
| 165 | + { | ||
| 166 | + parent::init(); | ||
| 167 | + | ||
| 168 | + if ($this->activeRecord !== null) { | ||
| 169 | + $this->model = \Yii::createObject($this->activeRecord); | ||
| 170 | + $this->model->scenario = $this->scenario; | ||
| 171 | + } else { | ||
| 172 | + $this->model = new DynamicModel($this->attributes); | ||
| 173 | + $this->model->attributeLabels = $this->labels; | ||
| 174 | + foreach ($this->rules as $rule) { | ||
| 175 | + if (is_array($rule) && isset($rule[0], $rule[1])) { | ||
| 176 | + $attributes = $rule[0]; | ||
| 177 | + $validator = $rule[1]; | ||
| 178 | + unset($rule[0], $rule[1]); | ||
| 179 | + $this->model->addRule($attributes, $validator, $rule); | ||
| 180 | + } else { | ||
| 181 | + throw new InvalidConfigException( | ||
| 182 | + 'Invalid validation rule: a rule must specify both attribute names and validator type.' | ||
| 183 | + ); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + } | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + /** | ||
| 190 | + * render html form with model | ||
| 191 | + * | ||
| 192 | + * @param \yii\web\View $view | ||
| 193 | + */ | ||
| 194 | + public function renderForm(View $view) | ||
| 195 | + { | ||
| 196 | + if ($this->ajax) { | ||
| 197 | + $js = <<<JS | ||
| 198 | +$(document).on('submit', '#{$this->formId}', function(e) { | ||
| 199 | + e.preventDefault(); | ||
| 200 | + e.stopImmediatePropagation(); | ||
| 201 | + $.post( | ||
| 202 | + $(this).attr("action"), $(this).serialize(), {$this->successCallback}).fail({$this->errorCallback}); | ||
| 203 | +}); | ||
| 204 | +JS; | ||
| 205 | + | ||
| 206 | + $view->registerJs($js, View::POS_READY); | ||
| 207 | + } | ||
| 208 | + /** | ||
| 209 | + * @var ActiveForm $form | ||
| 210 | + */ | ||
| 211 | + | ||
| 212 | + $form = ActiveForm::begin( | ||
| 213 | + [ | ||
| 214 | + 'id' => $this->formId, | ||
| 215 | + 'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax', | ||
| 216 | + 'class' => $this->classForm, | ||
| 217 | + ] | ||
| 218 | + ); | ||
| 219 | + | ||
| 220 | + $content = ''; | ||
| 221 | + foreach ($this->attributes as $field) { | ||
| 222 | + | ||
| 223 | + # если мы присвоили этому полю тип, то он его отрисовывает | ||
| 224 | + if (isset($this->inputOptions[$field]['type'])) { | ||
| 225 | + $function = $this->inputOptions[$field]['type']; | ||
| 226 | + $formStr = $form->field($this->model, $field) | ||
| 227 | + ->$function( | ||
| 228 | + isset($this->inputOptions[$field]['options']) | ||
| 229 | + ? $this->inputOptions[$field]['options'] : [] | ||
| 230 | + ); | ||
| 231 | + } # а если нет, то просто добавляет поле | ||
| 232 | + else { | ||
| 233 | + $formStr = $form->field($this->model, $field); | ||
| 234 | + } | ||
| 235 | + if (isset($this->labels[$field])) { | ||
| 236 | + $formStr->label(\Yii::t($this->locale, $this->labels[$field])); | ||
| 237 | + } | ||
| 238 | + if (isset($this->inputOptions[$field]['template'])) { | ||
| 239 | + | ||
| 240 | + $formStr = str_replace('{input}', $formStr, $this->inputOptions[$field]['template']); | ||
| 241 | + } | ||
| 242 | + $content .= $formStr; | ||
| 243 | + | ||
| 244 | + } | ||
| 245 | + $content .= $form->field($this->model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className())->label(false); | ||
| 246 | + | ||
| 247 | + | ||
| 248 | + $content .= str_replace('{button}', Html::submitButton(\Yii::t($this->locale, $this->buttonContent), $this->buttonOptions), $this->buttonTemplate); | ||
| 249 | + $content = str_replace('{form}', $content, $this->templateForm); | ||
| 250 | + echo $content; | ||
| 251 | + ActiveForm::end(); | ||
| 252 | + } | ||
| 253 | +} | ||
| 0 | \ No newline at end of file | 254 | \ No newline at end of file |
frontend/modules/forms/controllers/SaveController.php
0 → 100755
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace frontend\modules\forms\controllers; | ||
| 4 | + | ||
| 5 | +use artbox\core\forms\DynamicModel; | ||
| 6 | +use yii\web\Controller; | ||
| 7 | +use yii\web\Response; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Class SaveController | ||
| 11 | + * | ||
| 12 | + * @property \artbox\core\forms\Module $module | ||
| 13 | + * @package artbox\core\forms\controllers | ||
| 14 | + */ | ||
| 15 | +class SaveController extends Controller | ||
| 16 | +{ | ||
| 17 | + /** | ||
| 18 | + * Custom data saving via POST | ||
| 19 | + * | ||
| 20 | + * @return bool|\yii\web\Response | ||
| 21 | + * @throws \yii\base\InvalidConfigException | ||
| 22 | + */ | ||
| 23 | + public function actionNoAjax() | ||
| 24 | + { | ||
| 25 | + $returnUrl = \Yii::$app->request->referrer; | ||
| 26 | + $model = $this->module->model; | ||
| 27 | + if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) { | ||
| 28 | + if ($model instanceof DynamicModel) { | ||
| 29 | + $this->sendEmail($model); | ||
| 30 | + } else { | ||
| 31 | + if ($model->save()) { | ||
| 32 | + if ($this->module->sendEmail) { | ||
| 33 | + $this->sendEmail($model); | ||
| 34 | + return $this->redirect($returnUrl); | ||
| 35 | + } | ||
| 36 | + } else { | ||
| 37 | + return false; | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + return $this->redirect($returnUrl); | ||
| 41 | + | ||
| 42 | + } | ||
| 43 | + return $this->redirect($returnUrl); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Action that saves data submited via AJAX | ||
| 48 | + * | ||
| 49 | + * @return array | ||
| 50 | + * @throws \yii\base\InvalidConfigException | ||
| 51 | + */ | ||
| 52 | + public function actionAjax() | ||
| 53 | + { | ||
| 54 | + \Yii::$app->response->format = Response::FORMAT_JSON; | ||
| 55 | + $model = $this->module->model; | ||
| 56 | + if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) { | ||
| 57 | + if ($model instanceof DynamicModel) { | ||
| 58 | + $this->sendEmail($model); | ||
| 59 | + return ['status' => 'success']; | ||
| 60 | + } else { | ||
| 61 | + | ||
| 62 | + if ($model->save(false)) { | ||
| 63 | + if ($this->module->sendEmail) { | ||
| 64 | + $this->sendEmail($model); | ||
| 65 | + return ['status' => 'success']; | ||
| 66 | + } | ||
| 67 | + return ['status' => 'success']; | ||
| 68 | + } else { | ||
| 69 | + return ['status' => 'error','errors'=>$model->errors]; | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + return ['status' => 'error','error' => 'if doesent work']; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * @param $model | ||
| 79 | + * | ||
| 80 | + * @return bool | ||
| 81 | + * @throws \yii\base\InvalidConfigException | ||
| 82 | + */ | ||
| 83 | + public function sendEmail($model) | ||
| 84 | + { | ||
| 85 | + | ||
| 86 | + if ($this->module->mailer == null) { | ||
| 87 | + $mailer = \Yii::$app->mailer; | ||
| 88 | + } else { | ||
| 89 | + $mailer = \Yii::$app->get($this->module->mailer); | ||
| 90 | + } | ||
| 91 | + return $mailer->compose( | ||
| 92 | + [ | ||
| 93 | + 'html' => $this->module->emailFile, | ||
| 94 | + 'text' => $this->module->emailFile, | ||
| 95 | + ], | ||
| 96 | + [$model] | ||
| 97 | + ) | ||
| 98 | + ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) | ||
| 99 | + ->setTo($this->module->email) | ||
| 100 | + ->setSubject($this->module->subject) | ||
| 101 | + ->send(); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | +} | ||
| 0 | \ No newline at end of file | 105 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @var \yii\db\ActiveRecord $model | ||
| 4 | + * @var array $types | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | +use yii\helpers\Html; | ||
| 8 | +use yii\widgets\ActiveForm; | ||
| 9 | + | ||
| 10 | +$form = ActiveForm::begin([ | ||
| 11 | + 'id' => 'dynamic-form', | ||
| 12 | + 'enableAjaxValidation' => false, | ||
| 13 | + 'enableClientValidation' => true, | ||
| 14 | +]); | ||
| 15 | +foreach ($this->context->module->attributes as $field) { | ||
| 16 | + if (isset($types[$field])) { | ||
| 17 | + $function = $types[$field]['type']; | ||
| 18 | + echo $form->field($model, $field) | ||
| 19 | + ->$function( | ||
| 20 | + $types[$field]['options'] | ||
| 21 | + ); | ||
| 22 | + } else { | ||
| 23 | + echo $form->field($model, $field); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +echo Html::submitButton(); | ||
| 29 | + | ||
| 30 | +$form = ActiveForm::end(); |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace frontend\views\forms; | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +use yii\helpers\Html; | ||
| 7 | +use yii\web\View; | ||
| 8 | +use yii\widgets\ActiveForm; | ||
| 9 | +use artbox\core\forms\Module as CoreModule; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Class ContactModule | ||
| 14 | + * @package frontend\forms | ||
| 15 | + * | ||
| 16 | + * | ||
| 17 | + * Класс, который немного видоизменяет вывод формы контактов для того, чтобы в ней появилась гугловая рекапча | ||
| 18 | + * | ||
| 19 | + */ | ||
| 20 | +class ContactModule extends CoreModule | ||
| 21 | +{ | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + public function renderForm(View $view) | ||
| 25 | + { | ||
| 26 | + if ($this->ajax) { | ||
| 27 | + $js = <<<JS | ||
| 28 | +$(document).on('submit', '#{$this->formId}', function(e) { | ||
| 29 | + e.preventDefault(); | ||
| 30 | + e.stopImmediatePropagation(); | ||
| 31 | + $.post( | ||
| 32 | + $(this).attr("action"), $(this).serialize(), {$this->successCallback}).fail({$this->errorCallback}); | ||
| 33 | +}); | ||
| 34 | +JS; | ||
| 35 | + | ||
| 36 | + $view->registerJs($js, View::POS_READY); | ||
| 37 | + } | ||
| 38 | + /** | ||
| 39 | + * @var ActiveForm $form | ||
| 40 | + */ | ||
| 41 | + | ||
| 42 | + $form = ActiveForm::begin( | ||
| 43 | + [ | ||
| 44 | + 'id' => $this->formId, | ||
| 45 | + 'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax', | ||
| 46 | + 'class' => $this->classForm, | ||
| 47 | + ] | ||
| 48 | + ); | ||
| 49 | + $content = ''; | ||
| 50 | + foreach ($this->attributes as $field) { | ||
| 51 | + | ||
| 52 | + # если мы присвоили этому полю тип, то он его отрисовывает | ||
| 53 | + if (isset($this->inputOptions[$field]['type'])) { | ||
| 54 | + $function = $this->inputOptions[$field]['type']; | ||
| 55 | + $formStr = $form->field($this->model, $field) | ||
| 56 | + ->$function( | ||
| 57 | + isset($this->inputOptions[$field]['options']) | ||
| 58 | + ? $this->inputOptions[$field]['options'] : [] | ||
| 59 | + ); | ||
| 60 | + } # а если нет, то просто добавляет поле | ||
| 61 | + else { | ||
| 62 | + $formStr = $form->field($this->model, $field); | ||
| 63 | + } | ||
| 64 | + if (isset($this->labels[$field])) { | ||
| 65 | + $formStr->label(\Yii::t($this->locale, $this->labels[$field])); | ||
| 66 | + } | ||
| 67 | + if (isset($this->inputOptions[$field]['template'])) { | ||
| 68 | + | ||
| 69 | + $formStr = str_replace('{input}', $formStr, $this->inputOptions[$field]['template']); | ||
| 70 | + } | ||
| 71 | + $content .= $formStr; | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + $content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false); | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + $content .= str_replace('{button}', Html::submitButton(\Yii::t($this->locale, $this->buttonContent), $this->buttonOptions), $this->buttonTemplate); | ||
| 79 | + $content = str_replace('{form}', $content, $this->templateForm); | ||
| 80 | + echo $content; | ||
| 81 | + ActiveForm::end(); | ||
| 82 | + } | ||
| 83 | +} | ||
| 0 | \ No newline at end of file | 84 | \ No newline at end of file |
frontend/views/site/contact.php
| @@ -11,6 +11,7 @@ | @@ -11,6 +11,7 @@ | ||
| 11 | use frontend\assets\MapAsset; | 11 | use frontend\assets\MapAsset; |
| 12 | use yii\helpers\Html; | 12 | use yii\helpers\Html; |
| 13 | use yii\web\View; | 13 | use yii\web\View; |
| 14 | +use yii\widgets\ActiveForm; | ||
| 14 | 15 | ||
| 15 | MapAsset::register($this); | 16 | MapAsset::register($this); |
| 16 | $settings = Settings::getInstance(); | 17 | $settings = Settings::getInstance(); |
| @@ -119,7 +120,9 @@ JS; | @@ -119,7 +120,9 @@ JS; | ||
| 119 | </div> | 120 | </div> |
| 120 | 121 | ||
| 121 | <div class="col-md-8 col-md-offset-2"> | 122 | <div class="col-md-8 col-md-offset-2"> |
| 122 | - <?=$module->renderForm($this)?> | 123 | + |
| 124 | + | ||
| 125 | + <?= $module->renderForm($this) ?> | ||
| 123 | 126 | ||
| 124 | </div> | 127 | </div> |
| 125 | </div> | 128 | </div> |
frontend/web/css/site.css
| @@ -133,3 +133,11 @@ a.desc:after { | @@ -133,3 +133,11 @@ a.desc:after { | ||
| 133 | .background-white { | 133 | .background-white { |
| 134 | background-color: white; | 134 | background-color: white; |
| 135 | } | 135 | } |
| 136 | + | ||
| 137 | +/* | ||
| 138 | +Класс для выравнивания по центру формы инпута google ReCaptcha | ||
| 139 | +*/ | ||
| 140 | +.f_center { | ||
| 141 | + margin-left: 80%; | ||
| 142 | + transform: translateX(-50%); | ||
| 143 | +} | ||
| 136 | \ No newline at end of file | 144 | \ No newline at end of file |
frontend/web/js/script.js
frontend/widgets/ParthnersList.php
| 1 | <?php | 1 | <?php |
| 2 | -//====================================================================================================================== | ||
| 3 | -//Виджет, который получает массив данных о партнёрах, и делает html код для карусели | ||
| 4 | - | ||
| 5 | -namespace frontend\widgets; | ||
| 6 | -use yii\base\Widget; | ||
| 7 | -use common\models\Partner; | ||
| 8 | - | ||
| 9 | - | ||
| 10 | -class ParthnersList extends Widget | ||
| 11 | -{ | ||
| 12 | - public $filesList=[]; | ||
| 13 | - public $parthnersLinkList=[]; | ||
| 14 | - public function init() | ||
| 15 | - { | ||
| 16 | - parent::init(); | ||
| 17 | - #$this->filesList=preg_grep('$^customer-\d{1,3}\.(png|jpg|jpeg)$',scandir('../web/img',0)); | ||
| 18 | - $this->filesList=Partner::find() | ||
| 19 | - ->where(['status'=>true]) | ||
| 20 | - ->all(); | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - public function run(){ | ||
| 27 | - parent::run(); | ||
| 28 | - return $this->render('_parthnerList',['data'=>$this->filesList]); | ||
| 29 | - } | ||
| 30 | -} | 2 | + //====================================================================================================================== |
| 3 | + //Виджет, который получает массив данных о партнёрах, и делает html код для карусели | ||
| 4 | + | ||
| 5 | + namespace frontend\widgets; | ||
| 6 | + | ||
| 7 | + use yii\base\Widget; | ||
| 8 | + use common\models\Partner; | ||
| 9 | + | ||
| 10 | + class ParthnersList extends Widget | ||
| 11 | + { | ||
| 12 | + public $filesList = []; | ||
| 13 | + public $parthnersLinkList = []; | ||
| 14 | + public function init() | ||
| 15 | + { | ||
| 16 | + parent::init(); | ||
| 17 | + #$this->filesList=preg_grep('$^customer-\d{1,3}\.(png|jpg|jpeg)$',scandir('../web/img',0)); | ||
| 18 | + $this->filesList = Partner::find() | ||
| 19 | + ->where([ 'status' => true ]) | ||
| 20 | + ->all(); | ||
| 21 | + | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public function run() | ||
| 25 | + { | ||
| 26 | + parent::run(); | ||
| 27 | + return $this->render('_parthnerList', [ 'data' => $this->filesList ]); | ||
| 28 | + } | ||
| 29 | + } | ||
| 31 | 30 |