These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\relationships; |
||
10 | |||
11 | use craft\helpers\ArrayHelper; |
||
12 | use craft\helpers\Json; |
||
13 | use flipbox\craft\ember\helpers\QueryHelper; |
||
14 | use flipbox\organizations\elements\Organization; |
||
15 | use flipbox\organizations\Organizations; |
||
16 | use flipbox\organizations\queries\OrganizationTypeAssociationQuery; |
||
17 | use flipbox\organizations\records\OrganizationType; |
||
18 | use flipbox\organizations\records\OrganizationTypeAssociation; |
||
19 | use Tightenco\Collect\Support\Collection; |
||
20 | |||
21 | /** |
||
22 | * Manages Organization Types associated to Organizations |
||
23 | * |
||
24 | * @author Flipbox Factory <[email protected]> |
||
25 | * @since 2.0.0 |
||
26 | * |
||
27 | * @method OrganizationTypeAssociation findOrCreate($object) |
||
28 | * @method OrganizationTypeAssociation findOne($object = null) |
||
29 | * @method OrganizationTypeAssociation findOrFail($object) |
||
30 | */ |
||
31 | class OrganizationTypeRelationship implements RelationshipInterface |
||
32 | { |
||
33 | use RelationshipTrait; |
||
34 | |||
35 | /** |
||
36 | * @var Organization |
||
37 | */ |
||
38 | private $organization; |
||
39 | |||
40 | /** |
||
41 | * @param Organization $organization |
||
42 | */ |
||
43 | public function __construct(Organization $organization) |
||
44 | { |
||
45 | $this->organization = $organization; |
||
46 | } |
||
47 | |||
48 | |||
49 | /************************************************************ |
||
50 | * COLLECTION |
||
51 | ************************************************************/ |
||
52 | |||
53 | /** |
||
54 | * Get an array of types associated to an organization |
||
55 | * |
||
56 | * @return OrganizationType[]|Collection |
||
57 | */ |
||
58 | public function getCollection(): Collection |
||
59 | { |
||
60 | /** @noinspection PhpUndefinedMethodInspection */ |
||
61 | $query = OrganizationType::find() |
||
62 | ->organization($this->organization) |
||
63 | ->orderBy([ |
||
64 | 'sortOrder' => SORT_ASC |
||
65 | ]); |
||
66 | |||
67 | if (null === $this->collection) { |
||
68 | return Collection::make( |
||
69 | $query->all() |
||
70 | ); |
||
71 | }; |
||
72 | |||
73 | return Collection::make( |
||
74 | $query |
||
75 | ->id($this->collection->sortBy('sortOrder')->pluck('typeId')) |
||
76 | ->limit(null) |
||
77 | ->all() |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | |||
82 | /************************************************************ |
||
83 | * QUERY |
||
84 | ************************************************************/ |
||
85 | |||
86 | /** |
||
87 | * @param array $criteria |
||
88 | * @return OrganizationTypeAssociationQuery |
||
89 | */ |
||
90 | protected function query(array $criteria = []): OrganizationTypeAssociationQuery |
||
91 | { |
||
92 | /** @noinspection PhpUndefinedMethodInspection */ |
||
93 | $query = OrganizationTypeAssociation::find() |
||
94 | ->setOrganizationId($this->organization->getId() ?: false) |
||
95 | ->orderBy([ |
||
96 | 'sortOrder' => SORT_ASC |
||
97 | ]); |
||
98 | |||
99 | if (!empty($criteria)) { |
||
100 | QueryHelper::configure( |
||
101 | $query, |
||
102 | $criteria |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | return $query; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param OrganizationTypeAssociation|OrganizationType|int|string $type |
||
111 | * @return OrganizationTypeAssociation |
||
112 | */ |
||
113 | protected function create($type): OrganizationTypeAssociation |
||
114 | { |
||
115 | return (new OrganizationTypeAssociation()) |
||
116 | ->setOrganization($this->organization) |
||
117 | ->setType($this->resolveType($type)); |
||
118 | } |
||
119 | |||
120 | |||
121 | /******************************************* |
||
122 | * SAVE |
||
123 | *******************************************/ |
||
124 | |||
125 | /** |
||
126 | * @inheritDoc |
||
127 | */ |
||
128 | protected function associationDelta(): array |
||
129 | { |
||
130 | $existingAssociations = $this->query() |
||
131 | ->indexBy('typeId') |
||
132 | ->all(); |
||
133 | |||
134 | $associations = []; |
||
135 | $order = 1; |
||
136 | /** @var OrganizationTypeAssociation $newAssociation */ |
||
137 | foreach ($this->getRelationships()->sortBy('sortOrder') as $newAssociation) { |
||
138 | if (null === ($association = ArrayHelper::remove( |
||
139 | $existingAssociations, |
||
140 | $newAssociation->getTypeId() |
||
141 | ))) { |
||
142 | $association = $newAssociation; |
||
143 | } |
||
144 | |||
145 | $association->sortOrder = $order++; |
||
146 | |||
147 | $associations[] = $association; |
||
148 | } |
||
149 | |||
150 | return [$associations, $existingAssociations]; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @inheritDoc |
||
155 | */ |
||
156 | protected function handleAssociationError() |
||
157 | { |
||
158 | $this->organization->addError('types', 'Unable to save organization types.'); |
||
159 | } |
||
160 | |||
161 | |||
162 | /******************************************* |
||
163 | * UTILS |
||
164 | *******************************************/ |
||
165 | |||
166 | /** |
||
167 | * @param OrganizationTypeAssociation|OrganizationType|int|array|null $type |
||
168 | * @return int|null |
||
169 | */ |
||
170 | protected function findKey($type = null) |
||
171 | { |
||
172 | if (null === ($record = $this->resolveType($type))) { |
||
173 | Organizations::info(sprintf( |
||
174 | "Unable to resolve organization type: %s", |
||
175 | (string)Json::encode($type) |
||
176 | )); |
||
177 | return null; |
||
178 | } |
||
179 | |||
180 | foreach ($this->findAll() as $key => $association) { |
||
0 ignored issues
–
show
|
|||
181 | if ($association->getTypeId() == $record->id) { |
||
182 | return $key; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | return null; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param OrganizationTypeAssociation|OrganizationType|int|array|null $type |
||
191 | * @return OrganizationType|null |
||
192 | */ |
||
193 | protected function resolveType($type = null) |
||
194 | { |
||
195 | if (null === $type) { |
||
196 | return null; |
||
197 | } |
||
198 | |||
199 | if ($type instanceof OrganizationTypeAssociation) { |
||
200 | return $type->getType(); |
||
201 | } |
||
202 | |||
203 | if ($type instanceof OrganizationType) { |
||
204 | return $type; |
||
205 | } |
||
206 | |||
207 | if (is_array($type) && |
||
208 | null !== ($id = ArrayHelper::getValue($type, 'id')) |
||
209 | ) { |
||
210 | $type = ['id' => $id]; |
||
211 | } |
||
212 | |||
213 | return OrganizationType::findOne($type); |
||
214 | } |
||
215 | } |
||
216 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.