Index: node_import.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_import/node_import.module,v
retrieving revision 1.16
diff -c -r1.16 node_import.module
*** node_import.module	28 Jun 2005 00:28:55 -0000	1.16
--- node_import.module	24 Feb 2006 11:21:22 -0000
***************
*** 3,8 ****
--- 3,11 ----
  
  ini_set('auto_detect_line_endings', TRUE);
  
+ if (module_exist('taxonomy')) {
+   include_once('import_taxonomy.inc');
+ }
  if (module_exist('flexinode')) {
    include_once('import_flexinode.inc');
  }
***************
*** 42,48 ****
  function node_import_menu($may_cache) {
    $links = array();
    if ($may_cache) {
!     $links[] = array('path' => 'admin/node/node_import', 'title' => t('import'), 'callback' => 'node_import_page', 'weight' => 5, 'access' => user_access('import nodes'));
    }
    return $links;
  }
--- 45,55 ----
  function node_import_menu($may_cache) {
    $links = array();
    if ($may_cache) {
!     $links[] = array('path' => 'admin/node/node_import', 
!                      'title' => t('import'), 
!                      'callback' => 'node_import_page', 
!                      'weight' => 5, 
!                      'access' => user_access('import nodes'));
    }
    return $links;
  }
***************
*** 52,61 ****
  }
  
  function node_import_page() {
!   $edit = array_merge($_SESSION['node_import'], $_POST['edit']);
  
    // validate the form
    if ($_SESSION['node_import_page'] && $_POST) {
      $function = $_SESSION['node_import_page'] .'_validate';
      $function($_POST['op'], $edit);
    }
--- 59,72 ----
  }
  
  function node_import_page() {
!   $edit = array_merge((array)$_SESSION['node_import'], (array)$_POST['edit']);
! 
!   // This prevents drupal_get_form() from performing extra validation.
!   unset($_POST['edit']);
  
    // validate the form
    if ($_SESSION['node_import_page'] && $_POST) {
+     // Hmm.
      $function = $_SESSION['node_import_page'] .'_validate';
      $function($_POST['op'], $edit);
    }
***************
*** 69,97 ****
    $_SESSION['node_import'] = $edit;
    
    if ($_POST['op'] == t('Download rows with errors')) {
!     print $output;
    }
    else {
!     print theme('page', $output);
    }
  }
  
  function _node_import_start($edit) {
    if ($edit['file']) {
!     $output .= form_item(t('File'), $edit['filename'] .' ('. format_size($edit['file']->filesize) .')<br />'. form_submit(t('Use a different file')));
    }
    else {
!     $output .= form_file(t('Upload CSV file'), 'file', 48, t('Comma separated values file containing the data to be imported.'));
    }
    
    $types = module_invoke_all('node_import_types');
    $types['node_import'] = t('raw data (advanced)');
!   $output .= form_select(t('Type'), 'type', $edit['type'], $types);
! 
    // todo add an insert/update option
  
!   $output .= form_submit(t('Next'));
!   return form($output, 'post', 0, array('enctype' => 'multipart/form-data'));
  }
  
  function _node_import_start_validate($op, &$edit) {
--- 80,131 ----
    $_SESSION['node_import'] = $edit;
    
    if ($_POST['op'] == t('Download rows with errors')) {
!     return $output;
    }
    else {
!     return $output;
    }
  }
  
  function _node_import_start($edit) {
    if ($edit['file']) {
!     $form[] = array(
!       '#type' => 'item',
!       '#title' => t('File'),
!       '#value' => $edit['filename'] .' ('. format_size($edit['file']->filesize) .')'
!     );
!     $form[] = array(
!       '#type' => 'submit',
!       '#value' => t('Use a different file')
!     );
    }
    else {
!     $form['file'] = array(
!       '#type' => 'file',
!       '#title' => t('Upload CSV file'),
!       '#size' => 48,
!       '#description' => t('Comma separated values file containing the data to be imported.'),
!     );
    }
    
    $types = module_invoke_all('node_import_types');
    $types['node_import'] = t('raw data (advanced)');
!   $form['type'] = array(
!     '#type' => 'select',
!     '#title' => t('Type'),
!     '#default_value' => $edit['type'],
!     '#options' => $types,
!   );
    // todo add an insert/update option
  
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Next'),
!   );
!   $form['#method'] = 'post';
!   $form['#action'] = 0;
!   $form['#attributes'] = array('enctype' => 'multipart/form-data');
!   return drupal_get_form('node_import_start', $form);
  }
  
  function _node_import_start_validate($op, &$edit) {
***************
*** 129,140 ****
  }
  
  function _node_import_options($edit) {
!   $output .= form_item(t('File'), $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ');
  
    // todo add in a place to select fields to match on for update
  
    if ($edit['type'] != 'node_import') {
!     $output .= '<h3>'. t('Field matching') .'</h3>';
      $fields = array_merge(array('' => t('<none>')), module_invoke_all('node_import_fields', $edit['type']));
      $header = array(t('CSV header'), t('Import to field'), t('Sample data'));
      $data = array();
--- 163,182 ----
  }
  
  function _node_import_options($edit) {
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => t('File'),
!     '#value' => $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ',
!   );
  
    // todo add in a place to select fields to match on for update
  
    if ($edit['type'] != 'node_import') {
!     $form[] = array(
!       '#type' => 'item',
!       '#title' => t('Field matching'),
!       '#value' => ''
!     );
      $fields = array_merge(array('' => t('<none>')), module_invoke_all('node_import_fields', $edit['type']));
      $header = array(t('CSV header'), t('Import to field'), t('Sample data'));
      $data = array();
***************
*** 146,152 ****
        }
      }
      foreach ($csvheader as $i => $value) {
!       $data[] = array($value, form_select('', 'match]['. $i, $edit['match'][$i], $fields));
      }
      $j = 0;
      while (($row = fgetcsv($handle, 10000, ',')) && $j++ < 5) {
--- 188,201 ----
        }
      }
      foreach ($csvheader as $i => $value) {
!       $form['match]['. $i] = array(
!         '#type' => 'select',
!         '#title' => '',
!         '#default_value' => $edit['match'][$i],
!         '#options' => $fields
!       );
!       $form = form_builder('node_import_options', $form);
!       $data[] = array($value, form_render($form['match]['. $i]));
      }
      $j = 0;
      while (($row = fgetcsv($handle, 10000, ',')) && $j++ < 5) {
***************
*** 158,173 ****
      foreach ($datatmp as $i => $value) {
        $data[$i][] = implode(', ', $value);
      }
-     $output .= theme('table', $header, $data);
  
!     if ($global = implode('', module_invoke_all('node_import_global', $edit['type']))) {
!       $output .= form_group(t('Global fields'), str_replace('edit[', 'edit[global][', $global));
      }
    }
  
