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
|
3 |
|
public function export(array $tagGroups = []) |
29
|
|
|
{ |
30
|
3 |
|
Craft::log(Craft::t('Exporting TagGroups')); |
31
|
|
|
|
32
|
3 |
|
$tagGroupDefinitions = []; |
33
|
|
|
|
34
|
3 |
|
foreach ($tagGroups as $tagGroup) { |
35
|
2 |
|
$tagGroupDefinitions[$tagGroup->handle] = $this->getTagGroupDefinition($tagGroup); |
36
|
3 |
|
} |
37
|
|
|
|
38
|
3 |
|
return $tagGroupDefinitions; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get tagGroup definition. |
43
|
|
|
* |
44
|
|
|
* @param TagGroupModel $tagGroup |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
2 |
|
private function getTagGroupDefinition(TagGroupModel $tagGroup) |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
2 |
|
'name' => $tagGroup->name, |
52
|
2 |
|
'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($tagGroup->getFieldLayout()), |
53
|
2 |
|
]; |
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
|
4 |
|
public function import(array $tagGroupDefinitions, $force = false) |
65
|
|
|
{ |
66
|
4 |
|
Craft::log(Craft::t('Importing TagGroups')); |
67
|
|
|
|
68
|
4 |
|
$tagGroups = Craft::app()->tags->getAllTagGroups('handle'); |
69
|
|
|
|
70
|
4 |
|
foreach ($tagGroupDefinitions as $tagGroupHandle => $tagGroupDefinition) { |
71
|
2 |
|
$tagGroup = array_key_exists($tagGroupHandle, $tagGroups) |
72
|
2 |
|
? $tagGroups[$tagGroupHandle] |
73
|
2 |
|
: new TagGroupModel(); |
74
|
|
|
|
75
|
2 |
|
unset($tagGroups[$tagGroupHandle]); |
76
|
|
|
|
77
|
2 |
|
$this->populateTagGroup($tagGroup, $tagGroupDefinition, $tagGroupHandle); |
78
|
|
|
|
79
|
2 |
|
if (!Craft::app()->tags->saveTagGroup($tagGroup)) { // Save taggroup via craft |
80
|
2 |
|
$this->addErrors($tagGroup->getAllErrors()); |
81
|
|
|
|
82
|
2 |
|
continue; |
83
|
|
|
} |
84
|
4 |
|
} |
85
|
|
|
|
86
|
4 |
|
if ($force) { |
87
|
2 |
|
foreach ($tagGroups as $tagGroup) { |
88
|
|
|
Craft::app()->tags->deleteTagGroupById($tagGroup->id); |
89
|
2 |
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
4 |
|
return $this->getResultModel(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Populate taggroup. |
97
|
|
|
* |
98
|
|
|
* @param TagGroupModel $tagGroup |
99
|
|
|
* @param array $tagGroupDefinition |
100
|
|
|
* @param string $tagGroupHandle |
101
|
|
|
*/ |
102
|
2 |
|
private function populateTagGroup(TagGroupModel $tagGroup, array $tagGroupDefinition, $tagGroupHandle) |
103
|
|
|
{ |
104
|
2 |
|
$tagGroup->setAttributes([ |
105
|
2 |
|
'handle' => $tagGroupHandle, |
106
|
2 |
|
'name' => $tagGroupDefinition['name'], |
107
|
2 |
|
]); |
108
|
|
|
|
109
|
2 |
|
$fieldLayout = Craft::app()->schematic_fields->getFieldLayout($tagGroupDefinition['fieldLayout']); |
110
|
2 |
|
$tagGroup->setFieldLayout($fieldLayout); |
111
|
2 |
|
} |
112
|
|
|
} |
113
|
|
|
|