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 |
|
foreach ($settingDefinitions as $elementTypeName => $settings) { |
62
|
1 |
|
$elementType = 'craft\\elements\\'.$elementTypeName; |
63
|
1 |
|
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id'); |
64
|
1 |
|
if (!Craft::$app->elementIndexes->saveSettings($elementType, $mappedSettings)) { |
65
|
1 |
|
Schematic::error(' - Settings for '.$elementTypeName.' could not be saved'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get mapped element index settings, converting source ids to handles or back again. |
74
|
|
|
* |
75
|
|
|
* @param array $settings |
76
|
|
|
* @param string $fromIndex |
77
|
|
|
* @param string $toIndex |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
2 |
|
private function getMappedSettings(array $settings, $fromIndex, $toIndex) |
82
|
|
|
{ |
83
|
2 |
|
$mappedSettings = ['sourceOrder' => [], 'sources' => []]; |
84
|
|
|
|
85
|
2 |
|
if (isset($settings['sourceOrder'])) { |
86
|
2 |
|
foreach ($settings['sourceOrder'] as $row) { |
87
|
2 |
|
if ('key' == $row[0]) { |
88
|
2 |
|
$row[1] = $this->getSource('', $row[1], $fromIndex, $toIndex); |
89
|
|
|
} |
90
|
2 |
|
$mappedSettings['sourceOrder'][] = $row; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
if (isset($settings['sources'])) { |
95
|
2 |
|
foreach ($settings['sources'] as $source => $sourceSettings) { |
96
|
2 |
|
$mappedSource = $this->getSource('', $source, $fromIndex, $toIndex); |
97
|
2 |
|
$mappedSettings['sources'][$mappedSource] = [ |
98
|
2 |
|
'tableAttributes' => $this->getSources('', $sourceSettings['tableAttributes'], $fromIndex, $toIndex), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $mappedSettings; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|