Completed
Push — master ( 314343...05f115 )
by Bart
9s
created

Field::getCategoriesService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Models;
4
5
use Craft\Craft;
6
use Craft\FieldModel;
7
use Craft\FieldGroupModel;
8
9
/**
10
 * Schematic Field Model.
11
 *
12
 * A schematic field model for mapping data
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2016, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @link      http://www.nerds.company
19
 */
20
class Field
21
{
22
    /**
23
     * @return FieldFactory
24
     */
25
    protected function getFieldFactory()
26
    {
27
        return Craft::app()->schematic_fields->getFieldFactory();
28
    }
29
30
    /**
31
     * @param FieldModel $field
32
     * @param $includeContext
33
     *
34
     * @return array
35
     */
36
    public function getDefinition(FieldModel $field, $includeContext)
37
    {
38
        $definition = [
39
            'name' => $field->name,
40
            'required' => $field->required,
41
            'instructions' => $field->instructions,
42
            'translatable' => $field->translatable,
43
            'type' => $field->type,
44
            'settings' => $field->settings,
45
        ];
46
47
        if ($includeContext) {
48
            $definition['context'] = $field->context;
49
        }
50
51
        if (isset($definition['settings']['sources'])) {
52
            $definition['settings']['sources'] = $this->getMappedSources($field->type, $definition['settings']['sources'], 'id', 'handle');
53
        }
54
55
        if (isset($definition['settings']['source'])) {
56
            $definition['settings']['source'] = $this->getSource($field->type, $definition['settings']['source'], 'id', 'handle');
57
        }
58
59
        return $definition;
60
    }
61
62
    /**
63
     * @param array                $fieldDefinition
64
     * @param FieldModel           $field
65
     * @param string               $fieldHandle
66
     * @param FieldGroupModel|null $group
67
     */
68
    public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null)
69
    {
70
        $field->name = $fieldDefinition['name'];
71
        $field->handle = $fieldHandle;
72
        $field->required = $fieldDefinition['required'];
73
        $field->translatable = $fieldDefinition['translatable'];
74
        $field->instructions = $fieldDefinition['instructions'];
75
        $field->type = $fieldDefinition['type'];
76
        $field->settings = $fieldDefinition['settings'];
77
78
        if ($group) {
79
            $field->groupId = $group->id;
80
        }
81
82
        if (isset($fieldDefinition['settings']['sources'])) {
83
            $settings = $fieldDefinition['settings'];
84
            $settings['sources'] = $this->getMappedSources($field->type, $settings['sources'], 'handle', 'id');
85
            $field->settings = $settings;
86
        }
87
88
        if (isset($fieldDefinition['settings']['source'])) {
89
            $settings = $fieldDefinition['settings'];
90
            $settings['source'] = $this->getSource($field->type, $settings['source'], 'handle', 'id');
91
            $field->settings = $settings;
92
        }
93
    }
94
95
    /**
96
     * Get sources based on the indexFrom attribute and return them with the indexTo attribute.
97
     *
98
     * @param string       $fieldType
99
     * @param string|array $sources
100
     * @param string       $indexFrom
101
     * @param string       $indexTo
102
     *
103
     * @return array|string
104
     */
105
    private function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
106
    {
107
        $mappedSources = $sources;
108
        if (is_array($sources)) {
109
            $mappedSources = [];
110
            foreach ($sources as $source) {
111
                $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
112
            }
113
        }
114
115
        return $mappedSources;
116
    }
117
118
    /**
119
     * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
120
     *
121
     * @TODO Break up and simplify this method
122
     *
123
     * @param string $fieldType
124
     * @param string $source
125
     * @param string $indexFrom
126
     * @param string $indexTo
127
     *
128
     * @return string
129
     */
130
    private function getSource($fieldType, $source, $indexFrom, $indexTo)
131
    {
132
        if ($source == 'singles' || $source == '*') {
133
            return $source;
134
        }
135
136
        /** @var BaseElementModel $sourceObject */
137
        $sourceObject = null;
138
139
        if (strpos($source, ':') > -1) {
140
            list($sourceType, $sourceFrom) = explode(':', $source);
141
            switch ($sourceType) {
142
                case 'section':
143
                    $service = Craft::app()->sections;
144
                    $method = 'getSectionBy';
145
                    break;
146
                case 'group':
147
                    $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
148
                    $method = 'getGroupBy';
149
                    break;
150
                case 'folder':
151
                    $service = Craft::app()->assetSources;
152
                    $method = 'getSourceTypeBy';
153
                    break;
154
                case 'taggroup':
155
                    $service = Craft::app()->tags;
156
                    $method = 'getTagGroupBy';
157
                    break;
158
            }
159
        } elseif ($source !== 'singles') {
160
            //Backwards compatibility
161
            $sourceType = 'section';
162
            $sourceFrom = $source;
163
            $service = Craft::app()->sections;
164
            $method = 'getSectionBy';
165
        }
166
167
        if (isset($service) && isset($method) && isset($sourceFrom)) {
168
            $method = $method.$indexFrom;
169
            $sourceObject = $service->$method($sourceFrom);
170
        }
171
172
        if ($sourceObject && isset($sourceType)) {
173
            return $sourceType.':'.$sourceObject->$indexTo;
174
        }
175
        return '';
176
    }
177
}
178