1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Models; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\FieldModel; |
7
|
|
|
use Craft\FieldGroupModel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Field Model. |
11
|
|
|
* |
12
|
|
|
* A schematic field model for mapping data |
13
|
|
|
* |
14
|
|
|
* @author Nerds & Company |
15
|
|
|
* @copyright Copyright (c) 2015-2016, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @link http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class Field |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return FieldFactory |
24
|
|
|
*/ |
25
|
|
|
protected function getFieldFactory() |
26
|
|
|
{ |
27
|
|
|
return Craft::app()->schematic_fields->getFieldFactory(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return SectionsService |
32
|
|
|
*/ |
33
|
|
|
private function getSectionsService() |
34
|
|
|
{ |
35
|
|
|
return Craft::app()->sections; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return UserGroupsService |
40
|
|
|
*/ |
41
|
|
|
private function getUserGroupsService() |
42
|
|
|
{ |
43
|
|
|
return Craft::app()->userGroups; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return AssetSources |
48
|
|
|
*/ |
49
|
|
|
private function getAssetSourcesService() |
50
|
|
|
{ |
51
|
|
|
return Craft::app()->schematic_assetSources; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return CategoryGroupsService |
56
|
|
|
*/ |
57
|
|
|
private function getCategoryGroupsService() |
58
|
|
|
{ |
59
|
|
|
return Craft::app()->categoryGroups; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param FieldModel $field |
64
|
|
|
* @param $includeContext |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getDefinition(FieldModel $field, $includeContext) |
69
|
|
|
{ |
70
|
|
|
$definition = [ |
71
|
|
|
'name' => $field->name, |
72
|
|
|
'required' => $field->required, |
73
|
|
|
'instructions' => $field->instructions, |
74
|
|
|
'translatable' => $field->translatable, |
75
|
|
|
'type' => $field->type, |
76
|
|
|
'settings' => $field->settings, |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
if ($includeContext) { |
80
|
|
|
$definition['context'] = $field->context; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isset($definition['settings']['sources'])) { |
84
|
|
|
$definition['settings']['sources'] = $this->getMappedSources($definition['settings']['sources'], 'id', 'handle'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $definition; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $fieldDefinition |
92
|
|
|
* @param FieldModel $field |
93
|
|
|
* @param string $fieldHandle |
94
|
|
|
* @param FieldGroupModel|null $group |
95
|
|
|
*/ |
96
|
|
|
public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null) |
97
|
|
|
{ |
98
|
|
|
$field->name = $fieldDefinition['name']; |
99
|
|
|
$field->handle = $fieldHandle; |
100
|
|
|
$field->required = $fieldDefinition['required']; |
101
|
|
|
$field->translatable = $fieldDefinition['translatable']; |
102
|
|
|
$field->instructions = $fieldDefinition['instructions']; |
103
|
|
|
$field->type = $fieldDefinition['type']; |
104
|
|
|
$field->settings = $fieldDefinition['settings']; |
105
|
|
|
|
106
|
|
|
if ($group) { |
107
|
|
|
$field->groupId = $group->id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (isset($fieldDefinition['settings']['sources'])) { |
111
|
|
|
$settings = $fieldDefinition['settings']; |
112
|
|
|
$settings['sources'] = $this->getMappedSources($settings['sources'], 'handle', 'id'); |
113
|
|
|
$field->settings = $settings; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
119
|
|
|
* |
120
|
|
|
* @param string|array $sources |
121
|
|
|
* @param string $indexFrom |
122
|
|
|
* @param string $indexTo |
123
|
|
|
* |
124
|
|
|
* @return array|string |
125
|
|
|
*/ |
126
|
|
|
private function getMappedSources($sources, $indexFrom, $indexTo) |
127
|
|
|
{ |
128
|
|
|
$mappedSources = $sources; |
129
|
|
|
if (is_array($sources)) { |
130
|
|
|
$mappedSources = []; |
131
|
|
|
foreach ($sources as $source) { |
132
|
|
|
$mappedSources[] = $this->getSource($source, $indexFrom, $indexTo); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $mappedSources; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
141
|
|
|
* |
142
|
|
|
* @TODO Break up and simplify this method |
143
|
|
|
* |
144
|
|
|
* @param string $source |
145
|
|
|
* @param string $indexFrom |
146
|
|
|
* @param string $indexTo |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
private function getSource($source, $indexFrom, $indexTo) |
151
|
|
|
{ |
152
|
|
|
/** @var BaseElementModel $sourceObject */ |
153
|
|
|
$sourceObject = null; |
154
|
|
|
|
155
|
|
|
if (strpos($source, ':') > -1) { |
156
|
|
|
list($sourceType, $sourceFrom) = explode(':', $source); |
157
|
|
|
switch ($sourceType) { |
158
|
|
|
case 'section': |
159
|
|
|
$service = $this->getSectionsService(); |
160
|
|
|
$method = 'getSectionBy'; |
161
|
|
|
break; |
162
|
|
|
case 'group': |
163
|
|
|
$service = $this->getUserGroupsService(); |
164
|
|
|
$method = 'getGroupBy'; |
165
|
|
|
break; |
166
|
|
|
case 'folder': |
167
|
|
|
$service = $this->getAssetSourcesService(); |
168
|
|
|
$method = 'getSourceTypeBy'; |
169
|
|
|
break; |
170
|
|
|
} |
171
|
|
|
} elseif ($source !== 'singles') { |
172
|
|
|
//Backwards compatibility |
173
|
|
|
$sourceType = 'section'; |
174
|
|
|
$sourceFrom = $source; |
175
|
|
|
$service = $this->getSectionsService(); |
176
|
|
|
$method = 'getSectionBy'; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (isset($service) && isset($method) && isset($sourceFrom)) { |
180
|
|
|
$method = $method.$indexFrom; |
181
|
|
|
$sourceObject = $service->$method($sourceFrom); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($sourceObject && isset($sourceType)) { |
185
|
|
|
$source = $sourceType.':'.$sourceObject->$indexTo; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $source; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|