|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior; |
|
7
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
8
|
|
|
use NerdsAndCompany\Schematic\Interfaces\MapperInterface; |
|
9
|
|
|
use yii\base\Component as BaseComponent; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Schematic Element Index Mapper. |
|
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
|
|
|
* @method getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
23
|
|
|
* @method getSource(string $fieldType, string $source, string $indexFrom, string $indexTo) |
|
24
|
|
|
*/ |
|
25
|
|
|
class ElementIndexMapper extends BaseComponent implements MapperInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Load sources behaviors. |
|
29
|
|
|
* |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function behaviors(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
2 |
|
SourcesBehavior::class, |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function export(array $elementTypes): array |
|
43
|
|
|
{ |
|
44
|
1 |
|
$settingDefinitions = []; |
|
45
|
1 |
|
foreach ($elementTypes as $elementType) { |
|
46
|
1 |
|
$settings = Craft::$app->elementIndexes->getSettings($elementType); |
|
47
|
1 |
|
if (is_array($settings)) { |
|
48
|
1 |
|
$elementTypeName = str_replace('craft\\elements\\', '', $elementType); |
|
49
|
1 |
|
$settingDefinitions[$elementTypeName] = $this->getMappedSettings($settings, 'id', 'handle'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return $settingDefinitions; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function import(array $settingDefinitions, array $elementTypes): array |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->resetCraftEditableSectionsCache(); |
|
62
|
|
|
|
|
63
|
1 |
|
foreach ($settingDefinitions as $elementTypeName => $settings) { |
|
64
|
1 |
|
$elementType = 'craft\\elements\\'.$elementTypeName; |
|
65
|
1 |
|
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id'); |
|
66
|
1 |
|
if (!Craft::$app->elementIndexes->saveSettings($elementType, $mappedSettings)) { |
|
67
|
1 |
|
Schematic::error(' - Settings for '.$elementTypeName.' could not be saved'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get mapped element index settings, converting source ids to handles or back again. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $settings |
|
78
|
|
|
* @param string $fromIndex |
|
79
|
|
|
* @param string $toIndex |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
2 |
|
private function getMappedSettings(array $settings, $fromIndex, $toIndex) |
|
84
|
|
|
{ |
|
85
|
2 |
|
$mappedSettings = ['sourceOrder' => [], 'sources' => []]; |
|
86
|
|
|
|
|
87
|
2 |
|
if (isset($settings['sourceOrder'])) { |
|
88
|
2 |
|
foreach ($settings['sourceOrder'] as $row) { |
|
89
|
2 |
|
if ('key' == $row[0]) { |
|
90
|
2 |
|
$row[1] = $this->getSource('', $row[1], $fromIndex, $toIndex); |
|
91
|
|
|
} |
|
92
|
2 |
|
$mappedSettings['sourceOrder'][] = $row; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
if (isset($settings['sources'])) { |
|
97
|
2 |
|
foreach ($settings['sources'] as $source => $sourceSettings) { |
|
98
|
2 |
|
$mappedSource = $this->getSource('', $source, $fromIndex, $toIndex); |
|
99
|
2 |
|
$mappedSettings['sources'][$mappedSource] = [ |
|
100
|
2 |
|
'tableAttributes' => $this->getSources('', $sourceSettings['tableAttributes'], $fromIndex, $toIndex), |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
return $mappedSettings; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Reset craft editable sections cache using reflection. |
|
110
|
|
|
*/ |
|
111
|
1 |
|
private function resetCraftEditableSectionsCache() |
|
112
|
|
|
{ |
|
113
|
1 |
|
$obj = Craft::$app->sections; |
|
114
|
1 |
|
$refObject = new \ReflectionObject($obj); |
|
115
|
1 |
|
if ($refObject->hasProperty('_editableSectionIds')) { |
|
116
|
|
|
$refProperty1 = $refObject->getProperty('_editableSectionIds'); |
|
117
|
|
|
$refProperty1->setAccessible(true); |
|
118
|
|
|
$refProperty1->setValue($obj, $obj->getAllSectionIds()); |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|