Order.php 4 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 Order 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 => [
                    'product',
                    'name',
                    'email',
                    'message',
                ],
                self::SCENARIO_CALLBACK => [
                    'product',
                    '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 [
            [
                [
                    'product',
                    'phone',
                    'name',
                    'email',
                ],
                'required',
            ],
            [
                [ 'email' ],
                'email',
            ],
            //                [
            //                    [ 'phone' ],
            //                    'match',
            //                    'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
            //                ],
            [
                [
                    'product',
                    'name',
                    'phone',
                    'email',
                ],
                'string',
                'max' => 255,
            ],
            [
                [
                    'message',
                    'returnUrl,',
                ],
                'string',
            ],
            [
                [
                    'status',
                ],
                'boolean',
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'product'    => Yii::t('core', 'product'),
            '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'),
        ];
    }
}