Newer
Older
Alex Barth
committed
<?php
// $Id$
/**
* @file
* FeedsTermProcessor class.
*/
/**
* Feeds processor plugin. Create taxonomy terms from feed items.
*/
class FeedsTermProcessor extends FeedsProcessor {
/**
* Implements FeedsProcessor::process().
Alex Barth
committed
*/
public function process(FeedsSource $source, FeedsParserResult $parser_result) {
Alex Barth
committed
if (empty($this->config['vocabulary'])) {
throw new Exception(t('You must define a vocabulary for Taxonomy term processor before importing.'));
}
// Count number of created and updated nodes.
$created = $updated = $no_name = 0;
while ($item = $parser_result->shiftItem()) {
Alex Barth
committed
if (!($tid = $this->existingItemId($source, $parser_result)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {
Alex Barth
committed
// Map item to a term.
Alex Barth
committed
$term = new stdClass();
if ($tid && $this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
Alex Barth
committed
$term = taxonomy_term_load($tid);
Alex Barth
committed
$term->entity_type = 'taxonomy_term';
$term = $this->map($source, $parser_result, $term);
Alex Barth
committed
// Check if term name is set, otherwise continue.
Alex Barth
committed
if (empty($term->name)) {
Alex Barth
committed
$no_name++;
continue;
}
// Add term id if available.
if (!empty($tid)) {
Alex Barth
committed
$term->tid = $tid;
Alex Barth
committed
}
// Save the term.
Alex Barth
committed
$term->feeds_importer_id = $this->id;
$term->feed_nid = $source->feed_nid;
taxonomy_term_save($term);
Alex Barth
committed
if ($tid) {
$updated++;
}
else {
$created++;
}
}
}
// Set messages.
Alex Barth
committed
$vocabulary = $this->vocabulary();
Alex Barth
committed
if ($no_name) {
drupal_set_message(
format_plural(
$no_name,
'There was @number term that could not be imported because their name was empty. Check mapping settings on Taxomy term processor.',
'There were @number terms that could not be imported because their name was empty. Check mapping settings on Taxomy term processor.',
array('@number' => $no_name)
),
'error'
);
Alex Barth
committed
}
if ($created) {
drupal_set_message(format_plural($created, 'Created @number term in !vocabulary.', 'Created @number terms in !vocabulary.', array('@number' => $created, '!vocabulary' => $vocabulary->name)));
Alex Barth
committed
}
elseif ($updated) {
drupal_set_message(format_plural($updated, 'Updated @number term in !vocabulary.', 'Updated @number terms in !vocabulary.', array('@number' => $updated, '!vocabulary' => $vocabulary->name)));
Alex Barth
committed
}
else {
drupal_set_message(t('There are no new terms.'));
}
}
/**
* Implements FeedsProcessor::clear().
Alex Barth
committed
*/
public function clear(FeedsSource $source) {
Alex Barth
committed
$deleted = 0;
Alex Barth
committed
$vocabulary = $this->vocabulary();
Alex Barth
committed
FROM {taxonomy_term_data} td
WHERE td.vid = :vid
AND ft.id = :id
AND ft.feed_nid = :feed_nid",
array(
':vid' => $vocabulary->vid,
':id' => $this->id,
':feed_nid' => $source->feed_nid,
));
foreach ($terms as $term) {
Alex Barth
committed
if (taxonomy_term_delete($term->tid) == SAVED_DELETED) {
Alex Barth
committed
$deleted++;
}
}
// Set messages.
if ($deleted) {
drupal_set_message(format_plural($deleted, 'Deleted @number term from !vocabulary.', 'Deleted @number terms from !vocabulary.', array('@number' => $deleted, '!vocabulary' => $vocabulary->name)));
Alex Barth
committed
}
else {
drupal_set_message(t('No terms to be deleted.'));
}
}
/**
* Execute mapping on an item.
*/
protected function map(FeedsSource $source, FeedsParserResult $result, $target_term = NULL) {
// Prepare term object, have parent class do the iterating.
if (!$target_term) {
Alex Barth
committed
$target_term = new stdClass();
}
Alex Barth
committed
if (!$vocabulary = $this->vocabulary()) {
throw new Exception(t('No vocabulary specified for term processor'));
Alex Barth
committed
}
Alex Barth
committed
$target_term->vid = $vocabulary->vid;
$target_term = parent::map($source, $result, $target_term);
Alex Barth
committed
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'vocabulary' => 0,
'update_existing' => FEEDS_SKIP_EXISTING,
Alex Barth
committed
'mappings' => array(),
);
}
/**
* 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] = check_plain($vocab->name);
Alex Barth
committed
}
$form = array();
$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'],
);
$form['update_existing'] = array(
'#type' => 'radios',
'#title' => t('Update existing terms'),
'#description' => t('Select how existing terms should be updated. Existing terms will be determined using mappings that are a "unique target".'),
'#options' => array(
FEEDS_SKIP_EXISTING => 'Do not update existing terms',
FEEDS_REPLACE_EXISTING => 'Replace existing terms',
FEEDS_UPDATE_EXISTING => 'Update existing terms (slower than replacing them)',
),
Alex Barth
committed
'#default_value' => $this->config['update_existing'],
);
return $form;
}
Alex Barth
committed
/**
* Override parent::configFormValidate().
*/
public function configFormValidate(&$values) {
if (empty($values['vocabulary'])) {
form_set_error('vocabulary', t('Choose a vocabulary'));
}
}
Alex Barth
committed
/**
* Return available mapping targets.
*/
public function getMappingTargets() {
$targets = array(
'name' => array(
'name' => t('Term name'),
'description' => t('Name of the taxonomy term.'),
Alex Barth
committed
'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
if ($vocabulary = $this->vocabulary()) {
self::loadMappers();
feeds_alter('feeds_processor_targets', $targets, 'taxonomy_term', $vocabulary->machine_name);
}
Alex Barth
committed
return $targets;
}
/**
* Get id of an existing feed item term if available.
*/
protected function existingItemId(FeedsSource $source, FeedsParserResult $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;
}
}
}
return 0;
}
Alex Barth
committed
/**
* Return vocabulary to map to.
*/
public function vocabulary() {
Alex Barth
committed
// Legacy handling for old feeds importers.
Alex Barth
committed
if (is_numeric($this->config['vocabulary'])) {
Alex Barth
committed
$vocabularies = taxonomy_get_vocabularies();
return isset($vocabularies[$this->config['vocabulary']]) ? $vocabularies[$this->config['vocabulary']] : NULL;
Alex Barth
committed
}
else {
Alex Barth
committed
if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->config['vocabulary'])) {
return $vocabulary;
}
else {
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
if ($vocabulary->module == $this->config['vocabulary']) {
return $vocabulary;
}
Alex Barth
committed
}
}
}
}