You are here

function taxonomy_csv_term_export in Taxonomy CSV import/export 7.5

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.3 export/taxonomy_csv.export.api.inc \taxonomy_csv_term_export()
  4. 6.4 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:

  • export_format : format of the csv line (see taxonomy.api.inc)

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

$duplicate_terms: (Optional) Array of duplicate terms names indexed by tid.

Return value

Result array with:

  • '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 558
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_FLAT:
        $result['line'][] = $term->name;
        break;
      case TAXONOMY_CSV_FORMAT_FLAT_TID:
        $result['line'][] = $term->tid;
        break;
      case TAXONOMY_CSV_FORMAT_TID_FLAT:
        $result['line'][] = $term->tid;
        $result['line'][] = $term->name;
        break;
      case TAXONOMY_CSV_FORMAT_STRUCTURE:
      case TAXONOMY_CSV_FORMAT_TREE:
        $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_TID:
      case TAXONOMY_CSV_FORMAT_TID_TREE:
        $terms = taxonomy_csv_term_get_first_path($term, $terms_list);
        foreach ($terms as $parent) {
          $result['line'][] = $parent->tid;
          $result['line'][] = $parent->name;
        }
        $result['line'][] = $term->tid;
        $result['line'][] = $term->name;
        break;
      case TAXONOMY_CSV_FORMAT_POLYHIERARCHY:

        // @todo
        // Warning : taxonomy_csv_term_get_first_path() returns only first path.
        break;
      case TAXONOMY_CSV_FORMAT_TID_POLYHIERARCHY:

        // @todo
        // Warning : taxonomy_csv_term_get_first_path() returns only first path.
        break;
      case TAXONOMY_CSV_FORMAT_TREE_FIELDS:
        $terms = taxonomy_csv_term_get_first_path($term, $terms_list);
        foreach ($terms as $parent) {
          $result['line'][] = $parent->name;
        }

      // Continue with fields.
      case TAXONOMY_CSV_FORMAT_FIELDS:

        // Currently, manage only undefined language.
        $language = 'und';

        // Use of field_get_items is slower.
        foreach ($options['vocabulary'][$term->vid]->fields as $field_name => $field) {
          $count = 0;

          // Item is a Field.
          if (isset($field['type'])) {
            if (isset($term->{$field_name}[$language])) {
              switch ($field['type']) {

                // For taxonomy term reference, use name instead of value.
                case 'taxonomy_term_reference':
                  foreach ($term->{$field_name}[$language] as &$item) {
                    $result['line'][] = isset($terms_list[$item['tid']]) ? $terms_list[$item['tid']]->name : taxonomy_term_load($item['tid'])->name;
                  }
                  break;

                // For long text, need to escape the value.
                case 'text_long':
                case 'text_with_summary':
                  foreach ($term->{$field_name}[$language] as &$item) {
                    $result['line'][] = _taxonomy_csv_escape_line_break($item['value']);
                  }
                  break;
                default:

                  // Key is generally 'value' but it can be something else.
                  reset($field['columns']);
                  foreach ($term->{$field_name}[$language] as &$item) {
                    $result['line'][] = $item[key($field['columns'])];
                  }
                  break;
              }
              $count = count($term->{$field_name}[$language]);
            }
          }
          else {
            switch ($field_name) {

              // For taxonomy term parent, use name instead of value.
              case 'parent':

                // Don't export here: use Structure and fields.
                if ($options['export_format'] == TAXONOMY_CSV_FORMAT_TREE_FIELDS) {
                }
                else {
                  if (isset($term->parent)) {
                    foreach ($term->parent as $tid) {
                      $result['line'][] = isset($terms_list[$tid]) ? $terms_list[$tid]->name : taxonomy_term_load($tid)->name;
                    }
                    $count = count($term->parent);
                  }
                }
                break;
              case 'parents':

                // Don't export here: use Structure and fields.
                break;
              case 'description':
                $result['line'][] = _taxonomy_csv_escape_line_break($term->description);
                $count = 1;
                break;
              default:
                $result['line'][] = $term->{$field_name};
                $count = 1;
                break;
            }
          }

          // Add empty value the max number of values in the vocabulary times.
          if (isset($options['vocabulary'][$term->vid]->fields_unlimited[$field_name])) {
            if ($count < $options['vocabulary'][$term->vid]->fields_unlimited[$field_name]) {
              if ($options['export_format'] == TAXONOMY_CSV_FORMAT_TREE_FIELDS && ($field_name == 'parent' || $field_name == 'parents')) {
              }
              else {
                $result['line'] = array_merge($result['line'], array_fill(0, $options['vocabulary'][$term->vid]->fields_unlimited[$field_name] - $count, ''));
              }
            }
          }
          elseif ($count < $field['cardinality']) {
            $result['line'] = array_merge($result['line'], array_fill(0, $field['cardinality'] - $count, ''));
          }
        }
        break;
      case TAXONOMY_CSV_FORMAT_TRANSLATE:
        if (!module_exists('i18n_taxonomy')) {
          $result['msg'][] = 360;

          // Translation error.
          break;
        }
        switch ($options['vocabulary'][$term->vid]->i18n_mode) {
          case I18N_MODE_NONE:
          case I18N_MODE_LANGUAGE:
            $result['line'][] = $term->name;
            break;
          case I18N_MODE_LOCALIZE:
            $result['line'][] = $term->name;
            $languages = locale_language_list('name');
            unset($languages[language_default('language')]);
            foreach ($languages as $language => $value) {
              $translation = i18n_string_translate(array(
                'taxonomy',
                'term',
                $term->tid,
                'name',
              ), $term->name, array(
                'langcode' => $language,
              ));
              $result['line'][] = $translation ? $translation : '';
            }
            break;
          case I18N_MODE_TRANSLATE:
          case I18N_MODE_MULTIPLE:
            $languages = array(
              'und' => t('Language neutral'),
            );
            $languages += locale_language_list('name');
            $languages = array_flip(array_keys($languages));
            $result['line'] = array_fill(0, count($languages), '');
            if ($term->i18n_tsid == 0) {
              $result['line'][$languages[$term->language]] = $term->name;
            }
            else {
              $translation_set = i18n_translation_set_load($term->i18n_tsid);
              $translations = $translation_set
                ->get_translations();
              $language_min = $languages[$term->language];
              foreach ($translations as $language => $translated_term) {
                $result['line'][$languages[$language]] = $translated_term->name;

                // Check if this term is already exported (when there is a term
                // with a language before the current one).
                if ($languages[$language] < $language_min) {
                  $language_min = $languages[$language];
                }
              }

              // This term is already exported or will be exported with another
              // term.
              if ($language_min < $languages[$term->language]) {
                $result['line'] = array();
              }
            }
            break;
        }
        break;
      default:
        $result['msg'][] = 307;
    }
  }
  else {
    $result['msg'][] = 385;

    // Error no term to process.
  }

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