Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use rhosocial\user\User;
17
use Yii;
18
19
/**
20
 * Class OrganizationLimit
21
 *
22
 * @property integer $limit
23
 *
24
 * @package rhosocial\organization
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
class OrganizationLimit extends BaseBlameableModel
29
{
30
    public $contentAttribute = 'limit';
31
    public $contentAttributeRule = ['integer', 'min' => 0];
32
    public $updatedByAttribute = false;
33
    public $idAttribute = false;
34
    /**
35
     * @var string The host who owns this model.
36
     * Note: Please assign it with your own User model.
37
     */
38
    public $hostClass = User::class;
39
    public $defaultLimit = 10;
40
41
    /**
42
     * @return string
43
     */
44 51
    public static function tableName()
45
    {
46 51
        return '{{%organization_limit}}';
47
    }
48
49
    /**
50
     * @return array
51
     */
52 51
    protected function getLimitRules()
53
    {
54
        return [
55 51
            [$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 10]
56
        ];
57
    }
58
59
    /**
60
     * @return array
61
     */
62 51
    public function rules()
63
    {
64 51
        return array_merge(parent::rules(), $this->getLimitRules());
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function attributeLabels()
71
    {
72
        return [
73
            $this->guidAttribute => Yii::t('user', 'GUID'),
74
            $this->createdByAttribute => Yii::t('user', 'User GUID'),
75
            $this->contentAttribute => Yii::t('organization', 'Limit'),
76
            $this->ipAttribute => Yii::t('user', 'IP Address'),
77
            $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'),
78
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
79
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
80
        ];
81
    }
82
83
    /**
84
     * Get the upper limit of organizations the user could set up.
85
     * @param User|integer|string $user
86
     * @return int|boolean False if no limit.
87
     */
88 51
    public static function getLimit($user)
89
    {
90 51
        if (!($user instanceof User) || ($user->getIsNewRecord() && $user = $user->getGUID())) {
91
            $noInit = static::buildNoInitModel();
92
            $class = $noInit->hostClass;
93
            $user = $class::find()->guidOrId($user)->one();
94
        }
95
        /* @var $user User */
96 51
        if (empty($user->organizationLimitClass)) {
97
            return false;
98
        }
99 51
        $limit = $user->getOrganizationLimit()->one();
100
        /* @var $limit static */
101 51
        if (!$limit) {
102 51
            $limit = $user->create(static::class);
103 51
            $limit->save();
104
        }
105 51
        return $limit->limit;
106
    }
107
108
    /**
109
     * Set the upper limit of organizations the user could set up.
110
     * @param User|integer|string $user
111
     * @param int $limit
112
     * @return boolean
113
     */
114
    public static function setLimit($user, $limit = 1)
115
    {
116
        if (!($user instanceof User) || ($user->getIsNewRecord() && $user = $user->getGUID())) {
117
            $noInit = static::buildNoInitModel();
118
            $class = $noInit->hostClass;
119
            $user = $class::find()->guidOrId($user)->one();
120
        }
121
        $model = $user->getOrganizationLimit()->one();
122
        /* @var $model static */
123
        if (!$model) {
124
            $model = $user->create(static::class);
125
        }
126
        $limit = is_numeric($limit) ? (int)$limit : 1;
127
        $model->setContent($limit);
128
        return $model->save();
129
    }
130
}
131