Completed
Pull Request — master (#93)
by Bart
04:35
created

ElementIndexSettings::getMappedSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
crap 12
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
    protected function getElementsService()
24
    {
25
        return Craft::app()->elements;
26
    }
27
28
    /**
29
     * @return ElementIndexesService
30
     */
31
    protected function getElementIndexesService()
32
    {
33
        return Craft::app()->elementIndexes;
34
    }
35
36
    /**
37
     * @param array $settingDefinitions
38
     * @param bool  $force
39
     *
40
     * @return Result
41
     */
42
    public function import(array $settingDefinitions, $force = false)
43
    {
44
        Craft::log(Craft::t('Importing Element Index Settings'));
45
46
        foreach ($settingDefinitions as $elementType => $settings) {
47
            $mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
48
            if (!$this->getElementIndexesService()->saveSettings($elementType, $mappedSettings)) {
49
                $this->addError(Craft::t('Element Index Settings for {elementType} could not be installed', ['elementType' => $elementType]));
50
            }
51
        }
52
53
        return $this->getResultModel();
54
    }
55
56
    /**
57
     * @param array $data
58
     *
59
     * @return array
60
     */
61
    public function export(array $data = [])
62
    {
63
        Craft::log(Craft::t('Exporting Element Index Settings'));
64
65
        $settingDefinitions = [];
66
67
        // Get all element types
68
        $elementTypes = $this->getElementsService()->getAllElementTypes();
69
70
        // Loop through element types
71
        foreach ($elementTypes as $elementType) {
72
73
            // Get element type name
74
            $elementTypeName = preg_replace('/^Craft\\\(.*?)ElementType$/', '$1', get_class($elementType));
75
76
            // Get existing settings for element type
77
            $settings = $this->getElementIndexesService()->getSettings($elementTypeName);
78
79
            // If there are settings, export
80
            if (is_array($settings)) {
81
82
                // Group by element type and add to definitions
83
                $mappedSettings = $this->getMappedSettings($settings, 'id', 'handle');
84
                $settingDefinitions[$elementTypeName] = $mappedSettings;
85
            }
86
        }
87
88
        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
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
101
    {
102
        $mappedSettings = ['sources' => []];
103
        foreach ($settings['sources'] as $source => $sourceSettings) {
104
            $mappedSource = Craft::app()->schematic_sources->getSource(false, $source, $fromIndex, $toIndex);
105
            $tableAttributesSettings = [];
106
            foreach ($sourceSettings as $index => $columnSource) {
107
                $mappedColumnSource = Craft::app()->schematic_sources->getSource(false, $columnSource, $fromIndex, $toIndex);
108
                $tableAttributesSettings[$index] = $mappedColumnSource;
109
            }
110
            $mappedSettings['sources'][$mappedSource]['tableAttributes'] = $tableAttributesSettings;
111
        }
112
        return $mappedSettings;
113
    }
114
}
115