UserInfo.php 11.2 KB
<?php

    namespace common\models;

    use Yii;
    use yii\behaviors\AttributeBehavior;
    use yii\db\ActiveRecord;

    /**
     * This is the model class for table "user_info".
     * @property integer $user_id
     * @property integer $view_count
     * @property string  $busy
     * @property string  $date_visit
     * @property string  $experience
     * @property string  $rank
     * @property string  $salary
     * @property string  $job
     * @property string  $location
     * @property string  $soft
     * @property integer $user_info_id
     * @property string  $guarantee
     * @property integer $contract
     * @property integer $estimate
     * @property integer $purchase
     * @property integer $delivery
     * @property double  $prepayment
     * @property string  $about
     * @property integer $type
     * @property string  $geography
     * @property integer $salary_currency
     * @property string  $email
     * @property integer $hide_mail
     */
    class UserInfo extends \yii\db\ActiveRecord
    {

        // Константа обознащающая физическое лицо
        const USER_TYPE_FIZ = 1;

        // Константа обозначающая компанию
        const USER_TYPE_COMPANY = 2;

        // Константа обозначающая, что компания/физ.лицо свободно
        const USER_STATUS_FREE = 1;

        // Константа обозначающая, что компания/физ.лицо занято
        const USER_STATUS_BUSY = 2;

        // Константа обозначающая, что компания/физ.лицо хочет стать членом МФП
        const USER_MEMBER_FALSE = 1;

        // Константа обозначающая, что компания/физ.лицо не хочет стать членом МФП
        const USER_MEMBER_TRUE = 2;

        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'user_info';
        }

        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class' => 'common\behaviors\ShowImage',
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'email',
                        'city',
                    ],
                    'required',
                ],
                [
                    [
                        'is_customer',
                        'is_freelancer',
                        'salary_currency',
                    ],
                    'integer',
                ],
                [
                    [
                        'date_visit',
                        'geographies',
                    ],
                    'safe',
                ],
                [
                    [
                        'soft',
                        'about',
                        'city',
                        'country',
                        'image',
                        'poster',
                    ],
                    'string',
                ],
                [
                    [ 'prepayment' ],
                    'number',
                    'min' => 0,
                    'max' => 100,
                ],
                [
                    [ 'experience' ],
                    'integer',
                    'max' => date('Y'),
                    'min' => 1950,
                ],
                [
                    [
                        'rank',
                        'location',
                    ],
                    'string',
                    'max' => 50,
                ],
                [
                    [
                        'job',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'busy',
                        'member',
                    ],
                    'boolean',
                ],
                [
                    [
                        'contract',
                        'estimate',
                        'purchase',
                        'delivery',
                    ],
                    'string',
                    'max' => 50,
                ],
                [
                    [
                        'view_count',
                        'busy',
                        'member',
                        'salary',
                        'guarantee',
                        'prepayment',
                    ],
                    'default',
                    'value' => 0,
                ],
                [
                    [
                        'salary',
                        'guarantee',
                    ],
                    'integer',
                    'min' => 0,
                ],
                [
                    [
                        'social_vk',
                    ],
                    'match',
                    'pattern' => '/^(?:https?:\/\/)?(?:www\.)?vk\.com\/[\S]+\/?$/',
                ],
                [
                    [
                        'social_fb',
                    ],
                    'match',
                    'pattern' => '/^(?:https?:\/\/)?(?:www\.)?facebook\.com\/[\S]+\/?$/',
                ],
                [
                    [
                        'social_in',
                    ],
                    'match',
                    'pattern' => '/^(?:https?:\/\/)?(?:www\.)?linkedin\.com\/(?:pub|in)\/[\S]+\/?$/',
                ],
                [
                    [
                        'social_t',
                    ],
                    'match',
                    'pattern' => '/^(?:https?:\/\/)?(?:www\.)?twitter\.com\/[\S]+\/?$/',
                ],
                [
                    [
                        'geographies',
                    ],
                    'default',
                    'value' => [ ],
                ],
                [
                    [
                        'email',
                    ],
                    'email',
                ],
                [
                    [ 'hide_mail' ],
                    'integer',
                    'min' => 0,
                    'max' => 1,
                ],
                [
                    [ 'hide_mail' ],
                    'default',
                    'value' => 0,
                ],
                [
                    [
                        'social_vk',
                        'social_t',
                        'social_in',
                        'social_fb',
                    ],
                    'filter',
                    'filter' => function($value) {
                        if(empty( $value )) {
                            return $value;
                        }
                        if(!preg_match('/^https?:\/{2}.*$/', $value)) {
                            $value = 'https://' . $value;
                        }
                        return $value;
                    },
                ],
            ];
        }

        public function getBusyText()
        {
            return $this->busy ? 'Занят' : 'Свободный';
        }

        public function getLastVisit()
        {
            return \Yii::$app->formatter->asRelativeTime(new \DateTime($this->date_visit));
        }

        public function getLastVisitCabinet()
        {
            $time = strtotime($this->date_visit);
            $date = date('d.m.Y', $time);
            if($date == date('d.m.Y')) {
                return 'Сегодня';
            } elseif($date == date('d.m.Y', time() - 24 * 3600)) {
                return 'Вчера';
            } elseif($date == date('d.m.Y', time() - 48 * 3600)) {
                return '2 дня назад';
            } else {
                return date('d.m.Y', $time);
            }
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'user_id'         => Yii::t('app', 'user_id'),
                'view_count'      => Yii::t('app', 'view_count'),
                'busy'            => Yii::t('app', 'busy'),
                'date_visit'      => Yii::t('app', 'date_visit'),
                'experience'      => Yii::t('app', 'experience'),
                'rank'            => Yii::t('app', 'rank'),
                'salary'          => Yii::t('app', 'salary'),
                'job'             => Yii::t('app', 'job'),
                'location'        => Yii::t('app', 'location'),
                'soft'            => Yii::t('app', 'soft'),
                'user_info_id'    => Yii::t('app', 'user_info_id'),
                'guarantee'       => Yii::t('app', 'guarantee'),
                'contract'        => Yii::t('app', 'contract'),
                'estimate'        => Yii::t('app', 'estimate'),
                'purchase'        => Yii::t('app', 'purchase'),
                'delivery'        => Yii::t('app', 'delivery'),
                'prepayment'      => Yii::t('app', 'prepayment'),
                'about'           => Yii::t('app', 'about'),
                'type'            => Yii::t('app', 'type'),
                'member'          => Yii::t('app', 'member'),
                'alt_location'    => Yii::t('app', 'alt_location'),
                'country'         => Yii::t('app', 'country'),
                'city'            => Yii::t('app', 'city'),
                'image'           => Yii::t('app', 'image_ui'),
                'poster'          => Yii::t('app', 'poster'),
                'social_vk'       => Yii::t('app', 'social_vk'),
                'social_fb'       => Yii::t('app', 'social_fb'),
                'social_in'       => Yii::t('app', 'social_in'),
                'social_t'        => Yii::t('app', 'social_t'),
                'geography'       => Yii::t('app', 'geography'),
                'geographies'     => Yii::t('app', 'geographies'),
                'salary_currency' => Yii::t('app', 'salary_currency'),
                'is_customer'     => '',
                'is_freelancer'   => '',
                'hide_mail'       => Yii::t('app', 'hide_mail'),

            ];
        }

        public function getUser()
        {
            return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
        }

        public function beforeSave($insert)
        {
            $this->geography = json_encode($this->geographies, JSON_UNESCAPED_UNICODE);
            return parent::beforeSave($insert);
        }

        public function getName()
        {
            return $this->user->name;
        }

        public function getGeographies()
        {
            return json_decode($this->geography);
        }

        public function getGeographyString()
        {
            return implode(',', json_decode($this->geography));
        }

        public function setGeographies($value)
        {
            $this->geographies = $value;
        }

        public function getCurrency()
        {
            return $this->hasOne(Currency::className(), [ 'currency_id' => 'salary_currency' ]);
        }

    }