XPressME Integration Kit

Trac

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

Last change on this file since 105 was 105, checked in by toemon, 16 years ago

D3Forumコメント統合 WPからD3Fへの同期機能 暫定版

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