1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\CategoryGroupModel; |
7
|
|
|
use Craft\CategoryGroupLocaleModel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Category Groups Service. |
11
|
|
|
* |
12
|
|
|
* Sync Craft Setups. |
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 CategoryGroups extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export categoryGroups. |
24
|
|
|
* |
25
|
|
|
* @param CategoryGroupModel[] $categoryGroups |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
3 |
|
public function export(array $categoryGroups = []) |
30
|
|
|
{ |
31
|
3 |
|
Craft::log(Craft::t('Exporting Category Groups')); |
32
|
|
|
|
33
|
3 |
|
$categoryGroupDefinitions = []; |
34
|
|
|
|
35
|
3 |
|
foreach ($categoryGroups as $categoryGroup) { |
36
|
2 |
|
$categoryGroupDefinitions[$categoryGroup->handle] = $this->getCategoryGroupDefinition($categoryGroup); |
37
|
3 |
|
} |
38
|
|
|
|
39
|
3 |
|
return $categoryGroupDefinitions; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get category groups definition. |
44
|
|
|
* |
45
|
|
|
* @param CategoryGroupModel $categoryGroup |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
4 |
|
private function getCategoryGroupDefinition(CategoryGroupModel $categoryGroup) |
50
|
|
|
{ |
51
|
2 |
|
$fieldLayout = Craft::app()->fields->getLayoutById($categoryGroup->fieldLayoutId); |
52
|
|
|
return [ |
53
|
2 |
|
'name' => $categoryGroup->name, |
54
|
2 |
|
'hasUrls' => $categoryGroup->hasUrls, |
55
|
2 |
|
'template' => $categoryGroup->template, |
56
|
4 |
|
'maxLevels' => $categoryGroup->maxLevels, |
57
|
2 |
|
'locales' => $this->getLocaleDefinitions($categoryGroup->getLocales()), |
58
|
2 |
|
'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($fieldLayout), |
59
|
2 |
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get locale definitions. |
64
|
|
|
* |
65
|
|
|
* @param CategoryGroupLocaleModel[] $locales |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
2 |
View Code Duplication |
private function getLocaleDefinitions(array $locales) |
|
|
|
|
70
|
|
|
{ |
71
|
2 |
|
$localeDefinitions = []; |
72
|
|
|
|
73
|
2 |
|
foreach ($locales as $locale) { |
74
|
2 |
|
$localeDefinitions[$locale->locale] = $this->getLocaleDefinition($locale); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
return $localeDefinitions; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get locale definition. |
82
|
|
|
* |
83
|
|
|
* @param CategoryGroupLocaleModel $locale |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
2 |
|
private function getLocaleDefinition(CategoryGroupLocaleModel $locale) |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
2 |
|
'urlFormat' => $locale->urlFormat, |
91
|
2 |
|
'nestedUrlFormat' => $locale->nestedUrlFormat, |
92
|
2 |
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Attempt to import category groups. |
97
|
|
|
* |
98
|
|
|
* @param array $categoryGroupDefinitions |
99
|
|
|
* @param bool $force If set to true category groups not included in the import will be deleted |
100
|
|
|
* |
101
|
|
|
* @return Result |
102
|
|
|
*/ |
103
|
4 |
View Code Duplication |
public function import(array $categoryGroupDefinitions, $force = false) |
|
|
|
|
104
|
|
|
{ |
105
|
4 |
|
Craft::log(Craft::t('Importing Category Groups')); |
106
|
|
|
|
107
|
4 |
|
$categoryGroups = Craft::app()->categories->getAllGroups('handle'); |
108
|
|
|
|
109
|
4 |
|
foreach ($categoryGroupDefinitions as $categoryGroupHandle => $categoryGroupDefinition) { |
110
|
2 |
|
$categoryGroup = array_key_exists($categoryGroupHandle, $categoryGroups) |
111
|
2 |
|
? $categoryGroups[$categoryGroupHandle] |
112
|
2 |
|
: new CategoryGroupModel(); |
113
|
|
|
|
114
|
2 |
|
unset($categoryGroups[$categoryGroupHandle]); |
115
|
|
|
|
116
|
2 |
|
$this->populateCategoryGroup($categoryGroup, $categoryGroupDefinition, $categoryGroupHandle); |
117
|
|
|
|
118
|
2 |
|
if (!Craft::app()->categories->saveGroup($categoryGroup)) { // Save categorygroup via craft |
119
|
2 |
|
$this->addErrors($categoryGroup->getAllErrors()); |
120
|
|
|
|
121
|
2 |
|
continue; |
122
|
|
|
} |
123
|
4 |
|
} |
124
|
|
|
|
125
|
4 |
|
if ($force) { |
126
|
2 |
|
foreach ($categoryGroups as $categoryGroup) { |
127
|
|
|
Craft::app()->categories->deleteGroupById($categoryGroup->id); |
128
|
2 |
|
} |
129
|
2 |
|
} |
130
|
|
|
|
131
|
4 |
|
return $this->getResultModel(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Populate categorygroup. |
136
|
|
|
* |
137
|
|
|
* @param CategoryGroupModel $categoryGroup |
138
|
|
|
* @param array $categoryGroupDefinition |
139
|
|
|
* @param string $categoryGroupHandle |
140
|
|
|
*/ |
141
|
2 |
View Code Duplication |
private function populateCategoryGroup(CategoryGroupModel $categoryGroup, array $categoryGroupDefinition, $categoryGroupHandle) |
|
|
|
|
142
|
|
|
{ |
143
|
2 |
|
$categoryGroup->setAttributes([ |
144
|
2 |
|
'handle' => $categoryGroupHandle, |
145
|
2 |
|
'name' => $categoryGroupDefinition['name'], |
146
|
2 |
|
'hasUrls' => $categoryGroupDefinition['hasUrls'], |
147
|
2 |
|
'template' => $categoryGroupDefinition['template'], |
148
|
2 |
|
'maxLevels' => $categoryGroupDefinition['maxLevels'], |
149
|
2 |
|
]); |
150
|
|
|
|
151
|
2 |
|
$this->populateCategoryGroupLocales($categoryGroup, $categoryGroupDefinition['locales']); |
152
|
|
|
|
153
|
2 |
|
$fieldLayout = Craft::app()->schematic_fields->getFieldLayout($categoryGroupDefinition['fieldLayout']); |
154
|
2 |
|
$categoryGroup->setFieldLayout($fieldLayout); |
155
|
2 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Populate section locales. |
159
|
|
|
* |
160
|
|
|
* @param CategoryGroupModel $categoryGroup |
161
|
|
|
* @param $localeDefinitions |
162
|
|
|
*/ |
163
|
2 |
View Code Duplication |
private function populateCategoryGroupLocales(CategoryGroupModel $categoryGroup, $localeDefinitions) |
|
|
|
|
164
|
|
|
{ |
165
|
2 |
|
$locales = $categoryGroup->getLocales(); |
166
|
|
|
|
167
|
2 |
|
foreach ($localeDefinitions as $localeId => $localeDef) { |
168
|
2 |
|
$locale = array_key_exists($localeId, $locales) ? $locales[$localeId] : new CategoryGroupLocaleModel(); |
169
|
|
|
|
170
|
2 |
|
$locale->setAttributes([ |
171
|
2 |
|
'locale' => $localeId, |
172
|
2 |
|
'urlFormat' => $localeDef['urlFormat'], |
173
|
2 |
|
'nestedUrlFormat' => $localeDef['nestedUrlFormat'], |
174
|
2 |
|
]); |
175
|
|
|
|
176
|
|
|
// Todo: Is this a hack? I don't see another way. |
177
|
|
|
// Todo: Might need a sorting order as well? It's NULL at the moment. |
178
|
2 |
|
Craft::app()->db->createCommand()->insertOrUpdate('locales', [ |
179
|
2 |
|
'locale' => $locale->locale, |
180
|
2 |
|
], []); |
181
|
|
|
|
182
|
2 |
|
$locales[$localeId] = $locale; |
183
|
2 |
|
} |
184
|
|
|
|
185
|
2 |
|
$categoryGroup->setLocales($locales); |
186
|
2 |
|
} |
187
|
|
|
} |
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.