UserData.php 4.57 KB
<?php
    
    namespace artbox\core\models;
    
    use Yii;
    use yii\behaviors\AttributeBehavior;
    use yii\behaviors\BlameableBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveRecord;

    /**
     * This is the model class for table "user_data".
     *
     * @property integer $user_id
     * @property string  $name
     * @property string  $surname
     * @property string  $image
     * @property string  $email
     * @property string  $phone
     * @property integer $created_at
     * @property integer $updated_at
     * @property integer $visited_at
     * @property string  $fullname
     * @property User    $user
     */
    class UserData extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function primaryKey()
        {
            return [
                'user_id',
            ];
        }
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'user_data';
        }
    
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                TimestampBehavior::className(),
                'blameable' => [
                    'class'              => BlameableBehavior::className(),
                    'createdByAttribute' => 'user_id',
                    'updatedByAttribute' => false,
                ],
                [
                    'class'      => AttributeBehavior::className(),
                    'attributes' => [
                        ActiveRecord::EVENT_AFTER_FIND => 'visited_at',
                    ],
                    'value'      => function ($event) {
                        return time();
                    },
                ],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'name',
                        'surname',
                        'image',
                        'email',
                        'phone',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'email' ],
                    'email',
                ],
                [
                    [ 'phone' ],
                    'match',
                    'pattern' => '/^\+\d{1,3}\(\d+\)\d+-\d+-\d+$/',
                ],
                [
                    [ 'user_id' ],
                    'unique',
                ],
                [
                    [ 'user_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => User::className(),
                    'targetAttribute' => [ 'user_id' => 'id' ],
                ],
                [
                    [ 'image' ],
                    'filter',
                    'filter' => function ($value) {
                        if (!$value) {
                            return null;
                        } else {
                            return $value;
                        }
                    },
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'user_id'    => Yii::t('core', 'User ID'),
                'name'       => Yii::t('core', 'Name'),
                'surname'    => Yii::t('core', 'Surname'),
                'image'      => Yii::t('core', 'Image'),
                'email'      => Yii::t('core', 'Email'),
                'phone'      => Yii::t('core', 'Phone'),
                'created_at' => Yii::t('core', 'Created At'),
                'updated_at' => Yii::t('core', 'Updated At'),
                'visited_at' => Yii::t('core', 'Visited At'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getUser()
        {
            return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
        }
    
        /**
         * Get fullname
         *
         * @return string
         */
        public function getFullname()
        {
            if (empty( $this->name ) && empty( $this->surname )) {
                return \Yii::t('core', 'Anonymous');
            } else {
                return implode(
                    ' ',
                    [
                        $this->name ? : \Yii::t('core', 'Mr.'),
                        $this->surname,
                    ]
                );
            }
        }
    }