Completed
Pull Request — master (#63)
by Bart
03:06
created

TagGroups::populateTagGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\TagGroupModel;
7
8
/**
9
 * Schematic TagGroups Service.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2016, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @link      http://www.nerds.company
18
 */
19
class TagGroups extends Base
20
{
21
    /**
22
     * Export taggroups.
23
     *
24
     * @param TagGroupModel[] $tagGroups
25
     *
26
     * @return array
27
     */
28
    public function export(array $tagGroups = [])
29
    {
30
        Craft::log(Craft::t('Exporting TagGroups'));
31
32
        $tagGroupDefinitions = [];
33
34
        foreach ($tagGroups as $tagGroup) {
35
            $tagGroupDefinitions[$tagGroup->handle] = $this->getTagGroupDefinition($tagGroup);
36
        }
37
38
        return $tagGroupDefinitions;
39
    }
40
41
    /**
42
     * Get tagGroup definition.
43
     *
44
     * @param TagGroupModel $tagGroup
45
     *
46
     * @return array
47
     */
48
    private function getTagGroupDefinition(TagGroupModel $tagGroup)
49
    {
50
        return [
51
            'name' => $tagGroup->name,
52
            'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($tagGroup->getFieldLayout()),
53
        ];
54
    }
55
56
    /**
57
     * Attempt to import tagGroups.
58
     *
59
     * @param array $tagGroupDefinitions
60
     * @param bool  $force                If set to true tagGroups not included in the import will be deleted
61
     *
62
     * @return Result
63
     */
64
    public function import(array $tagGroupDefinitions, $force = false)
65
    {
66
        Craft::log(Craft::t('Importing TagGroups'));
67
68
        $tagGroups = Craft::app()->tags->getAllTagGroups('handle');
69
70
        foreach ($tagGroupDefinitions as $tagGroupHandle => $tagGroupDefinition) {
71
            $tagGroup = array_key_exists($tagGroupHandle, $tagGroups)
72
                ? $tagGroups[$tagGroupHandle]
73
                : new TagGroupModel();
74
75
            unset($tagGroups[$tagGroupHandle]);
76
77
            $this->populateTagGroup($tagGroup, $tagGroupDefinition, $tagGroupHandle);
78
79
            if (!Craft::app()->tags->saveTagGroup($tagGroup)) { // Save taggroup via craft
80
                $this->addErrors($tagGroup->getAllErrors());
81
82
                continue;
83
            }
84
        }
85
86
        if ($force) {
87
            foreach ($tagGroups as $tagGroup) {
88
                Craft::app()->tags->deleteTagGroupById($tagGroup->id);
89
            }
90
        }
91
92
        return $this->getResultModel();
93
    }
94
95
    /**
96
     * Populate taggroup.
97
     *
98
     * @param TagGroupModel $tagGroup
99
     * @param array          $tagGroupDefinition
100
     * @param string         $tagGroupHandle
101
     */
102
    private function populateTagGroup(TagGroupModel $tagGroup, array $tagGroupDefinition, $tagGroupHandle)
103
    {
104
        $tagGroup->setAttributes([
105
            'handle' => $tagGroupHandle,
106
            'name' => $tagGroupDefinition['name'],
107
        ]);
108
109
        $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($tagGroupDefinition['fieldLayout']);
110
        $tagGroup->setFieldLayout($fieldLayout);
111
    }
112
}
113