XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
comments.php
Go to the documentation of this file.
1 <?php
2 // $Id: comments.php 949 2012-04-14 04:18:10Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 include_once '../../include/cp_header.php';
12 define('RMCLOCATION','comments');
13 
14 function show_comments(){
15  global $xoopsSecurity;
16 
17  $db = XoopsDatabaseFactory::getDatabaseConnection();
18 
19  $keyw = rmc_server_var($_REQUEST, 'w', '');
20  $filter = rmc_server_var($_REQUEST, 'filter', '');
21 
22  $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_comments");
23  $sql .= $keyw!='' || $filter!='' ? ' WHERE ' : '';
24  $sql .= $keyw!='' ? "(content LIKE '%$keyw%' OR ip LIKE '%$keyw%')" : '';
25  $sql .= $filter!='' ? ($keyw!=''?' AND':'')." status='$filter'" : '';
29  $page = rmc_server_var($_GET, 'page', 1);
30  $limit = 15;
31  list($num) = $db->fetchRow($db->query($sql));
32 
33  $tpages = ceil($num / $limit);
35 
36  $start = $num<=0 ? 0 : ($page - 1) * $limit;
37 
38  $nav = new RMPageNav($num, $limit, $page, 5);
39  $nav->target_url('comments.php?page={PAGE_NUM}');
40 
41  $sql = str_replace("COUNT(*)",'*', $sql);
42  $sql .= " ORDER BY posted DESC LIMIT $start,$limit";
43  $result = $db->query($sql);
44  $comments = array();
45 
46  $ucache = array();
47  $ecache = array();
48 
49  while($row = $db->fetchArray($result)){
50  $com = new RMComment();
51  $com->assignVars($row);
52 
53  // Editor data
54  if(!isset($ecache[$com->getVar('user')])){
55  $ecache[$com->getVar('user')] = new RMCommentUser($com->getVar('user'));
56  }
57 
58  $editor = $ecache[$com->getVar('user')];
59 
60  if($editor->getVar('xuid')>0){
61 
62  if(!isset($ucache[$editor->getVar('xuid')])){
63  $ucache[$editor->getVar('xuid')] = new XoopsUser($editor->getVar('xuid'));
64  }
65 
66  $user = $ucache[$editor->getVar('xuid')];
67 
68  $poster = array(
69  'id' => $user->getVar('uid'),
70  'name' => $user->getVar('uname'),
71  'email' => $user->getVar('email'),
72  'posts' => $user->getVar('posts'),
73  'avatar'=> $user->getVar('user_avatar')!='' && $user->getVar('user_avatar')!='blank.gif' ? XOOPS_UPLOAD_URL.'/'.$user->getVar('user_avatar') : RMCURL.'/images/avatar.gif',
74  'rank' => $user->rank(),
75  );
76 
77  } else {
78 
79  $poster = array(
80  'id' => 0,
81  'name' => $editor->getVar('name'),
82  'email' => $editor->getVar('email'),
83  'posts' => 0,
84  'avatar'=> RMCURL.'/images/avatar.gif',
85  'rank' => ''
86  );
87 
88  }
89 
90  // Get item
91  $cpath = XOOPS_ROOT_PATH.'/modules/'.$row['id_obj'].'/class/'.$row['id_obj'].'controller.php';
92 
93  if(is_file($cpath)){
94  if(!class_exists(ucfirst($row['id_obj']).'Controller'))
95  include_once $cpath;
96 
97  $class = ucfirst($row['id_obj']).'Controller';
98  $controller = new $class();
99  $item = $controller->get_item($row['params'], $com);
100  if(method_exists($controller, 'get_item_url'))
101  $item_url = $controller->get_item_url($row['params'], $com);
102 
103  } else {
104 
105  $item = __('Unknow','rmcommon');
106  $item_url = '';
107 
108  }
109 
110  $comments[] = array(
111  'id' => $row['id_com'],
112  'text' => TextCleaner::getInstance()->clean_disabled_tags(TextCleaner::getInstance()->popuplinks(TextCleaner::getInstance()->nofollow($com->getVar('content')))),
113  'poster' => $poster,
114  'posted' => sprintf(__('Posted on %s','rmcommon'), formatTimestamp($com->getVar('posted'), 'l')),
115  'ip' => $com->getVar('ip'),
116  'item' => $item,
117  'item_url' => $item_url,
118  'module' => $row['id_obj'],
119  'status' => $com->getVar('status')
120  );
121  }
122 
123  $comments = RMEvents::get()->run_event('rmcommon.loading.admin.comments', $comments);
124 
125  xoops_cp_header();
127  RMTemplate::get()->add_style('comms-admin.css', 'rmcommon');
128  RMTemplate::get()->add_style('general.css', 'rmcommon');
129  RMTemplate::get()->add_script('include/js/jquery.checkboxes.js');
130  RMTemplate::get()->add_script('include/js/comments.js');
131  $script = '<script type="text/javascript">delmes = "'.__('Do you really want to delete this comment?','rmcommon').'";</script>';
132  RMTemplate::get()->add_head($script);
133  include RMTemplate::get()->get_template('rmc_comments.php','module','rmcommon');
134  xoops_cp_footer();
135 
136 }
137 
142 function set_comments_status($status){
143  global $xoopsSecurity;
144 
145  if ($status!='waiting' && $status!='approved' && $status!='spam'){
146  redirectMsg('comments.php', __('Invalid operation','rmcommon'), 1);
147  die();
148  }
149 
150  $coms = rmc_server_var($_POST, 'coms', array());
151  $page = rmc_server_var($_POST, 'page', 1);
152  $filter = rmc_server_var($_POST, 'filter', '');
153  $w = rmc_server_var($_POST, 'w', '');
154 
155  $qs = "page=$page&filter=$filter&w=$w";
156 
157  if(!$xoopsSecurity->check()){
158  redirectMsg('comments.php?'.$qs, __('Sorry, session token expired!','rmcommon'), 1);
159  die();
160  }
161 
162  if(!is_array($coms)){
163  redirectMsg('comments.php?'.$qs, __('Unrecognized data!','rmcommon'), 1);
164  die();
165  }
166 
167  $db = XoopsDatabaseFactory::getDatabaseConnection();
168  $sql = "UPDATE ".$db->prefix("rmc_comments")." SET status='$status' WHERE id_com IN (".implode(",",$coms).")";
169 
170  if($db->queryF($sql)){
171 
172  RMEvents::get()->run_event('rmcommon.updated.comments',$coms, $status);
173 
174  redirectMsg('comments.php?'.$qs, __('Comments updated successfully!','rmcommon'), 0);
175  die();
176 
177  } else {
178 
179  redirectMsg('comments.php?'.$qs, __('Errors occurrs while trying to update comments!','rmcommon'), 1);
180  die();
181 
182  }
183 
184 }
185 
186 function delete_comments(){
187  global $xoopsSecurity;
188 
189  $coms = rmc_server_var($_POST, 'coms', array());
190  $page = rmc_server_var($_POST, 'page', 1);
191  $filter = rmc_server_var($_POST, 'filter', '');
192  $w = rmc_server_var($_POST, 'w', '');
193 
194  $qs = "page=$page&filter=$filter&w=$w";
195 
196  if(!$xoopsSecurity->check()){
197  redirectMsg('comments.php?'.$qs, __('Sorry, session token expired!','rmcommon'), 1);
198  die();
199  }
200 
201  if(!is_array($coms)){
202  redirectMsg('comments.php?'.$qs, __('Unrecognized data!','rmcommon'), 1);
203  die();
204  }
205 
206  // We need to delete each comment separated
207  foreach ($coms as $id){
208  $com = new RMComment($id);
209 
210  if($com->isNew()) continue;
211 
212  $cpath = XOOPS_ROOT_PATH.'/modules/'.$com->getVar('id_obj').'/class/'.$com->getVar('id_obj').'controller.php';
213 
214  if(!$com->delete()) return;
215 
216  if(is_file($cpath)){
217  if(!class_exists(ucfirst($com->getVar('id_obj')).'Controller'))
218  include_once $cpath;
219 
220  $class = ucfirst($com->getVar('id_obj')).'Controller';
221  $controller = new $class();
222  $item = $controller->reduce_comments_number($com);
223 
224  } else {
225 
226  $item = __('Unknow','rmcommon');
227 
228  }
229 
230  }
231 
232  redirectMsg('comments.php', __('Comments deleted successfully!','rmcommon'), 0);
233 
234 }
235 
236 function edit_comment(){
237 
238  $id = rmc_server_var($_GET,'id',0);
239  $page = rmc_server_var($_GET, 'page', 1);
240  $filter = rmc_server_var($_GET, 'filter', '');
241  $w = rmc_server_var($_GET, 'w', '1');
242 
243  $qs = "w=$w&page=$page&filter=$filter";
244 
245  if($id<=0){
246  redirectMsg('comments.php?'.$qs, __('Sorry, comment id is not valid','rmcommon'), 1);
247  die();
248  }
249 
250  $comment = new RMComment($id);
251  if($comment->isNew()){
252  redirectMsg('comments.php?'.$qs, __('Sorry, comment does not found','rmcommon'), 1);
253  die();
254  }
255 
256  $cpath = XOOPS_ROOT_PATH.'/modules/'.$comment->getVar('id_obj').'/class/'.$comment->getVar('id_obj').'controller.php';
257 
258  if(is_file($cpath)){
259  include $cpath;
260  $class = ucfirst($comment->getVar('id_obj')).'Controller';
261  $controller = new $class();
262  }
263 
264  $form = new RMForm(__('Edit Comment', 'rmcommon'), 'editComment', 'comments.php');
265  $form->addElement(new RMFormLabel(__('In reply to', 'rmcommon'), $controller ? $controller->get_item($comment->getVar('params'), $comment):''));
266  $form->addElement(new RMFormLabel(__('Posted date','rmcommon'), formatTimestamp($comment->getVar('posted'), 'mysql')));
267  $form->addElement(new RMFormLabel(__('Module','rmcommon'), $comment->getVar('id_obj')));
268  $form->addElement(new RMFormLabel(__('IP','rmcommon'), $comment->getVar('ip')));
269 
270  $user = new RMCommentUser($comment->getVar('user'));
271  $ele = new RMFormUser(__('Poster','rmcommon'), 'user', false, $user->getVar('xuid')>0 ? $user->getVar('xuid') : 0);
272  $form->addElement($ele);
273 
274  $ele = new RMFormRadio(__('Status','rmcommon'), 'status', 1, 0, 2);
275  $ele->addOption(__('Approved', 'rmcommon'), 'approved', $comment->getVar('status')=='approved'?1:0);
276  $ele->addOption(__('Unapproved', 'rmcommon'), 'waiting', $comment->getVar('status')=='waiting'?1:0);
277  $form->addElement($ele);
278 
279  $form->addElement(new RMFormTextArea(__('Content','rmcommon'), 'content', null, null, $comment->getVar('content','e'),'100%','150px'), true);
280  $form->addElement(new RMFormHidden('page', $page));
281  $form->addElement(new RMFormHidden('filter', $filter));
282  $form->addElement(new RMFormHidden('w', $w));
283  $form->addElement(new RMFormHidden('id', $id));
284  $form->addElement(new RMFormHidden('action', 'save'));
285  $ele = new RMFormButtonGroup();
286  $ele->addButton('sbt', __('Update Comment','rmcommon'), 'submit');
287  $ele->addButton('cancel', __('Cancel','rmcommon'), 'button', 'onclick="history.go(-1);"');
288  $form->addElement($ele);
289 
291  xoops_cp_header();
292  $form->display();
293  xoops_cp_footer();
294 
295 }
296 
297 function save_comment(){
298  global $xoopsSecurity;
299 
300  $id = rmc_server_var($_POST,'id',0);
301  $page = rmc_server_var($_POST, 'page', 1);
302  $filter = rmc_server_var($_POST, 'filter', '');
303  $w = rmc_server_var($_POST, 'w', '1');
304 
305  $qs = "id=$id&w=$w&page=$page&filter=$filter";
306 
307  if(!$xoopsSecurity->check()){
308  redirectMsg('comments.php?action=edit&'.$qs, __('Sorry, session token expired!','rmcommon'), 1);
309  die();
310  }
311 
312  if ($id<=0){
313  redirectMsg('comments.php', __('Comment ID not specified!', 'rmcommon'), 1);
314  die();
315  }
316 
317  $comment = new RMComment($id);
318  if($comment->isNew()){
319  redirectMsg('comments.php?'.$qs, __('Specified comment does not exist!', 'rmcommon'), 1);
320  die();
321  }
322 
323  $status = rmc_server_var($_POST, 'status', 'unapproved');
324  $status = $status=='approved'?$status:'unapproved';
325 
326  $user = rmc_server_var($_POST, 'user', 0);
327  $content = rmc_server_var($_POST, 'content', '');
328 
329  // save basic info in comment object
330  $comment->setVar('content', $content);
331  $comment->setVar('status', $status);
332  // Modify, if neccessary, the user
333  $cuser = new RMCommentUser($comment->getVar('user'));
334  if ($cuser->getVar('xuid')!=$user){
335 
336  if ($user==0){
337  $cuser->setVar('xuid', 0);
338  $cuser->save();
339  } else {
340  $xuser = new XoopsUser($user);
341  $cuser = new RMCommentUser($xuser->getVar('email'));
342  $cuser->setVar('name', $xuser->getVar('uname'));
343  $cuser->setVar('email', $xuser->getVar('email'));
344  $cuser->setVar('xuid', $user);
345  $cuser->setVar('url', $xuser->getVar('url'));
346  $cuser->save();
347  }
348 
349  $comment->setVar('user', $cuser->id());
350 
351  }
352 
353  if ($comment->save()){
354  redirectMsg('comments.php?'.$qs, __('Comment updated successfully!','rmcommon'), 0);
355  } else {
356  redirectMsg('comments.php?action=edit&'.$qs, __('Errros ocurrs while trying to update comment!', 1).'<br />'.$comment->errors(), 1);
357  }
358 
359 }
360 
361 
362 
363 $action = rmc_server_var($_REQUEST, 'action', '');
364 
365 switch($action){
366  case 'approve':
367  set_comments_status('approved');
368  break;
369  case 'unapprove':
370  set_comments_status('waiting');
371  break;
372  case 'spam':
373  set_comments_status('spam');
374  break;
375  case 'delete':
376  delete_comments();
377  break;
378  case 'edit':
379  edit_comment();
380  break;
381  case 'save':
382  save_comment();
383  break;
384  default:
385  show_comments();
386  break;
387 }