Feedback.php 4.95 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,
                    ],
                ]
            );
        }
        
        public function setCalcJsonInfo()
        {
            $this->calc_json_info = json_encode($this->attributeValues);
        }
        
        public function getCalcJsonInfo()
        {
            return json_decode($this->calc_json_info);
        }
    }