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
|
|
|
* @link 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
|
|
|
|
73
|
|
|
// Get element type name |
74
|
1 |
|
$elementTypeName = preg_replace('/^Craft\\\(.*?)ElementType$/', '$1', get_class($elementType)); |
75
|
|
|
|
76
|
|
|
// Get existing settings for element type |
77
|
1 |
|
$settings = $this->getElementIndexesService()->getSettings($elementTypeName); |
78
|
|
|
|
79
|
|
|
// If there are settings, export |
80
|
1 |
|
if (is_array($settings)) { |
81
|
|
|
|
82
|
|
|
// Group by element type and add to definitions |
83
|
1 |
|
$mappedSettings = $this->getMappedSettings($settings, 'id', 'handle'); |
84
|
1 |
|
$settingDefinitions[$elementTypeName] = $mappedSettings; |
85
|
1 |
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
return $settingDefinitions; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get mapped element index settings, converting source ids to handles or back again |
93
|
|
|
* |
94
|
|
|
* @param array $settings |
95
|
|
|
* @param string $fromIndex |
96
|
|
|
* @param string $toIndex |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
2 |
|
private function getMappedSettings(array $settings, $fromIndex, $toIndex) |
101
|
|
|
{ |
102
|
2 |
|
$mappedSettings = ['sources' => []]; |
103
|
2 |
|
foreach ($settings['sources'] as $source => $sourceSettings) { |
104
|
2 |
|
$mappedSource = Craft::app()->schematic_sources->getSource(false, $source, $fromIndex, $toIndex); |
105
|
2 |
|
$tableAttributesSettings = []; |
106
|
2 |
|
foreach ($sourceSettings['tableAttributes'] as $index => $columnSource) { |
107
|
2 |
|
$mappedColumnSource = Craft::app()->schematic_sources->getSource(false, $columnSource, $fromIndex, $toIndex); |
108
|
2 |
|
$tableAttributesSettings[$index] = $mappedColumnSource; |
109
|
2 |
|
} |
110
|
2 |
|
$mappedSettings['sources'][$mappedSource] = ['tableAttributes' => $tableAttributesSettings]; |
111
|
2 |
|
} |
112
|
2 |
|
return $mappedSettings; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|