--- taxonomy_csv.module.1.5 2008-04-20 00:15:16.000000000 +0200 +++ taxonomy_csv.module 2009-02-18 18:18:03.000000000 +0100 @@ -1,6 +1,7 @@ Use this form to import taxonomy terms into a vocabulary from a CSV file.
Warning: If you want to update an existing vocabulary, make sure you have a backup before you proceed so you can roll back, if necessary.') . theme('more_help_link', url('admin/help/taxonomy_csv')); + return '
'. t('Use this form to import taxonomy terms into a vocabulary from a CSV file.'). '
'. t('Warning: If you want to update an existing vocabulary, make sure you have a backup before you proceed so you can roll back, if necessary.'). theme('more_help_link', url('admin/help/taxonomy_csv')); case 'admin/help#taxonomy_csv': - return t('
This module allows you to import taxonomy terms into a vocabulary from a CSV file.
The term name will be imported from the first column. You can specify how additional columns should be imported:
Animal,Mammal,Dog.If you want to import child term names as well as descriptions and synonyms for each term, you will need to do this in two steps. First, upload a file containing the parent and child term names with the Child term names option. Second, upload a file containing descriptions and synonyms for each term with the Term description, term synonyms option, while making sure to enable the Update if name matches option.
If you are unsure how to create a CSV file, you might want to use Excel or another spreadsheet application to export your data into a CSV file.
', array('!import-url' => url('admin/content/taxonomy/csv'))); + $output = ''. t('This module allows you to import taxonomy terms into a vocabulary from a CSV file.
The term name will be imported from the first column. You can specify how additional columns should be imported:', array('!import-url' => url('admin/content/taxonomy/csv'))) .'
'; + $output .= 'Animal,Mammal,Dog.'). 'Thesaurus,Taxonomy,Ontology.'). ''. t('If you want to import child term names as well as descriptions and synonyms for each term, you will need to do this in two steps. First, upload a file containing the parent and child term names with the Child term names option. Second, upload a file containing descriptions and synonyms for each term with the Term description, term synonyms option, while making sure to enable the Update if name matches option.'). '
'; + $output .= ''. t('If you want to import child term names as well as related terms, first import child term names as above and second upload a file containing related terms with Related terms and Update if name matches options.'). '
'; + $output .= ''. t('Be careful with the Update option: import of synonyms and related terms to a term currently implies lost of existing synonyms and related terms of this term. It\'s not a problem when the vocabulary is a new one.'). '
'; + $output .= ''. t('It is recommended to protect terms with quotation marks ("), specialy if they contain non-ASCII letters: "term 1","term 2","term 3".'). '
'. t('If you are unsure how to create a CSV file, you might want to use OpenOffice Calc or another spreadsheet application to export your data into a CSV file.'). '
'; + return $output; } } @@ -29,10 +41,10 @@ function taxonomy_csv_menu() { $items = array(); $items['admin/content/taxonomy/csv'] = array( - 'title' => 'CSV import', + 'title' => t('CSV import'), 'page callback' => 'drupal_get_form', 'page arguments' => array('taxonomy_csv_import'), - 'access arguments' => array('administer taxonomy'), + 'access arguments' => array(t('administer taxonomy')), 'weight' => 12, 'type' => MENU_LOCAL_TASK, ); @@ -74,6 +86,7 @@ function taxonomy_csv_import() { TAXONOMY_CSV_IGNORE => t('Ignore'), TAXONOMY_CSV_FIELDS => t('Term description, term synonyms (may be empty)'), TAXONOMY_CSV_CHILDREN => t('Child term names'), + TAXONOMY_CSV_RELATED => t('Related terms'), ), '#default_value' => TAXONOMY_CSV_IGNORE, '#description' => t('The first column is always imported as the term name. This option determines how additional columns will be imported. If your CSV file only contains one column, this option will be ignored.'), @@ -203,7 +216,8 @@ function taxonomy_csv_import_line($line, // Convert line to UTF-8. $line = array_map('_taxonomy_csv_import_line_to_utf8', $line); - if ($options['columns'] == TAXONOMY_CSV_CHILDREN) { + switch ($options['columns']) { + case TAXONOMY_CSV_CHILDREN : $parent = 0; for ($c = 0; $c < count($line); $c++) { if (empty($line[$c])) break; @@ -218,24 +232,50 @@ function taxonomy_csv_import_line($line, $term = taxonomy_csv_import_term($term, $options['update'] || $c < count($line) - 1); $parent = $term['tid']; } - } - else if (!empty($line[0])) { + break; + case TAXONOMY_CSV_FIELDS : + if (!empty($line[0])) { $term = array( 'name' => $line[0], 'vid' => $options['vid'], ); - - if ($options['columns'] == TAXONOMY_CSV_FIELDS) { if (count($line) > 1 && !empty($line[1])) { $term['description'] = $line[1]; } if (count($line) > 2) { $term['synonyms'] = implode("\n", array_filter(array_slice($line, 2))); } + taxonomy_csv_import_term($term, $options['update']); + } + break; + case TAXONOMY_CSV_RELATED : + if (!empty($line[0])) { + $term = array( + 'name' => $line[0], + 'vid' => $options['vid'], + ); + // each related term must exist before it can be related (need of its tid) + for ($c = 1; $c < count($line); $c++) { + if (empty($line[$c])) break; + $related_term = array( + 'name' => $line[$c], + 'vid' => $options['vid'], + ); + $related_term = taxonomy_csv_import_term($related_term, $options['update']); + $term['relations'][$c - 1] = $related_term['tid']; } - taxonomy_csv_import_term($term, $options['update']); } + break; + default : + if (!empty($line[0])) { + $term = array( + 'name' => $line[0], + 'vid' => $options['vid'], + ); + taxonomy_csv_import_term($term, $options['update']); + } + } } /** @@ -254,7 +294,8 @@ function _taxonomy_csv_import_line_to_ut */ function taxonomy_csv_import_term($term, $update = TRUE) { if ($update && ($existing_term = taxonomy_csv_find_term($term['name'], $term['vid'], isset($term['parent']) ? $term['parent'] : NULL))) { - foreach (array('description', 'synonyms') as $key) { + foreach (array('description', 'synonyms', 'relations') as $key) { + // Existing synonyms and relations are lost (see taxonomy_save_term too)! if (array_key_exists($key, $term)) $existing_term[$key] = $term[$key]; } @@ -267,7 +308,7 @@ function taxonomy_csv_import_term($term, } /** - * Find an existing term by name in the given vocabulary and given parent. + * Find, by its name, the first existing term in a given vocabulary and a given parent. */ function taxonomy_csv_find_term($name, $vid, $parent = NULL) { static $cache = array(); @@ -304,3 +345,4 @@ function taxonomy_csv_find_term($name, $ return $term; } +?>