1 |
85ad3d82
|
Assos Assos
|
<?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 |
a192dc0b
|
Assos Assos
|
$term = parent::newEntity($source);
|
35 |
85ad3d82
|
Assos Assos
|
$term->vid = $vocabulary->vid;
|
36 |
|
|
$term->vocabulary_machine_name = $vocabulary->machine_name;
|
37 |
a192dc0b
|
Assos Assos
|
|
38 |
85ad3d82
|
Assos Assos
|
return $term;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
a192dc0b
|
Assos Assos
|
/**
|
42 |
|
|
* Load an existing entity.
|
43 |
|
|
*/
|
44 |
|
|
protected function entityLoad(FeedsSource $source, $entity_id) {
|
45 |
|
|
$entity = parent::entityLoad($source, $entity_id);
|
46 |
|
|
|
47 |
|
|
// Avoid missing bundle errors when term has been loaded directly from db.
|
48 |
|
|
if (empty($entity->vocabulary_machine_name) && !empty($entity->vid)) {
|
49 |
|
|
$vocabulary = taxonomy_vocabulary_load($entity->vid);
|
50 |
|
|
$entity->vocabulary_machine_name = ($vocabulary) ? $vocabulary->machine_name : NULL;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
return $entity;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
85ad3d82
|
Assos Assos
|
/**
|
57 |
|
|
* Validates a term.
|
58 |
|
|
*/
|
59 |
|
|
protected function entityValidate($term) {
|
60 |
a192dc0b
|
Assos Assos
|
parent::entityValidate($term);
|
61 |
|
|
|
62 |
85ad3d82
|
Assos Assos
|
if (drupal_strlen($term->name) == 0) {
|
63 |
|
|
throw new FeedsValidationException(t('Term name missing.'));
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Saves a term.
|
69 |
|
|
*
|
70 |
|
|
* We de-array parent fields with only one item.
|
71 |
|
|
* This stops leftandright module from freaking out.
|
72 |
|
|
*/
|
73 |
|
|
protected function entitySave($term) {
|
74 |
|
|
if (isset($term->parent)) {
|
75 |
|
|
if (is_array($term->parent) && count($term->parent) == 1) {
|
76 |
|
|
$term->parent = reset($term->parent);
|
77 |
|
|
}
|
78 |
|
|
if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
|
79 |
|
|
throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
taxonomy_term_save($term);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* Deletes a series of terms.
|
87 |
|
|
*/
|
88 |
|
|
protected function entityDeleteMultiple($tids) {
|
89 |
|
|
foreach ($tids as $tid) {
|
90 |
|
|
taxonomy_term_delete($tid);
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Override parent::configDefaults().
|
96 |
|
|
*/
|
97 |
|
|
public function configDefaults() {
|
98 |
|
|
return array(
|
99 |
|
|
'vocabulary' => 0,
|
100 |
|
|
) + parent::configDefaults();
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
41cc1b08
|
Assos Assos
|
* Overrides parent::setTargetElement().
|
105 |
|
|
*
|
106 |
|
|
* Operate on a target item that is a taxonomy term.
|
107 |
85ad3d82
|
Assos Assos
|
*/
|
108 |
41cc1b08
|
Assos Assos
|
public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value, array $mapping = array()) {
|
109 |
85ad3d82
|
Assos Assos
|
switch ($target_element) {
|
110 |
|
|
case 'parent':
|
111 |
|
|
if (!empty($value)) {
|
112 |
|
|
$terms = taxonomy_get_term_by_name($value);
|
113 |
|
|
$parent_tid = '';
|
114 |
|
|
foreach ($terms as $term) {
|
115 |
|
|
if ($term->vid == $target_term->vid) {
|
116 |
|
|
$parent_tid = $term->tid;
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
if (!empty($parent_tid)) {
|
120 |
|
|
$target_term->parent[] = $parent_tid;
|
121 |
|
|
}
|
122 |
|
|
else {
|
123 |
|
|
$target_term->parent[] = 0;
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
else {
|
127 |
|
|
$target_term->parent[] = 0;
|
128 |
|
|
}
|
129 |
|
|
break;
|
130 |
41cc1b08
|
Assos Assos
|
|
131 |
85ad3d82
|
Assos Assos
|
case 'parentguid':
|
132 |
|
|
// value is parent_guid field value
|
133 |
a192dc0b
|
Assos Assos
|
$parent_tid = 0;
|
134 |
85ad3d82
|
Assos Assos
|
$query = db_select('feeds_item')
|
135 |
|
|
->fields('feeds_item', array('entity_id'))
|
136 |
|
|
->condition('entity_type', $this->entityType());
|
137 |
a192dc0b
|
Assos Assos
|
$term_ids = array_keys($query->condition('guid', $value)->execute()->fetchAllAssoc('entity_id'));
|
138 |
|
|
if (!empty($term_ids)) {
|
139 |
|
|
$terms = entity_load($this->entityType(), $term_ids);
|
140 |
|
|
foreach ($terms as $term) {
|
141 |
|
|
if ($term->vid == $target_term->vid) {
|
142 |
|
|
$parent_tid = $term->tid;
|
143 |
|
|
break;
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
$target_term->parent[] = $parent_tid;
|
148 |
85ad3d82
|
Assos Assos
|
break;
|
149 |
41cc1b08
|
Assos Assos
|
|
150 |
85ad3d82
|
Assos Assos
|
case 'weight':
|
151 |
|
|
if (!empty($value)) {
|
152 |
|
|
$weight = intval($value);
|
153 |
|
|
}
|
154 |
|
|
else {
|
155 |
|
|
$weight = 0;
|
156 |
|
|
}
|
157 |
|
|
$target_term->weight = $weight;
|
158 |
|
|
break;
|
159 |
41cc1b08
|
Assos Assos
|
|
160 |
|
|
case 'description':
|
161 |
|
|
if (!empty($mapping['format'])) {
|
162 |
|
|
$target_term->format = $mapping['format'];
|
163 |
|
|
}
|
164 |
|
|
elseif (!empty($this->config['input_format'])) {
|
165 |
|
|
$target_term->format = $this->config['input_format'];
|
166 |
|
|
}
|
167 |
|
|
else {
|
168 |
|
|
$target_term->format = filter_fallback_format();
|
169 |
|
|
}
|
170 |
|
|
$target_term->description = $value;
|
171 |
|
|
break;
|
172 |
|
|
|
173 |
85ad3d82
|
Assos Assos
|
default:
|
174 |
|
|
parent::setTargetElement($source, $target_term, $target_element, $value);
|
175 |
|
|
break;
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
/**
|
180 |
|
|
* Return available mapping targets.
|
181 |
|
|
*/
|
182 |
|
|
public function getMappingTargets() {
|
183 |
|
|
$targets = parent::getMappingTargets();
|
184 |
|
|
$targets += array(
|
185 |
|
|
'name' => array(
|
186 |
|
|
'name' => t('Term name'),
|
187 |
|
|
'description' => t('Name of the taxonomy term.'),
|
188 |
|
|
'optional_unique' => TRUE,
|
189 |
|
|
),
|
190 |
|
|
'parent' => array(
|
191 |
|
|
'name' => t('Parent: Term name'),
|
192 |
|
|
'description' => t('The name of the parent taxonomy term.'),
|
193 |
|
|
'optional_unique' => TRUE,
|
194 |
|
|
),
|
195 |
|
|
'parentguid' => array(
|
196 |
|
|
'name' => t('Parent: GUID'),
|
197 |
|
|
'description' => t('The GUID of the parent taxonomy term.'),
|
198 |
|
|
'optional_unique' => TRUE,
|
199 |
|
|
),
|
200 |
|
|
'weight' => array(
|
201 |
|
|
'name' => t('Term weight'),
|
202 |
|
|
'description' => t('Weight of the taxonomy term.'),
|
203 |
|
|
'optional_unique' => TRUE,
|
204 |
|
|
),
|
205 |
|
|
'description' => array(
|
206 |
|
|
'name' => t('Term description'),
|
207 |
|
|
'description' => t('Description of the taxonomy term.'),
|
208 |
41cc1b08
|
Assos Assos
|
'summary_callbacks' => array('text_feeds_summary_callback'),
|
209 |
|
|
'form_callbacks' => array('text_feeds_form_callback'),
|
210 |
85ad3d82
|
Assos Assos
|
),
|
211 |
|
|
);
|
212 |
|
|
|
213 |
41cc1b08
|
Assos Assos
|
$this->getHookTargets($targets);
|
214 |
|
|
|
215 |
85ad3d82
|
Assos Assos
|
return $targets;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
/**
|
219 |
|
|
* Get id of an existing feed item term if available.
|
220 |
|
|
*/
|
221 |
|
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
222 |
|
|
if ($tid = parent::existingEntityId($source, $result)) {
|
223 |
|
|
return $tid;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
// The only possible unique target is name.
|
227 |
|
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
228 |
|
|
if ($target == 'name') {
|
229 |
|
|
$vocabulary = $this->vocabulary();
|
230 |
|
|
if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
|
231 |
|
|
return $tid;
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
return 0;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
/**
|
239 |
|
|
* Return vocabulary to map to.
|
240 |
|
|
*/
|
241 |
|
|
public function vocabulary() {
|
242 |
|
|
if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->bundle())) {
|
243 |
|
|
return $vocabulary;
|
244 |
|
|
}
|
245 |
|
|
throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
|
246 |
|
|
}
|
247 |
41cc1b08
|
Assos Assos
|
|
248 |
|
|
/**
|
249 |
|
|
* Overrides FeedsProcessor::dependencies().
|
250 |
|
|
*/
|
251 |
|
|
public function dependencies() {
|
252 |
|
|
$dependencies = parent::dependencies();
|
253 |
|
|
$dependencies['taxonomy'] = 'taxonomy';
|
254 |
|
|
return $dependencies;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
85ad3d82
|
Assos Assos
|
} |