--- import_typepad_orig.module	2008-08-18 19:01:28.000000000 -0700
+++ import_typepad.module	2008-08-19 11:59:19.000000000 -0700
@@ -12,22 +12,22 @@
 define('_IMPORT_TYPEPAD', 'import_typepad');
 define('_ACTION_DELETE_FILE', 'Delete file from server');
 
-function import_typepad_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Import nodes from a typepad file.');
-    case 'admin/help#import_typepad': 
-      return t(' ', array('%admin-node-import_typepad' => url('admin/node/import_typepad'), '%admin-access-permission' => url('admin/access/permission')));
-  }
-}
-
-function import_typepad_menu($may_cache) {
-  $links = array();
-  if ($may_cache) {
-    $links[] = array('path' => 'admin/node/import_typepad', 'title' => t('type pad'), 'callback' => 'import_typepad_page', 'weight' => 5, 'access' => user_access('import nodes'));
-    $links[] = array('path' => 'admin/node/import_typepad/preview', 'title' => t('type pad'), 'callback' => 'import_typepad_preview', 'weight' => 5, 'access' => user_access('import nodes'), 'type' => MENU_CALLBACK);
-  }
-  return $links;
+function import_typepad_menu() {
+  $items = array();
+  $items['admin/content/import_typepad'] = array(
+    'title' => 'Import Typepad Content', 
+    'page callback' => 'import_typepad_page', 
+    'weight' => 5, 
+    'access arguments' => array('import nodes'),
+  );
+  $items['admin/content/import_typepad/preview'] = array(
+    'title' => 'type pad', 
+    'page callback' => 'import_typepad_preview', 
+    'weight' => 5, 
+    'access arguments' => array('import nodes'), 
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
 }
 
 function import_typepad_perm() {
@@ -37,53 +37,57 @@
 /**
  * Show the front page, with either the current working file, or a file selection form
  */
-function import_typepad_page($form_values=NULL) {
+function import_typepad_page() {
 
   if ($edit[_WORKING_FILE]) {
     $output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file')));
   }
-  else {
-    $form['upload_file'] = array(
-      '#type' => 'file',
-      '#title' => t('Upload Type Pad/Movable Type file'),
-      '#description' => t('The export file you wish to import'));
-    $form['serverside_file'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Server side file'),
-      '#description' => t('The file located on the server you wish to import'));
-  }
   
   // todo add an insert/update option
+  $output = drupal_get_form('import_typepad_form_select');
+
+  return $output;
+}
+
+function import_typepad_form_select(){
+  $form['upload_file'] = array(
+    '#type' => 'file',
+    '#title' => t('Upload Type Pad/Movable Type file'),
+    '#description' => t('The export file you wish to import'));
+  $form['serverside_file'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Server side file'),
+    '#description' => t('The file located on the server you wish to import'));
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Step 2'));
 
   $form['#attributes'] = array('enctype' => 'multipart/form-data');
-  
-  $output = drupal_get_form('typepad_step1', $form);
 
-  return $output;
+  return $form;
 }
 
 /**
  * Discover which file we are using, and move it into the appropriate position
  */
-function typepad_step1_validate($form_id, $form_values){
+function import_typepad_form_select_validate($form, &$form_state){
   global $user;
   global $base_url;
 
   $file = file_save_upload('upload_file');
 
+  print '--'. file_exists($form_state['values']['serverside_file']) .'--';
+
   if (is_object($file) && $file->error == 0 && $file->filesize > 0) {
     drupal_set_message(t('Using uploaded file'));
     file_move($file->filepath, 'drupal.import_typepad.'. strtr($base_url, array('http://' => '', '/' => '.')) .'.'. $user->uid, 1);
     //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath;
     variable_set(_WORKING_FILE, $file->filepath);
     variable_set(_FILE_TYPE, 'UPLOAD');
-  } else if (strlen($form_values['serverside_file'])>0 && file_exists($form_values['serverside_file'])){
-    drupal_set_message("Using server file ".$form_values['serverside_file']);
+  } else if (strlen($form_state['values']['serverside_file'])>0 && file_exists($form_state['values']['serverside_file'])){
+    drupal_set_message("Using server file ".$form_state['values']['serverside_file']);
     // See if there was a server file
-    variable_set(_WORKING_FILE, $form_values['serverside_file']);
+    variable_set(_WORKING_FILE, $form_state['values']['serverside_file']);
     variable_set(_FILE_TYPE, 'SERVER');
     //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile'];
   }
@@ -96,8 +100,8 @@
 /**
  * If the validation passed then simple redirect to the preview
  */
-function typepad_step1_submit($form_id, $form_values){
-  return 'admin/node/import_typepad/preview';
+function import_typepad_form_select_submit($form, &$form_state){
+  $form_state['redirect'] = 'admin/content/import_typepad/preview';
 }
 
 /**
@@ -106,18 +110,21 @@
  * In due course we'll pass the preview count via the parameters
  */
 function import_typepad_preview($previewCount = 10) {
-  $authors = array();
-  $cats = array();
   $output = '';
-
+  $authors = array();
+  
   $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], $previewCount, $authors, $cats, array());
 
+  // Previews
+  $output .= "<h3>".t("Preview Items To Import")."</h3>".$items;
+  $output .= "<h3>".t("Select Import Options")."</h3>".
+                    drupal_get_form('import_typepad_form_process',$authors,$cats);
 
-  // Refresh preview w/ entry count
 
-  // Previews
-  $output .= $items;
+  return $output;
+}
 
+function import_typepad_form_process($form_state,$authors,$cats){
   // Mappings and import
   // Get user list
   $users = array();
@@ -127,35 +134,34 @@
     $users[$user->uid] = $user->name;
   }
 
-  $import_form["author_title"] = array(
-    "#type" => "markup",
-    "#value" => t("<span>Author mappings</span>"));
+  $import_form["author_mappings"] = array(
+    "#type" => "fieldset",
+    "#title" => t("Author mappings"));
+  
   foreach ($authors as $author=>$uid){
-    $import_form[$author] = array(
+    $import_form["author_mappings"][$author] = array(
       "#type" => 'select',
       "#title"=> $author,
       '#options' => $users);
   }
 
-  $import_form["taxonomy_title"] = array(
-    "#type" => "markup",
-    "#value" => t("<span>Taxonomy mappings</span>"));
+  $import_form["taxonomy_mappings"] = array(
+    "#type" => "fieldset",
+    "#title" => t("Taxonomy mappings"));
 
   foreach ($cats as $cat=>$count){
     $item = _import_typepad_taxonomy_list($cat);
     $item['#title'] = "$cat ($count)";
-    $import_form['taxonomy_'.str_replace(' ','_',$cat)] = $item;
+    $import_form["taxonomy_mappings"]['taxonomy_'.str_replace(' ','_',$cat)] = $item;
   }
 
   $import_form["import_warning"] = array (
     "#type" => "markup",
-    "#value" => t('<span>Importing may take awhile, do not click \'Import\' more than once. To see progress, look at the <a href="?q=admin/node" target="_new">Administer -> Content</a> page in a new window.</span>')  );
+    "#value" => t('Importing may take a while, do not click \'Import\' more than once. To see progress, look at the <a href="?=admin/content/node">administer content</a> page in a new window.<br/><br/>'));
 
   $import_form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
 
-  $output .= drupal_get_form('import_typepad_process', $import_form);
-
-  return $output;
+  return $import_form;
 }
 
 /**
@@ -165,15 +171,15 @@
 function _import_typepad_taxonomy_list($name){
   if (user_access('access content')) {
     $default = '';
-    $vocabs = taxonomy_get_vocabularies('blog');
-    $items = array();
+    $vocabs = taxonomy_get_vocabularies();
+    $items = array(0=>t("(none)"));
     foreach ($vocabs as $vocab) {
       $tree = taxonomy_get_tree($vocab->vid);
       foreach ($tree as $term) {
-        $items[$term->tid] = _taxonomy_depth($term->depth) .' '.$term->name;
-	if ($name == $term->name){
-          $default = $term->tid;
-	}
+        $items[$term->tid] = $vocab->name." | ".$term->name;
+      	if ($name == $term->name){
+                $default = $term->tid;
+      	}
       }
     }
     return array(
@@ -186,15 +192,15 @@
 /**
  * Simply run the import, passing the form in for the taxonomy and author maps
  */
-function import_typepad_process_submit($formid, $form_elements){
+function import_typepad_form_process_submit($form, &$form_state){
   $cats = array(); // unused
-  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_elements, $cats, $form_elements);
+  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_state['values'], $cats, $form_state['values']);
   if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){
     unlink( variable_get(_WORKING_FILE, null) );
   }
   variable_del(_WORKING_FILE);
   variable_del(_FILE_TYPE);
-  return 'admin/node/import_typepad';
+  $form_state['redirect'] = 'admin/content/import_typepad';
 }
 
 /**
@@ -266,7 +272,7 @@
  *
  * @return preview
  */
-function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) {
+function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy, $type="blog") {
   $handle = fopen($path, 'r');
   if ($handle == null)
     return false;
@@ -345,7 +351,7 @@
       	  // Start new blog
           $blogCats = array();
       	  $currentBlog = new StdClass();
-      	  $currentBlog->type = 'blog';
+      	  $currentBlog->type = $type;
       	  $currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) );
       	  // We generate a hash for the author name which can be used in HTML forms
       	  $authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author);
