XPressME Integration Kit

Trac

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

Last change on this file since 860 was 860, checked in by toemon, 8 years ago

PHP7 対応 Fix #431
thx nao-pon

File size: 43.4 KB
Line 
1<?PHP
2// $Id: xoops_version.php,v 0.4 2007/07/21 01:35:02 toemon $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: toemon                                                      //
28// URL:http://www.toemon.com                           //
29// ------------------------------------------------------------------------- //
30function onaction_comment_post($comment_ID  = "")
31{
32        global $wpdb;
33        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
34        if (!empty($comment_type)) return ;     
35        return wp_comment_sync_to_d3forum($comment_ID,'post');
36}
37function onaction_edit_comment($comment_ID  = "")
38{
39        global $wpdb;
40        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
41        if (!empty($comment_type)) return ;     
42        return wp_comment_sync_to_d3forum($comment_ID,'edit');
43}
44function onaction_delete_comment($comment_ID  = "")
45{
46        global $wpdb;
47        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
48        if (!empty($comment_type)) return ;     
49        return wp_comment_sync_to_d3forum($comment_ID,'delete');
50}
51function onaction_delete_post($post_id)
52{
53        wp_post_delete_sync($post_id);
54}
55
56function onaction_comment_close($post_id)
57{
58        global $wpdb;
59        $status = $wpdb->get_var("SELECT comment_status FROM $wpdb->posts WHERE ID = $post_id");
60       
61        if ($status =='open')
62                $lock = 0;
63        else
64                $lock = 1;
65        d3forum_topic_rock($post_id,$lock);
66}
67
68function onaction_comment_apobe($comment_ID){
69        global $wpdb;
70        $comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $comment_ID");
71        $status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $comment_ID");
72        if(is_null($status)) return;
73        //      $status = wp_get_comment_status($comment_ID);
74        switch($status){
75                case 'approved':
76                case 1:
77                        if (empty($comment_type)) onaction_edit_comment($comment_ID);
78                        break;
79                case 'unapproved':
80                case 0:
81                        if (empty($comment_type)) onaction_edit_comment($comment_ID);
82                        break;
83                default:
84                        break;
85        }
86}
87
88function 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        $gmt_offset = get_option('gmt_offset');
462        $local_timestamp = $d3f_row->post_time + ($gmt_offset * 3600);
463        $comment_date = "'" . date('Y-m-d H:i:s' , $local_timestamp) . "'";
464        $comment_content = "'" . addSlashes($d3f_row->post_text) . "'";
465        $comment_approved = "'" . $d3f_row->approval . "'";
466        $user_ID = $d3f_row->uid;
467        $comment_date_gmt = "'" . gmdate('Y-m-d H:i:s' , $d3f_row->post_time) . "'";
468        $comment_type = '';
469        if ($d3f_row->pid > 0) {
470                $comment_parent = get_wp_comment_ID($d3f_row->pid);
471        } else {
472                $comment_parent = 0 ;
473        }
474
475
476                switch($mode){                         
477                        case 'reply':
478                        case 'newtopic' :                               
479                                $wp_sql  = "INSERT INTO $wp_comments ";
480                                $wp_sql .=    "(comment_post_ID , comment_author , comment_author_email , comment_author_url , comment_author_IP , ";
481                                $wp_sql .=    "comment_date , comment_content , comment_approved , user_id , comment_date_gmt, comment_parent) ";
482                                $wp_sql .=  "VALUES ";
483                                $wp_sql .=    "($comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, ";
484                                $wp_sql .=    "$comment_date, $comment_content, $comment_approved, $user_ID, $comment_date_gmt, $comment_parent)";
485
486                                $wp_res =       $xoops_db->query($wp_sql);
487                                if ($wp_res === false) die( '...Err. INSERT' . $wp_comments . '(' . $wp_sql . ')');
488                                $comment_ID = $xoops_db->insert_id;
489                                $wp_sql  = "UPDATE $wp_posts SET  comment_count = comment_count +1 WHERE ID = $comment_post_ID";
490                                $xoops_db->query($wp_sql);
491                                $wp_sql  = "INSERT INTO $wp_d3forum_link ";
492                                $wp_sql .=    "(comment_ID , post_id, wp_post_ID, forum_id, blog_id) ";
493                                $wp_sql .=  "VALUES ";
494                                $wp_sql .=    "($comment_ID, $post_id, $link_id, $d3f_forum_id, $blog_id)";             
495                                $xoops_db->query($wp_sql);                             
496                                if ($comment_approved ==0)      do_CommentWaiting($comment_ID, $post_id);
497                                break;
498                        case 'edit':
499                                $comment_ID = "SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id";
500                                $comment_ID = $xoops_db->get_var("SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id");
501                                if (empty($comment_ID)) die('...Err. EDIT' . $wp_comments . '(' . $wp_sql . ')');
502                                $wp_sql  = "UPDATE $wp_comments SET comment_content = $comment_content , comment_date_gmt = $comment_date_gmt WHERE comment_ID = $comment_ID";
503                                $wp_res = $xoops_db->query($wp_sql);
504                                if (empty($wp_res)) die( '...Err. UPDATE' . $wp_comments . '(' . $wp_sql . ')');
505                                break;
506                        case 'delete':
507                                // wordpress comments delete
508                                $comment_ID = get_wp_comment_ID($post_id);
509                                if ($comment_ID > 0){
510                                        $sql= "SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID";
511                                        $comment_type= $xoops_db->get_var("SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID");
512                                        if (!empty($comment_type)) break;
513                                        $xoops_db->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");                           
514                                        $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id");                             
515                                        $xoops_db->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");                         
516                                }
517                                break;
518                        default :
519                }                               
520               
521        return true ;
522}
523
524//  The content is reflected in the D3Forum comment when there is a change in the WordPress comment.
525function wp_comment_sync_to_d3forum($comment_ID = 0,$sync_mode){
526        global $xpress_config,$xoops_db,$xoops_config,$wpdb,$blog_id;
527       
528        if (empty($blog_id)) $blog_id =1;
529       
530        if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
531        $mydirname = $xoops_config->module_name;
532
533        $d3f_forum_id = $xpress_config->d3forum_forum_id;
534        $d3f_forum_dir  = $xpress_config->d3forum_module_dir;
535       
536        $d3forum_prefix = get_xoops_prefix() . $d3f_forum_dir . '_';
537        $xpress_prefix = get_wp_prefix();
538        $wp_comments =  $wpdb->comments;
539//      $wp_comments = $xpress_prefix . 'comments';
540        $wp_posts = $wpdb->posts;
541//      $wp_posts = $xpress_prefix . 'posts';
542        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
543        $d3f_topic = $d3forum_prefix . 'topics';
544        $d3f_posts = $d3forum_prefix . 'posts';
545
546        $sql  = "SELECT $wp_comments.comment_ID,$wp_comments.comment_post_ID, ";
547        $sql .=         "$wp_comments.comment_author, $wp_comments.comment_author_email,  $wp_comments.comment_date, $wp_comments.comment_date_gmt, ";
548        $sql .=         "$wp_comments.comment_author_url, $wp_comments.comment_author_IP, ";
549        $sql .=         "$wp_comments.comment_content, $wp_comments.comment_karma, ";
550        $sql .=         "$wp_comments.comment_approved, $wp_comments.comment_agent, ";
551        $sql .=         "$wp_comments.comment_type, $wp_comments.comment_parent, $wp_comments.user_id, ";
552        $sql .=         "$wp_posts.post_title ,$wp_posts.comment_count ";
553        $sql .= "FROM $wp_comments INNER JOIN  $wp_posts ON $wp_comments.comment_post_ID = $wp_posts.ID ";
554        $sql .= "WHERE (comment_ID = $comment_ID) AND ($wp_comments.comment_approved NOT LIKE 'spam') ";
555
556//      $row = $xoops_db->get_row($sql) ;
557        $row = $wpdb->get_row($sql) ;
558        if(empty($row)) die( 'READ ' . $wp_comments . '_NG...' .$sql);
559        if (! empty($row->comment_type)) return;
560       
561        $forum_id = $d3f_forum_id;
562        $d3forum_dirname =$d3f_forum_dir;
563        $topic_external_link_id = $row->comment_post_ID; //There is information on WP post_ID in topic_external_link_id of D3Forum
564        $topic_title = 'Re.' . addSlashes($row->post_title);
565        $post_time = strtotime($row->comment_date_gmt);
566        $modified_time = strtotime($row->comment_date_gmt);
567        require_once (get_xpress_dir_path() . 'include/general_functions.php');
568        if (empty($row->user_id)){
569                $uid = wp_comment_author_to_xoops_uid($row->comment_author,$row->comment_author_email);
570        } else {
571                $uid = wp_uid_to_xoops_uid($row->user_id,$mydirname);
572        }
573        $poster_ip = "'" . addslashes($row->comment_author_IP ). "'";
574        $modifier_ip = "'" . addslashes($row->comment_author_IP) . "'";
575        $subject = "'" . $topic_title . "'";
576    $post_text = "'" . addSlashes($row->comment_content) . "'";
577        $guest_name = "'" . addSlashes($row->comment_author) . "'";
578    $guest_email = "'" . $row->comment_author_email . "'";
579    $guest_url = "'" . $row->comment_author_url . "'";
580    $approval = $row->comment_approved;
581    $comment_count = $row->comment_count;
582    $comment_parent = $row->comment_parent;
583   
584        if ($sync_mode == 'delete'){
585                $mode = 'delete';
586                $delete_post_id = $xoops_db->get_var("SELECT post_id FROM $wp_d3forum_link WHERE comment_ID = $comment_ID AND  blog_id = $blog_id");
587                if (empty($delete_post_id)) return;
588                $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");
589                if (empty($topic_id)) return;
590        }else{
591                // Does the first comment (= topic) on the post exist?
592                $sql  = "SELECT * FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id";
593                $row = $xoops_db->get_row($sql) ;
594                $topic_first_post_id = $row->topic_first_post_id;
595                if (empty($row)){
596                        $mode = $mode = 'newtopic';
597                } else {
598                        $topic_id = $row->topic_id;
599                       
600                        // if comment on same ID exists then edits comment else reply comment
601                        $row = $xoops_db->get_row("SELECT * FROM $wp_d3forum_link WHERE comment_ID = $comment_ID AND blog_id = $blog_id" ) ;
602                       
603                        if (!empty($row)){
604                                $mode = $mode = 'edit';
605                                $edit_post_id = $row->post_id;
606                        } else {
607                                $mode = $mode = 'reply';
608                                $reply_pid = 0;
609                                if ($comment_parent > 0) {
610                                        $reply_pid = get_d3forum_post_ID($comment_parent);
611                                }
612                                if ($reply_pid == 0) {
613                                        $reply_pid = $topic_first_post_id; //reply_first_comment
614                                }
615                        }
616                }
617        }
618        $modified_time = $post_time;
619       
620        // make set part of INSERT or UPDATE (refalence d3forum main/post.php)
621        $set4sql = "modified_time= $modified_time , modifier_ip= $modifier_ip " ;
622        $set4sql .= ",subject= $subject " ;
623        $set4sql .= ",post_text= $post_text " ;
624       
625        if($uid == 0) {
626                @list( $guest_name , $trip_base ) = explode( '#' , $guest_name , 2 ) ;
627                if( ! trim( @$guest_name ) ) $guest_name = get_xoops_config('anonymous_name',$d3f_forum_dir) ;
628                if( ! empty( $trip_base ) && function_exists( 'crypt' ) ) {
629                        $salt = strtr( preg_replace( '/[^\.-z]/' , '.' , substr( $trip_base . 'H.' , 1 , 2 ) ) , ':;<=>?@[\]^_`' , 'ABCDEFGabcdef' ) ;
630                        $guest_trip = substr( crypt( $trip_base , $salt ) , -10 ) ;
631                } else {
632                        $guest_trip = '' ;
633                }
634                $guest_url = preg_match( '#^https?\://#' , $guest_url ) ? $guest_url : '' ;
635                foreach( array('guest_name','guest_email','guest_url','guest_trip') as $key ) {
636                        $set4sql .= ",$key='".addslashes($$key)."'" ;
637                }
638                if( ! empty( $guest_pass ) ) {
639                        $set4sql .= ",guest_pass_md5='".md5($guest_pass.'d3forum')."'" ;
640                }
641        }
642
643        $hide_uid = get_xoops_config('allow_hideuid',$d3f_forum_dir);
644       
645        switch($mode){
646                case 'edit':
647                        $edit_post = $xoops_db->get_row("SELECT * FROM $d3f_posts WHERE post_id= $edit_post_id ");
648                        if(empty($edit_post)) die( 'READ ' . $d3forum_comments . '_NG...' .$sql);
649                        // approval
650                        if( $approval ) {
651                                $set4sql .= ',approval=1' ;
652                                $topic_invisible = 0 ;
653                                $need_notify = true ;
654                        } else {
655                                $set4sql .= ',approval=0' ;
656                                $topic_invisible = 0 ;
657                                $need_admin_notify = true ;
658                        }
659                        // hide_uid
660                        if( $hide_uid ) {
661                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
662                        } else {
663                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
664                        }
665
666                        // update post specified post_id
667                        wp_d3forum_transact_make_post_history( $d3forum_dirname , $edit_post_id ) ;
668                        $sql = "UPDATE ".$d3f_posts." SET $set4sql WHERE post_id=$edit_post_id";
669                        $xoops_db->query($sql);
670                        $xoops_db->query($sql) ;
671                        if ($edit_post_pid == 0){
672                                $sql = "UPDATE ".$d3f_topic." SET topic_invisible=$topic_invisible WHERE topic_id=$topic_id";
673                                $xoops_db->query($sql);
674                        }
675                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , ! $edit_post_pid ) ;
676                        break;
677                       
678                case 'reply' :
679                        // approval
680                        if( $approval ) {
681                                $set4sql .= ',approval=1' ;
682                                $need_notify = true ;
683                        } else {
684                                $set4sql .= ',approval=0' ;
685                                $need_admin_notify = true ;
686                        }
687
688                        // hide_uid
689                        if( $hide_uid ) {
690                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
691                        } else {
692                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
693                        }
694
695                        // create post under specified post_id
696                        $sql = "INSERT INTO ".$d3f_posts." SET $set4sql,pid=$reply_pid,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
697                        $xoops_db->query($sql) ;
698                        $post_id = $xoops_db->insert_id;
699                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
700                       
701                        $wp_sql  = "INSERT INTO $wp_d3forum_link ";
702                        $wp_sql .=    "(comment_ID , post_id, wp_post_ID, forum_id, blog_id) ";
703                        $wp_sql .=  "VALUES ";
704                        $wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id, $d3f_forum_id, $blog_id)";             
705                        $xoops_db->query($wp_sql);
706                       
707                        break;
708
709                case 'newtopic':
710                        // approval
711                        if( $approval ) {
712                                $set4sql .= ',approval=1' ;
713                                $topic_invisible = 0 ;
714                                $need_notify = true ;
715                        } else {
716                                $set4sql .= ',approval=0' ;
717                                $topic_invisible = 0 ;
718                                $need_admin_notify = true ;
719                        }
720
721                        // hide_uid
722                        if( $hide_uid ) {
723                                $set4sql .= ",uid=0,uid_hidden='$uid'" ;
724                        } else {
725                                $set4sql .= ",uid='$uid',uid_hidden=0" ;
726                        }
727
728                        // create topic and get a new topic_id
729                        $sql = "INSERT INTO ".$d3f_topic." SET forum_id=$forum_id,topic_invisible=$topic_invisible,topic_external_link_id='".addslashes($topic_external_link_id)."'";
730                        $xoops_db->query($sql) ;
731                        $topic_id = $xoops_db->insert_id;
732                        // create post in the topic
733                        $sql = "INSERT INTO ".$d3f_posts." SET $set4sql,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
734                        $xoops_db->query($sql) ;
735                        $post_id = $xoops_db->insert_id;
736                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , true ) ;
737                       
738                        $wp_sql  = "INSERT INTO $wp_d3forum_link ";
739                        $wp_sql .=    "(comment_ID , post_id , wp_post_ID, forum_id, blog_id) ";
740                        $wp_sql .=  "VALUES ";
741                        $wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id, $d3f_forum_id, $blog_id)";             
742                        $xoops_db->query($wp_sql);
743
744                        break;
745                case 'delete':
746                        wp_d3forum_delete_post_recursive( $d3forum_dirname , $delete_post_id );
747                        wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
748                        break;
749                default:                               
750        }
751       
752        // increment post
753        if( is_object( @$xoopsUser ) && $mode != 'edit' ) {
754                $xoopsUser->incrementPost() ;
755        }
756        // set u2t_marked
757        $allow_mark = get_xoops_config('allow_mark',$d3f_forum_dir);
758
759        if( $uid && $allow_mark) {
760                $u2t_marked = empty( $_POST['u2t_marked'] ) ? 0 : 1 ;
761                $sql = "UPDATE ".$d3forum_prefix."users2topics"." SET u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP() WHERE uid=$uid AND topic_id=$topic_id" ;
762                if( ! $xoops_db->query($sql)){
763                        $sql = "INSERT INTO ".$d3forum_prefix."users2topics"." SET uid=$uid,topic_id=$topic_id,u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP()" ;
764                        $xoops_db->query($sql);
765                }
766        }
767}
768
769
770// ********************** refrence by d3forum *********************************************
771// delete posts recursively
772function wp_d3forum_delete_post_recursive( $d3forum_dirname , $post_id ,$isChild = false)
773{
774        global $wpdb,$blog_id;
775        global $xpress_config,$xoops_db;
776        $post_id = intval( $post_id ) ; // post_id is d3forum post(comments) id.
777        if (empty($blog_id)) $blog_id =1;
778
779       
780        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
781        $xpress_prefix = get_wp_prefix();
782       
783        $topic_id = $xoops_db->get_var("SELECT topic_id FROM ".$d3forum_prefix."posts WHERE post_id=$post_id");
784       
785        //It deletes it if there is a child comment.
786        $sql = "SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id" ;
787        $child_comments = $xoops_db->get_results("SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id");
788        foreach($child_comments as $child_comment){
789                        wp_d3forum_delete_post_recursive( $d3forum_dirname , $child_comment->post_id ,true) ;
790        }
791        wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , true ) ;
792        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
793        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."post_votes WHERE post_id=$post_id" ) ;
794       
795        $wp_comments = $wpdb->comments;
796        $wp_posts = $wpdb->posts;
797        $wp_d3forum_link = $xpress_prefix . 'd3forum_link';
798
799        $comment_ID = get_wp_comment_ID($post_id);  // get wordpress comment ID
800        if ($comment_ID > 0){
801                $comment_post_ID = $wpdb->get_var("SELECT comment_post_ID FROM $wp_comments WHERE comment_ID = $comment_ID");
802                if ($isChild){          //The first comment is deleted on the WordPress side.
803                        $wpdb->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");
804                        if (!empty($comment_post_ID)){
805                                $wpdb->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");
806                        }
807                }       
808                $xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id AND blog_id = $blog_id");
809        }
810}
811
812
813// delete a topic
814function wp_d3forum_delete_topic( $d3forum_dirname , $topic_id , $delete_also_posts = true )
815{
816        global $xpress_config,$xoops_db;
817        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
818
819        $topic_id = intval( $topic_id ) ;
820
821        // delete posts
822        if( $delete_also_posts ) {
823                $posts = $xoops_db->query("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id");
824                if( !empty($posts)) {
825                        foreach($posts as $post){
826                                wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
827                        }
828                }
829        }
830
831        // delete notifications about this topic
832
833        // delete topic
834        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" );
835        // delete u2t
836        $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" );
837}
838
839
840// store redundant informations to a topic from its posts
841// and rebuild tree informations (depth, order_in_tree)
842function wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , $sync_also_forum = true , $sync_topic_title = false )
843{
844        global $xpress_config,$xoops_db;
845        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
846        $xpress_prefix = get_wp_prefix();
847
848        $topic_id = intval( $topic_id ) ;
849
850        $forum_id = $xoops_db->get_var("SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id");
851
852        // get first_post_id
853        $first_post_id = $xoops_db->get_var("SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0");
854
855        // get last_post_id and total_posts
856        $sql = "SELECT MAX(post_id) as last_post_id,COUNT(post_id) as total_posts FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
857        $row = $xoops_db->get_row($sql);
858        $last_post_id = $row->last_post_id;
859        $total_posts = $row->total_posts;
860
861        if( empty( $total_posts ) ) {
862                // this is empty topic should be removed
863                wp_d3forum_delete_topic( $d3forum_dirname , $topic_id ,0) ;
864
865        } else {
866
867                // update redundant columns in topics table
868                $row = $xoops_db->get_row( "SELECT post_time,uid,subject,unique_path FROM ".$d3forum_prefix."posts WHERE post_id=$first_post_id" ) ;
869                $first_post_time = $row->post_time;
870                $first_uid = $row->uid;
871                $first_subject = $row->subject;
872                $unique_path = $row->unique_path;
873                $row = $xoops_db->get_row( "SELECT post_time,uid FROM ".$d3forum_prefix."posts WHERE post_id=$last_post_id" ) ;
874                $last_post_time = $row->post_time;
875                $last_uid = $row->uid;
876                // sync topic_title same as first post's subject if specified
877                $topictitle4set = $sync_topic_title ? "topic_title='".addslashes($first_subject)."'," : "" ;
878
879                $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" );
880
881                // rebuild tree informations
882                $tree_array = wp_d3forum_maketree_recursive( $d3forum_prefix."posts" , intval( $first_post_id ) , 'post_id' , array() , 0 , empty( $unique_path ) ? '.1' : $unique_path ) ;
883                if( ! empty( $tree_array ) ) {
884                        foreach( $tree_array as $key => $val ) {
885                                $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'] ) ;
886                        }
887                }
888        }
889
890        if( $sync_also_forum )
891                return wp_d3forum_sync_forum( $d3forum_dirname , $forum_id ) ;
892        else
893                return true ;
894}
895
896function wp_d3forum_maketree_recursive( $tablename , $post_id , $order = 'post_id' , $parray = array() , $depth = 0 , $unique_path = '.1' )
897{
898        global $xpress_config,$xoops_db;
899
900        $parray[] = array( 'post_id' => $post_id , 'depth' => $depth , 'unique_path' => $unique_path ) ;
901
902        $sql = "SELECT post_id,unique_path FROM $tablename WHERE pid=$post_id ORDER BY $order" ;
903        if( ! $result = $xoops_db->get_results( $sql )) {
904                return $parray ;
905        }
906        $new_post_ids = array() ;
907        $max_count_of_last_level = 0 ;
908        foreach($result as $row){
909                $new_post_id = $row->post_id;
910                $new_unique_path = $row->unique_path;
911                $new_post_ids[ intval( $new_post_id ) ] = $new_unique_path ;
912                if( ! empty( $new_unique_path ) ) {
913                        $count_of_last_level = intval( substr( strrchr( $new_unique_path , '.' ) , 1 ) ) ;
914                        if( $max_count_of_last_level < $count_of_last_level ) {
915                                $max_count_of_last_level = $count_of_last_level ;
916                        }
917                }
918        }
919        foreach( $new_post_ids as $new_post_id => $new_unique_path ) {
920                if( empty( $new_unique_path ) ) {
921                        $new_unique_path = $unique_path . '.' . ++ $max_count_of_last_level ;
922                }
923                $parray = wp_d3forum_maketree_recursive( $tablename , $new_post_id , $order , $parray , $depth + 1 , $new_unique_path ) ;
924        }
925        return $parray ;
926}
927
928function wp_d3forum_makecattree_recursive( $tablename , $cat_id , $order = 'cat_weight' , $parray = array() , $depth = 0 , $cat_title = '' )
929{
930        global $xoops_db;
931
932        $parray[] = array( 'cat_id' => $cat_id , 'depth' => $depth , 'cat_title' => $cat_title ) ;
933
934        $sql = "SELECT cat_id,cat_title FROM $tablename WHERE pid=$cat_id ORDER BY $order" ;
935        $results = $xoops_db->get_results( $sql ) ;
936        if( empty($results) ) {
937                return $parray ;
938        }
939        foreach($result as $row){
940                $new_cat_id = $row->cat_id;
941                $new_cat_title = $row->cat_title;
942                $parray = wp_d3forum_makecattree_recursive( $tablename , $new_cat_id , $order , $parray , $depth + 1 , $new_cat_title ) ;
943        }
944        return $parray ;
945}
946
947
948// store redundant informations to a forum from its topics
949function wp_d3forum_sync_forum( $d3forum_dirname , $forum_id , $sync_also_category = true )
950{
951        global $xoops_db;
952        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
953
954        $forum_id = intval( $forum_id ) ;
955
956        $sql = "SELECT cat_id FROM ".$d3forum_prefix."forums WHERE forum_id=$forum_id" ;
957        if( ! $cat_id = $xoops_db->get_var( $sql ) ) die( "ERROR SELECT forum in sync forum" ) ;
958
959        $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" ;
960        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT topics in sync forum" ) ;
961        $last_post_id = $row->last_post_id;
962        $last_post_time = $row->last_post_time;
963        $topics_count = $row->topics_count;
964        $posts_count = $row->posts_count;
965
966        $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" ) ;
967
968        if( $sync_also_category ) return wp_d3forum_sync_category( $d3forum_dirname , $cat_id ) ;
969        else return true ;
970}
971
972function get_d3forum_all_child_catid($d3forum_prefix,$sel_id, $order="", $idarray = array())
973{
974        global $xoops_db;
975        $sql = "SELECT * FROM ".$d3forum_prefix."categories WHERE pid =".$sel_id."";
976        if ( $order != "" ) {
977                $sql .= " ORDER BY $order";
978        }
979        $categories =$xoops_db->get_results($sql);
980        if ( empty($cat_ids)) {
981                return $idarray;
982        }
983        foreach( categories as $categorie ) {
984                $r_id = $categorie->cat_id;
985                array_push($idarray, $r_id);
986                $idarray = get_d3forum_all_child_catid($d3forum_prefix, $r_id,$order,$idarray);
987        }
988        return $idarray;
989}
990
991// store redundant informations to a category from its forums
992function wp_d3forum_sync_category( $d3forum_dirname , $cat_id )
993{
994        global $xoops_db;
995        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
996
997        $cat_id = intval( $cat_id ) ;
998
999        // get children
1000        $children = get_d3forum_all_child_catid( $d3forum_prefix."categories" , $cat_id ) ;
1001        $children[] = $cat_id ;
1002        $children = array_map( 'intval' , $children ) ;
1003
1004        // topics/posts information belonging this category directly
1005        $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" ;
1006        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
1007        $last_post_id = $row->last_post_id ;
1008        $last_post_time = $row->last_post_time ;
1009        $topics_count = $row->topics_count ;
1010        $posts_count = $row->posts_count ;
1011
1012        // topics/posts information belonging this category and/or subcategories
1013        $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).")" ;
1014        if( ! $row = $xoops_db->get_row( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
1015        $last_post_id_in_tree = $row->last_post_id_in_tree ;
1016        $last_post_time_in_tree = $row->last_post_time_in_tree ;
1017        $topics_count_in_tree = $row->topics_count_in_tree ;
1018        $posts_count_in_tree = $row->posts_count_in_tree ;
1019
1020        // update query
1021        $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" );
1022
1023        // do sync parents
1024        $pid = $xoops_db->get_var( "SELECT pid FROM ".$d3forum_prefix."categories WHERE cat_id=$cat_id" )  ;
1025        if( $pid != $cat_id && $pid > 0 ) {
1026                wp_d3forum_sync_category( $d3forum_dirname , $pid ) ;
1027        }
1028
1029        return true ;
1030}
1031
1032// make a new history entry for a post
1033function wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , $full_backup = false )
1034{
1035        global $xoops_db;
1036        $d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
1037
1038        $post_id = intval( $post_id ) ;
1039
1040        $results = $xoops_db->get_results( "SELECT * FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
1041        if(empty($results)) return ;
1042        $post_row = $results ;
1043        $data = array() ;
1044        $indexes = $full_backup ? array_keys( $post_row ) : array( 'subject' , 'post_text' ) ;
1045        foreach( $indexes as $index ) {
1046                $data[ $index ] = $post_row[ $index ] ;
1047        }
1048
1049        // check the latest data in history
1050        $old_data_serialized = $xoops_db->get_var( "SELECT data FROM ".$d3forum_prefix."post_histories WHERE post_id=$post_id ORDER BY history_time DESC" ) ;
1051        if( !empty( $old_data_serialized ) ) {
1052                $old_data = unserialize( $old_data_serialized ) ;
1053                if( $old_data == $data ) return ;
1054        }
1055
1056        $xoops_db->query( $xoops_db->prepare( "INSERT INTO ".$d3forum_prefix."post_histories SET post_id=$post_id, history_time=UNIX_TIMESTAMP(), data=%s", serialize( $data ) ) ) ;
1057}
1058
1059?>
Note: See TracBrowser for help on using the repository browser.