1 | <?php |
||
34 | class Organization extends User |
||
35 | { |
||
36 | use SelfBlameableTrait; |
||
37 | |||
38 | const TYPE_ORGANIZATION = 1; |
||
39 | const TYPE_DEPARTMENT = 2; |
||
40 | |||
41 | /** |
||
42 | * @var boolean Organization does not need password and corresponding features. |
||
43 | */ |
||
44 | public $passwordHashAttribute = false; |
||
45 | |||
46 | /** |
||
47 | * @var boolean Organization does not need password and corresponding features. |
||
48 | */ |
||
49 | public $passwordResetTokenAttribute = false; |
||
50 | |||
51 | /** |
||
52 | * @var boolean Organization does not need password and corresponding features. |
||
53 | */ |
||
54 | public $passwordHistoryClass = false; |
||
55 | |||
56 | /** |
||
57 | * @var boolean Organization does not need source. |
||
58 | */ |
||
59 | public $sourceAttribute = false; |
||
60 | |||
61 | /** |
||
62 | * @var boolean Organization does not need auth key. |
||
63 | */ |
||
64 | public $authKeyAttribute = false; |
||
65 | |||
66 | /** |
||
67 | * @var boolean Organization does not need access token. |
||
68 | */ |
||
69 | public $accessTokenAttribute = false; |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @var boolean Organization does not need login log. |
||
74 | */ |
||
75 | public $loginLogClass = false; |
||
76 | |||
77 | public $profileClass = Profile::class; |
||
78 | |||
79 | public $memberClass = Member::class; |
||
80 | private $noInitMember; |
||
81 | /** |
||
82 | * @return Member |
||
83 | */ |
||
84 | 5 | protected function getNoInitMember() |
|
85 | { |
||
86 | 5 | if (!$this->noInitMember) { |
|
87 | 5 | $class = $this->memberClass; |
|
88 | 5 | $this->noInitMember = $class::buildNoInitModel(); |
|
89 | } |
||
90 | 5 | return $this->noInitMember; |
|
91 | } |
||
92 | |||
93 | 18 | public function init() |
|
94 | { |
||
95 | 18 | $this->parentAttribute = 'parent_guid'; |
|
96 | 18 | if (class_exists($this->memberClass)) { |
|
97 | 18 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
98 | } |
||
99 | 18 | if ($this->skipInit) { |
|
100 | 18 | return; |
|
101 | } |
||
102 | 18 | parent::init(); |
|
103 | 18 | } |
|
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | public function attributeLabels() |
||
109 | { |
||
110 | return [ |
||
111 | 'guid' => Yii::t('app', 'GUID'), |
||
112 | 'id' => Yii::t('app', 'ID'), |
||
113 | 'ip' => Yii::t('app', 'IP'), |
||
114 | 'ip_type' => Yii::t('app', 'IP Address Type'), |
||
115 | 'parent' => Yii::t('app', 'Parent'), |
||
116 | 'created_at' => Yii::t('app', 'Create Time'), |
||
117 | 'updated_at' => Yii::t('app', 'Update Time'), |
||
118 | 'status' => Yii::t('app', 'Status'), |
||
119 | 'type' => Yii::t('app', 'Type'), |
||
120 | ]; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | 18 | public static function tableName() |
|
130 | |||
131 | 17 | protected function getTypeRules() |
|
132 | { |
||
133 | return [ |
||
134 | 17 | ['type', 'default', 'value' => static::TYPE_ORGANIZATION], |
|
135 | ['type', 'required'], |
||
136 | 17 | ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]], |
|
137 | ]; |
||
138 | } |
||
139 | |||
140 | 17 | public function rules() |
|
144 | |||
145 | /** |
||
146 | * Get Member Query. |
||
147 | * @return MemberQuery |
||
148 | */ |
||
149 | 5 | public function getMembers() |
|
153 | |||
154 | /** |
||
155 | * Get organization member users' query. |
||
156 | * @return BaseUserQuery |
||
157 | */ |
||
158 | 2 | public function getMemberUsers() |
|
165 | |||
166 | /** |
||
167 | * Get member with specified user. |
||
168 | * @param User|string|integer $user |
||
169 | * @return Member Null if `user` is not in this organization. |
||
170 | */ |
||
171 | 3 | public function getMember($user) |
|
175 | |||
176 | /** |
||
177 | * Add member to organization. |
||
178 | * @param Member|User|string|integer $member |
||
179 | * @see createMemberModel |
||
180 | * @see createMemberModelWithUser |
||
181 | * @return boolean |
||
182 | */ |
||
183 | 3 | public function addMember(&$member) |
|
198 | |||
199 | /** |
||
200 | * Create member model, and set organization with this. |
||
201 | * @param Member $member If this parameter is not new record, it's organization |
||
202 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
203 | * model and create new `Member` model. |
||
204 | * @see createMemberModelWithUser |
||
205 | * @return Member |
||
206 | */ |
||
207 | public function createMemberModel($member) |
||
215 | |||
216 | /** |
||
217 | * Create member model with user, and set organization with this. |
||
218 | * @param User|string|integer $user |
||
219 | * @return Member |
||
220 | */ |
||
221 | 16 | public function createMemberModelWithUser($user) |
|
233 | |||
234 | /** |
||
235 | * Remove member. |
||
236 | * @param Member|User $member |
||
237 | * @return boolean |
||
238 | */ |
||
239 | 1 | public function removeMember(&$member) |
|
250 | } |
||
251 |