new_message.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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[]='
  24. <script>
  25. function validate(form,list) {
  26. if (list.selectedIndex<0) {
  27. alert("Please select someone to send the message to.")
  28. return false
  29. } else {
  30. return true
  31. }
  32. }
  33. </script>';
  34. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  35. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  36. $htmlHeadXtra[] = '<script>
  37. $(document).ready(function () {
  38. $("#users").fcbkcomplete({
  39. json_url: "'.api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users",
  40. addontab: false,
  41. cache: false,
  42. filter_case: false,
  43. filter_hide: false,
  44. complete_text:"'.get_lang('StartToType').'",
  45. firstselected: false,
  46. onselect: check_users,
  47. filter_selected: true,
  48. newel: true
  49. });
  50. });
  51. function check_users() {
  52. //selecting only selected users
  53. $("#users option:selected").each(function() {
  54. var user_id = $(this).val();
  55. if (user_id != "" ) {
  56. $.ajax({
  57. url: "'.api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_id_exists",
  58. data: "user_id="+user_id,
  59. success: function(return_value) {
  60. if (return_value == 0 ) {
  61. alert("'.get_lang('UserDoesNotExist').'");
  62. //Deleting select option tag
  63. $("#users option[value="+user_id+"]").remove();
  64. //Deleting holder
  65. $(".holder li").each(function () {
  66. if ($(this).attr("rel") == user_id) {
  67. $(this).remove();
  68. }
  69. });
  70. }
  71. },
  72. });
  73. }
  74. });
  75. }
  76. var counter_image = 1;
  77. /*
  78. function remove_image_form(id_elem1) {
  79. var elem1 = document.getElementById(id_elem1);
  80. elem1.parentNode.removeChild(elem1);
  81. }
  82. */
  83. function add_image_form() {
  84. // Multiple filepaths for image form
  85. var filepaths = document.getElementById("filepaths");
  86. if (document.getElementById("filepath_"+counter_image)) {
  87. counter_image = counter_image + 1;
  88. } else {
  89. counter_image = counter_image;
  90. }
  91. var elem1 = document.createElement("div");
  92. elem1.setAttribute("id","filepath_"+counter_image);
  93. filepaths.appendChild(elem1);
  94. id_elem1 = "filepath_"+counter_image;
  95. id_elem1 = "\'"+id_elem1+"\'";
  96. 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 />";
  97. if (filepaths.childNodes.length == 6) {
  98. var link_attach = document.getElementById("link-more-attach");
  99. if (link_attach) {
  100. link_attach.innerHTML="";
  101. }
  102. }
  103. }
  104. </script>';
  105. $nameTools = get_lang('ComposeMessage');
  106. /* FUNCTIONS */
  107. /**
  108. * Shows the compose area + a list of users to select from.
  109. */
  110. function show_compose_to_any ($user_id) {
  111. $online_user_list = MessageManager::get_online_user_list($user_id);
  112. $default['user_list'] = 0;
  113. $online_user_list=null;
  114. $html = manage_form($default, $online_user_list);
  115. return $html;
  116. }
  117. function show_compose_reply_to_message($message_id, $receiver_id) {
  118. global $charset;
  119. $table_message = Database::get_main_table(TABLE_MESSAGE);
  120. $query = "SELECT user_sender_id FROM $table_message WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  121. $result = Database::query($query);
  122. $row = Database::fetch_array($result,'ASSOC');
  123. $html = null;
  124. if (!isset($row['user_sender_id'])) {
  125. $html = get_lang('InvalidMessageId');
  126. return $html;
  127. }
  128. $pre_html = '<div class="control-group">
  129. <label class="control-label">'.get_lang('SendMessageTo').': </label>
  130. <div class="controls">';
  131. $post = '</div></div>';
  132. $userInfo = api_get_user_info($row['user_sender_id']);
  133. $sent_to = $pre_html.'<strong>'.$userInfo['complete_name'].'</strong>'.$post;
  134. $default['users'] = array($row['user_sender_id']);
  135. $html .= manage_form($default, null, $sent_to);
  136. return $html;
  137. }
  138. function show_compose_to_user ($receiver_id) {
  139. global $charset;
  140. $userInfo = api_get_user_info($receiver_id);
  141. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  142. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  143. $default['users'] = array($receiver_id);
  144. $html .= manage_form($default);
  145. return $html;
  146. }
  147. function manage_form($default, $select_from_user_list = null, $sent_to = null) {
  148. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  149. $message_id = isset($_GET['message_id']) ? intval($_GET['message_id']) : null;
  150. $param_f = isset($_GET['f']) ? Security::remove_XSS($_GET['f']):'';
  151. $form = new FormValidator('compose_message',null,api_get_self().'?f='.$param_f, null, array('enctype'=>'multipart/form-data'));
  152. if (empty($group_id)) {
  153. if (isset($select_from_user_list)) {
  154. $form->add_textfield('id_text_name', get_lang('SendMessageTo'), true,array('class' => 'span4','id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off'));
  155. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  156. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  157. $form->addElement('hidden','user_list', 0, array('id'=>'user_list'));
  158. } else {
  159. if (!empty($sent_to)) {
  160. $form->addElement('html',$sent_to);
  161. }
  162. if (empty($default['users'])) {
  163. //fb select
  164. $form->addElement('select', 'users', get_lang('SendMessageTo'), array(), array('id' => 'users'));
  165. } else {
  166. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  167. }
  168. }
  169. } else {
  170. $usergroup = new UserGroup();
  171. $group_info = $usergroup->get($group_id);
  172. $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name']));
  173. $form->addElement('hidden','group_id',$group_id);
  174. $form->addElement('hidden','parent_id',$message_id);
  175. }
  176. $form->add_textfield('title', get_lang('Subject'),true , array('class' => 'span4'));
  177. $form->add_html_editor('content', get_lang('Message'), false, false, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  178. if (isset($_GET['re_id'])) {
  179. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  180. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_reply_info['title'];
  181. $form->addElement('hidden','re_id', intval($_GET['re_id']));
  182. $form->addElement('hidden','save_form','save_form');
  183. //adding reply mail
  184. $user_reply_info = UserManager::get_user_info_by_id($message_reply_info['user_sender_id']);
  185. $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']));
  186. }
  187. if (empty($group_id)) {
  188. $form->addElement('advanced_settings', get_lang('FilesAttachment').'<span id="filepaths">
  189. <div id="filepath_1">
  190. <input type="file" name="attach_1"/><br />
  191. '.get_lang('Description').'&nbsp;&nbsp;<input type="text" name="legend[]" /><br /><br />
  192. </div>
  193. </span>');
  194. $form->addElement('advanced_settings','<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a></span>&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. /* MAIN SECTION */
  240. if ($_GET['f']=='social') {
  241. $this_section = SECTION_SOCIAL;
  242. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/social/home.php','name' => get_lang('SocialNetwork'));
  243. } else {
  244. $this_section = SECTION_MYPROFILE;
  245. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/auth/profile.php','name' => get_lang('Profile'));
  246. }
  247. //Display::display_header(get_lang('ComposeMessage'));
  248. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  249. if ($group_id != 0) {
  250. $social_right_content .= '<div class=actions>';
  251. $social_right_content .= '<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'))).'</a>';
  252. $social_right_content .= '<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'))).'</a>';
  253. $social_right_content .= '</div>';
  254. } else {
  255. if ($_GET['f']=='social') {
  256. } else {
  257. $social_right_content .= '<div class=actions>';
  258. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  259. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  260. }
  261. if (api_get_setting('allow_message_tool') == 'true') {
  262. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.Display::return_icon('message_new.png',get_lang('ComposeMessage')).'</a>';
  263. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png',get_lang('Inbox')).'</a>';
  264. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.Display::return_icon('outbox.png',get_lang('Outbox')).'</a>';
  265. }
  266. $social_right_content .= '</div>';
  267. }
  268. }
  269. //LEFT COLUMN
  270. if (api_get_setting('allow_social_tool') == 'true') {
  271. $social_left_content = SocialManager::show_social_menu('messages');
  272. $social_right_content = '<div class="span9">';
  273. $social_right_content .= '<div class="actions">';
  274. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.Display::return_icon('back.png', get_lang('Back'), array(), 32).'</a>';
  275. $social_right_content .= '</div>';
  276. $social_right_content .= '</div>';
  277. $social_right_content .= '<div class="span9">';
  278. }
  279. //MAIN CONTENT
  280. if (!isset($_POST['compose'])) {
  281. if(isset($_GET['re_id'])) {
  282. $social_right_content .= show_compose_reply_to_message($_GET['re_id'], api_get_user_id());
  283. } elseif(isset($_GET['send_to_user'])) {
  284. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  285. } else {
  286. $social_right_content .= show_compose_to_any($_user['user_id']);
  287. }
  288. } else {
  289. $restrict = false;
  290. if (isset($_POST['users'])) {
  291. $restrict = true;
  292. } elseif (isset($_POST['group_id'])) {
  293. $restrict = true;
  294. } elseif(isset($_POST['hidden_user'])) {
  295. $restrict = true;
  296. }
  297. $default['title'] = $_POST['title'];
  298. $default['content'] = $_POST['content'];
  299. // comes from a reply button
  300. if (isset($_GET['re_id'])) {
  301. $social_right_content .= manage_form($default);
  302. } else {
  303. // post
  304. if ($restrict) {
  305. if (!isset($_POST['group_id'])) {
  306. $default['users'] = $_POST['users'];
  307. } else {
  308. $default['group_id'] = $_POST['group_id'];
  309. }
  310. if (isset($_POST['hidden_user'])) {
  311. $default['users'] = array($_POST['hidden_user']);
  312. }
  313. $social_right_content .= manage_form($default);
  314. } else {
  315. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'),'error');
  316. }
  317. }
  318. }
  319. if (api_get_setting('allow_social_tool') == 'true') {
  320. $social_right_content .= '</div>';
  321. }
  322. $app['title'] = get_lang('ComposeMessage');
  323. $tpl = $app['template'];
  324. if (api_get_setting('allow_social_tool') == 'true') {
  325. $tpl->assign('social_left_content', $social_left_content);
  326. $tpl->assign('social_right_content', $social_right_content);
  327. $social_layout = $tpl->get_template('layout/social_layout.tpl');
  328. $tpl->display($social_layout);
  329. } else {
  330. $content = $social_right_content;
  331. $tpl->assign('actions', $actions);
  332. $tpl->assign('message', $show_message);
  333. $tpl->assign('content', $content);
  334. $tpl->display_one_col_template();
  335. }