|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\models\FieldGroup; |
|
8
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
9
|
|
|
use NerdsAndCompany\Schematic\Converters\Models\Base; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Schematic Fields Converter. |
|
13
|
|
|
* |
|
14
|
|
|
* Sync Craft Setups. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Nerds & Company |
|
17
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
18
|
|
|
* @license MIT |
|
19
|
|
|
* |
|
20
|
|
|
* @see http://www.nerds.company |
|
21
|
|
|
*/ |
|
22
|
|
|
class Field extends Base |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var number[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $groups; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function getRecordDefinition(Model $record): array |
|
33
|
|
|
{ |
|
34
|
6 |
|
$definition = parent::getRecordDefinition($record); |
|
35
|
|
|
|
|
36
|
6 |
|
if ($record->groupId) { |
|
37
|
6 |
|
$definition['group'] = $record->group->name; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
6 |
|
$definition['attributes']['required'] = $definition['attributes']['required'] == true; |
|
41
|
6 |
|
unset($definition['attributes']['context']); |
|
42
|
6 |
|
unset($definition['attributes']['groupId']); |
|
43
|
6 |
|
unset($definition['attributes']['layoutId']); |
|
44
|
6 |
|
unset($definition['attributes']['tabId']); |
|
45
|
|
|
|
|
46
|
6 |
|
return $definition; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
5 |
|
public function saveRecord(Model $record, array $definition): bool |
|
53
|
|
|
{ |
|
54
|
5 |
|
if (array_key_exists('group', $definition)) { |
|
55
|
5 |
|
$record->groupId = $this->getGroupIdByName($definition['group']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
return Craft::$app->fields->saveField($record); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function deleteRecord(Model $record): bool |
|
65
|
|
|
{ |
|
66
|
5 |
|
return Craft::$app->fields->deleteField($record); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get group id by name. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* |
|
74
|
|
|
* @return int|null |
|
75
|
|
|
*/ |
|
76
|
5 |
|
private function getGroupIdByName(string $name) |
|
77
|
|
|
{ |
|
78
|
5 |
|
if (!isset($this->groups)) { |
|
79
|
5 |
|
$this->resetCraftFieldsServiceGroupsCache(); |
|
80
|
|
|
|
|
81
|
5 |
|
$this->groups = []; |
|
82
|
5 |
|
foreach (Craft::$app->fields->getAllGroups() as $group) { |
|
83
|
3 |
|
$this->groups[$group->name] = $group->id; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
5 |
|
if (!array_key_exists($name, $this->groups)) { |
|
87
|
4 |
|
$group = new FieldGroup(['name' => $name]); |
|
88
|
4 |
|
if (Craft::$app->fields->saveGroup($group)) { |
|
89
|
1 |
|
$this->groups[$name] = $group->id; |
|
90
|
|
|
} else { |
|
91
|
3 |
|
return Schematic::importError($group, $name); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $this->groups[$name]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Reset craft fields service groups cache using reflection. |
|
100
|
|
|
*/ |
|
101
|
5 |
|
private function resetCraftFieldsServiceGroupsCache() |
|
102
|
|
|
{ |
|
103
|
5 |
|
$obj = Craft::$app->fields; |
|
104
|
5 |
|
$refObject = new \ReflectionObject($obj); |
|
105
|
5 |
|
if ($refObject->hasProperty('_fetchedAllGroups')) { |
|
106
|
|
|
$refProperty = $refObject->getProperty('_fetchedAllGroups'); |
|
107
|
|
|
$refProperty->setAccessible(true); |
|
108
|
|
|
$refProperty->setValue($obj, false); |
|
109
|
|
|
} |
|
110
|
5 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|