diff --git a/common/models/Order.php b/common/models/Order.php new file mode 100644 index 0000000..a6d00c7 --- /dev/null +++ b/common/models/Order.php @@ -0,0 +1,165 @@ + [ + 'product', + 'name', + 'email', + 'message', + ], + self::SCENARIO_CALLBACK => [ + 'product', + 'name', + 'phone', + 'message', + 'returnUrl', + ], + ] + ); + return $scenarios; + } + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + [ + 'class' => TimestampBehavior::className(), + 'updatedAtAttribute' => false, + ], + [ + 'class' => AttributeBehavior::className(), + 'attributes' => [ + ActiveRecord::EVENT_BEFORE_INSERT => 'ip', + ], + 'value' => function ($event) { + return \Yii::$app->request->userIP; + }, + ], + [ + 'class' => AttributeBehavior::className(), + 'attributes' => [ + ActiveRecord::EVENT_BEFORE_INSERT => 'url', + ], + 'value' => function ($event) { + return \Yii::$app->request->referrer; + }, + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'product', + 'phone', + 'name', + 'email', + ], + 'required', + ], + [ + [ 'email' ], + 'email', + ], + // [ + // [ 'phone' ], + // 'match', + // 'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/', + // ], + [ + [ + 'product', + 'name', + 'phone', + 'email', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'message', + 'returnUrl,', + ], + 'string', + ], + [ + [ + 'status', + ], + 'boolean', + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product' => Yii::t('core', 'product'), + 'id' => Yii::t('core', 'id'), + 'name' => Yii::t('core', 'name'), + 'phone' => Yii::t('core', 'phone'), + 'created_at' => Yii::t('core', 'created_at'), + 'ip' => Yii::t('core', 'ip'), + 'url' => Yii::t('core', 'url'), + 'status' => Yii::t('core', 'status'), + 'message' => Yii::t('core', 'message'), + 'email' => Yii::t('core', 'email'), + ]; + } +} diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 8575678..3d07af4 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -124,4 +124,31 @@ } } } + + public function actionOrder() + { + Yii::$app->response->format = Response::FORMAT_JSON; + if (empty( Yii::$app->request->post() )) { + throw new BadRequestHttpException(); + } else { + $model = new Order(); + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return [ + 'success' => true, + 'message' => 'Success message', + 'alert' => '
+

Success

+

+ Success text +

+
', + ]; + } else { + return [ + 'success' => false, + 'error' => $model->errors, + ]; + } + } + } } diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index fd22fe0..a32ef0c 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -336,56 +336,111 @@ _________________________________________________________ --> _________________________________________________________ -->