new_message.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.messages
  5. */
  6. /**
  7. * This script shows a compose area (wysiwyg editor if supported, otherwise
  8. * a simple textarea) where the user can type a message.
  9. * There are three modes
  10. * - standard: type a message, select a user to send it to, press send
  11. * - reply on message (when pressing reply when viewing a message)
  12. * - send to specific user (when pressing send message in the who is online list)
  13. */
  14. /* INIT SECTION */
  15. // name of the language file that needs to be included
  16. $language_file= array('messages','userInfo', 'admin');
  17. $cidReset = true;
  18. //require_once '../inc/global.inc.php';
  19. api_block_anonymous_users();
  20. if (api_get_setting('allow_message_tool') !='true') {
  21. api_not_allowed();
  22. }
  23. $htmlHeadXtra[] = '<script>
  24. function validate(form,list) {
  25. if (list.selectedIndex<0) {
  26. alert("Please select someone to send the message to.")
  27. return false
  28. } else {
  29. return true
  30. }
  31. }
  32. $(document).ready(function() {
  33. $("#users").fcbkcomplete({
  34. json_url: "'.api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users",
  35. addontab: false,
  36. cache: false,
  37. filter_case: false,
  38. filter_hide: false,
  39. complete_text:"'.get_lang('StartToType').'",
  40. firstselected: false,
  41. onselect: check_users,
  42. filter_selected: true,
  43. newel: true
  44. });
  45. });
  46. function check_users() {
  47. //selecting only selected users
  48. $("#users option:selected").each(function() {
  49. var user_id = $(this).val();
  50. if (user_id != "" ) {
  51. $.ajax({
  52. url: "'.api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_id_exists",
  53. data: "user_id="+user_id,
  54. success: function(return_value) {
  55. if (return_value == 0 ) {
  56. alert("'.get_lang('UserDoesNotExist').'");
  57. //Deleting select option tag
  58. $("#users option[value="+user_id+"]").remove();
  59. //Deleting holder
  60. $(".holder li").each(function () {
  61. if ($(this).attr("rel") == user_id) {
  62. $(this).remove();
  63. }
  64. });
  65. }
  66. },
  67. });
  68. }
  69. });
  70. }
  71. var counter_image = 1;
  72. function add_image_form() {
  73. // Multiple filepaths for image form
  74. var filepaths = document.getElementById("filepaths");
  75. if (document.getElementById("filepath_"+counter_image)) {
  76. counter_image = counter_image + 1;
  77. } else {
  78. counter_image = counter_image;
  79. }
  80. var elem1 = document.createElement("div");
  81. elem1.setAttribute("id","filepath_"+counter_image);
  82. filepaths.appendChild(elem1);
  83. id_elem1 = "filepath_"+counter_image;
  84. id_elem1 = "\'"+id_elem1+"\'";
  85. document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" />&nbsp; <br />'.get_lang('Description').'&nbsp;&nbsp;<input type=\"text\" name=\"legend[]\" /><br /><br />";
  86. if (filepaths.childNodes.length == 6) {
  87. var link_attach = document.getElementById("link-more-attach");
  88. if (link_attach) {
  89. link_attach.innerHTML="";
  90. }
  91. }
  92. }
  93. </script>';
  94. $nameTools = get_lang('ComposeMessage');
  95. /* FUNCTIONS */
  96. /**
  97. * Shows the compose area + a list of users to select from.
  98. */
  99. function show_compose_to_any ($user_id) {
  100. $online_user_list = MessageManager::get_online_user_list($user_id);
  101. $default['user_list'] = 0;
  102. $online_user_list=null;
  103. $html = manage_form($default, $online_user_list);
  104. return $html;
  105. }
  106. function show_compose_reply_to_message($message_id, $receiver_id) {
  107. $table_message = Database::get_main_table(TABLE_MESSAGE);
  108. $query = "SELECT user_sender_id FROM $table_message WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  109. $result = Database::query($query);
  110. $row = Database::fetch_array($result,'ASSOC');
  111. $html = null;
  112. if (!isset($row['user_sender_id'])) {
  113. $html = get_lang('InvalidMessageId');
  114. return $html;
  115. }
  116. $pre_html = '<div class="control-group">
  117. <label class="control-label">'.get_lang('SendMessageTo').': </label>
  118. <div class="controls">';
  119. $post = '</div></div>';
  120. $userInfo = api_get_user_info($row['user_sender_id']);
  121. $sent_to = $pre_html.'<strong>'.$userInfo['complete_name'].'</strong>'.$post;
  122. $default['users'] = array($row['user_sender_id']);
  123. $html .= manage_form($default, null, $sent_to);
  124. return $html;
  125. }
  126. function show_compose_to_user ($receiver_id) {
  127. $userInfo = api_get_user_info($receiver_id);
  128. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  129. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  130. $default['users'] = array($receiver_id);
  131. $html .= manage_form($default);
  132. return $html;
  133. }
  134. /**
  135. * @param $default
  136. * @param null $select_from_user_list
  137. * @param null $sent_to
  138. * @return string
  139. */
  140. function manage_form($default, $select_from_user_list = null, $sent_to = null)
  141. {
  142. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  143. $message_id = isset($_GET['message_id']) ? intval($_GET['message_id']) : null;
  144. $param_f = isset($_GET['f']) ? Security::remove_XSS($_GET['f']):'';
  145. $form = new FormValidator('compose_message',null,api_get_self().'?f='.$param_f, null, array('enctype'=>'multipart/form-data'));
  146. if (empty($group_id)) {
  147. if (isset($select_from_user_list)) {
  148. $form->add_textfield('id_text_name', get_lang('SendMessageTo'), true, array('id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off'));
  149. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  150. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  151. $form->addElement('hidden','user_list', 0, array('id'=>'user_list'));
  152. } else {
  153. if (!empty($sent_to)) {
  154. $form->addElement('html', $sent_to);
  155. }
  156. if (empty($default['users'])) {
  157. //fb select
  158. $form->addElement('select', 'users', get_lang('SendMessageTo'), array(), array('id' => 'users'));
  159. } else {
  160. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  161. }
  162. }
  163. } else {
  164. $usergroup = new UserGroup();
  165. $group_info = $usergroup->get($group_id);
  166. $form->addElement('label', get_lang('ToGroup'), $group_info['name']);
  167. $form->addElement('hidden','group_id',$group_id);
  168. $form->addElement('hidden','parent_id',$message_id);
  169. }
  170. $form->add_textfield('title', get_lang('Subject'), true);
  171. $form->add_html_editor('content', get_lang('Message'), false, false, array('ToolbarSet' => 'Messages'));
  172. if (isset($_GET['re_id'])) {
  173. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  174. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_reply_info['title'];
  175. $form->addElement('hidden','re_id', intval($_GET['re_id']));
  176. $form->addElement('hidden','save_form','save_form');
  177. //adding reply mail
  178. $user_reply_info = UserManager::get_user_info_by_id($message_reply_info['user_sender_id']);
  179. $default['content'] = '<br />'.sprintf(get_lang('XWroteY'), api_get_person_name($user_reply_info['firstname'], $user_reply_info['lastname']), Security::filter_terms($message_reply_info['content']));
  180. }
  181. if (empty($group_id)) {
  182. $form->addElement('label', null, get_lang('FilesAttachment').'<span id="filepaths">
  183. <div id="filepath_1">
  184. <input type="file" name="attach_1"/><br />
  185. '.get_lang('Description').'&nbsp;&nbsp;
  186. <input type="text" name="legend[]" /><br /><br />
  187. </div>
  188. </span>');
  189. $form->addElement(
  190. 'label',
  191. null,
  192. '<span id="link-more-attach">
  193. <a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a></span>
  194. &nbsp;('.sprintf(get_lang('MaximunFileSizeX'),Text::format_file_size(api_get_setting('message_max_upload_filesize'))).')');
  195. }
  196. $form->addElement('style_submit_button','compose',api_xml_http_response_encode(get_lang('SendMessage')),'class="save"');
  197. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  198. if (!empty($group_id) && !empty($message_id)) {
  199. $message_info = MessageManager::get_message_by_id($message_id);
  200. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  201. }
  202. $form->setDefaults($default);
  203. $html = '';
  204. if ($form->validate()) {
  205. $check = Security::check_token('post');
  206. if ($check) {
  207. $user_list = $default['users'];
  208. $file_comments = $_POST['legend'];
  209. $title = $default['title'];
  210. $content = $default['content'];
  211. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  212. $parent_id = $default['parent_id'];
  213. if (is_array($user_list) && count($user_list)> 0) {
  214. //all is well, send the message
  215. foreach ($user_list as $user) {
  216. $res = MessageManager::send_message($user, $title, $content, $_FILES, $file_comments, $group_id, $parent_id, null, null, api_get_user_id());
  217. if ($res) {
  218. if (is_string($res)) {
  219. $html .= Display::return_message($res, 'error');
  220. } else {
  221. $user_info = api_get_user_info($user);
  222. $html .= Display::return_message(get_lang('MessageSentTo')." &nbsp;<b>".$user_info['complete_name']."</b>", 'confirmation', false);
  223. }
  224. }
  225. }
  226. } else {
  227. Display::display_error_message('ErrorSendingMessage');
  228. }
  229. }
  230. Security::clear_token();
  231. } else {
  232. $token = Security::get_token();
  233. $form->addElement('hidden','sec_token');
  234. $form->setConstants(array('sec_token' => $token));
  235. $html .= $form->return_form();
  236. }
  237. return $html;
  238. }
  239. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/messages/inbox.php','name' => get_lang('Messages'));
  240. $social_right_content = null;
  241. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  242. if ($group_id != 0) {
  243. $social_right_content .= '<div class=actions>';
  244. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/groups.php?id='.$group_id.'">'.
  245. Display::return_icon('back.png', get_lang('ComposeMessage')).'</a>';
  246. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php?group_id='.$group_id.'">'.
  247. Display::return_icon('message_new.png',get_lang('ComposeMessage')).'</a>';
  248. $social_right_content .= '</div>';
  249. } else {
  250. if ($_GET['f']=='social') {
  251. } else {
  252. $social_right_content .= '<div class=actions>';
  253. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  254. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  255. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  256. }
  257. if (api_get_setting('allow_message_tool') == 'true') {
  258. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.
  259. Display::return_icon('message_new.png',get_lang('ComposeMessage')).'</a>';
  260. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  261. Display::return_icon('inbox.png',get_lang('Inbox')).'</a>';
  262. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.
  263. Display::return_icon('outbox.png',get_lang('Outbox')).'</a>';
  264. }
  265. $social_right_content .= '</div>';
  266. }
  267. }
  268. //MAIN CONTENT
  269. if (!isset($_POST['compose'])) {
  270. if(isset($_GET['re_id'])) {
  271. $social_right_content .= show_compose_reply_to_message($_GET['re_id'], api_get_user_id());
  272. } elseif(isset($_GET['send_to_user'])) {
  273. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  274. } else {
  275. $social_right_content .= show_compose_to_any($_user['user_id']);
  276. }
  277. } else {
  278. $restrict = false;
  279. if (isset($_POST['users'])) {
  280. $restrict = true;
  281. } elseif (isset($_POST['group_id'])) {
  282. $restrict = true;
  283. } elseif(isset($_POST['hidden_user'])) {
  284. $restrict = true;
  285. }
  286. $default['title'] = $_POST['title'];
  287. $default['content'] = $_POST['content'];
  288. // comes from a reply button
  289. if (isset($_GET['re_id'])) {
  290. $social_right_content .= manage_form($default);
  291. } else {
  292. // post
  293. if ($restrict) {
  294. if (!isset($_POST['group_id'])) {
  295. $default['users'] = $_POST['users'];
  296. } else {
  297. $default['group_id'] = $_POST['group_id'];
  298. }
  299. if (isset($_POST['hidden_user'])) {
  300. $default['users'] = array($_POST['hidden_user']);
  301. }
  302. $social_right_content .= manage_form($default);
  303. } else {
  304. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'),'error');
  305. }
  306. }
  307. }
  308. if (api_get_setting('allow_social_tool') == 'true') {
  309. $social_right_content .= '</div>';
  310. }
  311. $tpl = $app['template'];
  312. $tpl->setTitle(get_lang('ComposeMessage'));
  313. $content = $social_right_content;
  314. $tpl->assign('actions', $actions);
  315. $tpl->assign('message', $show_message);
  316. $tpl->assign('content', $content);
  317. $tpl->display_one_col_template();