1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Schematic Element Index Settings Service. |
9
|
|
|
* |
10
|
|
|
* Sync Craft Setups. |
11
|
|
|
* |
12
|
|
|
* @author Nerds & Company |
13
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
14
|
|
|
* @license MIT |
15
|
|
|
* |
16
|
|
|
* @see http://www.nerds.company |
17
|
|
|
*/ |
18
|
|
|
class ElementIndexSettings extends Base |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return ElementsService |
22
|
|
|
*/ |
23
|
1 |
|
protected function getElementsService() |
24
|
|
|
{ |
25
|
1 |
|
return Craft::app()->elements; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return ElementIndexesService |
30
|
|
|
*/ |
31
|
2 |
|
protected function getElementIndexesService() |
32
|
|
|
{ |
33
|
2 |
|
return Craft::app()->elementIndexes; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $settingDefinitions |
38
|
|
|
* @param bool $force |
39
|
|
|
* |
40
|
|
|
* @return Result |
41
|
|
|
*/ |
42
|
1 |
|
public function import(array $settingDefinitions, $force = false) |
43
|
|
|
{ |
44
|
1 |
|
Craft::log(Craft::t('Importing Element Index Settings')); |
45
|
|
|
|
46
|
1 |
|
foreach ($settingDefinitions as $elementType => $settings) { |
47
|
1 |
|
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id'); |
48
|
1 |
|
if (!$this->getElementIndexesService()->saveSettings($elementType, $mappedSettings)) { |
49
|
1 |
|
$this->addError(Craft::t('Element Index Settings for {elementType} could not be installed', ['elementType' => $elementType])); |
50
|
1 |
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
return $this->getResultModel(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $data |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
1 |
|
public function export(array $data = []) |
62
|
|
|
{ |
63
|
1 |
|
Craft::log(Craft::t('Exporting Element Index Settings')); |
64
|
|
|
|
65
|
1 |
|
$settingDefinitions = []; |
66
|
|
|
|
67
|
|
|
// Get all element types |
68
|
1 |
|
$elementTypes = $this->getElementsService()->getAllElementTypes(); |
69
|
|
|
|
70
|
|
|
// Loop through element types |
71
|
1 |
|
foreach ($elementTypes as $elementType) { |
72
|
|
|
// Get element type name |
73
|
|
|
$elementTypeName = preg_replace('/^Craft\\\(.*?)ElementType$/', '$1', get_class($elementType)); |
74
|
1 |
|
|
75
|
|
|
// Get existing settings for element type |
76
|
|
|
$settings = $this->getElementIndexesService()->getSettings($elementTypeName); |
77
|
1 |
|
|
78
|
|
|
// If there are settings, export |
79
|
|
|
if (is_array($settings)) { |
80
|
1 |
|
// Group by element type and add to definitions |
81
|
|
|
$mappedSettings = $this->getMappedSettings($settings, 'id', 'handle'); |
82
|
|
|
$settingDefinitions[$elementTypeName] = $mappedSettings; |
83
|
1 |
|
} |
84
|
1 |
|
} |
85
|
1 |
|
|
86
|
1 |
|
return $settingDefinitions; |
87
|
|
|
} |
88
|
1 |
|
|
89
|
|
|
/** |
90
|
|
|
* Get mapped element index settings, converting source ids to handles or back again. |
91
|
|
|
* |
92
|
|
|
* @param array $settings |
93
|
|
|
* @param string $fromIndex |
94
|
|
|
* @param string $toIndex |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
private function getMappedSettings(array $settings, $fromIndex, $toIndex) |
99
|
|
|
{ |
100
|
2 |
|
$mappedSettings = ['sourceOrder' => [], 'sources' => []]; |
101
|
|
|
|
102
|
2 |
|
if (isset($settings['sourceOrder'])) { |
103
|
2 |
|
foreach ($settings['sourceOrder'] as $row) { |
104
|
2 |
|
if ($row[0] == 'key') { |
105
|
2 |
|
$row[1] = Craft::app()->schematic_sources->getSource(false, $row[1], $fromIndex, $toIndex); |
106
|
2 |
|
} |
107
|
2 |
|
$mappedSettings['sourceOrder'][] = $row; |
108
|
2 |
|
} |
109
|
2 |
|
} |
110
|
2 |
|
|
111
|
2 |
|
if (isset($settings['sources'])) { |
112
|
2 |
|
foreach ($settings['sources'] as $source => $sourceSettings) { |
113
|
2 |
|
$mappedSource = Craft::app()->schematic_sources->getSource(false, $source, $fromIndex, $toIndex); |
114
|
|
|
$tableAttributesSettings = []; |
115
|
2 |
|
foreach ($sourceSettings['tableAttributes'] as $index => $columnSource) { |
116
|
|
|
$mappedColumnSource = Craft::app()->schematic_sources->getSource(false, $columnSource, $fromIndex, $toIndex); |
117
|
|
|
$tableAttributesSettings[$index] = $mappedColumnSource; |
118
|
|
|
} |
119
|
|
|
$mappedSettings['sources'][$mappedSource] = ['tableAttributes' => $tableAttributesSettings]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $mappedSettings; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|