XPressME Integration Kit

Trac

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

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

D3Forumの不可視状態と、 WP3で導入された「ゴミ箱」の連携 Fixes#338

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