XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php @ 595

Last change on this file since 595 was 595, checked in by toemon, 14 years ago

MultiBlog時D3Forum一括インポート、エクスポートのバグ修正 Fixes#342

File size: 41.4 KB
Line 
1<?PHP
2// $Id: xoops_version.php,v 0.4 2007/07/21 01:35:02 toemon $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: toemon                                                      //
28// URL:http://www.toemon.com                           //
29// ------------------------------------------------------------------------- //
30function onaction_comment_post($comment_ID  = "")
31{
32        global $wpdb;
33        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
34        if (!empty($comment_type)) return ;     
35        return wp_comment_sync_to_d3forum($comment_ID,'post');
36}
37function onaction_edit_comment($comment_ID  = "")
38{
39        global $wpdb;
40        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
41        if (!empty($comment_type)) return ;     
42        return wp_comment_sync_to_d3forum($comment_ID,'edit');
43}
44function onaction_delete_comment($comment_ID  = "")
45{
46        global $wpdb;
47        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
48        if (!empty($comment_type)) return ;     
49        return wp_comment_sync_to_d3forum($comment_ID,'delete');
50}
51function onaction_delete_post($post_id)
52{
53        wp_post_delete_sync($post_id);
54}
55
56function onaction_comment_close($post_id)
57{
58        global $wpdb;
59        $status = $wpdb->get_var("SELECT comment_status FROM $wpdb->posts WHERE ID = $post_id");
60       
61        if ($status =='open')
62                $lock = 0;
63        else
64                $lock = 1;
65        d3forum_topic_rock($post_id,$lock);
66}
67
68function onaction_comment_apobe($comment_ID){
69        global $wpdb;
70        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
71        $status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $comment_ID");
72        if(is_null($status)) return;
73        //      $status = wp_get_comment_status($comment_ID);
74        switch($status){
75                case 'approved':
76                case 1:
77                        if (empty($comment_type)) onaction_edit_comment($comment_ID);
78                        break;
79                case 'unapproved':
80                case 0:
81                        if (empty($comment_type)) onaction_edit_comment($comment_ID);
82                        break;
83                default:
84                        break;
85        }
86}
87
88function disp_d3forum_comments($template_dir="", $file_name="")
89{
90        if (is_xpress_mobile()) return $file_path;
91        $file_path = dirname(__FILE__) . '/d3forum_comment_disp.php';
92        return $file_path;
93}
94
95
96function d3f_module_found($forum_dir ='d3forum'){               //use admin/admin_enhanced.php
97        return file_exists( get_xoops_root_path() . '/modules/' . $forum_dir);
98}
99
100function d3f_forum_id_found($forum_id = 1 , $forum_dir ='d3forum'){  //use admin/admin_enhanced.php
101        global $xoops_db;
102
103        $d3f_forums = get_xoops_prefix() . $forum_dir . '_forums';
104
105        $sql  = "SELECT forum_id ";
106        $sql .= "FROM $d3f_forums ";
107        $sql .= "WHERE ( forum_id = $forum_id )";
108
109        $forum_id =  $xoops_db->get_var($sql);
110        if (empty($forum_id)){
111                return false;
112        }
113        return $forum_id;
114}
115
116function get_d3forum_post_ID($wp_comment_ID){
117        global $xoops_db,$blog_id;
118       
119        if (empty($blog_id)) $blog_id =1;
120
121        $wp_d3forum_link = get_wp_prefix() . 'd3forum_link';
122       
123        $sql  = "SELECT post_id FROM $wp_d3forum_link WHERE comment_ID = $wp_comment_ID AND blog_id = $blog_id";
124        $post_id = $xoops_db->get_var($sql);
125        return $post_id;
126}               
127
128function get_wp_comment_ID($d3forum_post_ID){
129        global $xoops_db,$blog_id;
130       
131        if (empty($blog_id)) $blog_id =1;
132
133        $wp_d3forum_link = get_wp_prefix() . 'd3forum_link';
134       
135        $sql  = "SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $d3forum_post_ID AND blog_id = $blog_id";
136        $comment_ID = $xoops_db->get_var($sql);
137        return $comment_ID;
138}                               
139
140// Next, auto increment ID value used is acquired.
141function get_next_auto_increment_id($table_name,$id_name){
142        global $xoops_db;
143                $sql = "SELECT MAX($id_name) as last_id FROM $table_name";
144                $get_id = $xoops_db->get_var($sql);
145                if (empty($get_id)){
146                        return 1;
147                } else {
148                        return  $get_id + 1;
149                }
150}
151
152function is_d3forum_setting(){
153        global $xpress_config,$xoops_db;
154
155        $use_d3f = $xpress_config->is_use_d3forum;
156        if (empty($use_d3f)) {
157                return false;
158        }
159        $d3f_forum_id = $xpress_config->d3forum_forum_id;
160        $d3f_forum_dir  = $xpress_config->d3forum_module_dir;
161        if (! d3f_module_found($d3f_forum_dir)) die( "D3Forum Directory ($d3f_forum_dir) not found" ) ;
162        if (! d3f_forum_id_found($d3f_forum_id , $d3f_forum_dir)) die( "D3Forum ForumID($d3f_forum_id) not found" ) ;
163       
164        $xoops_db->query( "UPDATE ".get_xoops_prefix() . $d3f_forum_dir."_forums" ." SET forum_external_link_format='".addslashes($xpress_config->d3forum_external_link_format)."' WHERE forum_id= $d3f_forum_id" ) ;
165       
166        return true;
167}
168
169function d3forum_topic_rock($wp_post_id,$lock = '0')
170{
171        global $xpress_config,$xoops_db;
172       
173        $d3f_forum_id = $xpress_config->d3forum_forum_id;
174       
175        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
176        $d3f_forum_dir  = $xpress_config->d3forum_module_dir;
177       
178        $d3f_topic = get_xoops_prefix() . $d3f_forum_dir . '_topics';
179
180                $sql  = "UPDATE $d3f_topic ";
181                $sql .= "SET topic_locked = $lock ";
182                $sql .= "WHERE topic_external_link_id = $wp_post_id AND forum_id = $d3f_forum_id" ;
183                $xoops_db->query($sql);
184}
185
186// All comments of WordPress are exported to the D3Forum comment.
187function wp_to_d3forum($forum_id = 1, $d3f_prefix = 'd3forum'){
188        global $xpress_config,$xoops_db,$wpdb;
189        global $blog_id;
190        if (empty($blog_id)) $blog_id =1;
191
192        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
193       
194        $d3forum_prefix = get_xoops_prefix() . $d3f_prefix . '_';
195        $xpress_prefix = get_wp_prefix();
196       
197        $wp_comments = $wpdb->comments;
198        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
199        $wp_posts = $wpdb->posts;
200        $d3f_topic = $d3forum_prefix . 'topics';        // delete key forum_id
201        $d3f_forums = $d3forum_prefix . 'forums';       // delete key forum_id
202        $d3f_posts = $d3forum_prefix . 'posts'; // delete key topic_id
203        $d3f_users2topics = $d3forum_prefix . 'users2topics';   // delete key topic_id
204        $d3f_post_histories = $d3forum_prefix . 'post_histories';       // delete key post_id
205        $d3f_post_post_votes = $d3forum_prefix . 'post_votes';  // delete key post_id
206
207        //DELETE D3FORUM_TOPIC & D3FORUM_POSTS
208        $topics = $xoops_db->get_results("SELECT topic_id FROM $d3f_topic WHERE forum_id = $forum_id");
209        foreach($topics as $topic){
210                $now_topic_id = $topic->topic_id;
211                $posts = $xoops_db->get_results("SELECT post_id FROM $d3f_posts WHERE topic_id = $now_topic_id");
212                foreach($posts as $post){
213                        $now_post_id = $post->post_id;
214                        $xoops_db->query("DELETE FROM $d3f_post_histories WHERE post_id = $now_post_id");
215                        $xoops_db->query("DELETE FROM $d3f_post_post_votes WHERE post_id = $now_post_id");
216                }
217                $xoops_db->query("DELETE FROM $d3f_posts WHERE topic_id = $now_topic_id");
218                $xoops_db->query("DELETE FROM $d3f_users2topics WHERE topic_id = $now_topic_id");
219        }
220        $sql  = "UPDATE $d3f_forums ";
221        $sql .= "SET forum_topics_count = 0,forum_posts_count = 0,forum_last_post_id = 0,forum_last_post_time = 0 ";
222        $sql .= "WHERE forum_id = $forum_id" ;
223        $xoops_db->query($sql);
224       
225        $xoops_db->query("DELETE FROM $d3f_topic WHERE forum_id = $forum_id");
226       
227        $next_id = get_next_auto_increment_id($d3f_topic,'topic_id');
228        $xoops_db->query("ALTER TABLE $d3f_topic AUTO_INCREMENT = $next_id");
229       
230        $next_id = get_next_auto_increment_id($d3f_posts,'post_id');
231        $xoops_db->query("ALTER TABLE $d3f_posts AUTO_INCREMENT = $next_id");
232       
233        $next_id = get_next_auto_increment_id($d3f_post_histories,'history_id');
234        $xoops_db->query("ALTER TABLE $d3f_post_histories AUTO_INCREMENT = $next_id");
235
236        $next_id = get_next_auto_increment_id($d3f_post_post_votes,'vote_id');
237        $xoops_db->query("ALTER TABLE $d3f_post_post_votes AUTO_INCREMENT = $next_id");
238       
239        //All the records in the wp_d3forum_link table are deleted. 
240        $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE blog_id = $blog_id");
241
242        //The comment is copied from the wordpress comment.
243        $sql  = "SELECT comment_ID ";
244        $sql .= "FROM $wp_comments ";
245        $sql .= "WHERE (comment_approved NOT LIKE 'spam') AND (comment_type = '') ";
246        $sql .= "ORDER BY comment_ID";
247
248        $comment_count = 0;
249        $comments = $wpdb->get_results($sql);
250        foreach($comments as $comment){
251                $comment_ID = $comment->comment_ID;
252                wp_comment_sync_to_d3forum($comment_ID ,'insert');
253                $comment_count++;
254        }
255
256        $return_str = "...Export $comment_count Comment OK ";
257
258        return $return_str;
259}
260
261
262// All comments of D3Forum are import to the WordPress comment.
263function d3forum_to_wp($forum_id = 1, $d3f_prefix = 'd3forum'){
264        global $xpress_config,$xoops_db,$wpdb;
265        global $blog_id;
266        if (empty($blog_id)) $blog_id =1;
267        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
268       
269        $d3forum_prefix = get_xoops_prefix() . $d3f_prefix . '_';
270        $xpress_prefix = get_wp_prefix() ;
271       
272        $wp_comments = $wpdb->comments;
273        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
274        $wp_dummy = $xpress_prefix . 'dummy';
275       
276        $d3f_topic = $d3forum_prefix . 'topics';
277        $d3f_posts = $d3forum_prefix . 'posts';
278       
279        $db_xoops_users = get_xoops_prefix() . 'users';
280
281        // The track back data is taken out of the comment table, and it returns it to the initialized comment table.   
282        //copies it in the dummy table excluding a usual comment.
283        $xoops_db->query("CREATE TABLE $wp_dummy SELECT * FROM $wp_comments WHERE comment_type != ''");
284        //comment_ID of the dummy table is adjusted to all 0.
285        $xoops_db->query("UPDATE $wp_dummy SET `comment_ID` = 0");
286        //All the records in the comment table are deleted. 
287        $xoops_db->query("DELETE FROM $wp_comments WHERE 1");
288        //The auto increment value of the comment table is reset in '1'.
289        $xoops_db->query("ALTER TABLE $wp_comments AUTO_INCREMENT =1");
290        //The content of dummy table is returned to the comment table.
291        $xoops_db->query("INSERT INTO $wp_comments SELECT * FROM $wp_dummy");;
292        //The dummy table is deleted.
293        $xoops_db->query("DROP TABLE $wp_dummy");
294        //All the records in the wp_d3forum_link table are deleted. 
295        $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE  blog_id = $blog_id");
296
297        //All wp post comment count clear
298        $wp_posts = $wpdb->posts;
299        $xoops_db->query("UPDATE $wp_posts SET  comment_count = 0 WHERE 1 ");
300               
301//The comment is copied from the d3forum comment.
302        $d3f_sql  =     "SELECT $d3f_topic.forum_id, $d3f_topic.topic_external_link_id, $d3f_topic.topic_id, $d3f_posts.post_id, $d3f_posts.pid ";
303        $d3f_sql .=     "FROM $d3f_topic LEFT JOIN $d3f_posts ON $d3f_topic.topic_id = $d3f_posts.topic_id ";
304        $d3f_sql .=     "WHERE $d3f_topic.forum_id=$forum_id ";
305        $d3f_sql .= "ORDER BY $d3f_posts.post_id";
306
307        $d3f_res = $xoops_db->get_results($d3f_sql);
308        $import_count = 0;
309        foreach($d3f_res as $d3f_row){
310                $link_id = $d3f_row->topic_external_link_id;
311                $forum_id = $d3f_row->forum_id;
312                $topic_id = $d3f_row->topic_id;
313                $post_id = $d3f_row->post_id;
314                                       
315                if(empty($link_id)){ echo "<p><font color='#FF0000'>PASS: empty topic.topic_external_link_id in topic_id($topic_id)</font></p>" ; continue;}
316                if(empty($post_id)){ echo "<p><font color='#FF0000'>PASS: empty topic_id=$topic_id in $d3f_posts</font></p>" ; continue;}
317               
318                if ($d3f_row->pid == 0){
319                        $mode = 'newtopic';
320                }else{
321                        $mode = 'reply';
322                }
323                d3forum_sync_to_wp_comment( $mode , $link_id , $forum_id , $topic_id , $post_id);
324                $import_count++;
325        }
326        $return_str = "...Import $import_count Comment OK ";
327        return $return_str;
328}
329
330//When post of wordpress is deleted, the comment on relating d3forum is deleted.
331
332function wp_post_delete_sync($post_id){
333        global $xpress_config,$xoops_db;
334
335        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
336        $d3forum_dirname = $xpress_config->d3forum_module_dir;
337        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
338        $d3forum_forum_id = $xpress_config->d3forum_forum_id;
339
340        $post_id = intval( $post_id ) ;
341
342        $d3f_topics = $d3forum_prefix . 'topics';
343       
344        $sql = "SELECT topic_id,topic_first_post_id FROM $d3f_topics WHERE topic_external_link_id = $post_id AND forum_id = $d3forum_forum_id";
345        $row = $xoops_db->get_row($sql) ;
346       
347        if(empty($row)) return ;
348        $topic_id = $row->topic_id;
349        $topic_first_post_id = $row->topic_first_post_id;
350        wp_d3forum_delete_post_recursive( $d3forum_dirname , $topic_first_post_id ,true);
351//      wp_d3forum_delete_topic( $d3forum_dirname , $topic_id );
352        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
353}
354
355//  The content is reflected in the WordPress comment when there is a change in the D3Forum comment.
356
357function d3forum_sync_to_wp_comment( $mode , $link_id , $forum_id , $topic_id , $post_id = 0 ){
358        global $xpress_config,$xoops_db,$wpdb,$blog_id;
359
360        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
361
362        if (empty($blog_id)) $blog_id =1;
363        $d3f_forum_id = $xpress_config->d3forum_forum_id;
364
365        $d3forum_prefix = get_xoops_prefix() . $xpress_config->d3forum_module_dir . '_';
366        $xpress_prefix = get_wp_prefix();
367       
368        $wp_comments = $wpdb->comments;
369        $wp_posts = $wpdb->posts;
370        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
371       
372        $d3f_posts = $d3forum_prefix . 'posts';
373        $d3f_topics = $d3forum_prefix . 'topics';
374        $d3f_users2topics  = $d3forum_prefix . 'users2topics';
375        $d3f_post_votes = $d3forum_prefix . 'post_votes';
376
377        $db_xoops_users = get_xoops_prefix() . 'users';
378       
379        $comment_post_ID = $link_id;
380
381        $d3f_sql  =     "SELECT $d3f_posts.guest_name, ";
382        $d3f_sql .=     "$d3f_posts.guest_email, $d3f_posts.guest_url, $d3f_posts.poster_ip, $d3f_posts.post_time, ";
383        $d3f_sql .=     "$d3f_posts.post_text, $d3f_posts.approval, $d3f_posts.uid ,$d3f_posts.pid ";
384        $d3f_sql .=     "FROM $d3f_posts ";
385        $d3f_sql .=     "WHERE $d3f_posts.post_id = $post_id";
386
387        $d3f_row = $xoops_db->get_row($d3f_sql) ;
388        if (empty($d3f_row)) die('...Err. OPEN D3Forum Data (' .  $d3f_sql . ')');
389        $uid = $d3f_row->uid;
390        if (!empty($uid)) {
391                $xu_sql  = "SELECT uid ,name ,uname ,email , url FROM $db_xoops_users WHERE uid = $uid";
392                $xu_row =  $xoops_db->get_row($xu_sql);
393                if (empty($xu_row)){
394                        $user_display_name = '';
395                }else {
396                        if (empty($xu_row->name)){
397                                $user_display_name = $xu_row->uname;
398                        } else {
399                                $user_display_name = $xu_row->name;
400                        }
401                        $comment_author_email = "'" . $xu_row->email . "'";
402                        $comment_author_url = "'" . $xu_row->url . "'";
403                }
404                $comment_author = "'" . addSlashes($user_display_name) . "'";
405        } else {                                               
406                $comment_author = "'" . addSlashes($d3f_row->guest_name) . "'";
407                $comment_author_email = "'" . $d3f_row->guest_email . "'";
408                $comment_author_url = "'" . $d3f_row->guest_url . "'";
409        }
410        $comment_author_IP = "'" . $d3f_row->poster_ip . "'";
411        $comment_date = "'" . date('Y-m-d H:i:s' , $d3f_row->post_time) . "'";
412        $comment_content = "'" . addSlashes($d3f_row->post_text) . "'";
413        $comment_approved = "'" . $d3f_row->approval . "'";
414        $user_ID = $d3f_row->uid;
415        $comment_date_gmt = "'" . gmdate('Y-m-d H:i:s' , $d3f_row->post_time) . "'";
416        $comment_type = '';
417        if ($d3f_row->pid > 0) {
418                $comment_parent = get_wp_comment_ID($d3f_row->pid);
419        } else {
420                $comment_parent = 0 ;
421        }
422
423
424                switch($mode){                         
425                        case 'reply':
426                        case 'newtopic' :                               
427                                $wp_sql  = "INSERT INTO $wp_comments ";
428                                $wp_sql .=    "(comment_post_ID , comment_author , comment_author_email , comment_author_url , comment_author_IP , ";
429                                $wp_sql .=    "comment_date , comment_content , comment_approved , user_id , comment_date_gmt, comment_parent) ";
430                                $wp_sql .=  "VALUES ";
431                                $wp_sql .=    "($comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, ";
432                                $wp_sql .=    "$comment_date, $comment_content, $comment_approved, $user_ID, $comment_date_gmt, $comment_parent)";
433
434                                $wp_res =       $xoops_db->query($wp_sql);
435                                if ($wp_res === false) die( '...Err. INSERT' . $wp_comments . '(' . $wp_sql . ')');
436                                $comment_ID = mysql_insert_id();
437                                $wp_sql  = "UPDATE $wp_posts SET  comment_count = comment_count +1 WHERE ID = $comment_post_ID";
438                                $xoops_db->query($wp_sql);
439                                $wp_sql  = "INSERT INTO $wp_d3forum_link ";
440                                $wp_sql .=    "(comment_ID , post_id, wp_post_ID, forum_id, blog_id) ";
441                                $wp_sql .=  "VALUES ";
442                                $wp_sql .=    "($comment_ID, $post_id, $link_id, $d3f_forum_id, $blog_id)";             
443                                $xoops_db->query($wp_sql);                             
444                                if ($comment_approved ==0)      do_CommentWaiting($comment_ID, $post_id);
445                                break;
446                        case 'edit':
447                                $comment_ID = "SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id";
448                                $comment_ID = $xoops_db->get_var("SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id");
449                                if (empty($comment_ID)) die('...Err. EDIT' . $wp_comments . '(' . $wp_sql . ')');
450                                $wp_sql  = "UPDATE $wp_comments SET comment_content = $comment_content , comment_date_gmt = $comment_date_gmt WHERE comment_ID = $comment_ID";
451                                $wp_res = $xoops_db->query($wp_sql);
452                                if (empty($wp_res)) die( '...Err. UPDATE' . $wp_comments . '(' . $wp_sql . ')');
453                                break;
454                        case 'delete':
455                                // wordpress comments delete
456                                $comment_ID = get_wp_comment_ID($post_id);
457                                if ($comment_ID > 0){
458                                        $sql= "SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID";
459                                        $comment_type= $xoops_db->get_var("SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID");
460                                        if (!empty($comment_type)) break;
461                                        $xoops_db->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");                           
462                                        $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id");                             
463                                        $xoops_db->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");                         
464                                }
465                                break;
466                        default :
467                }                               
468               
469        return true ;
470}
471
472//  The content is reflected in the D3Forum comment when there is a change in the WordPress comment.
473function wp_comment_sync_to_d3forum($comment_ID = 0,$sync_mode){
474        global $xpress_config,$xoops_db,$xoops_config,$wpdb,$blog_id;
475       
476        if (empty($blog_id)) $blog_id =1;
477       
478        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
479        $mydirname = $xoops_config->module_name;
480
481        $d3f_forum_id = $xpress_config->d3forum_forum_id;
482        $d3f_forum_dir  = $xpress_config->d3forum_module_dir;
483       
484        $d3forum_prefix = get_xoops_prefix() . $d3f_forum_dir . '_';
485        $xpress_prefix = get_wp_prefix();
486        $wp_comments =  $wpdb->comments;
487//      $wp_comments = $xpress_prefix . 'comments';
488        $wp_posts = $wpdb->posts;
489//      $wp_posts = $xpress_prefix . 'posts';
490        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
491        $d3f_topic = $d3forum_prefix . 'topics';
492        $d3f_posts = $d3forum_prefix . 'posts';
493
494        $sql  = "SELECT $wp_comments.comment_ID,$wp_comments.comment_post_ID, ";
495        $sql .=         "$wp_comments.comment_author, $wp_comments.comment_author_email,  $wp_comments.comment_date, ";
496        $sql .=         "$wp_comments.comment_author_url, $wp_comments.comment_author_IP, ";
497        $sql .=         "$wp_comments.comment_content, $wp_comments.comment_karma, ";
498        $sql .=         "$wp_comments.comment_approved, $wp_comments.comment_agent, ";
499        $sql .=         "$wp_comments.comment_type, $wp_comments.comment_parent, $wp_comments.user_id, ";
500        $sql .=         "$wp_posts.post_title ,$wp_posts.comment_count ";
501        $sql .= "FROM $wp_comments INNER JOIN  $wp_posts ON $wp_comments.comment_post_ID = $wp_posts.ID ";
502        $sql .= "WHERE (comment_ID = $comment_ID) AND ($wp_comments.comment_approved NOT LIKE 'spam') ";
503
504//      $row = $xoops_db->get_row($sql) ;
505        $row = $wpdb->get_row($sql) ;
506        if(empty($row)) die( 'READ ' . $wp_comments . '_NG...' .$sql);
507        if (! empty($row->comment_type)) return;
508       
509        $forum_id = $d3f_forum_id;
510        $d3forum_dirname =$d3f_forum_dir;
511        $topic_external_link_id = $row->comment_post_ID; //There is information on WP post_ID in topic_external_link_id of D3Forum
512        $topic_title = 'Re.' . addSlashes($row->post_title);
513        $post_time = strtotime($row->comment_date);             // or   $row2['comment_date_gmt']
514        $modified_time = strtotime($row->comment_date); // or   $row2['comment_date_gmt']
515        require_once (get_xpress_dir_path() . 'include/general_functions.php');
516        if (empty($row->user_id)){
517                $uid = wp_comment_author_to_xoops_uid($row->comment_author,$row->comment_author_email);
518        } else {
519                $uid = wp_uid_to_xoops_uid($row->user_id,$mydirname);
520        }
521        $poster_ip = "'" . addslashes($row->comment_author_IP ). "'";
522        $modifier_ip = "'" . addslashes($row->comment_author_IP) . "'";
523        $subject = "'" . $topic_title . "'";
524    $post_text = "'" . addSlashes($row->comment_content) . "'";
525        $guest_name = "'" . addSlashes($row->comment_author) . "'";
526    $guest_email = "'" . $row->comment_author_email . "'";
527    $guest_url = "'" . $row->comment_author_url . "'";
528    $approval = $row->comment_approved;
529    $comment_count = $row->comment_count;
530    $comment_parent = $row->comment_parent;
531   
532        if ($sync_mode == 'delete'){
533                $mode = 'delete';
534                $delete_post_id = $xoops_db->get_var("SELECT post_id FROM $wp_d3forum_link WHERE comment_ID = $comment_ID AND  blog_id = $blog_id");
535                if (empty($delete_post_id)) return;
536                $topic_id = $xoops_db->get_var("SELECT topic_id FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id");
537                if (empty($topic_id)) return;
538        }else{
539                // Does the first comment (= topic) on the post exist?
540                $sql  = "SELECT * FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id";
541                $row = $xoops_db->get_row($sql) ;
542                $topic_first_post_id = $row->topic_first_post_id;
543                if (empty($row)){
544                        $mode = $mode = 'newtopic';
545                } else {
546                        $topic_id = $row->topic_id;
547                       
548                        // if comment on same ID exists then edits comment else reply comment
549                        $row = $xoops_db->get_row("SELECT * FROM $wp_d3forum_link WHERE comment_ID = $comment_ID AND blog_id = $blog_id" ) ;
550                       
551                        if (!empty($row)){
552                                $mode = $mode = 'edit';
553                                $edit_post_id = $row->post_id;
554                        } else {
555                                $mode = $mode = 'reply';
556                                $reply_pid = 0;
557                                if ($comment_parent > 0) {
558                                        $reply_pid = get_d3forum_post_ID($comment_parent);
559                                }
560                                if ($reply_pid == 0) {
561                                        $reply_pid = $topic_first_post_id; //reply_first_comment
562                                }
563                        }
564                }
565        }
566        $modified_time = $post_time;
567       
568        // make set part of INSERT or UPDATE (refalence d3forum main/post.php)
569        $set4sql = "modified_time= $modified_time , modifier_ip= $modifier_ip " ;
570        $set4sql .= ",subject= $subject " ;
571        $set4sql .= ",post_text= $post_text " ;
572       
573        if($uid == 0) {
574                @list( $guest_name , $trip_base ) = explode( '#' , $guest_name , 2 ) ;
575                if( ! trim( @$guest_name ) ) $guest_name = get_xoops_config('anonymous_name',$d3f_forum_dir) ;
576                if( ! empty( $trip_base ) && function_exists( 'crypt' ) ) {
577                        $salt = strtr( preg_replace( '/[^\.-z]/' , '.' , substr( $trip_base . 'H.' , 1 , 2 ) ) , ':;<=>?@[\]^_`' , 'ABCDEFGabcdef' ) ;
578                        $guest_trip = substr( crypt( $trip_base , $salt ) , -10 ) ;
579                } else {
580                        $guest_trip = '' ;
581                }
582                $guest_url = preg_match( '#^https?\://#' , $guest_url ) ? $guest_url : '' ;
583                foreach( array('guest_name','guest_email','guest_url','guest_trip') as $key ) {
584                        $set4sql .= ",$key='".addslashes($$key)."'" ;
585                }
586                if( ! empty( $guest_pass ) ) {
587                        $set4sql .= ",guest_pass_md5='".md5($guest_pass.'d3forum')."'" ;
588                }
589        }
590
591        $hide_uid = get_xoops_config('allow_hideuid',$d3f_forum_dir);
592       
593        switch($mode){
594                case 'edit':
595                        $edit_post = $xoops_db->get_row("SELECT * FROM $d3f_posts WHERE post_id= $edit_post_id ");
596                        if(empty($edit_post)) die( 'READ ' . $d3forum_comments . '_NG...' .$sql);
597                        // approval
598                        if( $approval ) {
599                                $set4sql .= ',approval=1' ;
600                                $topic_invisible = 0 ;
601                                $need_notify = true ;
602                        } else {
603                                $set4sql .= ',approval=0' ;
604                                $topic_invisible = 0 ;
605                                $need_admin_notify = true ;
606                        }
607                        // hide_uid
608                        if( $hide_uid ) {
609                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
610                        } else {
611                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
612                        }
613
614                        // update post specified post_id
615                        wp_d3forum_transact_make_post_history( $d3forum_dirname , $edit_post_id ) ;
616                        $sql = "UPDATE ".$d3f_posts." SET $set4sql WHERE post_id=$edit_post_id";
617                        $xoops_db->query($sql);
618                        $xoops_db->query($sql) ;
619                        if ($edit_post_pid == 0){
620                                $sql = "UPDATE ".$d3f_topic." SET topic_invisible=$topic_invisible WHERE topic_id=$topic_id";
621                                $xoops_db->query($sql);
622                        }
623                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , ! $edit_post_pid ) ;
624                        break;
625                       
626                case 'reply' :
627                        // approval
628                        if( $approval ) {
629                                $set4sql .= ',approval=1' ;
630                                $need_notify = true ;
631                        } else {
632                                $set4sql .= ',approval=0' ;
633                                $need_admin_notify = true ;
634                        }
635
636                        // hide_uid
637                        if( $hide_uid ) {
638                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
639                        } else {
640                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
641                        }
642
643                        // create post under specified post_id
644                        $sql = "INSERT INTO ".$d3f_posts." SET $set4sql,pid=$reply_pid,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
645                        $xoops_db->query($sql) ;
646                        $post_id = mysql_insert_id();
647                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
648                       
649                        $wp_sql  = "INSERT INTO $wp_d3forum_link ";
650                        $wp_sql .=    "(comment_ID , post_id, wp_post_ID, forum_id, blog_id) ";
651                        $wp_sql .=  "VALUES ";
652                        $wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id, $d3f_forum_id, $blog_id)";             
653                        $xoops_db->query($wp_sql);
654                       
655                        break;
656
657                case 'newtopic':
658                        // approval
659                        if( $approval ) {
660                                $set4sql .= ',approval=1' ;
661                                $topic_invisible = 0 ;
662                                $need_notify = true ;
663                        } else {
664                                $set4sql .= ',approval=0' ;
665                                $topic_invisible = 0 ;
666                                $need_admin_notify = true ;
667                        }
668
669                        // hide_uid
670                        if( $hide_uid ) {
671                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
672                        } else {
673                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
674                        }
675
676                        // create topic and get a new topic_id
677                        $sql = "INSERT INTO ".$d3f_topic." SET forum_id=$forum_id,topic_invisible=$topic_invisible,topic_external_link_id='".addslashes($topic_external_link_id)."'";
678                        $xoops_db->query($sql) ;
679                        $topic_id = mysql_insert_id();
680                        // create post in the topic
681                        $sql = "INSERT INTO ".$d3f_posts." SET $set4sql,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
682                        $xoops_db->query($sql) ;
683                        $post_id = mysql_insert_id();
684                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , true ) ;
685                       
686                        $wp_sql  = "INSERT INTO $wp_d3forum_link ";
687                        $wp_sql .=    "(comment_ID , post_id , wp_post_ID, forum_id, blog_id) ";
688                        $wp_sql .=  "VALUES ";
689                        $wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id, $d3f_forum_id, $blog_id)";             
690                        $xoops_db->query($wp_sql);
691
692                        break;
693                case 'delete':
694                        wp_d3forum_delete_post_recursive( $d3forum_dirname , $delete_post_id );
695                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
696                        break;
697                default:                               
698        }
699       
700        // increment post
701        if( is_object( @$xoopsUser ) && $mode != 'edit' ) {
702                $xoopsUser->incrementPost() ;
703        }
704        // set u2t_marked
705        $allow_mark = get_xoops_config('allow_mark',$d3f_forum_dir);
706
707        if( $uid && $allow_mark) {
708                $u2t_marked = empty( $_POST['u2t_marked'] ) ? 0 : 1 ;
709                $sql = "UPDATE ".$d3forum_prefix."users2topics"." SET u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP() WHERE uid=$uid AND topic_id=$topic_id" ;
710                if( ! $xoops_db->query($sql)){
711                        $sql = "INSERT INTO ".$d3forum_prefix."users2topics"." SET uid=$uid,topic_id=$topic_id,u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP()" ;
712                        $xoops_db->query($sql);
713                }
714        }
715}
716
717
718// ********************** refrence by d3forum *********************************************
719// delete posts recursively
720function wp_d3forum_delete_post_recursive( $d3forum_dirname , $post_id ,$isChild = false)
721{
722        global $wpdb,$blog_id;
723        global $xpress_config,$xoops_db;
724        $post_id = intval( $post_id ) ; // post_id is d3forum post(comments) id.
725        if (empty($blog_id)) $blog_id =1;
726
727       
728        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
729        $xpress_prefix = get_wp_prefix();
730       
731        $topic_id = $xoops_db->get_var("SELECT topic_id FROM ".$d3forum_prefix."posts WHERE post_id=$post_id");
732       
733        //It deletes it if there is a child comment.
734        $sql = "SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id" ;
735        $child_comments = $xoops_db->get_results("SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id");
736        foreach($child_comments as $child_comment){
737                        wp_d3forum_delete_post_recursive( $d3forum_dirname , $child_comment->post_id ,true) ;
738        }
739        wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , true ) ;
740        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
741        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."post_votes WHERE post_id=$post_id" ) ;
742       
743        $wp_comments = $wpdb->comments;
744        $wp_posts = $wpdb->posts;
745        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
746
747        $comment_ID = get_wp_comment_ID($post_id);  // get wordpress comment ID
748        if ($comment_ID > 0){
749                $comment_post_ID = $wpdb->get_var("SELECT comment_post_ID FROM $wp_comments WHERE comment_ID = $comment_ID");
750                if ($isChild){          //The first comment is deleted on the WordPress side.
751                        $wpdb->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");
752                        if (!empty($comment_post_ID)){
753                                $wpdb->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");
754                        }
755                }       
756                $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id AND blog_id = $blog_id");
757        }
758}
759
760
761// delete a topic
762function wp_d3forum_delete_topic( $d3forum_dirname , $topic_id , $delete_also_posts = true )
763{
764        global $xpress_config,$xoops_db;
765        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
766
767        $topic_id = intval( $topic_id ) ;
768
769        // delete posts
770        if( $delete_also_posts ) {
771                $posts = $xoops_db->query("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id");
772                if( !empty($posts)) {
773                        foreach($posts as $post){
774                                wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
775                        }
776                }
777        }
778
779        // delete notifications about this topic
780
781        // delete topic
782        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" );
783        // delete u2t
784        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" );
785}
786
787
788// store redundant informations to a topic from its posts
789// and rebuild tree informations (depth, order_in_tree)
790function wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , $sync_also_forum = true , $sync_topic_title = false )
791{
792        global $xpress_config,$xoops_db;
793        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
794        $xpress_prefix = get_wp_prefix();
795
796        $topic_id = intval( $topic_id ) ;
797
798        $forum_id = $xoops_db->get_var("SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id");
799
800        // get first_post_id
801        $first_post_id = $xoops_db->get_var("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0");
802
803        // get last_post_id and total_posts
804        $sql = "SELECT MAX(post_id) as last_post_id,COUNT(post_id) as total_posts FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
805        $row = $xoops_db->get_row($sql);
806        $last_post_id = $row->last_post_id;
807        $total_posts = $row->total_posts;
808
809        if( empty( $total_posts ) ) {
810                // this is empty topic should be removed
811                wp_d3forum_delete_topic( $d3forum_dirname , $topic_id ,0) ;
812
813        } else {
814
815                // update redundant columns in topics table
816                $row = $xoops_db->get_row( "SELECT post_time,uid,subject,unique_path FROM ".$d3forum_prefix."posts WHERE post_id=$first_post_id" ) ;
817                $first_post_time = $row->post_time;
818                $first_uid = $row->uid;
819                $first_subject = $row->subject;
820                $unique_path = $row->unique_path;
821                $row = $xoops_db->get_row( "SELECT post_time,uid FROM ".$d3forum_prefix."posts WHERE post_id=$last_post_id" ) ;
822                $last_post_time = $row->post_time;
823                $last_uid = $row->uid;
824                // sync topic_title same as first post's subject if specified
825                $topictitle4set = $sync_topic_title ? "topic_title='".addslashes($first_subject)."'," : "" ;
826
827                $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" );
828
829                // rebuild tree informations
830                $tree_array = wp_d3forum_maketree_recursive( $d3forum_prefix."posts" , intval( $first_post_id ) , 'post_id' , array() , 0 , empty( $unique_path ) ? '.1' : $unique_path ) ;
831                if( ! empty( $tree_array ) ) {
832                        foreach( $tree_array as $key => $val ) {
833                                $xoops_db->query( "UPDATE ".$d3forum_prefix."posts SET depth_in_tree=".$val['depth'].", order_in_tree=".($key+1).", unique_path='".addslashes($val['unique_path'])."' WHERE post_id=".$val['post_id'] ) ;
834                        }
835                }
836        }
837
838        if( $sync_also_forum )
839                return wp_d3forum_sync_forum( $d3forum_dirname , $forum_id ) ;
840        else
841                return true ;
842}
843
844function wp_d3forum_maketree_recursive( $tablename , $post_id , $order = 'post_id' , $parray = array() , $depth = 0 , $unique_path = '.1' )
845{
846        global $xpress_config,$xoops_db;
847
848        $parray[] = array( 'post_id' => $post_id , 'depth' => $depth , 'unique_path' => $unique_path ) ;
849
850        $sql = "SELECT post_id,unique_path FROM $tablename WHERE pid=$post_id ORDER BY $order" ;
851        if( ! $result = $xoops_db->get_results( $sql )) {
852                return $parray ;
853        }
854        $new_post_ids = array() ;
855        $max_count_of_last_level = 0 ;
856        foreach($result as $row){
857                $new_post_id = $row->post_id;
858                $new_unique_path = $row->unique_path;
859                $new_post_ids[ intval( $new_post_id ) ] = $new_unique_path ;
860                if( ! empty( $new_unique_path ) ) {
861                        $count_of_last_level = intval( substr( strrchr( $new_unique_path , '.' ) , 1 ) ) ;
862                        if( $max_count_of_last_level < $count_of_last_level ) {
863                                $max_count_of_last_level = $count_of_last_level ;
864                        }
865                }
866        }
867        foreach( $new_post_ids as $new_post_id => $new_unique_path ) {
868                if( empty( $new_unique_path ) ) {
869                        $new_unique_path = $unique_path . '.' . ++ $max_count_of_last_level ;
870                }
871                $parray = wp_d3forum_maketree_recursive( $tablename , $new_post_id , $order , $parray , $depth + 1 , $new_unique_path ) ;
872        }
873        return $parray ;
874}
875
876function wp_d3forum_makecattree_recursive( $tablename , $cat_id , $order = 'cat_weight' , $parray = array() , $depth = 0 , $cat_title = '' )
877{
878        global $xoops_db;
879
880        $parray[] = array( 'cat_id' => $cat_id , 'depth' => $depth , 'cat_title' => $cat_title ) ;
881
882        $sql = "SELECT cat_id,cat_title FROM $tablename WHERE pid=$cat_id ORDER BY $order" ;
883        $results = $xoops_db->get_results( $sql ) ;
884        if( empty($results) ) {
885                return $parray ;
886        }
887        foreach($result as $row){
888                $new_cat_id = $row->cat_id;
889                $new_cat_title = $row->cat_title;
890                $parray = wp_d3forum_makecattree_recursive( $tablename , $new_cat_id , $order , $parray , $depth + 1 , $new_cat_title ) ;
891        }
892        return $parray ;
893}
894
895
896// store redundant informations to a forum from its topics
897function wp_d3forum_sync_forum( $d3forum_dirname , $forum_id , $sync_also_category = true )
898{
899        global $xoops_db;
900        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
901
902        $forum_id = intval( $forum_id ) ;
903
904        $sql = "SELECT cat_id FROM ".$d3forum_prefix."forums WHERE forum_id=$forum_id" ;
905        if( ! $cat_id = $xoops_db->get_var( $sql ) ) die( "ERROR SELECT forum in sync forum" ) ;
906
907        $sql = "SELECT MAX(topic_last_post_id) as last_post_id ,MAX(topic_last_post_time) as last_post_time ,COUNT(topic_id) as topics_count,SUM(topic_posts_count) as posts_count FROM ".$d3forum_prefix."topics WHERE forum_id=$forum_id" ;
908        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT topics in sync forum" ) ;
909        $last_post_id = $row->last_post_id;
910        $last_post_time = $row->last_post_time;
911        $topics_count = $row->topics_count;
912        $posts_count = $row->posts_count;
913
914        $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" ) ;
915
916        if( $sync_also_category ) return wp_d3forum_sync_category( $d3forum_dirname , $cat_id ) ;
917        else return true ;
918}
919
920function get_d3forum_all_child_catid($d3forum_prefix,$sel_id, $order="", $idarray = array())
921{
922        global $xoops_db;
923        $sql = "SELECT * FROM ".$d3forum_prefix."categories WHERE pid =".$sel_id."";
924        if ( $order != "" ) {
925                $sql .= " ORDER BY $order";
926        }
927        $categories =$xoops_db->get_results($sql);
928        if ( empty($cat_ids)) {
929                return $idarray;
930        }
931        foreach( categories as $categorie ) {
932                $r_id = $categorie->cat_id;
933                array_push($idarray, $r_id);
934                $idarray = get_d3forum_all_child_catid($d3forum_prefix, $r_id,$order,$idarray);
935        }
936        return $idarray;
937}
938
939// store redundant informations to a category from its forums
940function wp_d3forum_sync_category( $d3forum_dirname , $cat_id )
941{
942        global $xoops_db;
943        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
944
945        $cat_id = intval( $cat_id ) ;
946
947        // get children
948        $children = get_d3forum_all_child_catid( $d3forum_prefix."categories" , $cat_id ) ;
949        $children[] = $cat_id ;
950        $children = array_map( 'intval' , $children ) ;
951
952        // topics/posts information belonging this category directly
953        $sql = "SELECT MAX(forum_last_post_id) as last_post_id,MAX(forum_last_post_time) as last_post_time,SUM(forum_topics_count) as topics_count,SUM(forum_posts_count) as posts_count FROM ".$d3forum_prefix."forums WHERE cat_id=$cat_id" ;
954        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
955        $last_post_id = $row->last_post_id ;
956        $last_post_time = $row->last_post_time ;
957        $topics_count = $row->topics_count ;
958        $posts_count = $row->posts_count ;
959
960        // topics/posts information belonging this category and/or subcategories
961        $sql = "SELECT MAX(forum_last_post_id) as last_post_id_in_tree,MAX(forum_last_post_time) as last_post_time_in_tree,SUM(forum_topics_count) as topics_count_in_tree,SUM(forum_posts_count) as posts_count_in_tree FROM ".$d3forum_prefix."forums WHERE cat_id IN (".implode(",",$children).")" ;
962        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
963        $last_post_id_in_tree = $row->last_post_id_in_tree ;
964        $last_post_time_in_tree = $row->last_post_time_in_tree ;
965        $topics_count_in_tree = $row->topics_count_in_tree ;
966        $posts_count_in_tree = $row->posts_count_in_tree ;
967
968        // update query
969        $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" );
970
971        // do sync parents
972        $pid = $xoops_db->get_var( "SELECT pid FROM ".$d3forum_prefix."categories WHERE cat_id=$cat_id" )  ;
973        if( $pid != $cat_id && $pid > 0 ) {
974                wp_d3forum_sync_category( $d3forum_dirname , $pid ) ;
975        }
976
977        return true ;
978}
979
980// make a new history entry for a post
981function wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , $full_backup = false )
982{
983        global $xoops_db;
984        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
985
986        $post_id = intval( $post_id ) ;
987
988        $results = $xoops_db->get_results( "SELECT * FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
989        if(empty($results)) return ;
990        $post_row = $results ;
991        $data = array() ;
992        $indexes = $full_backup ? array_keys( $post_row ) : array( 'subject' , 'post_text' ) ;
993        foreach( $indexes as $index ) {
994                $data[ $index ] = $post_row[ $index ] ;
995        }
996
997        // check the latest data in history
998        $old_data_serialized = $xoops_db->get_var( "SELECT data FROM ".$d3forum_prefix."post_histories WHERE post_id=$post_id ORDER BY history_time DESC" ) ;
999        if( !empty( $old_data_serialized ) ) {
1000                $old_data = unserialize( $old_data_serialized ) ;
1001                if( $old_data == $data ) return ;
1002        }
1003
1004        $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 ) )."'" ) ;
1005}
1006
1007?>
Note: See TracBrowser for help on using the repository browser.