--- import_typepad.module	2007-01-07 15:28:02.000000000 +1100
+++ import_typepad.module.mine	2007-10-26 14:04:45.000000000 +1000
@@ -11,6 +11,7 @@
 define('_WORKING_FILE', 'import_typepad_workingfile');
 define('_IMPORT_TYPEPAD', 'import_typepad');
 define('_ACTION_DELETE_FILE', 'Delete file from server');
+define('_CTYPE','import_typepad,content_type');
 
 function import_typepad_help($section) {
   switch ($section) {
@@ -116,7 +117,7 @@
   // Refresh preview w/ entry count
 
   // Previews
-  $output .= $items;
+ // $output .= $items;
 
   // Mappings and import
   // Get user list
@@ -137,6 +138,19 @@
       '#options' => $users);
   }
 
+  // get available types including CCK
+  $types["blog"] = "blog";
+  $result = db_query('SELECT type_name FROM {node_type};');
+  while ($type = db_fetch_object($result)) {
+    $types[$type->type_name] = $type->type_name;
+  }
+
+  $import_form["type"] = array(
+    "#type" => 'select',
+    "#title"=> t('Content type to map to'),
+    '#options' => $types);
+  
+
   $import_form["taxonomy_title"] = array(
     "#type" => "markup",
     "#value" => t("<span>Taxonomy mappings</span>"));
@@ -165,7 +179,7 @@
 function _import_typepad_taxonomy_list($name){
   if (user_access('access content')) {
     $default = '';
-    $vocabs = taxonomy_get_vocabularies('blog');
+    $vocabs = taxonomy_get_vocabularies();
     $items = array();
     foreach ($vocabs as $vocab) {
       $tree = taxonomy_get_tree($vocab->vid);
@@ -188,7 +202,10 @@
  */
 function import_typepad_process_submit($formid, $form_elements){
   $cats = array(); // unused
-  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_elements, $cats, $form_elements);
+  
+  variable_set(_CTYPE, $form_elements['type']);
+  
+  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null),$form_elements['type'], 0, $form_elements, $cats, $form_elements);
   if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){
     unlink( variable_get(_WORKING_FILE, null) );
   }
@@ -266,11 +283,12 @@
  *
  * @return preview
  */
-function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) {
+function _import_typepad_get_nodes($path, $type='blog', $preview, &$authors, &$cats, $taxonomy) {
   $handle = fopen($path, 'r');
   if ($handle == null)
     return false;
 
+  // this is the way the original author seemed todo things
   $success = 0;
 
   $run = true;
@@ -278,6 +296,7 @@
   $state = 0; // 0-unknown, 1-blog, 2-comment, 3-skip, 4-blog body, 5-blog excerpt
   $previewCount = 0;
   $blogCats = array();
+  $saveBlogs = array();
   
   $mapping = array('TITLE:'=>'title',
     'ALLOW COMMENTS:'=>'comment', 'EMAIL:'=>'mail', 'IP:'=>'hostname', 'URL:'=>'homepage');
@@ -334,18 +353,24 @@
               foreach ($blogCats as $id=>$val){
                 $c[] = $taxonomy['taxonomy_'.str_replace(' ','_',$val)];
               }
-              _import_typepad_save($currentBlog, $c);
+              // put these into an array for reversing later
+              // drupal wants the oldest entries in the database first
+              // due to its thread sorting methods
+              $blog['c']=$c;
+              $blog['currentBlog']=$currentBlog;
+              $saveBlogs[]=$blog;
+              
               $success++;
       	    } else if ($previewCount < $preview){
     	      $output .= node_view($currentBlog);
     	      $previewCount++;
             }
       	  }
-      		
+      		if(!$type) $type='blog';
       	  // 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);
@@ -358,7 +383,7 @@
       	  $currentBlog->comments = array();
       		
       	} else if ($state == 2) {
-      	  $currentBlog->comments[count($currentBlog->comments)-1]->name = substr($line, strlen("AUTHOR:"));
+      	  $currentBlog->comments[count($currentBlog->comments)-1]->name = ltrim(substr($line, strlen("AUTHOR:")));
       	} else {
       		echo "<h1>ERROR 1 State = $state</h1>";
       	}
@@ -415,6 +440,13 @@
     }
   }
 
+  if(sizeof($saveBlogs)>0) {
+   $saveBlogs=array_reverse($saveBlogs);
+    foreach($saveBlogs as $n => $blog) {
+      _import_typepad_save($blog['currentBlog'], $blog['c']);
+    }
+  }
+  
   fclose($handle);
   /*
   if ($errors) {
@@ -436,60 +468,58 @@
  */
 function _import_typepad_save($currentBlog, $terms){
   if ($currentBlog != null){
+
   // Apply all the substitutions
   $subReplace = $_REQUEST['import_typepad_substitution_replace'];
   $subWith = $_REQUEST['import_typepad_substitution_with'];
   
-  if (!is_null($subReplace))
+  if (!is_null($subReplace)) {
     foreach ($subReplace as $id=>$val){
       $currentBlog->teaser = str_replace($val, $subWith[$id], $currentBlog->teaser);
       $currentBlog->body = str_replace($val, $subWith[$id], $currentBlog->body);
     }
+  }
+
+  $currentBlog->comment=2;
+  
+  // we loose the ->body after node_save if we are going to use this for CCK
+  $_body = $currentBlog->body;
   
   // Save the entry
   node_save($currentBlog);
-
+  if(!strlen($currentBlog->body)) {
+    $currentBlog->body=$_body;
+  }
+  
+  // link to CCK entry for this
+  // mantis #83, #105, #391?
+  if (strstr($currentBlog->type, 'content_')) {
+     db_query("INSERT INTO {node_".$currentBlog->type."} (vid, nid, field_body_value) VALUES (%d, %d, '%s')", 
+                $currentBlog->nid, $currentBlog->nid, $currentBlog->body);
+  }
+  
   // Save taxonomy items
   $terms = array_unique($terms);
   taxonomy_node_save($currentBlog->nid, $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']));
-    //echo "<!-- max thread = $max -->\n";
-    // Strip the "/" from the end of the thread.
-    $max = rtrim($max, '/');
-
-    // Next, we increase this value by one.  Note that we can't
-    // use 1, 2, 3, ... 9, 10, 11 because we order by string and
-    // 10 would be right after 1.  We use 1, 2, 3, ..., 9, 91,
-    // 92, 93, ... instead.  Ugly but fast.
-    $decimals = (string) substr($max, 0, strlen($max) - 1);
-    $units = substr($max, -1, 1);
-    if ($units) {
-      $units++;
-    }
-    else {
-      $units = 1;
-    }
-
-    if ($units == 10) {
-      $units = '90';
-    }
 
-    // Finally, build the thread field for this new comment.
-    $thread = $decimals . $units .'/';
-          
+  $i=0;
+  //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";
+  
+  // needs to be in reverse order to typepad
+  // that is, oldest ones go first
+  // but typepad exports them newest first
+  $_comments=array_reverse($currentBlog->comments);
+  
+  foreach ($_comments as $comment){
+    //use drupals wierd int2vancode
+    $thread=int2vancode($i).'/';
     // 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);
+  	_comment_update_node_statistics($currentBlog->nid);
+    $i++;
   }
 }
+
 }
 ?>
