Vacancy.php 7.69 KB
<?php

    namespace common\models;

    use Yii;
    use yii\behaviors\BlameableBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveQuery;
    use yii\db\Expression;
    use yii\helpers\ArrayHelper;

    /**
     * This is the model class for table "vacancy".
     * @property integer             $vacancy_id
     * @property integer             $user_id
     * @property string              $name
     * @property string              $link
     * @property string              $date_add
     * @property integer             $user_add_id
     * @property integer             $view_count
     * @property string              $user_name
     * @property string              $city
     * @property string              $description
     * @property string              $phone
     * @property string              $salary
     * @property integer             $salary_currency
     * @property Employment[]        $employments
     * @property VacancyEmployment[] $vacancyEmployments
     */
    class Vacancy extends \yii\db\ActiveRecord
    {

        const STATUS_ACTIVE = 1;
        const STATUS_CLOSED = 3;

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

        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'              => BlameableBehavior::className(),
                    'createdByAttribute' => 'user_id',
                    'updatedByAttribute' => false,
                ],
                [
                    'class'              => TimestampBehavior::className(),
                    'createdAtAttribute' => 'date_add',
                    'updatedAtAttribute' => false,
                    'value'              => new Expression('NOW()'),
                ],
                'slug' => [
                    'class'         => 'common\behaviors\Slug',
                    'in_attribute'  => 'name',
                    'out_attribute' => 'link',
                    'translit'      => true,
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'name',
                        'description',
                        'city',
                        'link',
                    ],
                    'required',
                ],
                [
                    [ 'description' ],
                    'string',
                ],
                [
                    [
                        'employmentInput',
                        'specializationInput',
                    ],
                    'safe',
                ],
                [
                    [
                        'salary_currency',
                        'status',
                    ],
                    'integer',
                ],
                [
                    [ 'salary' ],
                    'integer',
                    'min' => 0,
                ],
                [
                    [
                        'employmentInput',
                        'specializationInput',
                    ],
                    'default',
                    'value' => [ ],
                ],
                [
                    [ 'view_count' ],
                    'default',
                    'value' => 0,
                ],
                [
                    [ 'status' ],
                    'default',
                    'value' => 1,
                ],
                [
                    [
                        'name',
                        'link',
                        'user_name',
                        'city',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'phone' ],
                    'match',
                    'pattern' => '/^\+?(?:\d{0,3})?[\(\s]?\d{0,5}[\)\s]?\d{3}[-\s]?\d{2}[-\s]?\d{2}$/',
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'vacancy_id'      => Yii::t('app', 'Vacancy ID'),
                'user_id'         => Yii::t('app', 'User ID'),
                'name'            => Yii::t('app', 'Название'),
                'link'            => Yii::t('app', 'URL'),
                'date_add'        => Yii::t('app', 'Дата добавления'),
                'user_add_id'     => Yii::t('app', 'User Add ID'),
                'view_count'      => Yii::t('app', 'Количество просмотров'),
                'user_name'       => Yii::t('app', 'Контактное лицо'),
                'city'            => Yii::t('app', 'Город'),
                'description'     => Yii::t('app', 'Описание'),
                'employmentInput' => Yii::t('app', 'Вид занятости'),
                'phone'           => Yii::t('app', 'Телефон'),
                'salary'          => Yii::t('app', 'Заработная плата'),
                'salary_currency' => Yii::t('app', 'Валюта'),
            ];
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getVacancyEmployments()
        {
            return $this->hasMany(VacancyEmployment::className(), [ 'vacancy_id' => 'vacancy_id' ]);
        }

        public function getEmployments()
        {
            return $this->hasMany(Employment::className(), [ 'employment_id' => 'employment_id' ])
                        ->viaTable('vacancy_employment', [ 'vacancy_id' => 'vacancy_id' ]);
        }

        public function getEmploymentInput()
        {
            return $this->getEmployments()
                        ->asArray()
                        ->column();
        }

        public function setEmploymentInput($value)
        {
            $this->employmentInput = $value;
        }

        public function getRequirements()
        {
            return Fields::getData($this->vacancy_id, Vacancy::className(), 'requirements');
        }

        public function getVacancySpecializations()
        {
            return $this->hasMany(VacancySpecialization::className(), [ 'specialization_id' => 'specialization_id' ]);
        }

        /**
         * @return ActiveQuery
         */
        public function getSpecializations()
        {
            return $this->hasMany(Specialization::className(), [ 'specialization_id' => 'specialization_id' ])
                        ->viaTable('vacancy_specialization', [ 'vacancy_id' => 'vacancy_id' ]);
        }

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

        /**
         * Return array of Vacancy's specialization IDs
         * @return integer[]
         */
        public function getSpecializationInput()
        {
            return $this->getSpecializations()
                        ->asArray()
                        ->indexBy('specialization_id')
                        ->column();
        }

        /**
         * Setter which allow to set Vacancy's specializations for further saving to the DB.
         *
         * @param integer[] $value
         */
        public function setSpecializationInput($value)
        {
            $this->specializationInput = $value;
        }

    }