You are here

function taxonomy_csv_vocabulary_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_vocabulary_export()
  2. 6.2 export/taxonomy_csv.export.api.inc \taxonomy_csv_vocabulary_export()
  3. 6.3 export/taxonomy_csv.export.api.inc \taxonomy_csv_vocabulary_export()
  4. 6.4 export/taxonomy_csv.export.api.inc \taxonomy_csv_vocabulary_export()
  5. 7.4 export/taxonomy_csv.export.api.inc \taxonomy_csv_vocabulary_export()

Prepare the export of a vocabulary.

@note If not used in a form, don't forget to use batch_process().

Parameters

$options: Array. Same as taxonomy_csv_export.

Return value

Array of errors or nothing (batch process to execute).

2 calls to taxonomy_csv_vocabulary_export()
taxonomy_csv_export in export/taxonomy_csv.export.api.inc
Process the export of a vocabulary.
taxonomy_csv_export_form_submit in export/taxonomy_csv.export.admin.inc
Handles CSV export form submission and launch batch set.

File

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

Code

function taxonomy_csv_vocabulary_export($options) {

  // Check options and return array of messages in case of errors.
  if ($options['check_options']) {

    // Invoke export admin file.
    $module_dir = drupal_get_path('module', 'taxonomy_csv');
    require_once "{$module_dir}/export/taxonomy_csv.export.admin.inc";
    $result = _taxonomy_csv_export_check_options($options);
    if (count($result)) {
      return $result;
    }
  }

  // Complete $options with some csv variables.
  $options['separator'] = $options['enclosure'] . $options['delimiter'] . $options['enclosure'];
  $line_ending = array(
    'Unix' => "\n",
    'Mac' => "\r",
    'MS-DOS' => "\r\n",
  );
  $options['end_of_line'] = $line_ending[$options['line_ending']];

  // Calculates number of terms to be exported.
  $options['total_terms'] = taxonomy_csv_vocabulary_count_terms($options['vocabulary_id']);

  // Get infos about fields of vocabulary, for any formats.
  $options['vocabulary'] = array();
  foreach ($options['vocabulary_id'] as $vocabulary_id) {
    $options['vocabulary'][$vocabulary_id] = taxonomy_vocabulary_load($vocabulary_id);
    $vocabulary =& $options['vocabulary'][$vocabulary_id];
    $vocabulary->instances = field_info_instances('taxonomy_term', $vocabulary->machine_name);

    // Prepare list of fields to be exported.
    $vocabulary->fields = array();

    // Not included, because referenced terms are exported by name.
    // $vocabulary->fields['tid'] = array('cardinality' => 1);
    $vocabulary->fields['name'] = array(
      'cardinality' => 1,
    );

    // Not included, because there is already 'vocabulary_machine_name'.
    // $vocabulary->fields['vid'] = array('cardinality' => 1);
    $vocabulary->fields['vocabulary_machine_name'] = array(
      'cardinality' => 1,
    );
    $vocabulary->fields['description'] = array(
      'cardinality' => 1,
    );
    $vocabulary->fields['format'] = array(
      'cardinality' => 1,
    );
    $vocabulary->fields['weight'] = array(
      'cardinality' => 1,
    );
    $vocabulary->fields['parent'] = array(
      'cardinality' => -1,
    );
    if (module_exists('i18n_taxonomy')) {
      switch ($vocabulary->i18n_mode) {
        case I18N_MODE_LANGUAGE:
        case I18N_MODE_LOCALIZE:
          $vocabulary->fields['language'] = array(
            'cardinality' => 1,
          );
          break;
        case I18N_MODE_TRANSLATE:
        case I18N_MODE_MULTIPLE:
          $vocabulary->fields['language'] = array(
            'cardinality' => 1,
          );
          $vocabulary->fields['i18n_tsid'] = array(
            'cardinality' => 1,
          );
          break;
      }
    }

    // @todo
    // $vocabulary->fields['guid'] = array('cardinality' => 1);
    // Prepare list of unlimited fields to be exported.
    $vocabulary->fields_unlimited = array();
    $vocabulary->fields_unlimited['parent'] = 1;
    if (is_array($vocabulary->instances)) {
      foreach ($vocabulary->instances as $field_name => $value) {
        $vocabulary->fields[$field_name] = field_info_field($field_name);

        // Get the list of fields with an unlimited number of values to avoid
        // the loop of check (used with custom fields export).
        // The minimum is set to one to avoid zero value.
        if ($vocabulary->fields[$field_name]['cardinality'] == -1) {
          $vocabulary->fields_unlimited[$field_name] = 1;
        }
      }
    }
  }

  // Prepare export batch.
  $batch = array(
    'title' => t('Exporting !total_terms terms to CSV file...', array(
      '!total_terms' => $options['total_terms'],
    )),
    'init_message' => t('Starting downloading of datas...') . '<br />' . t('Wait some seconds for pre-processing...'),
    'progress_message' => '',
    'error_message' => t('An error occurred during the export.'),
    'finished' => '_taxonomy_csv_vocabulary_export_finished',
    'file' => drupal_get_path('module', 'taxonomy_csv') . '/export/taxonomy_csv.export.api.inc',
    'progressive' => TRUE,
    'operations' => array(
      0 => array(
        '_taxonomy_csv_vocabulary_export_process',
        array(
          $options,
        ),
      ),
    ),
  );
  batch_set($batch);
}