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
namespace rhosocial\organization;
13
14
use rhosocial\base\models\models\BaseBlameableModel;
15
use Yii;
16
17
/**
18
 * Class SubordinateLimit
19
 *
20
 * @property integer $limit
21
 *
22
 * @package rhosocial\organization
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class SubordinateLimit extends BaseBlameableModel
27
{
28
    public $contentAttribute = 'limit';
29
    public $contentAttributeRule = ['integer', 'min' => 0];
30
    public $createdByAttribute = 'organization_guid';
31
    public $updatedByAttribute = false;
32
    public $idAttribute = false;
33
    /**
34
     * @var string The host who owns this model.
35
     * Note: Please assign it with your own Organization model.
36
     */
37
    public $hostClass = Organization::class;
38
    public $defaultLimit = 50;
39
40 19
    public static function tableName()
41
    {
42 19
        return '{{%organization_subordinate_limit}}';
43
    }
44
45 19
    protected function getLimitRules()
46
    {
47
        return [
48 19
            [$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 50]
49
        ];
50
    }
51
52 19
    public function rules()
53
    {
54 19
        return array_merge(parent::rules(), $this->getLimitRules());
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function attributeLabels()
61
    {
62
        return [
63
            $this->guidAttribute => Yii::t('user', 'GUID'),
64
            $this->createdByAttribute => Yii::t('organization', 'Organization GUID'),
65
            $this->contentAttribute => Yii::t('organization', 'Limit'),
66
            $this->ipAttribute => Yii::t('user', 'IP Address'),
67
            $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'),
68
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
69
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
70
        ];
71
    }
72
73
    /**
74
     * Get the upper limit of subordinates the user could set up.
75
     * @param $organization
76
     * @return int|boolean
77
     */
78 19
    public static function getLimit($organization)
79
    {
80 19
        if (!($organization instanceof Organization) || ($organization->getIsNewRecord() && $organization = $organization->getGUID())) {
81
            $noInit = static::buildNoInitModel();
82
            $class = $noInit->hostClass;
83
            $organization = $class::find()->guidOrId($organization)->one();
84
        }
85
        /* @var $organization Organization */
86 19
        if (empty($organization->subordinateLimitClass)) {
87
            return false;
88
        }
89 19
        $limit = static::find()->createdBy($organization)->one();
90
        /* @var $limit static */
91 19
        if (!$limit) {
92 19
            $limit = $organization->create(static::class);
93 19
            $limit->save();
94
        }
95 19
        return $limit->limit;
96
    }
97
}
98