1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Behaviors; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use TypeError; |
7
|
|
|
use yii\base\Behavior; |
8
|
|
|
use craft\base\Model; |
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Schematic Sources Behavior. |
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 SourcesBehavior extends Behavior |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Recursively find sources in definition attributes. |
26
|
|
|
* |
27
|
|
|
* @param string $fieldType |
28
|
|
|
* @param array $attributes |
29
|
|
|
* @param string $indexFrom |
30
|
|
|
* @param string $indexTo |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
32 |
|
public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
35
|
|
|
{ |
36
|
32 |
|
foreach ($attributes as $key => $attribute) { |
37
|
32 |
|
if ($key === 'source') { |
38
|
4 |
|
$attributes[$key] = $this->getSource($fieldType, $attribute, $indexFrom, $indexTo); |
39
|
32 |
|
} elseif ($key === 'sources') { |
40
|
4 |
|
$attributes[$key] = $this->getSources($fieldType, $attribute, $indexFrom, $indexTo); |
41
|
32 |
|
} elseif (is_array($attribute)) { |
42
|
32 |
|
$attributes[$key] = $this->findSources($fieldType, $attribute, $indexFrom, $indexTo); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
32 |
|
return $attributes; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
51
|
|
|
* |
52
|
|
|
* @param string $fieldType |
53
|
|
|
* @param string|array $sources |
54
|
|
|
* @param string $indexFrom |
55
|
|
|
* @param string $indexTo |
56
|
|
|
* |
57
|
|
|
* @return array|string |
58
|
|
|
*/ |
59
|
9 |
|
public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
60
|
|
|
{ |
61
|
9 |
|
$mappedSources = $sources; |
62
|
9 |
|
if (is_array($sources)) { |
63
|
7 |
|
$mappedSources = []; |
64
|
7 |
|
$sources = array_filter($sources); |
65
|
7 |
|
foreach ($sources as $source) { |
66
|
7 |
|
$mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
return $mappedSources; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
75
|
|
|
* |
76
|
|
|
* @TODO Break up and simplify this method |
77
|
|
|
* |
78
|
|
|
* @param string $fieldType |
79
|
|
|
* @param string $source |
80
|
|
|
* @param string $indexFrom |
81
|
|
|
* @param string $indexTo |
82
|
|
|
* |
83
|
|
|
* @return string|null |
84
|
|
|
* |
85
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
86
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
87
|
|
|
*/ |
88
|
9 |
|
public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
89
|
|
|
{ |
90
|
9 |
|
if (false === strpos($source, ':')) { |
91
|
7 |
|
return $source; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @var Model $sourceObject */ |
95
|
9 |
|
$sourceObject = null; |
96
|
|
|
|
97
|
9 |
|
list($sourceType, $sourceFrom) = explode(':', $source); |
98
|
9 |
|
switch ($sourceType) { |
99
|
9 |
|
case 'single': |
100
|
9 |
|
case 'section': |
101
|
9 |
|
case 'createEntries': |
102
|
9 |
|
case 'editPeerEntries': |
103
|
9 |
|
case 'deleteEntries': |
104
|
9 |
|
case 'deletePeerEntries': |
105
|
9 |
|
case 'deletePeerEntryDrafts': |
106
|
9 |
|
case 'editEntries': |
107
|
9 |
|
case 'editPeerEntryDrafts': |
108
|
9 |
|
case 'publishEntries': |
109
|
9 |
|
case 'publishPeerEntries': |
110
|
9 |
|
case 'publishPeerEntryDrafts': |
111
|
5 |
|
$service = Craft::$app->sections; |
112
|
5 |
|
$method = 'getSectionBy'; |
113
|
5 |
|
break; |
114
|
9 |
|
case 'group': |
115
|
7 |
|
case 'editCategories': |
116
|
5 |
|
$service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories; |
117
|
5 |
|
$method = 'getGroupBy'; |
118
|
5 |
|
break; |
119
|
7 |
|
case 'folder': |
120
|
5 |
|
case 'createFoldersInVolume': |
121
|
5 |
|
case 'deleteFilesAndFoldersInVolume': |
122
|
5 |
|
case 'saveAssetInVolume': |
123
|
5 |
|
case 'viewVolume': |
124
|
5 |
|
$service = Craft::$app->volumes; |
125
|
5 |
|
$method = 'getVolumeBy'; |
126
|
5 |
|
break; |
127
|
2 |
|
case 'taggroup': |
128
|
|
|
$service = Craft::$app->tags; |
129
|
|
|
$method = 'getTagGroupBy'; |
130
|
|
|
break; |
131
|
2 |
|
case 'field': |
132
|
2 |
|
$service = Craft::$app->fields; |
133
|
2 |
|
$method = 'getFieldBy'; |
134
|
2 |
|
break; |
135
|
|
|
case 'editGlobalSet': |
136
|
|
|
$service = Craft::$app->globals; |
137
|
|
|
$method = 'getSetBy'; |
138
|
|
|
break; |
139
|
|
|
case 'utility': |
140
|
|
|
return $source; |
141
|
|
|
} |
142
|
|
|
|
143
|
9 |
|
if (isset($service) && isset($method) && isset($sourceFrom)) { |
144
|
9 |
|
$method = $method.ucfirst($indexFrom); |
145
|
|
|
try { |
146
|
9 |
|
$sourceObject = $service->$method($sourceFrom); |
147
|
|
|
} catch (TypeError $e) { |
148
|
|
|
Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo); |
149
|
|
|
Schematic::error($e->getMessage()); |
150
|
|
|
|
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
9 |
|
if ($sourceObject) { |
156
|
8 |
|
return $sourceType.':'.$sourceObject->$indexTo; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
Schematic::warning('No mapping found for source '.$source); |
160
|
|
|
|
161
|
1 |
|
return null; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|