<?php
    
    namespace common\models;
    
    use Yii;
    use yii\behaviors\AttributeBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "feedback".
     *
     * @property integer $id
     * @property string  $name
     * @property string  $email
     * @property string  $phone
     * @property string  $message
     * @property integer $created_at
     * @property string  $ip
     * @property string  $url
     * @property bool    $status
     */
    class Feedback extends ActiveRecord
    {
        
        const SCENARIO_FEEDBACK = 'feedback';
        const SCENARIO_CALLBACK = 'callback';
    
        
        public $returnUrl;
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'feedback';
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            $scenarios = parent::scenarios();
            $scenarios = array_merge(
                $scenarios,
                [
                    self::SCENARIO_FEEDBACK => [
                        'name',
                        'email',
                        'message',
                        'returnUrl',
                    ],
                    self::SCENARIO_CALLBACK => [
                        '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 [
                [
                    [
                        'name',
                        'email',
                        'phone',
                    ],
                    'required',
                    'on' => self::SCENARIO_DEFAULT,
                ],
                [
                    [
                        'name',
                        'email',
                    ],
                    'required',
                    'on' => self::SCENARIO_FEEDBACK,
                ],
                [
                    [
                        'phone',
                        'name',
                    ],
                    'required',
                    'on' => self::SCENARIO_CALLBACK,
                ],
                [
                    [ 'email' ],
                    'email',
                ],
                //                [
                //                    [ 'phone' ],
                //                    'match',
                //                    'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
                //                ],
                [
                    [
                        'name',
                        'phone',
                        'email',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'message',
                    ],
                    'string',
                ],
                [
                    [
                        'status',
                    ],
                    'boolean',
                ],
                [
                    'returnUrl',
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                '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'),
            ];
        }
    }
