You are here

function node_import_options in Node import 6

Returns a list of options (form elements).

Parameters

$type: String. The node_import type.

$options: Array of currently filled in values.

$fields: Array of available fields.

$map: Array of how fields are mapped.

Return value

FAPI array. See hook_node_import_options().

Related topics

1 call to node_import_options()
node_import_add_form in ./node_import.admin.inc
Creates a new import task by letting the user fill in a wizard.

File

./node_import.inc, line 346
Public API of the Node import module.

Code

function node_import_options($type, $options, $fields, $map) {
  if (!is_array($options)) {
    $options = array();
  }
  $form = (array) module_invoke_all('node_import_options', $type, $options, $fields, $map);

  // Copy from modules/system/system.admin.inc
  $date_short = array(
    'Y-m-d H:i',
    'm/d/Y - H:i',
    'd/m/Y - H:i',
    'Y/m/d - H:i',
    'd.m.Y - H:i',
    'm/d/Y - g:ia',
    'd/m/Y - g:ia',
    'Y/m/d - g:ia',
    'M j Y - H:i',
    'j M Y - H:i',
    'Y M j - H:i',
    'M j Y - g:ia',
    'j M Y - g:ia',
    'Y M j - g:ia',
  );
  $date_short_choices = array();
  foreach ($date_short as $f) {
    $date_short_choices[$f] = format_date(time(), 'custom', $f);
  }
  $date_short_choices['custom'] = t('Custom format');
  $timezones = date_timezone_names(TRUE);
  foreach ((array) $fields as $fieldname => $fieldinfo) {

    // Set #node_import-group if not set.
    if (isset($form[$fieldname]) && !isset($form[$fieldname]['#node_import-group'])) {
      $form[$fieldname]['#node_import-group'] = $fieldinfo['group'];
    }

    // Set #description if not set.
    if (isset($form[$fieldname]) && !isset($form[$fieldname]['#description'])) {
      if (count($fieldinfo['tips']) > 1) {
        $form[$fieldname]['#description'] = '<ul class="tips">';
        $form[$fieldname]['#description'] .= '<li>' . implode('</li><li>', $fieldinfo['tips']) . '</li>';
        $form[$fieldname]['#description'] .= '</ul>';
      }
      else {
        if (!empty($fieldinfo['tips'])) {
          $form[$fieldname]['#description'] = implode('', $fieldinfo['tips']);
        }
      }
    }

    // Set #weight if not set.
    if (isset($form[$fieldname]) && !isset($form[$fieldname]['#weight'])) {
      $form[$fieldname]['#weight'] = $fieldinfo['weight'];
    }
    $map_count = node_import_field_map_count($fieldname, $map);

    // Add multiple_separator option for fields that can have multiple
    // values AND that is mapped to exactly one file column.
    if ($fieldinfo['has_multiple'] && $map_count == 1 && (!isset($form[$fieldname]) || !isset($form[$fieldname]['multiple_separator']))) {
      if (!isset($form[$fieldname])) {
        $form[$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#node_import-group' => $fieldinfo['group'],
        );
      }
      $form[$fieldname]['multiple_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Multiple values are separated by'),
        '#size' => 6,
        '#default_value' => isset($options[$fieldname]['multiple_separator']) ? $options[$fieldname]['multiple_separator'] : $fieldinfo['multiple_separator'],
      );
    }

    // Add hierarchy_separator option for fields that can have
    // hierarchical values AND (that can have multiple values OR
    // that can not have multiple values but is mapped to exactly
    // one file column).
    if ($fieldinfo['has_hierarchy'] && ($fieldinfo['has_multiple'] || $map_count == 1) && $map_count > 0 && (!isset($form[$fieldname]) || !isset($form[$fieldname]['hierarchy_separator']))) {
      if (!isset($form[$fieldname])) {
        $form[$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#node_import-group' => $fieldinfo['group'],
        );
      }
      $form[$fieldname]['hierarchy_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Hierarchy is specified by'),
        '#size' => 6,
        '#default_value' => isset($options[$fieldname]['hierarchy_separator']) ? $options[$fieldname]['hierarchy_separator'] : $fieldinfo['hierarchy_separator'],
      );
    }

    // Add hierarchy_reverse option for fields that can have
    // hierarchical values AND that can not have multiple values
    // AND that is mapped to more than one file column.
    if ($fieldinfo['has_hierarchy'] && $map_count > 1 && !$fieldinfo['has_multiple'] && (!isset($form[$fieldname]) || !isset($form[$fieldname]['hierarchy_reverse']))) {
      if (!isset($form[$fieldname])) {
        $form[$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#node_import-group' => $fieldinfo['group'],
        );
      }
      if ($map_count > 1 && !$fieldinfo['has_multiple']) {
        $form[$fieldname]['hierarchy_reverse'] = array(
          '#type' => 'checkbox',
          '#title' => t('Reverse file columns for hierarchy'),
          '#default_value' => isset($options[$fieldname]['hierarchy_reverse']) ? $options[$fieldname]['hierarchy_reverse'] : $fieldinfo['hierarchy_reverse'],
        );
      }
    }

    // Add a custom date format option for fields that are dates.
    if ($fieldinfo['input_format'] == 'date' && $map_count > 0) {
      if (!isset($form[$fieldname])) {
        $form[$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#node_import-group' => $fieldinfo['group'],
        );
      }
      $form[$fieldname]['timezone'] = array(
        '#type' => 'select',
        '#title' => t('Timezone'),
        '#default_value' => isset($options[$fieldname]['timezone']) ? $options[$fieldname]['timezone'] : date_default_timezone_name(),
        '#options' => $timezones,
        '#description' => t('Select the default time zone. If in doubt, choose the timezone that is closest to your location which has the same rules for daylight saving time.'),
      );
      $form[$fieldname]['date_format'] = array(
        '#type' => 'select',
        '#title' => t('Date format'),
        '#default_value' => isset($options[$fieldname]['date_format']) ? $options[$fieldname]['date_format'] : variable_get('date_format_short', 'm/d/Y - H::i'),
        '#options' => $date_short_choices,
        '#description' => t('Select the date format for import. If you choose <em>Custom format</em> enter the custom format below.'),
      );
      $form[$fieldname]['date_custom'] = array(
        '#type' => 'textfield',
        '#title' => t('Custom date format'),
        '#attributes' => array(
          'class' => 'custom-format',
        ),
        '#default_value' => isset($options[$fieldname]['date_custom']) ? $options[$fieldname]['date_custom'] : variable_get('date_format_short', 'm/d/Y - H::i'),
        '#description' => t('See the <a href="@url" target="_blank">PHP manual</a> for available options.', array(
          '@url' => 'http://php.net/manual/function.date.php',
        )),
      );
    }

    // Add directory selection for fields that are filepaths.
    if ($fieldinfo['input_format'] == 'filepath' && $map_count > 0) {
      if (!isset($form[$fieldname])) {
        $form[$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#node_import-group' => $fieldinfo['group'],
        );
      }
      $form[$fieldname][] = array(
        '#value' => t('You need to FTP the files you reference in this field manually to the correct location (%path) before doing the import.', array(
          '%path' => file_create_path(isset($fieldinfo['to_directory']) ? $fieldinfo['to_directory'] : ''),
        )),
      );
      $form[$fieldname]['manually_moved'] = array(
        '#type' => 'hidden',
        '#value' => TRUE,
      );

      /*
      TODO: we disable this until I get the time to figure out why files are not
      moved to the correct location if you do not move them manually.

      //TODO: as the directory must be relative... we could let the user choose from a select box
      //TODO: we might use the sample data to find the correct directory
            $form[$fieldname]['from_directory'] = array(
              '#type' => 'textfield',
              '#title' => t('Copy from'),
              '#field_prefix' => node_import_directory() . '/',
              '#field_suffix' => '/',
              '#default_value' => isset($options[$fieldname]['from_directory']) ? $options[$fieldname]['from_directory'] : '',
              '#description' => t('Fill in the directory which contains the files.'),
            );

            $form[$fieldname]['manually_moved'] = array(
              '#type' => 'checkbox',
              '#title' => t('Files have been manually moved'),
              '#default_value' => isset($options[$fieldname]['manually_moved']) ? $options[$fieldname]['manually_moved'] : 0,
              '#description' => t('Check this box if you have already moved the files to the correct location on your server (%path).', array('%path' => file_create_path(isset($fieldinfo['to_directory']) ? $fieldinfo['to_directory'] : ''))),
            );
      */

      /* TODO
            $form[$fieldname]['delete_on_success'] = array(
              '#type' => 'checkbox',
              '#title' => t('Delete files after import'),
              '#default_value' => isset($options[$fieldname]['delete_on_success']) ? $options[$fieldname]['delete_on_success'] : 1,
              '#description' => t('Check this box if you want to delete the files from the server after a succesful import.'),
            );
      */
    }
  }
  drupal_alter('node_import_options', $form, $type, $options, $fields, $map);
  return $form;
}