Project.php 11.8 KB
<?php

    namespace common\models;

    use common\behaviors\MapsBehavior;
    use common\modules\comment\models\CommentProject;
    use common\modules\fileloader\behaviors\FileloaderBehavior;
    use Yii;
    use yii\behaviors\BlameableBehavior;
    use yii\behaviors\TimestampBehavior;
    use yii\db\Expression;

    /**
     * This is the model class for table "project".
     * @property integer                 $project_id
     * @property integer                 $user_id
     * @property string                  $name
     * @property string                  $link
     * @property integer                 $project_pid
     * @property string                  $date_add
     * @property string                  $date_end
     * @property integer                 $user_add_id
     * @property double                  $view_count
     * @property string                  $budget
     * @property string                  $city
     * @property string                  $street
     * @property string                  $house
     * @property integer                 $payment_variant
     * @property integer                 $deadline
     * @property string                  $description
     * @property integer                 $contractual
     * @property ProjectPayment[]        $projectPayments
     * @property ProjectSpecialization[] $projectSpecializations
     * @property Specialization[]        $specializations
     * @property Currency                $budgetCurrency
     * @property Project                 $parent
     * @property int                     $hidden
     * @property int[]                   $fileloader
     * @method File[] getFileloaderFiles()
     */
    class Project extends \yii\db\ActiveRecord
    {

        public $files;

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

        /**
         * @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,
                ],
                'fileloader' => [
                    'class' => FileloaderBehavior::className(),
                ],
                'maps'       => [
                    'class'               => MapsBehavior::className(),
                    'location_attributes' => [
                        'city',
                        'street',
                        'house',
                    ],
                    'required_attributes' => [
                        'city',
                    ],
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'name',
                        'link',
                    ],
                    'required',
                ],
                [
                    [
                        'project_pid',
                        'payment_variant',
                        'contractual',
                        'budget_currency',
                    ],
                    'integer',
                ],
                [
                    [ 'description' ],
                    'string',
                ],
                [
                    [
                        'specializationInput',
                        'paymentInput',
                        'files',
                    ],
                    'safe',
                ],
                [
                    [
                        'name',
                        'link',
                        'budget',
                        'city',
                        'street',
                        'house',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'view_count' ],
                    'default',
                    'value' => 0,
                ],
                [
                    [
                        'paymentInput',
                        'specializationInput',
                    ],
                    'default',
                    'value' => [ ],
                ],
                [
                    [ 'deadline' ],
                    'integer',
                    'min' => 1,
                ],
                [
                    [
                        'deadline',
                    ],
                    'default',
                    'value' => 1,
                ],
                [
                    [ 'date_end' ],
                    'default',
                    'value' => date('Y-m-d H:i:s'),
                ],
                [
                    [ 'hidden' ],
                    'boolean',
                ],
                [
                    [
                        'hidden',
                        'budget',
                        'total_budget',
                    ],
                    'default',
                    'value' => 0,
                ],
                [
                    [ 'date_end' ],
                    'filter',
                    'filter' => function($value) {
                        $unix = strtotime($value);
                        if($unix <= time()) {
                            $unix = time() + ( 3600 * 24 * 7 );
                        }
                        return date('Y-m-d', $unix);
                    },
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'project_id'           => Yii::t('app', 'project_id'),
                'user_id'              => Yii::t('app', 'user_id'),
                'name'                 => Yii::t('app', 'name'),
                'link'                 => Yii::t('app', 'link'),
                'project_pid'          => Yii::t('app', 'project_pid'),
                'date_add'             => Yii::t('app', 'date_add'),
                'date_end'             => Yii::t('app', 'date_end'),
                'user_add_id'          => Yii::t('app', 'user_add_id'),
                'view_count'           => Yii::t('app', 'view_count'),
                'budget'               => Yii::t('app', 'budget'),
                'city'                 => Yii::t('app', 'city'),
                'street'               => Yii::t('app', 'street'),
                'house'                => Yii::t('app', 'house'),
                'payment_variant'      => Yii::t('app', 'payment_variant'),
                'deadline'             => Yii::t('app', 'deadline'),
                'description'          => Yii::t('app', 'description'),
                'contractual'          => Yii::t('app', 'contractual'),
                'file'                 => Yii::t('app', 'file'),
                'specializationInput'  => Yii::t('app', 'specializationInput'),
                'paymentInput'         => Yii::t('app', 'paymentInput'),
                'specializationString' => Yii::t('app', 'specializationString'),
                'hidden'               => Yii::t('app', 'hidden_project'),
            ];
        }

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

        public function getPayments()
        {
            return $this->hasMany(Payment::className(), [ 'payment_id' => 'payment_id' ])
                        ->viaTable('project_payment', [ 'project_id' => 'project_id' ]);
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getUser()
        {
            return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getParent()
        {
            return $this->hasOne(self::className(), [ 'project_id' => 'project_pid' ]);
        }

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

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

        public function getSpecializations()
        {
            return $this->hasMany(Specialization::className(), [ 'specialization_id' => 'specialization_id' ])
                        ->viaTable('project_specialization', [ 'project_id' => 'project_id' ]);
        }

        public function getSpecializationInput()
        {
            return $this->getSpecializations()
                        ->asArray()
                        ->indexBy('specialization_id')
                        ->column();
        }

        public function setSpecializationInput($value)
        {
            $this->specializationInput = $value;
        }

        public function getPaymentInput()
        {
            return $this->getPayments()
                        ->asArray()
                        ->column();
        }

        public function setPaymentInput($value)
        {
            $this->paymentInput = $value;
        }

        public function getSpecializationString()
        {
            return implode(', ', $this->getSpecializations()
                                      ->select([
                                          'specialization_name',
                                          'specialization_id',
                                      ])
                                      ->asArray()
                                      ->indexBy('specialization_id')
                                      ->column());
        }

        public function setSpecializationString($value)
        {
            $this->specializationString = $value;
        }

        /**
         * @return File[]
         */
        public function getFilesList()
        {
            $files = json_decode($this->files);
            if(!empty( $files )) {
                return File::findAll($files);
            } else {
                return [ ];
            }
        }

        public function getIsBookmarked()
        {
            $type = Bookmark::TYPE_PROJECT;
            if(!empty( \Yii::$app->user->identity )) {
                if(!empty( $this->hasOne(Bookmark::className(), [ 'model_id' => 'project_id' ])
                                ->andWhere([
                                    'model'   => $this->className(),
                                    'user_id' => \Yii::$app->user->getId(),
                                    'type'    => $type,
                                ])
                                ->one() )
                ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        public function getComments()
        {
            return $this->hasMany(CommentProject::className(), [ 'model_id' => 'project_id' ])
                        ->andWhere([ 'model' => $this->className() ]);
        }

    }