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
|
51 |
|
public static function tableName() |
42
|
|
|
{ |
43
|
51 |
|
return '{{%organization_limit}}'; |
44
|
|
|
} |
45
|
|
|
|
46
|
51 |
|
protected function getLimitRules() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
51 |
|
[$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 10] |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
51 |
|
public function rules() |
54
|
|
|
{ |
55
|
51 |
|
return array_merge(parent::rules(), $this->getLimitRules()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function attributeLabels() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
$this->guidAttribute => Yii::t('user', 'GUID'), |
62
|
|
|
$this->createdByAttribute => Yii::t('user', 'User GUID'), |
63
|
|
|
$this->contentAttribute => Yii::t('organization', 'Limit'), |
64
|
|
|
$this->ipAttribute => Yii::t('user', 'IP Address'), |
65
|
|
|
$this->ipTypeAttribute => Yii::t('user', 'IP Address Type'), |
66
|
|
|
$this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
67
|
|
|
$this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the upper limit of organizations the user could set up. |
73
|
|
|
* @param User|integer|string $user |
74
|
|
|
* @return int|boolean False if no limit. |
75
|
|
|
*/ |
76
|
51 |
|
public static function getLimit($user) |
77
|
|
|
{ |
78
|
51 |
|
if (!($user instanceof User) || ($user->getIsNewRecord() && $user = $user->getGUID())) { |
79
|
|
|
$noInit = static::buildNoInitModel(); |
80
|
|
|
$class = $noInit->hostClass; |
81
|
|
|
$user = $class::find()->guidOrId($user)->one(); |
82
|
|
|
} |
83
|
|
|
/* @var $user User */ |
84
|
51 |
|
if (empty($user->organizationLimitClass)) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
51 |
|
$limit = static::find()->createdBy($user)->one(); |
88
|
|
|
/* @var $limit static */ |
89
|
51 |
|
if (!$limit) { |
90
|
51 |
|
$limit = $user->create(static::class); |
91
|
51 |
|
$limit->save(); |
92
|
|
|
} |
93
|
51 |
|
return $limit->limit; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|