1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Export Category Service. |
7
|
|
|
* |
8
|
|
|
* Handles exporting categories |
9
|
|
|
* |
10
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2015, Bob Olde Hampsink |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* @link http://github.com/boboldehampsink |
15
|
|
|
*/ |
16
|
|
|
class Export_CategoryService extends BaseApplicationComponent implements IExportElementType |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Return export template. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
1 |
|
public function getTemplate() |
24
|
|
|
{ |
25
|
1 |
|
return 'export/sources/_category'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get category groups. |
30
|
|
|
* |
31
|
|
|
* @return array|bool |
32
|
|
|
*/ |
33
|
1 |
|
public function getGroups() |
34
|
|
|
{ |
35
|
|
|
// Return editable groups for user |
36
|
1 |
|
return craft()->categories->getEditableGroups(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return category fields. |
41
|
|
|
* |
42
|
|
|
* @param array $settings |
43
|
|
|
* @param bool $reset |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
2 |
|
public function getFields(array $settings, $reset) |
48
|
|
|
{ |
49
|
|
|
// Set criteria |
50
|
2 |
|
$criteria = new \CDbCriteria(); |
51
|
2 |
|
$criteria->condition = 'settings = :settings'; |
52
|
2 |
|
$criteria->params = array( |
53
|
2 |
|
':settings' => JsonHelper::encode($settings), |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
// Check if we have a map already |
57
|
2 |
|
$stored = craft()->export->findMap($criteria); |
58
|
|
|
|
59
|
2 |
|
if (!count($stored) || $reset) { |
60
|
|
|
|
61
|
|
|
// Set the static fields for this type |
62
|
|
|
$fields = array( |
63
|
1 |
|
ExportModel::HandleId => array('name' => Craft::t('ID'), 'checked' => 0), |
64
|
1 |
|
ExportModel::HandleTitle => array('name' => Craft::t('Title'), 'checked' => 1), |
65
|
1 |
|
ExportModel::HandleSlug => array('name' => Craft::t('Slug'), 'checked' => 0), |
66
|
1 |
|
ExportModel::HandleParent => array('name' => Craft::t('Parent'), 'checked' => 0), |
67
|
1 |
|
ExportModel::HandleAncestors => array('name' => Craft::t('Ancestors'), 'checked' => 0), |
68
|
1 |
|
); |
69
|
|
|
|
70
|
|
|
// Set the dynamic fields for this type |
71
|
1 |
|
foreach (craft()->fields->getLayoutByType(ElementType::Category)->getFields() as $field) { |
72
|
1 |
|
$data = $field->getField(); |
73
|
1 |
|
$fields[$data->handle] = array('name' => $data->name, 'checked' => 1, 'fieldtype' => $data->type); |
74
|
1 |
|
} |
75
|
1 |
|
} else { |
76
|
|
|
|
77
|
|
|
// Get the stored map |
78
|
1 |
|
$fields = $stored->map; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Return fields |
82
|
2 |
|
return $fields; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set entry criteria. |
87
|
|
|
* |
88
|
|
|
* @param array $settings |
89
|
|
|
* |
90
|
|
|
* @return ElementCriteriaModel |
91
|
|
|
*/ |
92
|
1 |
|
public function setCriteria(array $settings) |
93
|
|
|
{ |
94
|
|
|
// Match with current data |
95
|
1 |
|
$criteria = craft()->elements->getCriteria(ElementType::Category); |
96
|
1 |
|
$criteria->order = 'id '.$settings['sort']; |
97
|
1 |
|
$criteria->offset = $settings['offset']; |
98
|
1 |
|
$criteria->limit = $settings['limit']; |
99
|
1 |
|
$criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null; |
100
|
|
|
|
101
|
|
|
// Look in same group when replacing |
102
|
1 |
|
$criteria->groupId = $settings['elementvars']['group']; |
103
|
|
|
|
104
|
1 |
|
return $criteria; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get category attributes. |
109
|
|
|
* |
110
|
|
|
* @param array $map |
111
|
|
|
* @param BaseElementModel $element |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
1 |
|
public function getAttributes(array $map, BaseElementModel $element) |
116
|
|
|
{ |
117
|
1 |
|
$attributes = array(); |
118
|
|
|
|
119
|
|
|
// Try to parse checked fields through prepValue |
120
|
1 |
|
foreach ($map as $handle => $data) { |
121
|
1 |
|
if ($data['checked']) { |
122
|
|
|
try { |
123
|
1 |
|
$attributes[$handle] = $element->$handle; |
124
|
1 |
|
} catch (\Exception $e) { |
125
|
1 |
|
$attributes[$handle] = null; |
126
|
|
|
} |
127
|
1 |
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
// Get parent for categories |
131
|
1 |
|
if (array_key_exists(ExportModel::HandleParent, $map)) { |
132
|
1 |
|
$attributes[ExportModel::HandleParent] = $element->getAncestors() ? $element->getAncestors(1)->first() : ''; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
// Get ancestors for categories |
136
|
1 |
|
if (array_key_exists(ExportModel::HandleAncestors, $map)) { |
137
|
1 |
|
$attributes[ExportModel::HandleAncestors] = $element->getAncestors() ? implode('/', $element->getAncestors()->find()) : ''; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
// Call hook allowing 3rd-party plugins to modify attributes |
141
|
1 |
|
craft()->plugins->call('modifyExportAttributes', array(&$attributes, $element)); |
142
|
|
|
|
143
|
1 |
|
return $attributes; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|