View source
<?php
module_load_include('inc', 'taxonomy_xml', 'csv_format');
function taxonomy_xml_csvancestry_parse(&$data, $vid) {
$output = '';
unset($GLOBALS['$_taxonomy_xml_terms']);
$terms = array();
$new_terms = array();
$skipped_terms = array();
$vocabulary = array();
if ($vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
}
else {
drupal_set_message(t('No vocab to import into. Either make one or choose one.'));
return;
}
$rows = explode("\n", $data);
$terms = array();
$batch_settings = array(
'title' => t('Processing all queued import requests.'),
'file' => drupal_get_path('module', 'taxonomy_xml') . '/csvancestry_format.inc',
'operations' => array(),
'finished' => 'cvsancestry_import_finished',
);
foreach ($rows as $row) {
$row_data = csv_string_to_array($row);
$batch_settings['operations'][] = array(
'cvsancestry_import_row',
array(
$vid,
$row_data,
),
);
}
batch_set($batch_settings);
drupal_set_message(t('Queued %rowcount rows of data', array(
'%rowcount' => count($rows),
)));
return "OK, processing is being done in batch...";
}
function cvsancestry_import_finished($success, $results, $operations) {
$results = array_merge(array(
'new_terms' => array(),
'old_terms' => array(),
), $results);
if ($success) {
$message = t('%new new and %old old terms processed', array(
'%new' => count($results['new_terms']),
'%old' => count($results['old_terms']),
));
}
else {
$message = t('Failed ', array());
}
drupal_set_message($message);
}
function cvsancestry_import_row($vid, $row_data, &$context) {
module_load_include('inc', 'taxonomy_xml', 'taxonomy_xml.process');
global $_taxonomy_xml_terms;
while (!empty($row_data) && empty($term_name)) {
$term_name = array_pop($row_data);
}
if (empty($term_name)) {
return;
}
$term = isset($_taxonomy_xml_terms[$term_name]) ? $_taxonomy_xml_terms[$term_name] : NULL;
if (!$term) {
$term = _taxonomy_xml_get_term_placeholder($term_name, $vid);
if (empty($term->tid)) {
$context['results']['new_terms'][] = $term_name;
}
else {
$context['results']['old_terms'][] = $term_name;
}
$_taxonomy_xml_terms[$term_name] = $term;
}
$parent_name = array_pop($row_data);
if (!empty($parent_name)) {
csvancestry_set_parent($term, $parent_name);
}
$term_data = (array) $term;
taxonomy_term_save($term);
$retrieved_term = taxonomy_xml_get_term_by_name_from_vocab($term_name, $vid);
$_taxonomy_xml_terms[$term_name] = $term;
}
function csvancestry_set_parent($term, $parent_name) {
if ($parent_name == $term->name) {
drupal_set_message(t("Not setting %name as a child of itself", array(
'%name' => $term->name,
)));
continue;
}
$parent_term = _taxonomy_xml_get_term_placeholder($parent_name, $term->vid);
if (empty($parent_term->tid)) {
$parent_data = (array) $parent_term;
taxonomy_term_save($term);
$parent_term = taxonomy_xml_get_term_by_name_from_vocab($parent_name, $term->vid);
}
if ($parent_term && isset($parent_term->tid)) {
drupal_set_message(t("!name # %tid is a child of !parent # %ptid ", array(
'!name' => $term->name,
'%tid' => $term->tid,
'!parent' => l($parent_term->name, 'taxonomy/term/' . $parent_term->tid),
'%ptid' => $parent_term->tid,
)));
$term->parents[$parent_term->tid] = $parent_term->tid;
}
}