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
|
|
|
class ElementIndexMapper extends BaseComponent implements MapperInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Load sources behaviors. |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function behaviors(): array |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
SourcesBehavior::className(), |
|
|
|
|
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function export(array $elementTypes): array |
40
|
|
|
{ |
41
|
|
|
$settingDefinitions = []; |
42
|
|
|
foreach ($elementTypes as $elementType) { |
43
|
|
|
$settings = Craft::$app->elementIndexes->getSettings($elementType); |
44
|
|
|
if (is_array($settings)) { |
45
|
|
|
$elementTypeName = str_replace('craft\\elements\\', '', $elementType); |
46
|
|
|
$settingDefinitions[$elementTypeName] = $this->getMappedSettings($settings, 'id', 'handle'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $settingDefinitions; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @TODO: Look into issue with element index settings |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function import(array $settingDefinitions, array $elementTypes): array |
58
|
|
|
{ |
59
|
|
|
Schematic::warning(' - Element index settings may not be imported properly because craft prunes the sources'); |
60
|
|
|
foreach ($settingDefinitions as $elementTypeName => $settings) { |
61
|
|
|
$elementType = 'craft\\elements\\'.$elementTypeName; |
62
|
|
|
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id'); |
63
|
|
|
if (!Craft::$app->elementIndexes->saveSettings($elementType, $mappedSettings)) { |
64
|
|
|
Schematic::error(' - Settings for '.$elementTypeName.' could not be saved'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get mapped element index settings, converting source ids to handles or back again. |
73
|
|
|
* |
74
|
|
|
* @param array $settings |
75
|
|
|
* @param string $fromIndex |
76
|
|
|
* @param string $toIndex |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function getMappedSettings(array $settings, $fromIndex, $toIndex) |
81
|
|
|
{ |
82
|
|
|
$mappedSettings = ['sourceOrder' => [], 'sources' => []]; |
83
|
|
|
|
84
|
|
|
if (isset($settings['sourceOrder'])) { |
85
|
|
|
foreach ($settings['sourceOrder'] as $row) { |
86
|
|
|
if ('key' == $row[0]) { |
87
|
|
|
$row[1] = $this->getSource(false, $row[1], $fromIndex, $toIndex); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
$mappedSettings['sourceOrder'][] = $row; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($settings['sources'])) { |
94
|
|
|
foreach ($settings['sources'] as $source => $sourceSettings) { |
95
|
|
|
$mappedSource = $this->getSource(false, $source, $fromIndex, $toIndex); |
|
|
|
|
96
|
|
|
$mappedSettings['sources'][$mappedSource] = [ |
97
|
|
|
'tableAttributes' => $this->getSources('', $sourceSettings['tableAttributes'], $fromIndex, $toIndex), |
|
|
|
|
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $mappedSettings; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.