Index: trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 105)
+++ trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 106)
@@ -569,7 +569,6 @@
 	switch($mode){
 		case 'edit':
-			$sql = "SELECT pid  FROM $d3f_posts WHERE post_id= $edit_post_id ";
-			$edit_post_pid = $xoops_db->get_var($sql);
-			if(empty($edit_post_pid)) die( 'READ ' . $d3forum_comments . '_NG...' .$sql);
+			$edit_post = $xoops_db->get_row("SELECT * FROM $d3f_posts WHERE post_id= $edit_post_id ");
+			if(empty($edit_post)) die( 'READ ' . $d3forum_comments . '_NG...' .$sql);
 			// approval
 			if( $approval ) {
@@ -593,8 +592,8 @@
 			$sql = "UPDATE ".$d3f_posts." SET $set4sql WHERE post_id=$edit_post_id";
 			$xoops_db->query($sql);
-			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN UPDATE post" ) ;
+			$xoops_db->query($sql) ;
 			if ($edit_post_pid == 0){
 				$sql = "UPDATE ".$d3f_topic." SET topic_invisible=$topic_invisible WHERE topic_id=$topic_id";
-				if( ! $xoops_db->query($sql) ) die( "DB ERROR IN UPDATE post" ) ;
+				$xoops_db->query($sql);
 			}
 			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , ! $edit_post_pid ) ;
@@ -620,5 +619,5 @@
 			// create post under specified post_id
 			$sql = "INSERT INTO ".$d3f_posts." SET $set4sql,pid=$reply_pid,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
-			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT post $sql" ) ;
+			$xoops_db->query($sql) ;
 			$post_id = mysql_insert_id();
 			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
@@ -653,9 +652,9 @@
 			// create topic and get a new topic_id
 			$sql = "INSERT INTO ".$d3f_topic." SET forum_id=$forum_id,topic_invisible=$topic_invisible,topic_external_link_id='".addslashes($topic_external_link_id)."'";
-			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT topic $sql" ) ;
+			$xoops_db->query($sql) ;
 			$topic_id = mysql_insert_id();
 			// create post in the topic
 			$sql = "INSERT INTO ".$d3f_posts." SET $set4sql,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
-			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT post $sql" ) ;
+			$xoops_db->query($sql) ;
 			$post_id = mysql_insert_id();
 			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , true ) ;
@@ -697,24 +696,20 @@
 function wp_d3forum_delete_post_recursive( $d3forum_dirname , $post_id ,$isChild = false)
 {
-	$post_id = intval( $post_id ) ;
+	$post_id = intval( $post_id ) ; // post_id is d3forum post(comments) id.
 	global $XPressME,$xoops_db;
 	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
 	$xpress_prefix = get_wp_prefix();
 	
+	$topic_id = $xoops_db->get_var("SELECT topic_id FROM ".$d3forum_prefix."posts WHERE post_id=$post_id");
+	
+	//It deletes it if there is a child comment. 
 	$sql = "SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id" ;
-	$found_id = $xoops_db->get_var($sql); 
-	if( empty($found_id))  die( "DB ERROR in wp_d3forum_delete_post_recursive ($sql)" ) ;
-
-	list( $topic_id ) = $xoops_db->get_results( "SELECT topic_id FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" );
-	$sql = "SELECT * FROM ".$d3forum_prefix."posts"." WHERE post_id=$post_id";
-	$topics= $xoops_db->get_results($sql);
-	foreach($topics as $topic){
-		$child_post_id = $topic->post_id ;
-		wp_d3forum_delete_post_recursive( $d3forum_dirname , $child_post_id ,true) ;
-	}
-
+	$child_comments = $xoops_db->get_results("SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id"); 
+	foreach($child_comments as $child_comment){
+			wp_d3forum_delete_post_recursive( $d3forum_dirname , $child_comment->post_id ,true) ;
+	}
 	wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , true ) ;
-	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."_posts WHERE post_id=$post_id" ) ;
-	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."_post_votes WHERE post_id=$post_id" ) ;
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."post_votes WHERE post_id=$post_id" ) ;
 	
 	$wp_comments = $xpress_prefix . 'comments';
