1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* FeedsTermProcessor class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Feeds processor plugin. Create taxonomy terms from feed items.
|
10
|
*/
|
11
|
class FeedsTermProcessor extends FeedsProcessor {
|
12
|
/**
|
13
|
* Define entity type.
|
14
|
*/
|
15
|
public function entityType() {
|
16
|
return 'taxonomy_term';
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Implements parent::entityInfo().
|
21
|
*/
|
22
|
protected function entityInfo() {
|
23
|
$info = parent::entityInfo();
|
24
|
$info['label plural'] = t('Terms');
|
25
|
$info['bundle name'] = t('Vocabulary');
|
26
|
return $info;
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Creates a new term in memory and returns it.
|
31
|
*/
|
32
|
protected function newEntity(FeedsSource $source) {
|
33
|
$vocabulary = $this->vocabulary();
|
34
|
$term = new stdClass();
|
35
|
$term->vid = $vocabulary->vid;
|
36
|
$term->vocabulary_machine_name = $vocabulary->machine_name;
|
37
|
return $term;
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Validates a term.
|
42
|
*/
|
43
|
protected function entityValidate($term) {
|
44
|
if (drupal_strlen($term->name) == 0) {
|
45
|
throw new FeedsValidationException(t('Term name missing.'));
|
46
|
}
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Saves a term.
|
51
|
*
|
52
|
* We de-array parent fields with only one item.
|
53
|
* This stops leftandright module from freaking out.
|
54
|
*/
|
55
|
protected function entitySave($term) {
|
56
|
if (isset($term->parent)) {
|
57
|
if (is_array($term->parent) && count($term->parent) == 1) {
|
58
|
$term->parent = reset($term->parent);
|
59
|
}
|
60
|
if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
|
61
|
throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
|
62
|
}
|
63
|
}
|
64
|
taxonomy_term_save($term);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Deletes a series of terms.
|
69
|
*/
|
70
|
protected function entityDeleteMultiple($tids) {
|
71
|
foreach ($tids as $tid) {
|
72
|
taxonomy_term_delete($tid);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Override parent::configDefaults().
|
78
|
*/
|
79
|
public function configDefaults() {
|
80
|
return array(
|
81
|
'vocabulary' => 0,
|
82
|
) + parent::configDefaults();
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Overrides parent::setTargetElement().
|
87
|
*
|
88
|
* Operate on a target item that is a taxonomy term.
|
89
|
*/
|
90
|
public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value, array $mapping = array()) {
|
91
|
switch ($target_element) {
|
92
|
case 'parent':
|
93
|
if (!empty($value)) {
|
94
|
$terms = taxonomy_get_term_by_name($value);
|
95
|
$parent_tid = '';
|
96
|
foreach ($terms as $term) {
|
97
|
if ($term->vid == $target_term->vid) {
|
98
|
$parent_tid = $term->tid;
|
99
|
}
|
100
|
}
|
101
|
if (!empty($parent_tid)) {
|
102
|
$target_term->parent[] = $parent_tid;
|
103
|
}
|
104
|
else {
|
105
|
$target_term->parent[] = 0;
|
106
|
}
|
107
|
}
|
108
|
else {
|
109
|
$target_term->parent[] = 0;
|
110
|
}
|
111
|
break;
|
112
|
|
113
|
case 'parentguid':
|
114
|
// value is parent_guid field value
|
115
|
$query = db_select('feeds_item')
|
116
|
->fields('feeds_item', array('entity_id'))
|
117
|
->condition('entity_type', $this->entityType());
|
118
|
$parent_tid = $query->condition('guid', $value)->execute()->fetchField();
|
119
|
$target_term->parent[] = ($parent_tid) ? $parent_tid : 0;
|
120
|
break;
|
121
|
|
122
|
case 'weight':
|
123
|
if (!empty($value)) {
|
124
|
$weight = intval($value);
|
125
|
}
|
126
|
else {
|
127
|
$weight = 0;
|
128
|
}
|
129
|
$target_term->weight = $weight;
|
130
|
break;
|
131
|
|
132
|
case 'description':
|
133
|
if (!empty($mapping['format'])) {
|
134
|
$target_term->format = $mapping['format'];
|
135
|
}
|
136
|
elseif (!empty($this->config['input_format'])) {
|
137
|
$target_term->format = $this->config['input_format'];
|
138
|
}
|
139
|
else {
|
140
|
$target_term->format = filter_fallback_format();
|
141
|
}
|
142
|
$target_term->description = $value;
|
143
|
break;
|
144
|
|
145
|
default:
|
146
|
parent::setTargetElement($source, $target_term, $target_element, $value);
|
147
|
break;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Return available mapping targets.
|
153
|
*/
|
154
|
public function getMappingTargets() {
|
155
|
$targets = parent::getMappingTargets();
|
156
|
$targets += array(
|
157
|
'name' => array(
|
158
|
'name' => t('Term name'),
|
159
|
'description' => t('Name of the taxonomy term.'),
|
160
|
'optional_unique' => TRUE,
|
161
|
),
|
162
|
'parent' => array(
|
163
|
'name' => t('Parent: Term name'),
|
164
|
'description' => t('The name of the parent taxonomy term.'),
|
165
|
'optional_unique' => TRUE,
|
166
|
),
|
167
|
'parentguid' => array(
|
168
|
'name' => t('Parent: GUID'),
|
169
|
'description' => t('The GUID of the parent taxonomy term.'),
|
170
|
'optional_unique' => TRUE,
|
171
|
),
|
172
|
'weight' => array(
|
173
|
'name' => t('Term weight'),
|
174
|
'description' => t('Weight of the taxonomy term.'),
|
175
|
'optional_unique' => TRUE,
|
176
|
),
|
177
|
'description' => array(
|
178
|
'name' => t('Term description'),
|
179
|
'description' => t('Description of the taxonomy term.'),
|
180
|
'summary_callbacks' => array('text_feeds_summary_callback'),
|
181
|
'form_callbacks' => array('text_feeds_form_callback'),
|
182
|
),
|
183
|
);
|
184
|
|
185
|
$this->getHookTargets($targets);
|
186
|
|
187
|
return $targets;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Get id of an existing feed item term if available.
|
192
|
*/
|
193
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
194
|
if ($tid = parent::existingEntityId($source, $result)) {
|
195
|
return $tid;
|
196
|
}
|
197
|
|
198
|
// The only possible unique target is name.
|
199
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
200
|
if ($target == 'name') {
|
201
|
$vocabulary = $this->vocabulary();
|
202
|
if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
|
203
|
return $tid;
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
return 0;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Return vocabulary to map to.
|
212
|
*/
|
213
|
public function vocabulary() {
|
214
|
if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->bundle())) {
|
215
|
return $vocabulary;
|
216
|
}
|
217
|
throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Overrides FeedsProcessor::dependencies().
|
222
|
*/
|
223
|
public function dependencies() {
|
224
|
$dependencies = parent::dependencies();
|
225
|
$dependencies['taxonomy'] = 'taxonomy';
|
226
|
return $dependencies;
|
227
|
}
|
228
|
|
229
|
}
|