|
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\elements\db\UserQuery; |
|
12
|
|
|
use craft\helpers\ArrayHelper; |
|
13
|
|
|
use flipbox\ember\exceptions\RecordNotFoundException; |
|
14
|
|
|
use flipbox\ember\helpers\SiteHelper; |
|
15
|
|
|
use flipbox\ember\services\traits\elements\MultiSiteAccessor; |
|
16
|
|
|
use flipbox\organizations\db\OrganizationQuery; |
|
17
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
|
18
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
|
19
|
|
|
use yii\base\Component; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @method OrganizationElement create($config = []) |
|
26
|
|
|
* @method OrganizationElement find($identifier, int $siteId = null) |
|
27
|
|
|
* @method OrganizationElement get($identifier, int $siteId = null) |
|
28
|
|
|
* @method OrganizationQuery getQuery($criteria = []) |
|
29
|
|
|
*/ |
|
30
|
|
|
class Organizations extends Component |
|
31
|
|
|
{ |
|
32
|
|
|
use MultiSiteAccessor; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
36 |
|
public function init() |
|
38
|
|
|
{ |
|
39
|
36 |
|
$settings = OrganizationPlugin::getInstance()->getSettings(); |
|
40
|
36 |
|
$this->cacheDuration = $settings->organizationsCacheDuration; |
|
41
|
36 |
|
$this->cacheDependency = $settings->organizationsCacheDependency; |
|
42
|
|
|
|
|
43
|
36 |
|
parent::init(); |
|
44
|
36 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public static function elementClass(): string |
|
50
|
|
|
{ |
|
51
|
3 |
|
return OrganizationElement::class; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $identifier |
|
56
|
|
|
* @param int|null $siteId |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
9 |
|
protected function identifierCondition($identifier, int $siteId = null): array |
|
60
|
|
|
{ |
|
61
|
|
|
$base = [ |
|
62
|
9 |
|
'siteId' => SiteHelper::ensureSiteId($siteId), |
|
63
|
|
|
'status' => null |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
9 |
|
if (is_array($identifier)) { |
|
67
|
3 |
|
return array_merge($base, $identifier); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
if (!is_numeric($identifier) && is_string($identifier)) { |
|
71
|
3 |
|
$base['slug'] = $identifier; |
|
72
|
|
|
} else { |
|
73
|
3 |
|
$base['id'] = $identifier; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
return $base; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $organization |
|
81
|
|
|
* @return OrganizationElement |
|
82
|
|
|
* @throws \yii\base\InvalidConfigException |
|
83
|
|
|
*/ |
|
84
|
9 |
|
public function resolve($organization) |
|
85
|
|
|
{ |
|
86
|
9 |
|
if (is_array($organization) && |
|
87
|
9 |
|
null !== ($id = ArrayHelper::getValue($organization, 'id')) |
|
88
|
|
|
) { |
|
89
|
3 |
|
return $this->find($id); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
if (null !== ($object = $this->find($organization))) { |
|
93
|
3 |
|
return $object; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
return $this->create($organization); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param UserQuery $query |
|
101
|
|
|
* @param OrganizationElement $organization |
|
102
|
|
|
* @return bool |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
* |
|
105
|
|
|
* @deprecated |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function saveAssociations( |
|
108
|
|
|
UserQuery $query, |
|
109
|
|
|
OrganizationElement $organization |
|
110
|
|
|
): bool { |
|
111
|
3 |
|
return OrganizationPlugin::getInstance()->getOrganizationUsers()->saveAssociations( |
|
112
|
3 |
|
$organization, |
|
113
|
3 |
|
$query |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param UserQuery $query |
|
119
|
|
|
* @param OrganizationElement $organization |
|
120
|
|
|
* @return bool |
|
121
|
|
|
* @throws \Exception |
|
122
|
|
|
* |
|
123
|
|
|
* @deprecated |
|
124
|
|
|
*/ |
|
125
|
3 |
|
public function dissociate( |
|
126
|
|
|
UserQuery $query, |
|
127
|
|
|
OrganizationElement $organization |
|
128
|
|
|
): bool { |
|
129
|
3 |
|
return OrganizationPlugin::getInstance()->getOrganizationUsers()->dissociate( |
|
130
|
3 |
|
$organization, |
|
131
|
3 |
|
$query |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param UserQuery $query |
|
137
|
|
|
* @param OrganizationElement $organization |
|
138
|
|
|
* @return bool |
|
139
|
|
|
* @throws \Exception |
|
140
|
|
|
* |
|
141
|
|
|
* @deprecated |
|
142
|
|
|
*/ |
|
143
|
3 |
|
public function associate( |
|
144
|
|
|
UserQuery $query, |
|
145
|
|
|
OrganizationElement $organization |
|
146
|
|
|
): bool { |
|
147
|
3 |
|
return OrganizationPlugin::getInstance()->getOrganizationUsers()->associate( |
|
148
|
3 |
|
$organization, |
|
149
|
3 |
|
$query |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/******************************************* |
|
154
|
|
|
* EXCEPTIONS |
|
155
|
|
|
*******************************************/ |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @throws RecordNotFoundException |
|
159
|
|
|
*/ |
|
160
|
3 |
|
protected function recordNotFoundException() |
|
161
|
|
|
{ |
|
162
|
3 |
|
throw new RecordNotFoundException('Record does not exist.'); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|