@@ -722,19 +717,14 @@
 	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
 
-	$comment_ID = get_wp_comment_ID($post_id);
+	$comment_ID = get_wp_comment_ID($post_id);  // get wordpress comment ID
 	if ($comment_ID > 0){
-		$sql = "SELECT comment_post_ID FROM $wp_comments WHERE comment_ID = $comment_ID";
-		$comment_post_ID = $xoops_db->get_var($sql);
-
+		$comment_post_ID = $xoops_db->get_var("SELECT comment_post_ID FROM $wp_comments WHERE comment_ID = $comment_ID");
 		if ($isChild){		//The first comment is deleted on the WordPress side. 
-			$wp_sql = "DELETE FROM $wp_comments WHERE comment_ID = $comment_ID";
-			$xoops_db->query($wp_sql);
+			$xoops_db->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");
 			if (!empty($comment_post_ID)){
-				$wp_sql  = "UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID";
-				$xoops_db->query($wp_sql);
-			}
-		}		
-		$wp_sql = "DELETE FROM $wp_d3forum_link WHERE post_id = $post_id";
-		$xoops_db->query($wp_sql);
+				$xoops_db->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");
+			}
+		}	
+		$xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id");
 	}
 }
@@ -752,9 +742,9 @@
 	// delete posts
 	if( $delete_also_posts ) {
-		$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
-		$posts = $xoops_db->query($sql);
-		if( empty($posts)) die( 'SQL ERROR '.__LINE__ ) ;
-		foreach($posts as $post){
-			wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
+		$posts = $xoops_db->query("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id");
+		if( !empty($posts)) {
+			foreach($posts as $post){
+				wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
+			}
 		}
 	}
@@ -763,8 +753,7 @@
 
 	// delete topic
-	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" )) die( 'SQL ERROR '.__LINE__ ) ;
-
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" );
 	// delete u2t
-	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" )) die( 'SQL ERROR '.__LINE__ ) ;
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" );
 }
 
@@ -780,15 +769,12 @@
 	$topic_id = intval( $topic_id ) ;
 
-	$sql = "SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" ;
-	if( ! $forum_id = $xoops_db->get_var($sql) ) die( "ERROR SELECT topic in sync topic" ) ;
-
+	$forum_id = $xoops_db->get_var("SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id");
 
 	// get first_post_id
-	$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0" ;
-	if( ! $first_post_id = $xoops_db->get_var($sql) ) die( "ERROR SELECT first_post in sync topic" ) ;
+	$first_post_id = $xoops_db->get_var("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0");
 
 	// get last_post_id and total_posts
 	$sql = "SELECT MAX(post_id) as last_post_id,COUNT(post_id) as total_posts FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
-	if( ! $row = $xoops_db->get_row($sql) ) die( "ERROR SELECT last_post in sync topic" ) ;
+	$row = $xoops_db->get_row($sql);
 	$last_post_id = $row->last_post_id;
 	$total_posts = $row->total_posts;
@@ -812,5 +798,5 @@
 		$topictitle4set = $sync_topic_title ? "topic_title='".addslashes($first_subject)."'," : "" ;
 
