Completed
Pull Request — master (#114)
by Bart
02:07
created

Fields::getRecords()   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 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Field;
7
use craft\base\Model;
8
use craft\models\FieldGroup;
9
10
/**
11
 * Schematic Fields Service.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Fields extends Base
22
{
23
    /**
24
     * @var number[]
25
     */
26
    private $groups;
27
28
    /**
29
     * Get all field groups.
30
     *
31
     * @return FieldGroup[]
32
     */
33
    protected function getRecords()
34
    {
35
        return Craft::$app->fields->getAllFields();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function import(array $definitions, array $records = [], array $defaultAttributes = [])
42
    {
43
        parent::import($definitions, $records, $defaultAttributes);
44
        // Update field version so that contentbehavior is regenerated
45
        Craft::$app->fields->updateFieldVersion();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function getRecordDefinition(Model $record)
52
    {
53
        $definition = parent::getRecordDefinition($record);
54
        if ($record instanceof Field) {
55
            $definition['group'] = $record->group->name;
56
            unset($definition['attributes']['groupId']);
57
            unset($definition['attributes']['layoutId']);
58
            unset($definition['attributes']['tabId']);
59
        }
60
61
        return $definition;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function saveRecord(Model $record, array $definition)
68
    {
69
        $record->groupId = $this->getGroupIdByName($definition['group']);
70
71
        return Craft::$app->fields->saveField($record);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function deleteRecord(Model $record)
78
    {
79
        return Craft::$app->fields->deleteField($record);
80
    }
81
82
    /**
83
     * Get group id by name.
84
     *
85
     * @param string $name
86
     *
87
     * @return
88
     */
89
    private function getGroupIdByName($name)
90
    {
91
        if (!isset($this->groups)) {
92
            $this->groups = [];
93
            foreach (Craft::$app->fields->getAllGroups() as $group) {
94
                $this->groups[$group->name] = $group->id;
95
            }
96
        }
97
        if (!array_key_exists($name, $this->groups)) {
98
            $group = new FieldGroup(['name' => $name]);
99
            if (Craft::$app->fields->saveGroup($group)) {
100
                $this->groups[$name] = $group->id;
101
            } else {
102
                $this->importError($group, $name);
103
            }
104
        }
105
106
        return $this->groups[$name];
107
    }
108
}
109