1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\services; |
10
|
|
|
|
11
|
|
|
use craft\db\Query; |
12
|
|
|
use craft\elements\User as UserElement; |
13
|
|
|
use flipbox\ember\helpers\ArrayHelper; |
14
|
|
|
use flipbox\ember\helpers\ObjectHelper; |
15
|
|
|
use flipbox\ember\services\traits\records\AccessorByString; |
16
|
|
|
use flipbox\organizations\db\UserTypeQuery; |
17
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
18
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
19
|
|
|
use flipbox\organizations\Organizations as OrganizationsPlugin; |
20
|
|
|
use flipbox\organizations\records\UserAssociation; |
21
|
|
|
use flipbox\organizations\records\UserType; |
22
|
|
|
use flipbox\organizations\records\UserTypeAssociation; |
23
|
|
|
use yii\base\Component; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Flipbox Factory <[email protected]> |
27
|
|
|
* @since 1.0.0 |
28
|
|
|
* |
29
|
|
|
* @method UserType create(array $attributes = []) |
30
|
|
|
* @method UserType find($identifier) |
31
|
|
|
* @method UserType get($identifier) |
32
|
|
|
* @method UserType findByString($identifier) |
33
|
|
|
* @method UserType getByString($identifier) |
34
|
|
|
* @method UserType findByCondition($condition = []) |
35
|
|
|
* @method UserType getByCondition($condition = []) |
36
|
|
|
* @method UserType findByCriteria($criteria = []) |
37
|
|
|
* @method UserType getByCriteria($criteria = []) |
38
|
|
|
* @method UserType[] findAll(string $toScenario = null) |
39
|
|
|
* @method UserType[] findAllByCondition($condition = []) |
40
|
|
|
* @method UserType[] getAllByCondition($condition = []) |
41
|
|
|
* @method UserType[] findAllByCriteria($criteria = []) |
42
|
|
|
* @method UserType[] getAllByCriteria($criteria = []) |
43
|
|
|
* @method UserTypeQuery getQuery($config = []): ActiveQuery($criteria = []) |
44
|
|
|
*/ |
45
|
|
|
class UserTypes extends Component |
46
|
|
|
{ |
47
|
|
|
use AccessorByString; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function init() |
53
|
|
|
{ |
54
|
|
|
$settings = OrganizationPlugin::getInstance()->getSettings(); |
55
|
|
|
$this->cacheDuration = $settings->userTypesCacheDuration; |
56
|
|
|
$this->cacheDependency = $settings->userTypesCacheDependency; |
57
|
|
|
|
58
|
|
|
parent::init(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
protected static function stringProperty(): string |
65
|
|
|
{ |
66
|
|
|
return 'handle'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public static function recordClass(): string |
73
|
|
|
{ |
74
|
|
|
return UserType::class; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $type |
79
|
|
|
* @return UserType |
80
|
|
|
*/ |
81
|
|
|
public function resolve($type) |
82
|
|
|
{ |
83
|
|
|
if ($type = $this->find($type)) { |
84
|
|
|
return $type; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$type = ArrayHelper::toArray($type, [], false); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$object = $this->create($type); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
$object = new UserType(); |
93
|
|
|
ObjectHelper::populate( |
94
|
|
|
$object, |
95
|
|
|
$type |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param UserTypeQuery $query |
104
|
|
|
* @param UserElement $user |
105
|
|
|
* @param OrganizationElement $organization |
106
|
|
|
* @return bool |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function saveAssociations( |
110
|
|
|
UserTypeQuery $query, |
111
|
|
|
UserElement $user, |
112
|
|
|
OrganizationElement $organization |
113
|
|
|
): bool { |
114
|
|
|
if (null === ($models = $query->getCachedResult())) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$associationService = OrganizationsPlugin::getInstance()->getUserTypeAssociations(); |
119
|
|
|
|
120
|
|
|
$userAssociationId = $this->associationId($user->getId(), $organization->getId()); |
121
|
|
|
|
122
|
|
|
$query = $associationService->getQuery([ |
123
|
|
|
$associationService::SOURCE_ATTRIBUTE => $userAssociationId |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$query->setCachedResult( |
127
|
|
|
$this->toAssociations($models, $userAssociationId) |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $associationService->save($query); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/******************************************* |
135
|
|
|
* USER / ORGANIZATION ASSOCIATION ID |
136
|
|
|
*******************************************/ |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $types |
140
|
|
|
* @param int $userAssociationId |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private function toAssociations( |
144
|
|
|
array $types, |
145
|
|
|
int $userAssociationId |
146
|
|
|
) { |
147
|
|
|
$associations = []; |
148
|
|
|
foreach ($types as $type) { |
149
|
|
|
$associations[] = new UserTypeAssociation([ |
150
|
|
|
'typeId' => $type->id, |
151
|
|
|
'userId' => $userAssociationId |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $associations; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $userId |
160
|
|
|
* @param int $organizationId |
161
|
|
|
* @return Query |
162
|
|
|
*/ |
163
|
|
|
private function associationIdQuery(int $userId, int $organizationId): Query |
164
|
|
|
{ |
165
|
|
|
return (new Query()) |
166
|
|
|
->select(['id']) |
167
|
|
|
->from([UserAssociation::tableName()]) |
168
|
|
|
->where([ |
169
|
|
|
'organizationId' => $organizationId, |
170
|
|
|
'userId' => $userId, |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $userId |
176
|
|
|
* @param int $organizationId |
177
|
|
|
* @return string|null |
178
|
|
|
*/ |
179
|
|
|
private function associationId(int $userId, int $organizationId) |
180
|
|
|
{ |
181
|
|
|
$id = $this->associationIdQuery($userId, $organizationId)->scalar(); |
182
|
|
|
return is_string($id) ? $id : null; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|