|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\base\Field; |
|
8
|
|
|
use craft\models\FieldLayout; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Schematic Fields Service. |
|
12
|
|
|
* |
|
13
|
|
|
* Sync Craft Setups. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nerds & Company |
|
16
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://www.nerds.company |
|
20
|
|
|
*/ |
|
21
|
|
|
class Fields extends Base |
|
22
|
|
|
{ |
|
23
|
|
|
//============================================================================================================== |
|
24
|
|
|
//================================================ EXPORT ==================================================== |
|
25
|
|
|
//============================================================================================================== |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get all field groups |
|
29
|
|
|
* |
|
30
|
|
|
* @return FieldGroup[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function getRecords() |
|
33
|
|
|
{ |
|
34
|
|
|
return Craft::$app->fields->getAllGroups(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get all field definitions per group |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function export(array $records = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$fieldGroups = $records ?: $this->getRecords(); |
|
45
|
|
|
$result = []; |
|
46
|
|
|
foreach ($fieldGroups as $group) { |
|
47
|
|
|
$fields = $group->getFields(); |
|
48
|
|
|
if (count($fields) > 0) { |
|
49
|
|
|
$result[$group->name] = parent::export($group->getFields()); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get section definition. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Model $record |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getRecordDefinition(Model $record) |
|
63
|
|
|
{ |
|
64
|
|
|
$attributes = parent::getRecordDefinition($record); |
|
65
|
|
|
if ($record instanceof Field) { |
|
66
|
|
|
unset($attributes['groupId']); |
|
67
|
|
|
unset($attributes['layoutId']); |
|
68
|
|
|
unset($attributes['tabId']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $attributes; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
//============================================================================================================== |
|
75
|
|
|
//================================================ IMPORT ==================================================== |
|
76
|
|
|
//============================================================================================================== |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Attempt to import fields. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $groupDefinitions |
|
82
|
|
|
* @param bool $force if set to true items not in the import will be deleted |
|
83
|
|
|
* |
|
84
|
|
|
* @return Result |
|
85
|
|
|
*/ |
|
86
|
|
|
public function import($force = false, array $groupDefinitions = null) |
|
87
|
|
|
{ |
|
88
|
|
|
Craft::info('Importing Fields', 'schematic'); |
|
89
|
|
|
|
|
90
|
|
|
if (!empty($groupDefinitions)) { |
|
91
|
|
|
$this->setGlobalContext(); |
|
92
|
|
|
$this->groups = Craft::$app->fields->getAllGroups('name'); |
|
|
|
|
|
|
93
|
|
|
$this->fields = Craft::$app->fields->getAllFields('handle'); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
foreach ($groupDefinitions as $name => $fieldDefinitions) { |
|
96
|
|
|
try { |
|
97
|
|
|
$this->beginTransaction(); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$group = $this->createFieldGroupModel($name); |
|
100
|
|
|
|
|
101
|
|
|
$this->importFields($fieldDefinitions, $group, $force); |
|
102
|
|
|
|
|
103
|
|
|
$this->commitTransaction(); |
|
|
|
|
|
|
104
|
|
|
} catch (\Exception $e) { |
|
105
|
|
|
$this->rollbackTransaction(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$this->addError($e->getMessage()); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->unsetData($name, $fieldDefinitions); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($force) { // Remove not imported data |
|
114
|
|
|
$this->deleteFieldsAndGroups(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->getResultModel(); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Save field group. |
|
123
|
|
|
* |
|
124
|
|
|
* @param FieldGroupModel $group |
|
125
|
|
|
* |
|
126
|
|
|
* @throws Exception |
|
127
|
|
|
*/ |
|
128
|
|
|
private function saveFieldGroupModel(FieldGroupModel $group) |
|
129
|
|
|
{ |
|
130
|
|
|
if (!Craft::$app->fields->saveGroup($group)) { |
|
131
|
|
|
$this->addErrors($group->getAllErrors()); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
throw new Exception('Failed to save group'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Save field. |
|
139
|
|
|
* |
|
140
|
|
|
* @param FieldModel $field |
|
141
|
|
|
* |
|
142
|
|
|
* @throws \Exception |
|
143
|
|
|
*/ |
|
144
|
|
|
private function saveFieldModel(FieldModel $field) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->validateFieldModel($field); // Validate field |
|
147
|
|
|
if ($field->context === 'global') { |
|
148
|
|
|
$this->setGlobalContext(); |
|
149
|
|
|
} |
|
150
|
|
|
if (!Craft::$app->fields->saveField($field)) { |
|
151
|
|
|
$this->addErrors($field->getAllErrors()); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
throw new Exception('Failed to save field'); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Removes fields that where not imported. |
|
159
|
|
|
*/ |
|
160
|
|
|
private function deleteFields() |
|
161
|
|
|
{ |
|
162
|
|
|
$fieldsService = Craft::$app->fields; |
|
163
|
|
|
foreach ($this->fields as $field) { |
|
|
|
|
|
|
164
|
|
|
$fieldsService->deleteFieldById($field->id); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Removes groups that where not imported. |
|
170
|
|
|
*/ |
|
171
|
|
|
private function deleteGroups() |
|
172
|
|
|
{ |
|
173
|
|
|
$fieldsService = Craft::$app->fields; |
|
174
|
|
|
foreach ($this->groups as $group) { |
|
|
|
|
|
|
175
|
|
|
$fieldsService->deleteGroupById($group->id); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Removes fields and groups that where not imported. |
|
181
|
|
|
*/ |
|
182
|
|
|
private function deleteFieldsAndGroups() |
|
183
|
|
|
{ |
|
184
|
|
|
$this->deleteFields(); |
|
185
|
|
|
$this->deleteGroups(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Creates new or updates existing group model. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $group |
|
192
|
|
|
* |
|
193
|
|
|
* @return FieldGroupModel |
|
194
|
|
|
*/ |
|
195
|
|
|
private function createFieldGroupModel($group) |
|
196
|
|
|
{ |
|
197
|
|
|
$groupModel = (array_key_exists($group, $this->groups) ? $this->groups[$group] : new FieldGroupModel()); |
|
|
|
|
|
|
198
|
|
|
$groupModel->name = $group; |
|
199
|
|
|
|
|
200
|
|
|
$this->saveFieldGroupModel($groupModel); |
|
201
|
|
|
|
|
202
|
|
|
return $groupModel; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param string $field |
|
207
|
|
|
* |
|
208
|
|
|
* @return FieldModel |
|
209
|
|
|
*/ |
|
210
|
|
|
private function getFieldModel($field) |
|
211
|
|
|
{ |
|
212
|
|
|
return array_key_exists($field, $this->fields) ? $this->fields[$field] : new FieldModel(); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Validates field type, throw error when it's incorrect. |
|
217
|
|
|
* |
|
218
|
|
|
* @param FieldModel $field |
|
219
|
|
|
* |
|
220
|
|
|
* @throws \Exception |
|
221
|
|
|
*/ |
|
222
|
|
|
private function validateFieldModel(FieldModel $field) |
|
223
|
|
|
{ |
|
224
|
|
|
if (!$field->getFieldType()) { |
|
225
|
|
|
$fieldType = $field->type; |
|
226
|
|
|
($fieldType == 'Matrix') |
|
227
|
|
|
? $this->addError("One of the field's types does not exist. Are you missing a plugin?") |
|
|
|
|
|
|
228
|
|
|
: $this->addError("Field type '$fieldType' does not exist. Are you missing a plugin?"); |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
throw new Exception('Failed to save field'); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Import field group fields. |
|
236
|
|
|
* |
|
237
|
|
|
* @param array $fieldDefinitions |
|
238
|
|
|
* @param FieldGroupModel $group |
|
239
|
|
|
* @param bool $force |
|
240
|
|
|
* |
|
241
|
|
|
* @throws \Exception |
|
242
|
|
|
*/ |
|
243
|
|
|
private function importFields(array $fieldDefinitions, FieldGroupModel $group, $force = false) |
|
244
|
|
|
{ |
|
245
|
|
|
$fieldFactory = $this->getFieldFactory(); |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
foreach ($fieldDefinitions as $fieldHandle => $fieldDef) { |
|
248
|
|
|
$field = $this->getFieldModel($fieldHandle); |
|
249
|
|
|
$schematicFieldModel = $fieldFactory->build($fieldDef['type']); |
|
250
|
|
|
|
|
251
|
|
|
if ($schematicFieldModel->getDefinition($field, true) === $fieldDef) { |
|
252
|
|
|
Craft::info('Skipping `{name}`, no changes detected', ['name' => $field->name], 'schematic'); |
|
|
|
|
|
|
253
|
|
|
continue; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
Craft::info('Importing `{name}`', ['name' => $fieldDef['name']], 'schematic'); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
$schematicFieldModel->populate($fieldDef, $field, $fieldHandle, $group, $force); |
|
259
|
|
|
$this->saveFieldModel($field); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Unset group and field data else $force flag will delete it. |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $name |
|
267
|
|
|
* @param array $definitions |
|
268
|
|
|
*/ |
|
269
|
|
|
private function unsetData($name, array $definitions) |
|
270
|
|
|
{ |
|
271
|
|
|
if (array_key_exists($name, $this->groups)) { |
|
|
|
|
|
|
272
|
|
|
unset($this->groups[$name]); |
|
273
|
|
|
foreach ($definitions as $handle => $definition) { |
|
274
|
|
|
unset($this->fields[$handle]); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Set global field context. |
|
281
|
|
|
*/ |
|
282
|
|
|
private function setGlobalContext() |
|
283
|
|
|
{ |
|
284
|
|
|
Craft::$app->content->fieldContext = 'global'; |
|
285
|
|
|
Craft::$app->content->contentTable = 'content'; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.