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 MemberLimit |
||
19 | * |
||
20 | * @property integer $limit |
||
21 | * |
||
22 | * @package rhosocial\organization |
||
23 | * @version 1.0 |
||
24 | * @author vistart <[email protected]> |
||
25 | */ |
||
26 | class MemberLimit 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 = 100; |
||
39 | |||
40 | 50 | public static function tableName() |
|
41 | { |
||
42 | 50 | return '{{%organization_member_limit}}'; |
|
43 | } |
||
44 | |||
45 | 50 | protected function getLimitRules() |
|
46 | { |
||
47 | return [ |
||
48 | 50 | [$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 100] |
|
49 | ]; |
||
50 | } |
||
51 | |||
52 | 50 | public function rules() |
|
53 | { |
||
54 | 50 | return array_merge(parent::rules(), $this->getLimitRules()); |
|
55 | } |
||
56 | |||
57 | public function attributeLabels() |
||
58 | { |
||
59 | return [ |
||
60 | $this->guidAttribute => Yii::t('user', 'GUID'), |
||
61 | $this->createdByAttribute => Yii::t('organization', 'Organization GUID'), |
||
62 | $this->contentAttribute => Yii::t('organization', 'Limit'), |
||
63 | $this->ipAttribute => Yii::t('user', 'IP Address'), |
||
64 | $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'), |
||
65 | $this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
||
66 | $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
||
67 | ]; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get the upper limit of members. |
||
72 | * @param $organization |
||
73 | * @return int|boolean |
||
74 | */ |
||
75 | 50 | public static function getLimit($organization) |
|
76 | { |
||
77 | 50 | if (!($organization instanceof Organization) || ($organization->getIsNewRecord() && $organization = $organization->getGUID())) { |
|
78 | $noInit = static::buildNoInitModel(); |
||
79 | $class = $noInit->hostClass; |
||
80 | $organization = $class::find()->guidOrId($organization)->one(); |
||
81 | } |
||
82 | /* @var $organization Organization */ |
||
83 | 50 | if (empty($organization->memberLimitClass)) { |
|
84 | return false; |
||
85 | } |
||
86 | 50 | $limit = static::find()->createdBy($organization)->one(); |
|
87 | /* @var $limit static */ |
||
88 | 50 | if (!$limit) { |
|
89 | 50 | $limit = $organization->create(static::class); |
|
90 | 50 | $limit->save(); |
|
91 | } |
||
92 | 50 | return $limit->limit; |
|
93 | } |
||
94 | } |
||
95 |