You are here

function taxonomy_csv_term_export in Taxonomy CSV import/export 6.5

Same name and namespace in other branches
  1. 6.2 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()
  2. 6.3 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()
  3. 6.4 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()
  4. 7.5 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()

Export a term to a line matching the options.

Parameters

$term: Full term object to export.

$options: An associative array of export options:

// Specific to def_links:

  • def_links_terms_ids : 'name_if_needed' (default), 'name' or 'tid'
  • def_links_vocabularies_ids : 'none' (default), 'name' or 'vid'

$terms_list: (Optional) Array of all term objects to export, used to avoid to repeat fetch of terms. Currently needed only with tree_structure export.

$duplicate_terms: (Optional) Array of duplicate terms names indexed by tid. Duplicate terms are managed only with def_links.

Return value

Result array: 'line' => array of exported items, 'msg' => array of messages arrays.

1 call to taxonomy_csv_term_export()
_taxonomy_csv_vocabulary_export_process in export/taxonomy_csv.export.api.inc
Batch process of vocabulary export.

File

export/taxonomy_csv.export.api.inc, line 464
Validate export options and manage export process.

Code

function taxonomy_csv_term_export($term, $options, &$terms_list = array(), $duplicate_terms = array()) {

  // Define default values.
  $result = array(
    'line' => array(),
    'msg' => array(),
  );

  // Only count check because term and options are already checked.
  if (count($term)) {
    switch ($options['export_format']) {
      case TAXONOMY_CSV_FORMAT_ALONE_TERMS:
        $result['line'] = array(
          $term->name,
        );
        break;
      case TAXONOMY_CSV_FORMAT_TID_NAME:
        $result['line'] = array(
          $term->tid,
          $term->name,
        );
        break;
      case TAXONOMY_CSV_FORMAT_TREE_STRUCTURE:
        $terms = taxonomy_csv_term_get_first_path($term, $terms_list);
        foreach ($terms as $parent) {
          $result['line'][] = $parent->name;
        }
        $result['line'][] = $term->name;
        break;
      case TAXONOMY_CSV_FORMAT_POLYHIERARCHY:

        // Warning : taxonomy_csv_term_get_first_path() returns only first path.
        break;
      case TAXONOMY_CSV_FORMAT_DEFINITION_LINKS:

        // Prepare identifiants of main term and links.
        // Check if each term is a duplicate, because identifiants are names,
        // except for duplicate terms. For them, tid is appended to name.
        $terms = array(
          'main' => array(
            $term,
          ),
          // Synonyms are included in term object in Drupal 6.
          'parents' => taxonomy_get_parents($term->tid),
          'children' => taxonomy_get_children($term->tid),
          'relations' => taxonomy_get_related($term->tid),
        );
        foreach (array(
          'main',
          'parents',
          'children',
          'relations',
        ) as $link) {
          $ids[$link] = array();
          foreach ($terms[$link] as $item) {

            // Option is to use term id.
            if ($options['def_links_terms_ids'] == 'tid') {
              $ids[$link][] = $item->tid;
            }
            elseif (isset($duplicate_terms[$item->tid])) {
              $ids[$link][] = $item->name . ' ' . $item->tid;
            }
            else {
              $ids[$link][] = $item->name;
            }
          }
        }

        // Prepare main term vocabulary identifiant.
        // Vocabulary identifiant depends on def_links_vocabularies_ids.
        $vocabulary = taxonomy_vocabulary_load($term->vid);

        // Display a warning if a term has no vocabulary.
        // See http://drupal.org/node/1359260.
        if (!$vocabulary) {
          $result['msg'][] = 409;

          // Warning not a good term.
          break;
        }
        $vid = $options['def_links_vocabularies_ids'] == 'none' ? '' : $vocabulary->{$options['def_links_vocabularies_ids']};

        // Prepare vocabularies identifiants of related terms.
        $relations_vocs = array();

        // Option 'none' is impossible with multiple vocabularies, so use name.
        $vocabulary_option = $options['def_links_vocabularies_ids'] == 'none' ? 'name' : $options['def_links_vocabularies_ids'];
        foreach ($terms['relations'] as $item) {

          // Check if related term vocabulary is main term vocabulary.
          $vocabulary = taxonomy_vocabulary_load($item->vid);
          $relations_vocs[] = $item->vid == $term->vid ? $vid : $vocabulary->{$vocabulary_option};
        }
        $result['line'] = array_merge(array(
          $term->name,
          // Main term identifiant: use tid, name or nothing.
          $options['def_links_terms_ids'] == 'name_if_needed' && $term->name == $ids['main'][0] ? '' : $ids['main'][0],
          $vid,
          _taxonomy_csv_escape_line_break($term->description),
          $term->weight,
          count($term->synonyms),
          count($ids['parents']),
          count($ids['children']),
          count($ids['relations']),
        ), $term->synonyms, $ids['parents'], $ids['children'], $ids['relations'], $relations_vocs);
        break;
      case TAXONOMY_CSV_FORMAT_FIELDS:
        $result['line'] = array(
          $term->name,
          $term->vid,
          $term->description,
          'plain_text',
          $term->weight,
        );
        foreach ($term->parents as $tid) {
          $result['line'][] = isset($terms_list[$tid]) ? $terms_list[$tid]->name : taxonomy_csv_term_load($tid)->name;
        }

        // Add empty value the max number of values in the vocabulary times.
        if (count($term->parents) < $options['vocabulary'][$term->vid]->fields_unlimited['parents']) {
          $result['line'] = array_merge($result['line'], array_fill(0, $options['vocabulary'][$term->vid]->fields_unlimited['parents'] - count($term->parents), ''));
        }
        if (module_exists('i18n_taxonomy')) {
          switch ($option['vocabulary'][$term->vid]->i18n_mode) {
            case I18N_MODE_LANGUAGE:
            case I18N_MODE_LOCALIZE:
              $result['line'][] = $term->language;
              break;
            case I18N_MODE_TRANSLATE:
            case I18N_MODE_MULTIPLE:
              $result['line'][] = $term->language;

              // $result['line'][] = $term->i18n_tsid;
              break;
          }
        }
        foreach ($term->synonyms as $synonym) {
          $result['line'][] = $synonym;
        }
        if (count($term->synonyms) < $options['vocabulary'][$term->vid]->fields_unlimited['synonyms']) {
          $result['line'] = array_merge($result['line'], array_fill(0, $options['vocabulary'][$term->vid]->fields_unlimited['synonyms'] - count($term->synonyms), ''));
        }
        foreach ($term->relations as $tid) {
          $result['line'][] = isset($terms_list[$tid]) ? $terms_list[$tid]->name : taxonomy_csv_term_load($tid)->name;
        }
        if (count($term->relations) < $options['vocabulary'][$term->vid]->fields_unlimited['relations']) {
          $result['line'] = array_merge($result['line'], array_fill(0, $options['vocabulary'][$term->vid]->fields_unlimited['relations'] - count($term->relations), ''));
        }
        break;
      case TAXONOMY_CSV_FORMAT_PARENTS:
        $result['line'] = array_merge(array(
          $term->name,
        ), taxonomy_csv_term_get_parents_names($term->tid));
        break;
      case TAXONOMY_CSV_FORMAT_CHILDREN:
        $result['line'] = array_merge(array(
          $term->name,
        ), taxonomy_csv_term_get_children_names($term->tid));
        break;
      case TAXONOMY_CSV_FORMAT_RELATIONS:
        $result['line'] = array_merge(array(
          $term->name,
        ), taxonomy_csv_term_get_relations_names($term->tid));
        break;
      case TAXONOMY_CSV_FORMAT_SYNONYMS:
        $result['line'] = array_merge(array(
          $term->name,
        ), $term->synonyms);
        break;
      case TAXONOMY_CSV_FORMAT_DEFINITIONS:
        $result['line'] = array_merge(array(
          $term->name,
          $term->weight,
          _taxonomy_csv_escape_line_break($term->description),
        ), $term->synonyms);
        break;
      case TAXONOMY_CSV_FORMAT_DESCRIPTIONS:
        $result['line'] = array(
          $term->name,
          _taxonomy_csv_escape_line_break($term->description),
        );
        break;
      case TAXONOMY_CSV_FORMAT_WEIGHTS:
        $result['line'] = array(
          $term->name,
          $term->weight,
        );
        break;
      case TAXONOMY_CSV_FORMAT_FIELDS:

        // @todo Extra fields.
        break;
      default:

        // Check external formats. Use it only if it works.
        $funcname = "taxonomy_csv_term_export_{$options['export_format']}";
        if (taxonomy_csv_format_check($options['export_format'], $funcname)) {
          $result = $funcname($term, $options, $duplicate_terms);
        }
        else {
          $result['msg'][] = 307;

          // Error unknown export format.
        }
    }
  }
  else {
    $result['msg'][] = 385;

    // Error no term to process.
  }

  // Clean result.
  $result['msg'] = array_unique($result['msg']);
  sort($result['msg']);
  return $result;
}