Newer
Older
Alex Barth
committed
<?php
// $Id$
/**
* @file
* FeedsTermProcessor class.
*/
/**
* Feeds processor plugin. Create taxonomy terms from feed items.
*/
class FeedsTermProcessor extends FeedsProcessor {
/**
* Implementation of FeedsProcessor::process().
*/
public function process(FeedsImportBatch $batch, FeedsSource $source) {
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 = $batch->shiftItem()) {
Alex Barth
committed
if (!($tid = $this->existingItemId($batch, $source)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {
Alex Barth
committed
// Map item to a term.
$term = array();
if ($tid && $this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
$term = (array) taxonomy_get_term($tid, TRUE);
$term = module_invoke_all('feeds_taxonomy_load', $term);
}
$term = $this->map($batch, $term, $source->feed_nid);
Alex Barth
committed
// Check if term name is set, otherwise continue.
if (empty($term['name'])) {
$no_name++;
continue;
}
// Add term id if available.
if (!empty($tid)) {
$term['tid'] = $tid;
}
// Save the term.
$term['importer_id'] = $this->id;
$term['feed_nid'] = $this->feed_nid;
Alex Barth
committed
taxonomy_save_term($term);
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.'));
}
}
/**
Alex Barth
committed
*/
public function clear(FeedsBatch $batch, FeedsSource $source) {
Alex Barth
committed
$deleted = 0;
Alex Barth
committed
$vocabulary = $this->vocabulary();
$result = db_query("SELECT td.tid
FROM {term_data} td
JOIN {feeds_term_item} ft ON td.tid = ft.tid
WHERE td.vid = %d
AND ft.id = '%s'
AND ft.feed_nid = %d",
$vocabulary->vid, $this->id, $source->feed_nid);
Alex Barth
committed
while ($term = db_fetch_object($result)) {
if (taxonomy_del_term($term->tid) == SAVED_DELETED) {
$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(FeedsImportBatch $batch, $target_term = NULL) {
// Prepare term object, have parent class do the iterating.
if (!$target_term) {
$target_term = array();
}
Alex Barth
committed
$vocabulary = $this->vocabulary();
$target_term['vid'] = $vocabulary->vid;
$target_term = parent::map($batch, $target_term);
// Taxonomy module expects synonyms to be supplied as a single string.
if (isset($target_term['synonyms']) && is_array(isset($target_term['synonyms']))) {
$target_term['synonyms'] = implode("\n", $target_term['synonyms']);
}
return $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'));
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
Alex Barth
committed
if (strpos($vocab->module, 'features_') === 0) {
$options[$vocab->module] = $vocab->name;
}
else {
$options[$vid] = $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;
}
/**
* 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.'),
),
'synonyms' => array(
'name' => t('Term synonyms'),
'description' => t('One synonym or an array of synonyms of the taxonomy term.'),
),
Alex Barth
committed
);
// Let implementers of hook_feeds_term_processor_targets() add their targets.
Alex Barth
committed
$vocabulary = $this->vocabulary();
drupal_alter('feeds_term_processor_targets', $targets, $vocabulary->vid);
Alex Barth
committed
return $targets;
}
/**
* Get id of an existing feed item term if available.
*/
protected function existingItemId(FeedsImportBatch $batch, FeedsSource $source) {
Alex Barth
committed
// The only possible unique target is name.
foreach ($this->uniqueTargets($batch) as $target => $value) {
Alex Barth
committed
if ($target == 'name') {
Alex Barth
committed
$vocabulary = $this->vocabulary();
if ($tid = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d", $value, $vocabulary->vid))) {
Alex Barth
committed
return $tid;
}
}
}
return 0;
}
Alex Barth
committed
/**
* Return vocabulary to map to.
*
* Feeds supports looking up vocabularies by their module name as part of an
* effort to use the vocabulary.module field as machine name to make
* vocabularies exportable.
*/
public function vocabulary() {
Alex Barth
committed
$vocabularies = taxonomy_get_vocabularies();
if (is_numeric($this->config['vocabulary'])) {
return $vocabularies[$this->config['vocabulary']];
}
else {
foreach ($vocabularies as $vocabulary) {
if ($vocabulary->module == $this->config['vocabulary']) {
return $vocabulary;
}
}
}
}