new_message.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php // $Id: new_message.php 22097 2009-07-15 04:30:05Z ivantcholakov $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2009 Dokeos SPRL
  6. Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
  7. Copyright (c) Facultad de Matematicas, UADY (M�xico)
  8. Copyright (c) Evie, Free University of Brussels (Belgium)
  9. Copyright (c) 2009 Isaac Flores Paz <isaac.flores.paz@gmail.com>
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. * This script shows a compose area (wysiwyg editor if supported, otherwise
  23. * a simple textarea) where the user can type a message.
  24. * There are three modes
  25. * - standard: type a message, select a user to send it to, press send
  26. * - reply on message (when pressing reply when viewing a message)
  27. * - send to specific user (when pressing send message in the who is online list)
  28. */
  29. /*
  30. ==============================================================================
  31. INIT SECTION
  32. ==============================================================================
  33. */
  34. // name of the language file that needs to be included
  35. $language_file= 'messages';
  36. $cidReset=true;
  37. require_once '../inc/global.inc.php';
  38. api_block_anonymous_users();
  39. if (api_get_setting('allow_message_tool')!='true'){
  40. api_not_allowed();
  41. }
  42. require_once api_get_path(LIBRARY_PATH).'text.lib.php';
  43. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  44. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  45. require_once api_get_path(LIBRARY_PATH).'message.lib.php';
  46. $request=api_is_xml_http_request();
  47. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  48. /*
  49. -----------------------------------------------------------
  50. Constants and variables
  51. -----------------------------------------------------------
  52. */
  53. $htmlHeadXtra[]='
  54. <script language="javascript">
  55. function validate(form,list)
  56. {
  57. if(list.selectedIndex<0)
  58. {
  59. alert("Please select someone to send the message to.")
  60. return false
  61. }
  62. else
  63. return true
  64. }
  65. </script>';
  66. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  67. $htmlHeadXtra[] = '<script type="text/javascript">
  68. $(document).ready(function (){
  69. cont=0;
  70. $("#id_text_name").bind("keyup", function(){
  71. name=$("#id_text_name").get(0).value;
  72. $.ajax({
  73. contentType: "application/x-www-form-urlencoded",
  74. beforeSend: function(objeto) {
  75. /*$("#id_div_search").html("Searching...");*/ },
  76. type: "POST",
  77. url: "../social/select_options.php",
  78. data: "search="+name,
  79. success: function(datos){
  80. $("#id_div_search").html(datos)
  81. $("#id_search_name").bind("click", function(){
  82. name_option=$("select#id_search_name option:selected").text();
  83. code_option=$("select#id_search_name option:selected").val();
  84. $("#user_list").attr("value", code_option);
  85. $("#id_text_name").attr("value", name_option);
  86. $("#id_div_search").html("");
  87. cont++;
  88. });
  89. }
  90. });
  91. });
  92. });
  93. var counter_image = 1;
  94. function remove_image_form(id_elem1) {
  95. var elem1 = document.getElementById(id_elem1);
  96. elem1.parentNode.removeChild(elem1);
  97. }
  98. function add_image_form() {
  99. counter_image = counter_image + 1;
  100. // Multiple filepaths for image form
  101. var filepaths = document.getElementById("filepaths");
  102. var elem1 = document.createElement("div");
  103. elem1.setAttribute("id","filepath_"+counter_image);
  104. filepaths.appendChild(elem1);
  105. id_elem1 = "filepath_"+counter_image;
  106. id_elem1 = "\'"+id_elem1+"\'";
  107. document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" size=\"28\" />&nbsp;<input type=\"text\" name=\"legend[]\" size=\"28\" />&nbsp;<a href=\"javascript:remove_image_form("+id_elem1+")\"><img src=\"'.api_get_path(WEB_CODE_PATH).'img/delete.gif\"></a>";
  108. }
  109. </script>';
  110. $nameTools = api_xml_http_response_encode(get_lang('ComposeMessage'));
  111. /*
  112. ==============================================================================
  113. FUNCTIONS
  114. ==============================================================================
  115. */
  116. /**
  117. * Shows the compose area + a list of users to select from.
  118. */
  119. function show_compose_to_any ($user_id) {
  120. $online_user_list = MessageManager::get_online_user_list($user_id);
  121. $default['user_list'] = 0;
  122. $online_user_list=null;
  123. manage_form($default, $online_user_list);
  124. }
  125. function show_compose_reply_to_message ($message_id, $receiver_id) {
  126. global $charset;
  127. $table_message = Database::get_main_table(TABLE_MESSAGE);
  128. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".$receiver_id." AND id='".$message_id."';";
  129. $result = Database::query($query,__FILE__,__LINE__);
  130. $row = Database::fetch_array($result);
  131. if (!isset($row[1])) {
  132. echo api_xml_http_response_encode(get_lang('InvalidMessageId'));
  133. die();
  134. }
  135. echo api_xml_http_response_encode(get_lang('To').':&nbsp;<strong>'. GetFullUserName($row[1]).'</strong>');
  136. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  137. $default['user_list'] = $row[1];
  138. manage_form($default);
  139. }
  140. function show_compose_to_user ($receiver_id) {
  141. global $charset;
  142. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($receiver_id).'</strong>';
  143. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  144. $default['user_list'] = $receiver_id;
  145. manage_form($default);
  146. }
  147. function manage_form ($default, $select_from_user_list = null) {
  148. global $charset;
  149. $table_message = Database::get_main_table(TABLE_MESSAGE);
  150. $request=api_is_xml_http_request();
  151. $group_id = intval($_REQUEST['group_id']);
  152. $message_id = intval($_GET['message_id']);
  153. $form = new FormValidator('compose_message',null,null,null,array('enctype'=>'multipart/form-data'));
  154. if (empty($group_id)) {
  155. if (isset($select_from_user_list)) {
  156. $form->add_textfield('id_text_name', api_xml_http_response_encode(get_lang('SendMessageTo')),true,array('size' => 40,'id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off','style'=>'padding:0px'));
  157. $form->addRule('id_text_name', api_xml_http_response_encode(get_lang('ThisFieldIsRequired')), 'required');
  158. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  159. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  160. } else {
  161. if ($default['user_list']==0) {
  162. $form->add_textfield('id_text_name', api_xml_http_response_encode(get_lang('SendMessageTo')),true,array('size' => 40,'id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off','style'=>'padding:0px'));
  163. $form->addRule('id_text_name', api_xml_http_response_encode(get_lang('ThisFieldIsRequired')), 'required');
  164. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  165. }
  166. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  167. }
  168. } else {
  169. $group_info = GroupPortalManager::get_group_data($group_id);
  170. $form->addElement('html','<div class="row"><div class="label">'.get_lang('ToGroup').'</div><div class="formw">'.api_xml_http_response_encode($group_info['name']).'</div></div>');
  171. $form->addElement('hidden','group_id',$group_id);
  172. $form->addElement('hidden','parent_id',$message_id);
  173. }
  174. $form->add_textfield('title', api_xml_http_response_encode(get_lang('Title')));
  175. $form->add_html_editor('content', '', false, false, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  176. if (isset($_GET['re_id'])) {
  177. $form->addElement('hidden','re_id',Security::remove_XSS($_GET['re_id']));
  178. $form->addElement('hidden','save_form','save_form');
  179. }
  180. if (empty($group_id)) {
  181. $form->addElement('html','<div class="row"><div class="label">'.get_lang('FilesAttachment').'</div><div class="formw">
  182. <span id="filepaths">
  183. <div id="filepath_1">
  184. <input type="file" name="attach_1" size="28" />
  185. <input type="text" name="legend[]" size="28" />
  186. </div></span></div></div>');
  187. $form->addElement('html','<div class="row"><div class="formw"><a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a>&nbsp;('.get_lang('MaximunFileSizeXMB').')</div></div>');
  188. }
  189. $form->addElement('style_submit_button','compose',api_xml_http_response_encode(get_lang('SendMessage')),'class="save"');
  190. $form->setRequiredNote(api_xml_http_response_encode('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>'));
  191. if (!empty($group_id) && !empty($message_id)) {
  192. $message_info = MessageManager::get_message_by_id($message_id);
  193. $default['title']=get_lang('Re:').api_html_entity_decode($message_info['title'],ENT_QUOTES,$charset);
  194. }
  195. $form->setDefaults($default);
  196. if ($form->validate()) {
  197. $values = $form->exportValues();
  198. $receiver_user_id = $values['user_list'];
  199. $title = $values['title'];
  200. $content = $values['content'];
  201. $file_comments = $_POST['legend'];
  202. $group_id = $values['group_id'];
  203. $parent_id = $values['parent_id'];
  204. //all is well, send the message
  205. MessageManager::send_message($receiver_user_id, $title, $content, $_FILES, $file_comments, $group_id, $parent_id);
  206. MessageManager::display_success_message($receiver_user_id);
  207. } else {
  208. $form->display();
  209. }
  210. }
  211. /*
  212. ==============================================================================
  213. MAIN SECTION
  214. ==============================================================================
  215. */
  216. if (isset($_GET['rs'])) {
  217. $interbreadcrumb[] = array ('url' => 'inbox.php', 'name' => get_lang('Messages'));
  218. $interbreadcrumb[]= array (
  219. 'url' => '../social/'.$_SESSION['social_dest'],
  220. 'name' => get_lang('SocialNetwork')
  221. );
  222. } else {
  223. $interbreadcrumb[] = array ('url' => 'main/auth/profile.php', 'name' => get_lang('Profile'));
  224. $interbreadcrumb[]= array (
  225. 'url' => 'inbox.php',
  226. 'name' => get_lang('Inbox')
  227. );
  228. }
  229. $interbreadcrumb[]= array (
  230. 'url' => '#',
  231. 'name' => get_lang('ComposeMessage')
  232. );
  233. Display::display_header('');
  234. $group_id = intval($_REQUEST['group_id']);
  235. echo '<div class=actions>';
  236. if ($group_id != 0) {
  237. echo '<a href="'.api_get_path(WEB_PATH).'main/social/groups.php?id='.$group_id.'">'.Display::return_icon('back.png',api_xml_http_response_encode(get_lang('ComposeMessage'))).api_xml_http_response_encode(get_lang('BackToGroup')).'</a>';
  238. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php?group_id='.$group_id.'">'.Display::return_icon('message_new.png',api_xml_http_response_encode(get_lang('ComposeMessage'))).api_xml_http_response_encode(get_lang('ComposeMessage')).'</a>';
  239. } else {
  240. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png',api_xml_http_response_encode(get_lang('Inbox'))).api_xml_http_response_encode(get_lang('Inbox')).'</a>';
  241. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.Display::return_icon('message_new.png',api_xml_http_response_encode(get_lang('ComposeMessage'))).api_xml_http_response_encode(get_lang('ComposeMessage')).'</a>';
  242. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.Display::return_icon('outbox.png',api_xml_http_response_encode(get_lang('Outbox'))).api_xml_http_response_encode(get_lang('Outbox')).'</a>';
  243. }
  244. echo '</div>';
  245. if (!isset($_POST['compose'])) {
  246. if(isset($_GET['re_id'])) {
  247. $message_id = $_GET['re_id'];
  248. $receiver_id = api_get_user_id();
  249. show_compose_reply_to_message($message_id, $receiver_id);
  250. } elseif(isset($_GET['send_to_user'])) {
  251. show_compose_to_user($_GET['send_to_user']);
  252. } else {
  253. show_compose_to_any($_user['user_id']);
  254. }
  255. } else {
  256. $restrict = false;
  257. if (isset($_POST['id_text_name'])) {
  258. $restrict = $_POST['id_text_name'];
  259. } else if (isset($_POST['group_id'])) {
  260. $restrict = $_POST['group_id'];
  261. }
  262. if (isset($_GET['re_id'])) {
  263. $default['title'] = api_xml_http_response_encode($_POST['title']);
  264. $default['content'] = api_xml_http_response_encode($_POST['content']);
  265. //$default['user_list'] = $_POST['user_list'];
  266. manage_form($default);
  267. } else {
  268. if ($restrict) {
  269. $default['title'] = api_xml_http_response_encode($_POST['title']);
  270. if (!isset($_POST['group_id'])) {
  271. $default['id_text_name'] = api_xml_http_response_encode($_POST['id_text_name']);
  272. $default['user_list'] = $_POST['user_list'];
  273. } else {
  274. $default['group_id'] = $_POST['group_id'];
  275. }
  276. manage_form($default);
  277. } else {
  278. Display::display_error_message(api_xml_http_response_encode(get_lang('ErrorSendingMessage')));
  279. }
  280. }
  281. }
  282. /*
  283. ==============================================================================
  284. FOOTER
  285. ==============================================================================
  286. */
  287. if ($request===false) {
  288. Display::display_footer();
  289. }
  290. ?>