Feedback.php 5.16 KB
<?php

namespace common\models;

/**
 * User: timur
 * Date: 31.01.18
 * Time: 10:56
 */

use artbox\core\models\Feedback as ArtboxFeedback;

/**
 * Class Feedback
 *
 * @property string $topic
 * @property string $calc_json_info
 * @package common\models
 */
class Feedback extends ArtboxFeedback
{

    const SCENARIO_CALCULATOR = 'calculator';
    const calculator_attributes = [
        'adress',
        'module_install_angle',
        'latitude',
        'longitude',
        'south_deviation',
        'power_station_type',
        'area',
        'power',
        'budget',
        'auth_day',
        'auth_month',
        'auth_pwr_all',
        'auth_pwr_days',
    ];

    const translate_attributes = [
        'adress'               => "Адрес",
        'module_install_angle' => "Угол установки фотомодулей",
        'latitude'             => "Широта",
        'longitude'            => "Долгота",
        'south_deviation'      => "Отклонение от юга",
        'power_station_type'   => "Тип станции",
        'area'                 => "Площадь",
        'power'                => "Мощность",
        'budget'               => "Бюджет",
        'auth_day'             => "Суточное потребление в кВт*ч",
        'auth_month'           => "Месячное потребление в кВт*ч",
        'auth_pwr_all'         => "Мощность всех потребителей потребление в кВт*ч",
        'auth_pwr_days'        => "Суток автономности",
    ];

    public $attributeValues = [];

    public function __set($name, $value)
    {
        if (in_array($name, self::calculator_attributes)) {
            if (isset($value) && !empty($value)) {
                $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(
            parent::scenarios(),
            [
                parent::SCENARIO_FEEDBACK => [
                    'name',
                    'email',
                    'phone',
                    'message',
                    'returnUrl',
                    'topic',
                ],
                parent::SCENARIO_CALLBACK => [
                    'name',
                    'phone',
                    'message',
                    'returnUrl',
                    'topic',
                ],
                self::SCENARIO_CALCULATOR => array_merge(
                    [
                        'name',
                        'phone',
                        'email',
                        'returnUrl',
                        'topic',
                        'calc_json_info',
                    ],
                    self::calculator_attributes
                ),
            ]
        );
    }

    public function rules()
    {

        return array_merge(
            parent::rules(),
            [
                [
                    [
                        'topic',
                    ],
                    'required',
                ],
                [
                    [
                        'topic',
                    ],
                    'string',
                    'max' => 100,
                ],
                [
                    [
                        'name',
                        'phone',
                        'email',
                        'calc_json_info',

                        'adress',
                        'module_install_angle',
                    ],
                    'required',
                    'on' => self::SCENARIO_CALCULATOR,
                ],
             
                [['phone'],'string','length'=>[10,17],'message'=>'Введите данные в форму согласно заданному шаблону: +38(132) 456 78 89'],
                //[['phone'],'match','pattern'=>'/\+3?8?\(?([\d]{3}([ .-]?))\)?([ .-]?)([\d]{3}([ .-]?))\2([\d]{2}([ .-]?))([\d]{2}([ .-]?))/','message'=>'некорректный ввод поля. Значение должно иметь вид: +38(123) 456 78 79'],
                
                [['name'],'string','min'=>2,'message'=>\Yii::t('app','lowName')],
                [['name'],'string','max'=>25,'message'=>\Yii::t('app','upName')],
                [['email'],'string','max'=>25,'message'=>\Yii::t('app','upEmail')],
                [['name'],'match','pattern' => '/^[а-яА-ЯёЁa-zA-Z\-\s]+$/','message'=> \Yii::t('app', 'wrongName')],
                [['email'],'email','message'=>\Yii::t('app','wrongEmail')],

            ]
        );
    }

    public function setCalcJsonInfo()
    {
        $this->calc_json_info = json_encode($this->attributeValues);
    }

    public function getCalcJsonInfo()
    {
        return json_decode($this->calc_json_info);
    }
}