Feedback.php 4.64 KB
<?php
    
    namespace artbox\core\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';
        const SCENARIO_CONTACT = 'contact';
        
        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',
                    ],
                    self::SCENARIO_CALLBACK => [
                        'name',
                        'phone',
                        'message',
                    ],
                    self::SCENARIO_CONTACT => [
                        'name',
                        'phone',
                        'email',
                        'message',
                    ],
                ]
            );
            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 [
                [
                    [
                        'phone',
                        'name',
                        'email',
                    ],
                    'required',
                ],
                [
                    [ 'email' ],
                    'email',
                ],
                //                [
                //                    [ 'phone' ],
                //                    'match',
                //                    'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
                //                ],
                [
                    [
                        'name',
                        'phone',
                        'email',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'message',
                    ],
                    'string',
                ],
                [
                    [
                        'status',
                    ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'         => Yii::t('core', 'id'),
                'name'       => Yii::t('app', 'name'),
                'phone'      => Yii::t('app', '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'),
            ];
        }
    }