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\traits\SelfBlameableTrait; |
16
|
|
|
use rhosocial\base\models\queries\BaseUserQuery; |
17
|
|
|
use rhosocial\user\User; |
18
|
|
|
use rhosocial\organization\queries\MemberQuery; |
19
|
|
|
use rhosocial\organization\queries\DepartmentQuery; |
20
|
|
|
use rhosocial\organization\queries\OrganizationQuery; |
21
|
|
|
use Yii; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Base Organization. |
25
|
|
|
* This class is an abstract class that can not be instantiated directly. |
26
|
|
|
* You can use [[Organization]] or [[Department]] instead. |
27
|
|
|
* |
28
|
|
|
* @method Member createMember(array $config) Create member who is subordinate to this. |
29
|
|
|
* @property integer $type Whether indicate this instance is an organization or a department. |
30
|
|
|
|
31
|
|
|
* @version 1.0 |
32
|
|
|
* @author vistart <[email protected]> |
33
|
|
|
*/ |
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() |
127
|
|
|
{ |
128
|
18 |
|
return '{{%organization}}'; |
129
|
|
|
} |
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() |
141
|
|
|
{ |
142
|
17 |
|
return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get Member Query. |
147
|
|
|
* @return MemberQuery |
148
|
|
|
*/ |
149
|
5 |
|
public function getMembers() |
150
|
|
|
{ |
151
|
5 |
|
return $this->hasMany($this->memberClass, [$this->getNoInitMember()->createdByAttribute => $this->guidAttribute])->inverseOf('organization'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get organization member users' query. |
156
|
|
|
* @return BaseUserQuery |
157
|
|
|
*/ |
158
|
2 |
|
public function getMemberUsers() |
159
|
|
|
{ |
160
|
2 |
|
$noInit = $this->getNoInitMember(); |
161
|
2 |
|
$class = $noInit->memberUserClass; |
162
|
2 |
|
$noInitUser = $class::buildNoInitModel(); |
163
|
2 |
|
return $this->hasMany($class, [$noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute])->via('members')->inverseOf('atOrganizations'); |
164
|
|
|
} |
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) |
172
|
|
|
{ |
173
|
3 |
|
return $this->getMembers()->user($user)->one(); |
|
|
|
|
174
|
|
|
} |
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) |
184
|
|
|
{ |
185
|
3 |
|
if ($this->getIsNewRecord()) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
3 |
|
$model = null; |
189
|
3 |
|
if ($member instanceof Member) { |
190
|
|
|
$model = $this->createMemberModel($member); |
191
|
|
|
} |
192
|
3 |
|
if (($member instanceof User) || is_string($member) || is_int($member)) { |
193
|
3 |
|
$model = $this->createMemberModelWithUser($member); |
194
|
|
|
} |
195
|
3 |
|
$member = $model; |
196
|
3 |
|
return ($member instanceof Member) ? $member->save() : false; |
197
|
|
|
} |
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) |
208
|
|
|
{ |
209
|
|
|
if (!$member->getIsNewRecord()) { |
210
|
|
|
$member->setOrganization($this); |
|
|
|
|
211
|
|
|
return $member; |
212
|
|
|
} |
213
|
|
|
return $this->createMemberModelWithUser($member->memberUser); |
214
|
|
|
} |
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) |
222
|
|
|
{ |
223
|
|
|
$config = [ |
224
|
16 |
|
'memberUser' => $user, |
225
|
16 |
|
'organization' => $this, |
226
|
16 |
|
'nickname' => '', |
227
|
|
|
]; |
228
|
16 |
|
if ($user->profile) { |
229
|
16 |
|
$config['nickname'] = $user->profile->nickname; |
230
|
|
|
} |
231
|
16 |
|
return $this->createMember($config); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Remove member. |
236
|
|
|
* @param Member|User $member |
237
|
|
|
* @return boolean |
238
|
|
|
*/ |
239
|
1 |
|
public function removeMember(&$member) |
240
|
|
|
{ |
241
|
1 |
|
if ($this->getIsNewRecord()) { |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
1 |
|
if ($member instanceof $this->memberClass) { |
245
|
1 |
|
$member = $member->{$member->memberAttribute}; |
246
|
|
|
} |
247
|
1 |
|
$member = $this->getMember($member); |
248
|
1 |
|
return $member && $member->delete() > 0; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|