You are here

function node_import_add_form in Node import 6

Creates a new import task by letting the user fill in a wizard.

1 string reference to 'node_import_add_form'
node_import_menu in ./node_import.module
Implementation of hook_menu().

File

./node_import.admin.inc, line 130

Code

function node_import_add_form(&$form_state) {
  global $user;
  $form = array();

  // Check if the user can create something.
  $types = node_import_types();
  if (empty($types)) {
    form_set_error('', t('You are not allowed to create any content. <a href="!permissions">Assign the correct permissions</a> (such as <em>create story content</em>) on the administration page.', array(
      '!permissions' => url('admin/user/permissions'),
    )));
  }

  // ------------------------------------------------------------
  // Get the currently filled in values of the form.
  $form_state['storage'] = isset($form_state['storage']) ? $form_state['storage'] : array();
  $form_state['values'] = isset($form_state['values']) ? $form_state['values'] : array();
  $values = array_merge($form_state['storage'], $form_state['values']);

  // ------------------------------------------------------------
  // Check and store the page we are on.
  $pages = array(
    'intro' => array(
      'title' => t('Introduction'),
      'description' => t('The <em>Node import</em> module allows you to import content (such as users, vocabularies, terms or nodes) into your site from a tabular file (such as Comma or Tab Separated Values files). This wizard guides you through a number of steps to configure such an import.') . (module_exists('advanced_help') ? ' ' . theme('advanced_help_topic', 'node_import', 'new-import', t('[more help]')) : ''),
    ),
    'type' => array(
      'title' => t('Select content type'),
      'description' => t('Select the content type for the data in the file. You can only select content types you are allowed to create.'),
    ),
    'file' => array(
      'title' => t('Select file'),
      'description' => t('Select the file which contains the data to import. You can select a previously uploaded file or upload a new file now.') . (variable_get('node_import:ftp:enabled', 0) != 0 && (variable_get('node_import:ftp:user', 0) == $user->uid || variable_get('node_import:ftp:user', 0) == 0) ? ' ' . t('Alternatively, you can upload a new file to %path using FTP (or other means). Press %reload after this FTP upload has finished to inform this form about this newly uploaded file.', array(
        '%path' => node_import_directory(),
        '%reload' => t('Reload'),
      )) : ''),
    ),
    'file_options' => array(
      'title' => t('Set file options'),
      'description' => t('Enter the file format options for the file. The options specify how to interpret the file. Select one of the predefined file formats or enter the record separator, field separator, text delimiter and escape character yourself.'),
    ),
    'map' => array(
      'title' => t('Map file columns'),
      'description' => t('For each content type field, select the columns in the file that contain the data for this field.'),
    ),
    'options' => array(
      'title' => t('Set import options'),
      'description' => t('On this page you can set some additional options.'),
    ),
    'defaults' => array(
      'title' => t('Set default values'),
      'description' => t('For each content type field, you can set the default value to use here. The default value is used when the field is not mapped to a file column or when the data in the mapped file column is empty for a record.'),
    ),
    'preview' => array(
      'title' => t('Preview import'),
      'description' => t('Here you can preview the import before importing the content. If there are errors, you may want to make changes to the previous pages. Press %reload if you change the number of records to preview.', array(
        '%reload' => t('Reload'),
      )),
    ),
    'start' => array(
      'title' => t('Start import'),
      'description' => t('Last chance to check all selected options. If you want to start the import, click %start.', array(
        '%start' => t('Start import'),
      )),
    ),
  );
  $titles = node_import_extract_property($pages, 'title');
  array_shift($titles);
  $page_keys = array_keys($pages);
  $first_page = $page_keys[0];
  $last_page = $page_keys[count($page_keys) - 1];
  $page = isset($values['page']) ? $values['page'] : $first_page;
  $form['#pages'] = $pages;
  $form_state['storage']['page'] = $page;

  // ------------------------------------------------------------
  // Set title and description for the current page.
  $steps = count($pages) - 1;
  $step = array_search($page, $page_keys);
  if ($page !== 'intro') {
    drupal_set_title(t('@title (step @step of @steps)', array(
      '@title' => $pages[$page]['title'],
      '@step' => $step,
      '@steps' => $steps,
    )));
  }
  $form['help'] = array(
    '#value' => '<div class="help">' . $pages[$page]['description'] . '</div>',
    '#weight' => -50,
  );

  // ------------------------------------------------------------
  // Introduction page.
  if ($page == 'intro') {
    $form['intro'] = array(
      '#value' => t('<h3>Buttons</h3><dl><dt>%back</dt><dd>Go to the previous page.</dd><dt>%reload</dt><dd>Reload the page without resetting the already filled in values.</dd><dt>%reset</dt><dd>Reset the values of the current page to their defaults.</dd><dt>%next</dt><dd>Go to the next page.</dd></dl>', array(
        '%next' => t('Next'),
        '%back' => t('Back'),
        '%reset' => t('Reset page'),
        '%reload' => t('Reload page'),
      )),
    );
    if (variable_get('node_import:annoy', 1)) {
      $form['intro']['#value'] .= t('<div class="warning"><p><strong>The <em>Node import</em> module is still in development</strong>. Check on the <a href="http://drupal.org/project/node_import" title="Node import project page">project page</a> to see if support for a specific module has been added. You can help the developers by</p><ul><li><a href="@documentation-path">writing documentation</a>,</li><li><a href="http://drupal.org/node/add/project-issue/4840">reporting bugs</a> (please read the issue submission instructions),</li><li><a href="http://drupal.org/project/node_import" title="Node import project page">fund the development by donating some money</a>.</li></ul></div>', array(
        '@documentation-path' => url('admin/advanced_help/node_import'),
      ));
    }
  }

  // ------------------------------------------------------------
  // Select or upload a file.
  if ($page == 'file') {
    $files = node_import_list_files(TRUE);
    $form['file_select'] = array(
      '#type' => 'item',
      '#title' => t('Select file'),
      '#theme' => 'node_import_file_select',
    );
    if (!empty($files)) {
      foreach ($files as $fid => $file) {
        $file_owner = user_load(array(
          'uid' => $file->uid,
        ));
        $form['file_select'][$fid] = array(
          'filename' => array(
            '#value' => $file->filename,
          ),
          'filepath' => array(
            '#value' => $file->filepath,
          ),
          'filesize' => array(
            '#value' => format_size($file->filesize),
          ),
          'timestamp' => array(
            '#value' => format_date($file->timestamp, 'small'),
          ),
          'uid' => array(
            '#value' => $file->uid == 0 ? t('Public FTPd file') : theme('username', $file_owner),
          ),
        );
      }
      $form['file_select']['fid'] = array(
        '#type' => 'radios',
        '#options' => node_import_extract_property($files, 'filename'),
        '#default_value' => isset($values['fid']) ? $values['fid'] : 0,
      );
    }
    else {
      $form['file_select']['fid'] = array(
        '#type' => 'item',
        '#value' => t('No files available.'),
      );
    }
    $form['#attributes'] = array(
      'enctype' => 'multipart/form-data',
    );
    $form['upload'] = array(
      '#type' => 'fieldset',
      '#title' => t('Upload file'),
      '#description' => t('<strong>The file you are uploading must be in UTF8 encoding.</strong>'),
      '#collapsible' => TRUE,
      '#collapsed' => !empty($files),
    );
    $form['upload']['file_upload'] = array(
      '#type' => 'file',
    );
    $form['upload']['file_upload_button'] = array(
      '#type' => 'submit',
      '#value' => t('Upload file'),
      '#submit' => array(
        'node_import_add_form_submit_upload_file',
      ),
    );
  }

  // ------------------------------------------------------------
  // Select file options.
  if ($page == 'file_options') {
    $record_separators = node_import_format_options('record separators') + array(
      '' => t('Other'),
    );
    $field_separators = node_import_format_options('field separators') + array(
      '' => t('Other'),
    );
    $text_delimiters = node_import_format_options('text delimiters') + array(
      '' => t('Other'),
    );
    $escape_characters = node_import_format_options('escape characters') + array(
      '' => t('Other'),
    );
    $file_options = isset($values['file_options']) ? $values['file_options'] : array();
    $has_headers = isset($values['has_headers']) ? $values['has_headers'] : 1;
    $form['has_headers'] = array(
      '#type' => 'checkbox',
      '#title' => t('First row contains column names'),
      '#description' => t('Check this option if the first row of the file contains names for each column. If the first row of the file contains data to import, uncheck this option.'),
      '#default_value' => $has_headers,
    );
    $form['file_options'] = array(
      '#title' => t('File options'),
      '#tree' => TRUE,
      '#theme' => 'node_import_file_format_select',
    );
    $file_formats = node_import_format_options('file formats');
    $file_format_options = node_import_extract_property($file_formats, 'title');
    $file_format_options[''] = t('Delimiter Separated Values');
    $form['file_options']['file_format'] = array(
      '#type' => 'radios',
      '#title' => t('File format'),
      '#options' => $file_format_options,
      '#default_value' => $file_options['file_format'],
    );
    foreach ($file_formats as $format => $info) {
      $form['file_options'][$format] = array(
        'record separator' => array(
          '#value' => $record_separators[$info['record separator']],
        ),
        'field separator' => array(
          '#value' => $field_separators[$info['field separator']],
        ),
        'text delimiter' => array(
          '#value' => $text_delimiters[$info['text delimiter']],
        ),
        'escape character' => array(
          '#value' => $escape_characters[$info['escape character']],
        ),
      );
    }
    foreach (array(
      'record separator' => $record_separators,
      'field separator' => $field_separators,
      'text delimiter' => $text_delimiters,
      'escape character' => $escape_characters,
    ) as $key => $options) {
      $form['file_options'][$key] = array(
        '#type' => 'radios',
        '#options' => $options,
        '#default_value' => $file_options[$key],
      );
      $form['file_options']['other ' . $key] = array(
        '#type' => 'textfield',
        '#size' => 6,
        '#default_value' => isset($file_options['other ' . $key]) ? $file_options['other ' . $key] : '',
      );
    }
    if (isset($values['samples'])) {
      $form['samples'] = array(
        '#type' => 'item',
        '#title' => t('Sample data'),
        '#value' => theme('node_import_sample_data', $values['samples'], variable_get('node_import:length_of_samples', 200)),
      );
    }
  }

  // ------------------------------------------------------------
  // Select the content type.
  if ($page == 'type') {
    $type_titles = node_import_extract_property($types, 'title');
    $default = count($type_titles) == 1 ? key($type_titles) : '';
    $form['type'] = array(
      '#type' => 'radios',
      '#title' => t('Content type'),
      '#options' => $type_titles,
      '#default_value' => isset($values['type']) ? $values['type'] : $default,
    );
  }

  // ------------------------------------------------------------
  // Map file columns.
  if ($page == 'map') {
    $form['map'] = array(
      '#tree' => TRUE,
      '#theme' => 'node_import_field_table',
      '#node_import-columns' => array(
        t('Content field'),
        t('Maps to column'),
      ),
      '#node_import-empty' => t('There are no content fields you can map.'),
    );
    $fields = node_import_fields($values['type']);
    $map = $form_state['storage']['headers'];
    $map_required = array(
      '' => t('- Please choose -'),
    ) + $map;
    $map_optional = array(
      '' => t('- None -'),
    ) + $map;
    foreach ($fields as $fieldname => $fieldinfo) {
      if ($fieldinfo['is_mappable']) {
        $description = '';
        if (count($fieldinfo['tips']) > 1) {
          $description = $form[$fieldname]['#description'] = '<ul class="tips">';
          $description .= '<li>' . implode('</li><li>', $fieldinfo['tips']) . '</li>';
          $description .= '</ul>';
        }
        else {
          if (!empty($fieldinfo['tips'])) {
            $description = implode('', $fieldinfo['tips']);
          }
        }
        $form['map'][$fieldname] = array(
          '#title' => $fieldinfo['title'],
          '#description' => $description,
          '#node_import-group' => $fieldinfo['group'],
          '#type' => 'select',
          '#options' => $fieldinfo['map_required'] ? $map_required : $map_optional,
          '#multiple' => $fieldinfo['has_multiple'] || $fieldinfo['has_hierarchy'],
          '#default_value' => isset($values['map'][$fieldname]) ? $values['map'][$fieldname] : '',
        );
      }
    }
    if (isset($values['samples'])) {
      $form['samples'] = array(
        '#type' => 'item',
        '#title' => t('Sample data'),
        '#value' => theme('node_import_sample_data', $values['samples'], variable_get('node_import:length_of_samples', 200)),
      );
    }
  }

  // ------------------------------------------------------------
  // Default values.
  if ($page == 'defaults') {
    $fields = node_import_fields($values['type']);
    $form['defaults'] = array(
      '#tree' => TRUE,
      '#theme' => 'node_import_field_table',
      '#node_import-columns' => array(
        t('Content field'),
        t('Default value'),
      ),
      '#node_import-empty' => t('There are no default values you can set.'),
    );
    $values['defaults'] = isset($values['defaults']) ? $values['defaults'] : array();
    $form['defaults'] = array_merge($form['defaults'], node_import_defaults($values['type'], $values['defaults'], $fields, $values['map']));
  }

  // ------------------------------------------------------------
  // Options.
  if ($page == 'options') {
    $fields = node_import_fields($values['type']);
    $form['options'] = array(
      '#tree' => TRUE,
      '#theme' => 'node_import_field_table',
      '#node_import-columns' => array(
        t('Content field'),
        t('Options'),
      ),
      '#node_import-empty' => t('There are no additional options you can set.'),
    );
    $values['options'] = isset($values['options']) ? $values['options'] : array();
    $form['options'] = array_merge($form['options'], node_import_options($values['type'], $values['options'], $fields, $values['map']));
  }

  // ------------------------------------------------------------
  // Preview.
  if ($page == 'preview') {
    $form['preview_count'] = array(
      '#type' => 'select',
      '#title' => t('Number of records to preview'),
      '#default_value' => isset($values['preview_count']) ? $values['preview_count'] : variable_get('node_import:preview_count', 5),
      '#options' => drupal_map_assoc(array(
        5,
        10,
        15,
        25,
        50,
        100,
        150,
        200,
      )),
    );
    $form['preview'] = array(
      '#title' => t('Previews'),
    );
    foreach ($values['previews'] as $i => $preview) {
      $form['preview'][] = array(
        '#type' => 'item',
        '#title' => t('Record @count', array(
          '@count' => $i + 1,
        )),
        '#value' => $preview,
      );
    }
  }

  // ------------------------------------------------------------
  // Start import.
  if ($page == 'start') {
    $files = node_import_list_files();
    $file = $files[$values['fid']];
    $form['name'] = array(
      '#title' => t('Name'),
      '#type' => 'textfield',
      '#default_value' => isset($values['name']) ? $values['name'] : $file->filename,
      '#maxlength' => 64,
      '#description' => t('You can set a name for this import here so you can more easily differentiate between several imports.'),
    );
    $form[] = node_import_task_details($values);
  }

  // ------------------------------------------------------------
  // Add Back, Next and/or Start buttons.
  $form['buttons-bottom'] = array(
    '#weight' => 50,
  );
  $form['buttons-bottom']['back_button'] = array(
    '#type' => 'submit',
    '#value' => t('Back'),
    '#submit' => array(
      'node_import_add_form_submit_back',
    ),
    '#disabled' => $page == $first_page,
  );
  $form['buttons-bottom']['reload_button'] = array(
    '#type' => 'submit',
    '#value' => t('Reload page'),
    '#submit' => array(
      'node_import_add_form_submit_reload',
    ),
    '#disabled' => $page == $first_page,
  );
  $form['buttons-bottom']['reset_button'] = array(
    '#type' => 'submit',
    '#value' => t('Reset page'),
    '#submit' => array(
      'node_import_add_form_submit_reset',
    ),
    '#disabled' => $page == $first_page,
  );
  $form['buttons-bottom']['next_button'] = array(
    '#type' => 'submit',
    '#value' => $page == $last_page ? t('Start import') : t('Next'),
    '#validate' => array(
      'node_import_add_form_validate_next',
    ),
    '#submit' => array(
      'node_import_add_form_submit_next',
    ),
    '#disabled' => empty($types),
  );
  $form['buttons-top'] = $form['buttons-bottom'];
  $form['buttons-top']['#weight'] = -40;
  return $form;
}