diff --git a/common/models/Feedback.php b/common/models/Feedback.php index b30ee2a..af2c59e 100644 --- a/common/models/Feedback.php +++ b/common/models/Feedback.php @@ -13,6 +13,7 @@ * Class Feedback * * @property string $topic + * @property string $calc_json_info * * @package common\models */ @@ -20,7 +21,39 @@ { const SCENARIO_CALCULATOR = 'calculator'; + const calculator_attributes = [ + 'adress', 'module_install_angle', 'latitude', 'longitude', + 'south_deviation', 'power_station_type', 'area', 'power', + 'budget', 'day_consumption', 'month_consumption', 'consumptors_total_power', + 'autonomous_days' + ]; + private $attributeValues = []; + + public function __set($name, $value) + { + if(in_array($name, self::calculator_attributes)){ + $this->attributeValues[$name] = $value; + } + else{ + parent::__set($name, $value); + } + } + + public function __get($name) + { + if(in_array($name, self::calculator_attributes)){ + return $this->attributeValues[$name]??''; + } + else{ + return parent::__get($name); + } + } + + public function getCalculatorAttributes(){ + return $this->attributeValues; + } + public function scenarios() { return array_merge( @@ -40,14 +73,16 @@ 'returnUrl', 'topic', ], - self::SCENARIO_CALCULATOR => [ - 'name', - 'phone', - 'returnUrl', - 'topic', - 'calc_json_info', - - ] + self::SCENARIO_CALCULATOR => array_merge( + [ + 'name', + 'phone', + 'returnUrl', + 'topic', + 'calc_json_info', + ], + self::calculator_attributes + ) ] ); } @@ -73,7 +108,15 @@ ], [ [ - 'calc_json_info' + 'name', + 'phone', + 'email', + 'returnUrl', + 'topic', + 'calc_json_info', + + 'adress', + 'module_install_angle' ], 'required', 'on' => self::SCENARIO_CALCULATOR, @@ -82,5 +125,13 @@ ); } + public function setCalcJsonInfo( array $value) + { + $this->calc_json_info = json_encode($value); + } + public function getCalcJsonInfo() : array + { + return json_decode($this->calc_json_info); + } } \ No newline at end of file diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 1ab03b7..9b45c48 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -158,11 +158,45 @@ if (empty(Yii::$app->request->post())) { throw new BadRequestHttpException(); } else { - $model = new Feedback(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + + switch (Yii::$app->request->post('topic')){ + case Feedback::SCENARIO_FEEDBACK : + $model = new Feedback(['scenario' => Feedback::SCENARIO_FEEDBACK]); + $view = 'feedback'; + $isLoaded = $model->load(Yii::$app->request->post()); + break; + + case Feedback::SCENARIO_CALLBACK : + $model = new Feedback(['scenario' => Feedback::SCENARIO_CALLBACK]); + $view = 'feedback'; + $isLoaded = $model->load(Yii::$app->request->post()); + break; + + case Feedback::SCENARIO_CALCULATOR: + $model = new Feedback(['scenario' => Feedback::SCENARIO_CALCULATOR]); + $view = 'calculator'; + $isLoaded = $model->load(Yii::$app->request->post()); + $newPost = []; + foreach (Feedback::calculator_attributes as $calculator_attribute_name){ + $calculator_attribute_value = Yii::$app->request->post($calculator_attribute_name); + if( isset($calculator_attribute_value) && !empty($calculator_attribute_value) ){ + $newPost[$calculator_attribute_name] = $calculator_attribute_value; + } + } + $model->setCalcJsonInfo($newPost); + break; + + default: + $model = new Feedback(); + $view = 'feedback'; + $isLoaded = $model->load(Yii::$app->request->post()); + } + + + if ($isLoaded && $model->save()) { $mailer->compose( - 'feedback', + $view, [ 'model' => $model, ] diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index ed9ab6c..ad6cafc 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -245,16 +245,11 @@