-		if( ! $xoops_db->query( "UPDATE ".$d3forum_prefix."topics SET {$topictitle4set} topic_posts_count=$total_posts, topic_first_uid=$first_uid, topic_first_post_id=$first_post_id, topic_first_post_time=$first_post_time, topic_last_uid=$last_uid, topic_last_post_id=$last_post_id, topic_last_post_time=$last_post_time WHERE topic_id=$topic_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
+		$xoops_db->query( "UPDATE ".$d3forum_prefix."topics SET {$topictitle4set} topic_posts_count=$total_posts, topic_first_uid=$first_uid, topic_first_post_id=$first_post_id, topic_first_post_time=$first_post_time, topic_last_uid=$last_uid, topic_last_post_id=$last_post_id, topic_last_post_time=$last_post_time WHERE topic_id=$topic_id" );
 
 		// rebuild tree informations
@@ -899,5 +885,5 @@
 	$posts_count = $row->posts_count;
 
-	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."forums SET forum_topics_count=".intval($topics_count).",forum_posts_count=".intval($posts_count).", forum_last_post_id=".intval($last_post_id).", forum_last_post_time=".intval($last_post_time)." WHERE forum_id=$forum_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
+	$xoops_db->query( "UPDATE ".$d3forum_prefix."forums SET forum_topics_count=".intval($topics_count).",forum_posts_count=".intval($posts_count).", forum_last_post_id=".intval($last_post_id).", forum_last_post_time=".intval($last_post_time)." WHERE forum_id=$forum_id" ) ;
 
 	if( $sync_also_category ) return wp_d3forum_sync_category( $d3forum_dirname , $cat_id ) ;
@@ -905,4 +891,23 @@
 }
 
+function get_d3forum_all_child_catid($d3forum_prefix,$sel_id, $order="", $idarray = array())
+{
+	global $xoops_db;
+	$sql = "SELECT * FROM ".$d3forum_prefix."categories WHERE pid =".$sel_id."";
+	if ( $order != "" ) {
+		$sql .= " ORDER BY $order";
+	}
+	$categories =$xoops_db->get_results($sql);
+	if ( empty($cat_ids)) {
+		return $idarray;
+	}
+	foreach( categories as $categorie ) {
+		$r_id = $categorie->cat_id;
+		array_push($idarray, $r_id);
+		$idarray = get_d3forum_all_child_catid($d3forum_prefix, $r_id,$order,$idarray);
+	}
+	return $idarray;
+}
+
 // store redundant informations to a category from its forums
 function wp_d3forum_sync_category( $d3forum_dirname , $cat_id )
@@ -914,7 +919,5 @@
 
 	// get children
-	include_once get_xoops_root_path()."/class/xoopstree.php" ;
-	$mytree = new XoopsTree( $d3forum_prefix."categories" , "cat_id" , "pid" ) ;
-	$children = $mytree->getAllChildId( $cat_id ) ;
+	$children = get_d3forum_all_child_catid( $d3forum_prefix."categories" , $cat_id ) ;
 	$children[] = $cat_id ;
 	$children = array_map( 'intval' , $children ) ;
@@ -937,5 +940,5 @@
 
 	// update query
-	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."categories SET cat_topics_count=".intval($topics_count).",cat_posts_count=".intval($posts_count).", cat_last_post_id=".intval($last_post_id).", cat_last_post_time=".intval($last_post_time).",cat_topics_count_in_tree=".intval($topics_count_in_tree).",cat_posts_count_in_tree=".intval($posts_count_in_tree).", cat_last_post_id_in_tree=".intval($last_post_id_in_tree).", cat_last_post_time_in_tree=".intval($last_post_time_in_tree)." WHERE cat_id=$cat_id" ) ) die( 'SQL ERROR '.__LINE__ ) ;
+	$xoops_db->query( "UPDATE ".$d3forum_prefix."categories SET cat_topics_count=".intval($topics_count).",cat_posts_count=".intval($posts_count).", cat_last_post_id=".intval($last_post_id).", cat_last_post_time=".intval($last_post_time).",cat_topics_count_in_tree=".intval($topics_count_in_tree).",cat_posts_count_in_tree=".intval($posts_count_in_tree).", cat_last_post_id_in_tree=".intval($last_post_id_in_tree).", cat_last_post_time_in_tree=".intval($last_post_time_in_tree)." WHERE cat_id=$cat_id" );
 
 	// do sync parents
@@ -972,5 +975,5 @@
 	}
 
-	if( ! $xoops_db->query( "INSERT INTO ".$d3forum_prefix."post_histories SET post_id=$post_id, history_time=UNIX_TIMESTAMP(), data='".mysql_real_escape_string( serialize( $data ) )."'" ) ) die( "DB ERROR ON making post_history".__LINE__ ) ;
+	$xoops_db->query( "INSERT INTO ".$d3forum_prefix."post_histories SET post_id=$post_id, history_time=UNIX_TIMESTAMP(), data='".mysql_real_escape_string( serialize( $data ) )."'" ) ;
 }
 
