PageRenderTime 80ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bxpress/trunk/moderate.php

http://bitcero-modules.googlecode.com/
PHP | 519 lines | 359 code | 116 blank | 44 comment | 62 complexity | 7dc82a740fd3cb2414c3ba182f753e77 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: moderate.php 857 2011-12-14 10:52:30Z mambax7@gmail.com $
  3. // --------------------------------------------------------------
  4. // bXpress Forums
  5. // An simple forums module for XOOPS and Common Utilities
  6. // Author: Eduardo Cortés <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. include '../../mainfile.php';
  11. $id = rmc_server_var($_REQUEST, 'id', 0);
  12. if ($id<=0){
  13. redirect_header('./', 2, __('Please, specify the forum you want to moderate!','bxpress'));
  14. die();
  15. }
  16. $forum = new bXForum($id);
  17. if ($forum->isNew()){
  18. redirect_header('./', 2, __('Specified forum doesn\'t exists!','bxpress'));
  19. die();
  20. }
  21. // Comprobamos los permisos de moderador
  22. if (!$xoopsUser || (!$forum->isModerator($xoopsUser->uid()) && !$xoopsUser->isAdmin())){
  23. redirect_header('forum.php?id='.$id, 2, __('Sorry, you don\'t have permission to do this action!','bxpress'));
  24. die();
  25. }
  26. /**
  27. * @desc Muestra todas las opciones configurables
  28. */
  29. function showItemsAndOptions(){
  30. global $xoopsUser, $db, $xoopsOption, $tpl, $xoopsModule, $xoopsConfig, $xoopsSecurity;
  31. global $xoopsModuleConfig, $forum;
  32. $xoopsOption['template_main'] = "bxpress_moderate.html";
  33. $xoopsOption['module_subpage'] = "moderate";
  34. include 'header.php';
  35. /**
  36. * Cargamos los temas
  37. */
  38. $tbl1 = $db->prefix("bxpress_topics");
  39. $tbl2 = $db->prefix("bxpress_forumtopics");
  40. $sql = "SELECT COUNT(*) FROM $tbl1 WHERE id_forum='".$forum->id()."' ";
  41. list($num)=$db->fetchRow($db->queryF($sql));
  42. $page = isset($_REQUEST['pag']) ? $_REQUEST['pag'] : '';
  43. $limit = $xoopsModuleConfig['topicperpage'] > 0 ? $xoopsModuleConfig['topicperpage'] : 15;
  44. if ($page > 0){ $page -= 1; }
  45. $start = $page * $limit;
  46. $tpages = (int)($num / $limit);
  47. if($num % $limit > 0) $tpages++;
  48. $pactual = $page + 1;
  49. if ($pactual>$tpages){
  50. $rest = $pactual - $tpages;
  51. $pactual = $pactual - $rest + 1;
  52. $start = ($pactual - 1) * $limit;
  53. }
  54. if ($tpages > 0) {
  55. $nav = new RMPageNav($num, $limit, $pactual);
  56. $nav->target_url('moderate.php?id='.$forum->id().'&amp;pag={PAGE_NUM}');
  57. $tpl->assign('itemsNavPage', $nav->render(false));
  58. }
  59. $sql = str_replace("COUNT(*)", '*', $sql);
  60. $sql .= " ORDER BY sticky DESC, date DESC LIMIT $start,$limit";
  61. $result = $db->query($sql);
  62. while ($row = $db->fetchArray($result)){
  63. $topic = new bXTopic();
  64. $topic->assignVars($row);
  65. $last = new bXPost($topic->lastPost());
  66. $lastpost = array();
  67. if (!$last->isNew()){
  68. $lastpost['date'] = bXFunctions::formatDate($last->date());
  69. $lastpost['by'] = sprintf(__('By: %s','bxpress'), $last->uname());
  70. $lastpost['id'] = $last->id();
  71. if ($xoopsUser){
  72. $lastpost['new'] = $last->date()>$xoopsUser->getVar('last_login') && (time()-$last->date()) < $xoopsModuleConfig['time_new'];
  73. } else {
  74. $lastpost['new'] = (time()-$last->date())<=$xoopsModuleConfig['time_new'];
  75. }
  76. }
  77. $tpages = ceil($topic->replies()/$xoopsModuleConfig['perpage']);
  78. if ($tpages>1){
  79. $pages = bXFunctions::paginateIndex($tpages);
  80. } else {
  81. $pages = null;
  82. }
  83. $tpl->append('topics', array('id'=>$topic->id(), 'title'=>$topic->title(),'replies'=>$topic->replies(),
  84. 'views'=>$topic->views(),'by'=>sprintf(__('By: %s','bxpress'), $topic->posterName()),
  85. 'last'=>$lastpost,'popular'=>($topic->replies()>=$forum->hotThreshold()),
  86. 'sticky'=>$topic->sticky(),'pages'=>$pages, 'tpages'=>$tpages,
  87. 'approved'=>$topic->approved(),'closed'=>$topic->status()));
  88. }
  89. $tpl->assign('forum', array('id'=>$forum->id(), 'title'=>$forum->name()));
  90. $tpl->assign('lang_topic', __('Topic','bxpress'));
  91. $tpl->assign('lang_replies', __('Replies','bxpress'));
  92. $tpl->assign('lang_views', __('Views','bxpress'));
  93. $tpl->assign('lang_lastpost', __('Last Post','bxpress'));
  94. $tpl->assign('lang_sticky', __('Sticky','bxpress'));
  95. $tpl->assign('lang_moderating', __('Moderating Forum','bxpress'));
  96. $tpl->assign('lang_pages', __('Pages','bxpress'));
  97. $tpl->assign('lang_move', __('Move','bxpress'));
  98. $tpl->assign('lang_open', __('Unlock','bxpress'));
  99. $tpl->assign('lang_close', __('Lock','bxpress'));
  100. $tpl->assign('lang_dosticky', __('Sticky','bxpress'));
  101. $tpl->assign('lang_dounsticky', __('Unsticky','bxpress'));
  102. $tpl->assign('lang_approved',__('Approved','bxpress'));
  103. $tpl->assign('lang_app',__('Approve','bxpress'));
  104. $tpl->assign('lang_noapp',__('Unapprove','bxpress'));
  105. $tpl->assign('lang_delete', __('Delete','bxpress'));
  106. $tpl->assign('lang_confirm', __('Do you really want to delete selected topics?','bxpress'));
  107. $tpl->assign('token_input',$xoopsSecurity->getTokenHTML());
  108. bXFunctions::makeHeader();
  109. RMTemplate::get()->add_xoops_style('style.css', 'bxpress');
  110. include 'footer.php';
  111. }
  112. /**
  113. * @desc Mover temas de un foro a otro
  114. */
  115. function moveTopics(){
  116. global $db, $xoopsModuleConfig, $xoopsSecurity, $forum, $xoopsUser, $xoopsOption, $xoopsConfig;
  117. $topics = isset($_REQUEST['topics']) ? $_REQUEST['topics'] : null;
  118. $ok = isset($_POST['ok']) ? $_POST['ok'] : 0;
  119. $moveforum = rmc_server_var($_POST, 'moveforum', 0);
  120. if (empty($topics) || (is_array($topics) && empty($topics))){
  121. redirect_header('moderate.php?id='.$moveforum, 2, __('Select at least a topic to moderate!','bxpress'));
  122. die();
  123. }
  124. $topics = !is_array($topics) ? array($topics) : $topics;
  125. if ($ok){
  126. if (!$xoopsSecurity->check()){
  127. redirect_header('moderate.php?id='.$moveforum, 2, __('Session token expired!','bxpress'));
  128. die();
  129. }
  130. if ($moveforum<=0){
  131. redirect_header('moderate.php?id='.$forum->id(), 2, __('Please select the target forum','bxpress'));
  132. die();
  133. }
  134. $mf = new bXForum($moveforum);
  135. if ($mf->isNew()){
  136. redirect_header('moderate.php?id='.$forum->id(), 2, __('Specified forum does not exists!','bxpress'));
  137. die();
  138. }
  139. $lastpost = false;
  140. foreach ($topics as $k){
  141. $topic = new bXTopic($k);
  142. if ($topic->forum()!=$forum->id()) continue;
  143. //Verificamos si el tema contiene el último mensaje del foro
  144. if(!$lastpost && array_key_exists($forum->lastPostId(),$topic->getPosts(0))){
  145. $lastpost = true;
  146. }
  147. $topic->setForum($moveforum);
  148. if ($topic->save()){
  149. //Decrementa el número de temas
  150. $forum->setTopics(($forum->topics()-1>0) ? $forum->topics()-1 : 0);
  151. $forum->setPosts(($forum->posts()-($topic->replies() + 1)>0) ? $forum->posts()-($topic->replies()+1) : 0);
  152. $forum->save();
  153. $mf->setPosts($mf->posts()+($topic->replies() + 1));
  154. $mf->addTopic();
  155. $mf->save();
  156. //Cambiamos el foro de los mensajes del tema
  157. if ($topic->getPosts()){
  158. foreach ($topic->getPosts() as $k=>$v){
  159. $v->setForum($moveforum);
  160. $v->save();
  161. }
  162. }
  163. }
  164. }
  165. //Actualizamos el último mensaje del foro
  166. if($lastpost){
  167. $post = $forum->getLastPost();
  168. $forum->setPostId($post);
  169. $forum->save();
  170. }
  171. //Actualizamos el último mensaje del foro al que fue movido el tema
  172. $post = $mf->getLastPost();
  173. $post ? $mf->setPostId($post) : '';
  174. $mf->save();
  175. redirect_header('moderate.php?id='.$forum->id(), 1, __('Topics has been relocated!','bxpress'));
  176. die();
  177. } else {
  178. global $xoopsTpl;
  179. $tpl = $xoopsTpl;
  180. $xoopsOption['template_main'] = "bxpress_moderateforms.html";
  181. $xoopsOption['module_subpage'] = "moderate";
  182. include 'header.php';
  183. bXFunctions::makeHeader();
  184. $form = new RMForm(__('Move Topics','bxpress'), 'frmMove', 'moderate.php');
  185. $form->addElement(new RMFormHidden('id', $forum->id()));
  186. $form->addElement(new RMFormHidden('op','move'));
  187. $form->addElement(new RMFormHidden('ok','1'));
  188. $i = 0;
  189. foreach ($topics as $k){
  190. $form->addElement(new RMFormHidden('topics['.$i.']',$k));
  191. ++$i;
  192. }
  193. $form->addElement(new RMFormSubTitle('&nbsp',1,''));
  194. $form->addElement(new RMFormSubTitle(__('Select the forum where you wish to move selected topics','bxpress'),1,'even'));
  195. $ele = new RMFormSelect(__('Forum','bxpress'), 'moveforum');
  196. $ele->addOption(0,'',1);
  197. $tbl1 = $db->prefix("bxpress_categories");
  198. $tbl2 = $db->prefix("bxpress_forums");
  199. $sql = "SELECT b.*, a.title FROM $tbl1 a, $tbl2 b WHERE b.cat=a.id_cat AND b.active='1' AND id_forum<>".$forum->id()." ORDER BY a.order, b.order";
  200. $result = $db->query($sql);
  201. $categories = array();
  202. while ($row = $db->fetchArray($result)){
  203. $cforum = array('id'=>$row['id_forum'], 'name'=>$row['name']);
  204. if (isset($categores[$row['cat']])){
  205. $categories[$row['cat']]['forums'][] = $cforum;
  206. } else {
  207. $categories[$row['cat']]['title'] = $row['title'];
  208. $categories[$row['cat']]['forums'][] = $cforum;
  209. }
  210. }
  211. foreach ($categories as $cat){
  212. $ele->addOption(0, $cat['title'], 0, true, 'color: #000; font-weight: bold; font-style: italic; border-bottom: 1px solid #c8c8c8;');
  213. foreach ($cat['forums'] as $cforum){
  214. $ele->addOption($cforum['id'], $cforum['name'],0,false,'padding-left: 10px;');
  215. }
  216. }
  217. $form->addElement($ele, true, "noselect:0");
  218. $ele = new RMFormButtonGroup();
  219. $ele->addButton('sbt',__('Move Topics Now!','bxpress'),'submit');
  220. $ele->addButton('cancel', __('Cancel','bxpress'), 'button', 'onclick="history.go(-1);"');
  221. $form->addElement($ele);
  222. $tpl->assign('moderate_form', $form->render());
  223. include 'footer.php';
  224. }
  225. }
  226. /**
  227. * @desc Cerrar o abrir un tema
  228. */
  229. function closeTopic($close){
  230. global $xoopsSecurity, $forum, $xoopsUser;
  231. $topics = isset($_REQUEST['topics']) ? $_REQUEST['topics'] : null;
  232. if (empty($topics) || (is_array($topics) && empty($topics))){
  233. redirect_header('moderate.php?id='.$forum->id(), 2, __('Select at least one topic to moderate!','bxpress'));
  234. die();
  235. }
  236. $topics = !is_array($topics) ? array($topics) : $topics;
  237. foreach ($topics as $k){
  238. $topic = new bXTopic($k);
  239. if ($topic->isNew()) continue;
  240. $topic->setStatus($close);
  241. $topic->save();
  242. }
  243. redirect_header('moderate.php?id='.$forum->id(), 1, __('Action completed!','bxpress'));
  244. }
  245. /**
  246. * @desc Cerrar o abrir un tema
  247. */
  248. function stickyTopic($sticky){
  249. global $forum, $xoopsSecurity;
  250. if (!$xoopsSecurity->check()){
  251. redirect_header('moderate.php?id='.$forum->id(), 2, __('Session token expired!','bxpress'));
  252. die();
  253. }
  254. $topics = isset($_REQUEST['topics']) ? $_REQUEST['topics'] : null;
  255. if (empty($topics) || (is_array($topics) && empty($topics))){
  256. redirect_header('moderate.php?id='.$forum->id(), 2, __('Select at least one topic to set as sticky!','bxpress'));
  257. die();
  258. }
  259. $topics = !is_array($topics) ? array($topics) : $topics;
  260. foreach ($topics as $k){
  261. $topic = new bXTopic($k);
  262. if ($topic->isNew()) continue;
  263. $topic->setSticky($sticky);
  264. $topic->save();
  265. }
  266. redirect_header('moderate.php?id='.$forum->id(), 1, __('Action completed!','bxpress'));
  267. }
  268. /**
  269. * @desc Eliminar temas
  270. */
  271. function deleteTopics(){
  272. global $db, $xoopsModuleConfig, $bxpress, $forum, $xoopsUser, $xoopsSecurity;
  273. $ok = isset($_POST['ok']) ? $_POST['ok'] : 0;
  274. $topics = isset($_REQUEST['topics']) ? $_REQUEST['topics'] : null;
  275. if (empty($topics) || (is_array($topics) && empty($topics))){
  276. redirect_header('moderate.php?id='.$forum->id(), 2, __('Select at least one topic to delete!','bxpress'));
  277. die();
  278. }
  279. $topics = !is_array($topics) ? array($topics) : $topics;
  280. $lastpost = false;
  281. if (!$xoopsSecurity->check()){
  282. redirect_header('moderate.php?id='.$forum->id(), 2, __('Session token expired!','bxpress'));
  283. die();
  284. }
  285. foreach ($topics as $k){
  286. $topic = new bXTopic($k);
  287. if ($topic->isNew()) continue;
  288. if ($topic->forum()!=$forum->id()) continue;
  289. //Verificamos si el tema contiene el último mensaje del foro
  290. if(!$lastpost && array_key_exists($forum->lastPostId(),$topic->getPosts(0))){
  291. $lastpost = true;
  292. }
  293. $topic->delete();
  294. }
  295. //Actualizamos el último mensaje del foro
  296. if($lastpost){
  297. $forum = new bXForum($forum->id());
  298. $post = $forum->getLastPost();
  299. $forum->setPostId($post);
  300. $forum->save();
  301. }
  302. redirect_header('moderate.php?id='.$forum->id(), 1, __('Action completed!','bxpress'));
  303. }
  304. /**
  305. * @desc Aprueba o no un tema
  306. **/
  307. function approvedTopics($app=0){
  308. global $forum, $xoopsSecurity;
  309. $topics = isset($_REQUEST['topics']) ? $_REQUEST['topics'] : null;
  310. if (empty($topics) || (is_array($topics) && empty($topics))){
  311. redirect_header('moderate.php?id='.$forum->id(), 2, __('Select at least one topic to moderate','bxpress'));
  312. die();
  313. }
  314. $topics = !is_array($topics) ? array($topics) : $topics;
  315. if (!$xoopsSecurity->check()){
  316. redirect_header('moderate.php?id='.$forum->id(), 2, __('Session token expired!','bxpress'));
  317. die();
  318. }
  319. $lastpost = false;
  320. foreach ($topics as $k){
  321. $topic = new bXTopic($k);
  322. if ($topic->isNew()) continue;
  323. $lastapp = $topic->approved();
  324. $topic->setApproved($app);
  325. $topic->save();
  326. }
  327. //Actualizamos el último mensaje del foro
  328. $post = $forum->getLastPost();
  329. $forum->setPostId($post);
  330. $forum->save();
  331. redirect_header('moderate.php?id='.$forum->id(), 1, __('Action completed!','bxpress'));
  332. }
  333. /**
  334. * @desc Aprueba o no un mensaje editado
  335. **/
  336. function approvedPosts($app=0){
  337. global $xoopsUser, $xoopsSecurity;
  338. $posts=isset($_REQUEST['posts']) ? intval($_REQUEST['posts']) : 0;
  339. //Verifica que el mensaje sea válido
  340. if ($posts<=0){
  341. redirect_header('./topic.php?id='.$posts,1,__('Topic not valid!','bxpress'));
  342. die();
  343. }
  344. //Comprueba que el mensaje exista
  345. $post=new bXPost($posts);
  346. if ($post->isNew()){
  347. redirect_header('./topic.php?id='.$posts,1,__('Post doesn\'t exists!','bxpress'));
  348. die();
  349. }
  350. //Comprueba si usuario es moderador del foro
  351. $forum=new bXForum($post->forum());
  352. if (!$forum->isModerator($xoopsUser->uid()) || !$xoopsUser->isAdmin()){
  353. redirect_header('./topic.php?id='.$posts,1,__('You don\'t have permission to do this action!','bxpress'));
  354. die();
  355. }
  356. if (!$xoopsSecurity->check()){
  357. redirect_header('./topic.php?id='.$posts, 2, __('Session token expired!','bxpress'));
  358. die();
  359. }
  360. $post->setApproved($app);
  361. if ($post->editText()){
  362. $post->setText($post->editText());
  363. }
  364. $post->setEditText('');
  365. $post->save();
  366. redirect_header('./topic.php?id='.$post->topic(), 1, __('Operation completed!','bxpress'));
  367. }
  368. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  369. switch($op){
  370. case 'move':
  371. moveTopics();
  372. break;
  373. case 'close':
  374. closeTopic(1);
  375. break;
  376. case 'open':
  377. closeTopic(0);
  378. break;
  379. case 'sticky':
  380. stickyTopic(1);
  381. break;
  382. case 'unsticky':
  383. stickyTopic(0);
  384. break;
  385. case 'delete':
  386. deleteTopics();
  387. break;
  388. case 'approved':
  389. approvedTopics(1);
  390. break;
  391. case 'noapproved':
  392. approvedTopics();
  393. break;
  394. case 'approvedpost':
  395. approvedPosts(1);
  396. break;
  397. case 'noapprovedpost':
  398. approvedPosts();
  399. break;
  400. default:
  401. showItemsAndOptions();
  402. break;
  403. }