new_message.php 12 KB

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