!   $output .= form_submit(t('Preview'));
!   $output .= form_submit(t('Back'));
!   return form($output);
  }
  
  function _node_import_options_validate($op, &$edit) {
--- 207,238 ----
      foreach ($datatmp as $i => $value) {
        $data[$i][] = implode(', ', $value);
      }
  
!     $form[] = array(
!       '#type' => 'item',
!       '#title' => '',
!       '#value' => theme('table', $header, $data)
!     );
! 
!     if ($global = module_invoke_all('node_import_global', $edit['type'])) {
!       $form['global'] = array(
!         '#type' => 'fieldset',
!         '#title' => t('Global fields'),
!         '#tree' => TRUE,
!         'global' => $global,
!       );
      }
    }
  
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Preview'),
!   );
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Back'),
!   );
!   return drupal_get_form('node_import_opts', $form);
  }
  
  function _node_import_options_validate($op, &$edit) {
***************
*** 180,201 ****
  }
  
  function _node_import_preview($edit) {
!   $output .= form_item(t('File'), $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ');
! 
!   $output .= '<h3>'. t('Preview') .'</h3>';
!   $output .= form_select(t('Number of entries'), 'preview_count', $edit['preview_count'], drupal_map_assoc(array(5, 10, 15, 25, 50, 100, 150, 200)));
    if (!$edit['preview_count']) {
      $edit['preview_count'] = 5;
    }
-   $output .= form_submit(t('Apply'));
-   
-   $output .= _node_import_get_nodes($edit['file']->filepath, $edit['type'], $edit['type'] == 'node_import' ? NULL : $edit['match'], $edit['global'], $edit['preview_count']);
  
!   $output .= '<p>'. t('Importing may take awhile, do not click \'Import\' more than once. To see progress, look at the Administer >> Content page in a new window.') .'</p>';
! 
!   $output .= form_submit(t('Import'));
!   $output .= form_submit(t('Back'));
!   return form($output);
  }
  
  function _node_import_preview_validate($op, &$edit) {
--- 245,289 ----
  }
  
  function _node_import_preview($edit) {
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => 'Preview',
!     '#value' => '<p>'. t('Importing may take awhile, do not click \'Import\' more than once. To see progress, look at the Administer >> Content page in a new window.') .'</p>'
!   );
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => t('File'),
!     '#value' => $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ',
!   );
    if (!$edit['preview_count']) {
      $edit['preview_count'] = 5;
    }
  
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => '',
!     '#value' => _node_import_get_nodes($edit['file']->filepath, $edit['type'], $edit['type'] == 'node_import' ? NULL : $edit['match'], $edit['global']['global'], $edit['preview_count'])
!   );
! 
!   $form['preview_count'] = array(
!     '#type' => 'select',
!     '#title' => t('Number of entries to preview'),
!     '#default_value' => $edit['preview_count'],
!     '#options' => drupal_map_assoc(array(5, 10, 15, 25, 50, 100, 150, 200)),
!   );
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Apply'),
!   );
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Import'),
!   );
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Back'),
!   );
!   return drupal_get_form('_node_import_prev', $form);
  }
  
  function _node_import_preview_validate($op, &$edit) {
***************
*** 210,226 ****
    else if ($op == t('Import')) {
      $_SESSION['node_import_page'] = '_node_import_import';
    }
  }
  
  function _node_import_import(&$edit) {
!   $output .= form_item(t('File'), $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ');
!   $output .= form_submit(t('Delete CSV file from server')); 
  
    $edit['errors'] = 0;
!   $output .= _node_import_get_nodes($edit['file']->filepath, $edit['type'], $edit['type'] == 'node_import' ? NULL : $edit['match'], $edit['global'], $edit['errors']);
    unset($edit['match']);
  
!   return form($output);
  }
  
  function _node_import_import_validate($op, &$edit) {
--- 298,328 ----
    else if ($op == t('Import')) {
      $_SESSION['node_import_page'] = '_node_import_import';
    }
+   else if ($op == t('Apply')) {
+     $_SESSION['node_import_page'] = '_node_import_preview';
+   }
  }
  
  function _node_import_import(&$edit) {
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => t('File'),
!     '#value' => $edit['filename'] .' ('. format_size($edit['file']->filesize) .') ',
!   );
!   $form[] = array(
!     '#type' => 'submit',
!     '#value' => t('Delete CSV file from server'),
!   );
  
    $edit['errors'] = 0;
!   $form[] = array(
!     '#type' => 'item',
!     '#title' => '',
!     '#value' => _node_import_get_nodes($edit['file']->filepath, $edit['type'], $edit['type'] == 'node_import' ? NULL : $edit['match'], $edit['global']['global'], $edit['errors'])
!   );
    unset($edit['match']);
  
!   return drupal_get_form('node_import_import', $form);
  }
  
  function _node_import_import_validate($op, &$edit) {
***************
*** 263,271 ****
    $j = 0;
    $success = 0;
    while (($row = fgetcsv($handle, 10000, ',')) && ($j++ < $preview || $preview == 0)) {
!     $node = array2object(array_merge(array('type' => $type), module_invoke_all('node_import_static', $type), $global));
      foreach ($row as $i => $value) {
!       $node->$match[$i] = $value;
        if ($match[$i] == 'name') {
          if ($account = user_load(array('name' => $value))) {
            $node->uid = $account->uid;
--- 365,373 ----
    $j = 0;
    $success = 0;
    while (($row = fgetcsv($handle, 10000, ',')) && ($j++ < $preview || $preview == 0)) {
!     $node = (object) array_merge(array('type' => $type), (array)module_invoke_all('node_import_static', $type), (array)$global, (array)_node_import_static());
      foreach ($row as $i => $value) {
!       !empty($match[$i]) && $node->$match[$i] = $value;
        if ($match[$i] == 'name') {
          if ($account = user_load(array('name' => $value))) {
            $node->uid = $account->uid;
***************
*** 286,292 ****
        }
      }
  
!     $node = node_validate($node);
  
      if (!node_access('create', $node)) {
        drupal_set_message(t('You are not authorized to post this type of node.'), 'error'); // todo be more specific about what type
--- 388,394 ----
        }
      }
  
!     node_validate($node);
  
      if (!node_access('create', $node)) {
        drupal_set_message(t('You are not authorized to post this type of node.'), 'error'); // todo be more specific about what type
***************
*** 326,329 ****
--- 428,438 ----
    return $output;
  }
  
+ /**
+  * Returns array of static fields for all nodes
+  */
+ function _node_import_static() {
+   return array('date' => date('c'));
+ }
+ 
  ?>
