You are here

function taxonomy_csv_term_export in Taxonomy CSV import/export 6.3

Same name and namespace in other branches
  1. 6.5 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()
  2. 6.2 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'

$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 380
Validate export options and manage export process.

Code

function taxonomy_csv_term_export($term, $options, $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_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);
        $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,
          $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_TREE_STRUCTURE:
        break;
      case TAXONOMY_CSV_FORMAT_POLYHIERARCHY:
        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,
          $term->description,
        ), $term->synonyms);
        break;
      case TAXONOMY_CSV_FORMAT_DESCRIPTIONS:
        $result['line'] = array(
          $term->name,
          $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;
}