@@ -381,9 +387,9 @@
       	} else if ($state == 2){
 	  //echo "<!-- $name $val $sep -->\n";
       	  if (strlen($name) > 0 && array_key_exists($name, $mapping)){
-      	    $currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = $value;
+      	    $currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = trim($value);
       	  } else {
-      	    $currentBlog->comments[count($currentBlog->comments)-1]->comment .= $rawLine;
+      	    $currentBlog->comments[count($currentBlog->comments)-1]->comment .= trim($rawLine);
       	  }
       	} else if ($state == 3){
       		// Do nothing (Skip)
@@ -425,7 +431,7 @@
   }
   */
   if ($success) {
-    drupal_set_message(t('Successfully imported %count.', array('%count' => format_plural($success, '1 node', '%count nodes'))));
+    drupal_set_message(t('Successfully imported %count.', array('%count' => $success . format_plural($success, ' node', ' nodes'))));
   }
   
   return $output;
@@ -451,13 +457,12 @@
 
   // Save taxonomy items
   $terms = array_unique($terms);
-  taxonomy_node_save($currentBlog->nid, $terms);
+  taxonomy_node_save($currentBlog, $terms);
   
   //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";
   foreach ($currentBlog->comments as $comment){
     // Most of the following code is ripped straight from comment.module
-    $cid = db_next_id('{comments}_cid');
-  	
+
     // This is a comment with no parent comment (depth 0): we start
     // by retrieving the maximum thread level.
     $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
@@ -486,9 +491,10 @@
     $thread = $decimals . $units .'/';
           
     // Type pad doesn't have threaded comments, or subject lines, so we have to improvise them
-  	db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", 
-  	  $cid, $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, 0/*score*/, ''/*$users*/, $thread, $comment->name, $comment->mail, $comment->homepage);
-  	_comment_update_node_statistics($nid);
+  	db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, $thread, $comment->name, $comment->mail, $comment->homepage);
+  	
+  	db_last_insert_id('comments', 'cid');
+//  	_comment_update_node_statistics($nid);
   }
 }
 }
