Newer
Older
Alex Barth
committed
<?php
/**
* @file
* FeedsTermProcessor class.
*/
/**
* Feeds processor plugin. Create taxonomy terms from feed items.
*/
class FeedsTermProcessor extends FeedsProcessor {
public function entityType() {
return 'taxonomy_term';
Alex Barth
committed
/**
* Implements parent::entityInfo().
Alex Barth
committed
*/
protected function entityInfo() {
$info = parent::entityInfo();
$info['label plural'] = t('Terms');
return $info;
Alex Barth
committed
}
/**
* Creates a new term in memory and returns it.
Alex Barth
committed
*/
protected function newEntity(FeedsSource $source) {
Alex Barth
committed
$vocabulary = $this->vocabulary();
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->vocabulary_machine_name = $vocabulary->machine_name;
return $term;
}
/**
* Loads an existing term.
*/
protected function entityLoad(FeedsSource $source, $tid) {
return taxonomy_term_load($tid);
}
/**
* Validates a term.
*/
protected function entityValidate($term) {
throw new FeedsValidationException(t('Term name missing.'));
Alex Barth
committed
}
/**
* Saves a term.
Franz Glauber Vanderlinde
committed
*
* We de-array parent fields with only one item.
* This stops leftandright module from freaking out.
*/
protected function entitySave($term) {
Franz Glauber Vanderlinde
committed
if (isset($term->parent)) {
if (is_array($term->parent) && count($term->parent) == 1) {
$term->parent = reset($term->parent);
}
if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
}
}
taxonomy_term_save($term);
* Deletes a series of terms.
protected function entityDeleteMultiple($tids) {
foreach ($tids as $tid) {
taxonomy_term_delete($tid);
}
Alex Barth
committed
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'vocabulary' => 0,
Franz Glauber Vanderlinde
committed
'force_update' => TRUE,
) + parent::configDefaults();
Alex Barth
committed
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$options = array(0 => t('Select a vocabulary'));
Alex Barth
committed
foreach (taxonomy_get_vocabularies() as $vocab) {
$options[$vocab->machine_name] = $vocab->name;
Alex Barth
committed
}
$form = parent::configForm($form_state);
Alex Barth
committed
$form['vocabulary'] = array(
'#type' => 'select',
'#title' => t('Import to vocabulary'),
'#description' => t('Choose the vocabulary to import into. <strong>CAUTION:</strong> when deleting terms through the "Delete items" tab, Feeds will delete <em>all</em> terms from this vocabulary.'),
'#options' => $options,
'#default_value' => $this->config['vocabulary'],
);
Franz Glauber Vanderlinde
committed
$form['force_update'] = array(
'#type' => 'checkbox',
'#title' => t('Force update'),
'#description' => t('Force update of imported terms even if no data change. For import of <strong>term parents</strong> you need to import twice and set this in the second pass.'),
'#default_value' => $this->config['force_update'],
);
Alex Barth
committed
return $form;
}
Alex Barth
committed
/**
* Override parent::configFormValidate().
*/
public function configFormValidate(&$values) {
if (empty($values['vocabulary'])) {
form_set_error('vocabulary', t('Choose a vocabulary'));
}
}
Franz Glauber Vanderlinde
committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Override setTargetElement to operate on a target item that is a term.
*/
public function setTargetElement(FeedsSource $source, $target_node, $target_element, $value) {
switch ($target_element) {
case 'parent':
if (!empty($value)) {
$terms = taxonomy_get_term_by_name($value);
$parent_tid = '';
foreach ($terms as $term) {
if ($term->vid == $target_node->vid) {
$parent_tid = $term->tid;
}
}
if (!empty($parent_tid)) {
$target_node->parent[] = $parent_tid;
}
else {
$target_node->parent[] = 0;
}
}
else {
$target_node->parent[] = 0;
}
break;
case 'parentguid':
// value is parent_guid field value
$query = db_select('feeds_item')
->fields('feeds_item', array('entity_id'))
->condition('entity_type', $this->entityType());
$parent_tid = $query->condition('guid', $value)->execute()->fetchField();
$target_node->parent[] = ($parent_tid) ? $parent_tid : 0;
break;
case 'weight':
if (!empty($value)) {
$weight = intval($value);
}
else {
$weight = 0;
}
$target_node->weight = $weight;
break;
default:
parent::setTargetElement($source, $target_node, $target_element, $value);
break;
}
}
Alex Barth
committed
/**
* Return available mapping targets.
*/
public function getMappingTargets() {
$targets = parent::getMappingTargets();
$targets += array(
Alex Barth
committed
'name' => array(
'name' => t('Term name'),
'description' => t('Name of the taxonomy term.'),
Alex Barth
committed
'optional_unique' => TRUE,
),
Franz Glauber Vanderlinde
committed
'parent' => array(
'name' => t('Parent Term Name'),
'description' => t('Name of the Parent Term.'),
'optional_unique' => TRUE,
),
'parentguid' => array(
'name' => t('Parent GUID'),
'description' => t('The globally unique identifier of the item\'s parent term.'),
'optional_unique' => TRUE,
),
'weight' => array(
'name' => t('Term Weight'),
'description' => t('Weight of the Term.'),
'optional_unique' => TRUE,
),
'description' => array(
'name' => t('Term description'),
'description' => t('Description of the taxonomy term.'),
),
Alex Barth
committed
);
// Let implementers of hook_feeds_term_processor_targets() add their targets.
Alex Barth
committed
self::loadMappers();
feeds_alter('feeds_processor_targets', $targets, 'taxonomy_term', $this->vocabulary()->machine_name);
Alex Barth
committed
}
catch (Exception $e) {
// Do nothing.
}
Alex Barth
committed
return $targets;
}
/**
* Get id of an existing feed item term if available.
*/
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
if ($tid = parent::existingEntityId($source, $result)) {
Alex Barth
committed
// The only possible unique target is name.
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
Alex Barth
committed
if ($target == 'name') {
Alex Barth
committed
$vocabulary = $this->vocabulary();
Alex Barth
committed
if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
Alex Barth
committed
return $tid;
}
Franz Glauber Vanderlinde
committed
} else if($target == 'guid') {
$query = db_select('feeds_item')
->fields('feeds_item', array('entity_id'))
->condition('entity_type', 'taxonomy_term');
$tid = $query->condition('guid', $value)->execute()->fetchField();
return ($tid) ? $tid : 0;
Alex Barth
committed
}
}
return 0;
}
Alex Barth
committed
/**
* Return vocabulary to map to.
*/
public function vocabulary() {
Alex Barth
committed
if (isset($this->config['vocabulary'])) {
Alex Barth
committed
if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->config['vocabulary'])) {
return $vocabulary;
}
Alex Barth
committed
}
Alex Barth
committed
throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
Alex Barth
committed
}
Franz Glauber Vanderlinde
committed
/**
* Override getHash()
*
* @return string
*/
protected function getHash($entity_id) {
if($this->config['force_update']) {
return ''; // dummy to always update
} else {
return parent::getHash($entity_id);
}
}