PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/contact/trunk/admin/index.php

http://bitcero-modules.googlecode.com/
PHP | 219 lines | 141 code | 55 blank | 23 comment | 13 complexity | de9c00a0ab053e31ae58c706e40aa559 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id$
  3. // --------------------------------------------------------------
  4. // Contact
  5. // A simple contact module for Xoops
  6. // Author: Eduardo Cort?Šs <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. define('RMCLOCATION','dashboard');
  11. require 'header.php';
  12. /**
  13. * Shows all messages sent by users and stored in database
  14. */
  15. function cm_show_messages(){
  16. global $xoopsDB, $xoopsModuleConfig, $xoopsSecurity;
  17. // Styles
  18. RMTemplate::get()->add_style('admin.css', 'contact');
  19. // Pagination
  20. $page = rmc_server_var($_GET, 'page', 1);
  21. $page = $page<=0 ? 1 : $page;
  22. $result = $xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix("contactme"));
  23. list($num) = $xoopsDB->fetchRow($result);
  24. $limit = $xoopsModuleConfig['limit'];
  25. $tpages = ceil($num / $limit);
  26. $page = $page > $tpages ? $tpages : $page;
  27. $start = $num<=0 ? 0 : ($page - 1) * $limit;
  28. $nav = new RMPageNav($num, $limit, $page, 5);
  29. $nav->target_url('index.php?page={PAGE_NUM}');
  30. // Get messages
  31. $messages = array();
  32. $result = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("contactme")." ORDER BY id_msg DESC LIMIT $start,$limit");
  33. $time = new RMTimeFormatter(0, __('%M% %d%, %Y%', 'contact'));
  34. while($row = $xoopsDB->fetchArray($result)){
  35. $msg = new CTMessage();
  36. $msg->assignVars($row);
  37. if($msg->getVar('xuid'))
  38. $user = new XoopsUser($msg->getVar('xuid'));
  39. $messages[] = array(
  40. 'id' => $msg->id(),
  41. 'subject' => $msg->getVar('subject'),
  42. 'ip' => $msg->getVar('ip'),
  43. 'email' => $msg->getVar('email'),
  44. 'name' => $msg->getVar('name'),
  45. 'company' => $msg->getVar('org'),
  46. 'body' => $msg->getVar('body'),
  47. 'phone' => $msg->getVar('phone'),
  48. 'register' => $msg->getVar('register'),
  49. 'xuid' => $msg->getVar('xuid'),
  50. 'uname' => $msg->getVar('xuid')>0 ? $user->uname() : '',
  51. 'date' => $time->format($msg->getVar('date'))
  52. );
  53. }
  54. RMTemplate::get()->add_local_script('admin.js', 'contact');
  55. RMTemplate::get()->add_local_script('jquery.checkboxes.js', 'rmcommon');
  56. xoops_cp_header();
  57. include RMTemplate::get()->get_template('admin/ct_dashboard.php', 'module', 'contact');
  58. xoops_cp_footer();
  59. }
  60. /**
  61. * Delete a set of selected messages provided by ids array
  62. */
  63. function cm_delete_messages(){
  64. global $xoopsSecurity, $xoopsDB;
  65. $ids = rmc_server_var($_POST, 'ids', array());
  66. $page = rmc_server_var($_POST, 'page', 1);
  67. if(!$xoopsSecurity->check()){
  68. redirectMsg('index.php?page='.$page, __('Session token expired!','contact'), 1);
  69. die();
  70. }
  71. if(empty($ids)){
  72. redirectMsg('index.php?page='.$page, __('Select at least one message to delete!','contact'), 1);
  73. die();
  74. }
  75. $sql = "DELETE FROM ".$xoopsDB->prefix("contactme")." WHERE id_msg IN (".implode(",", $ids).")";
  76. if($xoopsDB->queryF($sql)){
  77. redirectMsg('index.php?page='.$page, __('Messages deleted successfully!','contact'), 0);
  78. } else {
  79. redirectMsg('index.php?page='.$page, __('Messages could not be deleted!','contact')."<br />".$xoopsDB->error(), 0);
  80. }
  81. }
  82. /**
  83. * Shows the form to reply a message
  84. */
  85. function cm_reply_message(){
  86. global $xoopsModuleConfig, $xoopsSecurity;
  87. $id = rmc_server_var($_GET, 'id', 0);
  88. $page = rmc_server_var($_GET, 'page', 1);
  89. if($id<=0)
  90. redirectMsg('index.php?page='.$page, __('You must specify a message to reply!','contact'));
  91. $msg = new CTMessage($id);
  92. $subject = $msg->getVar('subject');
  93. $subject = (FALSE===strpos($subject, 'RE:')) ? 'RE: '.$subject : $subject;
  94. $name = $msg->getVar('name');
  95. $email = $msg->getVar('email');
  96. $date = formatTimestamp($msg->getVar('date'), __('M/d/Y - H:i:s','contact'));
  97. $ip = $msg->getVar('ip');
  98. if($xoopsModuleConfig['quote'])
  99. $quote = "\n\n\n--- ".__('Original text','contact')." ---\n".__('Received on:','contact')." $date\n".__('Remote address:','contact')." $ip\n\n".$msg->getVar('body', 'e');
  100. else
  101. $quote = '';
  102. include_once RMCPATH.'/class/form.class.php';
  103. $editor = new RMFormEditor('','message', '95%', '300px', $quote, 'simple');
  104. RMTemplate::get()->add_style('admin.css', 'contact');
  105. RMTemplate::get()->add_local_script('jquery.validate.min.js','rmcommon', 'include');
  106. RMTemplate::get()->add_local_script('admin.js','contact');
  107. xoops_cp_header();
  108. include RMTemplate::get()->get_template('admin/cm_reply.php', 'module', 'contact');
  109. xoops_cp_footer();
  110. }
  111. /**
  112. * Sends a reply made from reply form
  113. */
  114. function cm_send_reply(){
  115. global $xoopsSecurity, $xoopsModuleConfig, $xoopsConfig;
  116. $id = rmc_server_var($_POST, 'id', 0);
  117. $page = rmc_server_var($_POST, 'page', 1);
  118. if($id<=0){
  119. redirectMsg('index.php?page='.$page, __('You must specify a message ID','contact'), 1);
  120. die();
  121. }
  122. if(!$xoopsSecurity->check()){
  123. redirectMsg('index.php?page='.$page, __('Session token expired!','contact'), 1);
  124. die();
  125. }
  126. $msg = new CTMessage($id);
  127. if($msg->isNew()){
  128. redirectMsg('index.php?page='.$page, __('Sorry, specified message does not exists!','contact'), 1);
  129. die();
  130. }
  131. $subject = rmc_server_var($_POST, 'subject', '');
  132. $message = rmc_server_var($_POST, 'message', 1);
  133. if($subject=='' || $message==''){
  134. redirectMsg('index.php?action=reply&id='.$id.'&page='.$page, __('Please fill al required fields!','contact'), 1);
  135. die();
  136. }
  137. $xoopsMailer =& getMailer();
  138. $xoopsMailer->useMail();
  139. $xoopsMailer->setBody($message.'\n--------------\n'.__('Message sent with ContactMe!','contact').'\nFrom '.$xoopsModuleConfig['url']);
  140. $xoopsMailer->setToEmails($msg->getVar('email'));
  141. $xoopsMailer->setFromEmail($xoopsConfig['from']);
  142. $xoopsMailer->setFromName($xoopsConfig['fromname']);
  143. $xoopsMailer->setSubject($subject);
  144. if (!$xoopsMailer->send(true)){
  145. redirectMsg('index.php?action=reply&id='.$id.'&page='.$page, __('Message could not be delivered. Please try again.','contact').'<br />'.$xoopsMailer->getErrors(), 1);
  146. die();
  147. }
  148. redirectMsg('index.php?page='.$page, __('Message sent successfully!','contact'), 0);
  149. }
  150. $action = rmc_server_var($_REQUEST, 'action', '');
  151. switch($action){
  152. case 'reply':
  153. cm_reply_message();
  154. break;
  155. case 'delete':
  156. cm_delete_messages();
  157. break;
  158. case 'sendmsg':
  159. cm_send_reply();
  160. break;
  161. default:
  162. cm_show_messages();
  163. break;
  164. }