new_message.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. $cidReset = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. api_block_anonymous_users();
  17. if (api_get_setting('allow_message_tool') !== 'true') {
  18. api_not_allowed();
  19. }
  20. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  21. /* Constants and variables */
  22. $htmlHeadXtra[] = '
  23. <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. </script>';
  33. $htmlHeadXtra[] = '<script>
  34. var counter_image = 1;
  35. function add_image_form() {
  36. // Multiple filepaths for image form
  37. var filepaths = document.getElementById("file_uploads");
  38. if (document.getElementById("filepath_"+counter_image)) {
  39. counter_image = counter_image + 1;
  40. } else {
  41. counter_image = counter_image;
  42. }
  43. var elem1 = document.createElement("div");
  44. elem1.setAttribute("id","filepath_"+counter_image);
  45. filepaths.appendChild(elem1);
  46. id_elem1 = "filepath_"+counter_image;
  47. id_elem1 = "\'"+id_elem1+"\'";
  48. document.getElementById("filepath_"+counter_image).innerHTML = "<div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('FilesAttachment').'</label><input class=\"col-sm-8\" type=\"file\" name=\"attach_"+counter_image+"\" /></div><div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('Description').'</label><div class=\"col-sm-8\"><input style=\"width:100%\" type=\"text\" name=\"legend[]\" /></div></div>";
  49. if (filepaths.childNodes.length == 6) {
  50. var link_attach = document.getElementById("link-more-attach");
  51. if (link_attach) {
  52. link_attach.innerHTML="";
  53. }
  54. }
  55. }
  56. </script>';
  57. $nameTools = get_lang('ComposeMessage');
  58. /**
  59. * Shows the compose area + a list of users to select from.
  60. */
  61. function show_compose_to_any($user_id)
  62. {
  63. $default['user_list'] = 0;
  64. $online_user_list = null;
  65. $html = manageForm($default, $online_user_list);
  66. return $html;
  67. }
  68. function show_compose_reply_to_message($message_id, $receiver_id)
  69. {
  70. $table_message = Database::get_main_table(TABLE_MESSAGE);
  71. $query = "SELECT user_sender_id
  72. FROM $table_message
  73. WHERE user_receiver_id = ".intval($receiver_id)." AND id='".intval($message_id)."';";
  74. $result = Database::query($query);
  75. $row = Database::fetch_array($result, 'ASSOC');
  76. if (!isset($row['user_sender_id'])) {
  77. $html = get_lang('InvalidMessageId');
  78. return $html;
  79. }
  80. $userInfo = api_get_user_info($row['user_sender_id']);
  81. $default['users'] = array($row['user_sender_id']);
  82. $html = manageForm($default, null, $userInfo['complete_name']);
  83. return $html;
  84. }
  85. function show_compose_to_user($receiver_id)
  86. {
  87. $userInfo = api_get_user_info($receiver_id);
  88. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  89. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  90. $default['users'] = array($receiver_id);
  91. $html .= manageForm($default);
  92. return $html;
  93. }
  94. /**
  95. * @param $default
  96. * @param null $select_from_user_list
  97. * @param null $sent_to
  98. * @return string
  99. */
  100. function manageForm($default, $select_from_user_list = null, $sent_to = null)
  101. {
  102. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  103. $message_id = isset($_GET['message_id']) ? intval($_GET['message_id']) : null;
  104. $param_f = isset($_GET['f']) && $_GET['f'] == 'social' ? 'social' : null;
  105. $form = new FormValidator(
  106. 'compose_message',
  107. null,
  108. api_get_self().'?f='.$param_f,
  109. null,
  110. array('enctype' => 'multipart/form-data')
  111. );
  112. if (empty($group_id)) {
  113. if (isset($select_from_user_list)) {
  114. $form->addText(
  115. 'id_text_name',
  116. get_lang('SendMessageTo'),
  117. true,
  118. array(
  119. 'id'=>'id_text_name',
  120. 'onkeyup'=>'send_request_and_search()',
  121. 'autocomplete'=>'off'
  122. )
  123. );
  124. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  125. $form->addElement('html', '<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  126. $form->addElement('hidden', 'user_list', 0, array('id'=>'user_list'));
  127. } else {
  128. if (!empty($sent_to)) {
  129. $form->addLabel(get_lang('SendMessageTo'), $sent_to);
  130. }
  131. if (empty($default['users'])) {
  132. //fb select
  133. $form->addElement(
  134. 'select_ajax',
  135. 'users',
  136. get_lang('SendMessageTo'),
  137. array(),
  138. [
  139. 'multiple' => 'multiple',
  140. 'url' => api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users'
  141. ]
  142. );
  143. } else {
  144. $form->addElement('hidden', 'hidden_user', $default['users'][0], array('id' => 'hidden_user'));
  145. }
  146. }
  147. } else {
  148. $userGroup = new UserGroup();
  149. $group_info = $userGroup->get($group_id);
  150. $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name']));
  151. $form->addElement('hidden', 'group_id', $group_id);
  152. $form->addElement('hidden', 'parent_id', $message_id);
  153. }
  154. $form->addText('title', get_lang('Subject'), true);
  155. $form->addHtmlEditor(
  156. 'content',
  157. get_lang('Message'),
  158. false,
  159. false,
  160. array('ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250')
  161. );
  162. if (isset($_GET['re_id'])) {
  163. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  164. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_reply_info['title'];
  165. $form->addElement('hidden', 're_id', intval($_GET['re_id']));
  166. $form->addElement('hidden', 'save_form', 'save_form');
  167. // Adding reply mail
  168. $user_reply_info = api_get_user_info($message_reply_info['user_sender_id']);
  169. $default['content'] = '<p><br/></p>'.sprintf(
  170. get_lang('XWroteY'),
  171. $user_reply_info['complete_name'],
  172. Security::filter_terms($message_reply_info['content'])
  173. );
  174. }
  175. if (isset($_GET['forward_id'])) {
  176. $forwardId = (int) $_GET['forward_id'];
  177. $message_reply_info = MessageManager::get_message_by_id($forwardId);
  178. $default['title'] = '['.get_lang('MailSubjectForwardShort').": ".$message_reply_info['title'].']';
  179. $form->addHidden('forward_id', $forwardId);
  180. $form->addHidden('save_form', 'save_form');
  181. $receiverInfo = api_get_user_info($message_reply_info['user_receiver_id']);
  182. $forwardMessage = '---------- '.get_lang('ForwardedMessage').' ---------'.'<br />';
  183. $forwardMessage .= get_lang('Date').': '.api_get_local_time($message_reply_info['send_date']).'<br />';
  184. $forwardMessage .= get_lang('Subject').': '.$message_reply_info['title'].'<br />';
  185. $forwardMessage .= get_lang('To').': '.$receiverInfo['complete_name'].' - '.$receiverInfo['email'].' <br />';
  186. $default['content'] = '<p><br/></p>'.$forwardMessage.'<br />'.Security::filter_terms($message_reply_info['content']);
  187. }
  188. if (empty($group_id)) {
  189. $form->addElement(
  190. 'label',
  191. '',
  192. '<div id="file_uploads"><div id="filepath_1">
  193. <div id="filepaths" class="form-horizontal">
  194. <div id="paths-file" class="form-group">
  195. <label class="col-sm-4">'.get_lang('FilesAttachment').'</label>
  196. <input class="col-sm-8" type="file" name="attach_1"/>
  197. </div>
  198. </div>
  199. <div id="paths-description" class="form-group">
  200. <label class="col-sm-4">'.get_lang('Description').'</label>
  201. <div class="col-sm-8">
  202. <input id="file-descrtiption" style="width:100%;" type="text" name="legend[]" />
  203. </div>
  204. </div>
  205. </div>
  206. </div>
  207. '
  208. );
  209. $form->addLabel(
  210. '',
  211. '<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a></span>&nbsp;('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')'
  212. );
  213. }
  214. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  215. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  216. if (!empty($group_id) && !empty($message_id)) {
  217. $message_info = MessageManager::get_message_by_id($message_id);
  218. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  219. }
  220. $form->setDefaults($default);
  221. $html = '';
  222. if ($form->validate()) {
  223. $check = Security::check_token('post');
  224. if ($check) {
  225. $user_list = $default['users'];
  226. $file_comments = $_POST['legend'];
  227. $title = $default['title'];
  228. $content = $default['content'];
  229. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  230. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  231. $forwardId = isset($_POST['forward_id']) ? $_POST['forward_id'] : false;
  232. if (is_array($user_list) && count($user_list) > 0) {
  233. //all is well, send the message
  234. foreach ($user_list as $userId) {
  235. $res = MessageManager::send_message(
  236. $userId,
  237. $title,
  238. $content,
  239. $_FILES,
  240. $file_comments,
  241. $group_id,
  242. $parent_id,
  243. 0,
  244. 0,
  245. null,
  246. false,
  247. $forwardId
  248. );
  249. if ($res) {
  250. $userInfo = api_get_user_info($userId);
  251. Display::addFlash(Display::return_message(
  252. get_lang('MessageSentTo')."&nbsp;<b>".$userInfo['complete_name']."</b>",
  253. 'confirmation',
  254. false
  255. ));
  256. }
  257. }
  258. } else {
  259. Display::addFlash(Display::return_message('ErrorSendingMessage', 'error'));
  260. }
  261. }
  262. Security::clear_token();
  263. } else {
  264. $token = Security::get_token();
  265. $form->addElement('hidden', 'sec_token');
  266. $form->setConstants(array('sec_token' => $token));
  267. $html .= $form->returnForm();
  268. }
  269. return $html;
  270. }
  271. $socialToolIsActive = isset($_GET['f']) && $_GET['f'] == 'social';
  272. /* MAIN SECTION */
  273. if ($socialToolIsActive) {
  274. $this_section = SECTION_SOCIAL;
  275. $interbreadcrumb[] = array(
  276. 'url' => api_get_path(WEB_PATH).'main/social/home.php',
  277. 'name' => get_lang('SocialNetwork')
  278. );
  279. } else {
  280. $this_section = SECTION_MYPROFILE;
  281. $interbreadcrumb[] = array(
  282. 'url' => api_get_path(WEB_PATH).'main/auth/profile.php',
  283. 'name' => get_lang('Profile')
  284. );
  285. }
  286. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  287. $social_right_content = null;
  288. if ($group_id != 0) {
  289. $social_right_content .= '<div class=actions>';
  290. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/group_view.php?id='.$group_id.'">'.
  291. Display::return_icon('back.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  292. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php?group_id='.$group_id.'">'.
  293. Display::return_icon('message_new.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  294. $social_right_content .= '</div>';
  295. } else {
  296. if ($socialToolIsActive) {
  297. } else {
  298. $social_right_content .= '<div class=actions>';
  299. if (api_get_setting('allow_social_tool') === 'true' && api_get_setting('allow_message_tool') === 'true') {
  300. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  301. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  302. }
  303. if (api_get_setting('allow_message_tool') === 'true') {
  304. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.
  305. Display::return_icon('message_new.png', get_lang('ComposeMessage')).'</a>';
  306. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  307. Display::return_icon('inbox.png', get_lang('Inbox')).'</a>';
  308. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.
  309. Display::return_icon('outbox.png', get_lang('Outbox')).'</a>';
  310. }
  311. $social_right_content .= '</div>';
  312. }
  313. }
  314. // LEFT COLUMN
  315. $social_left_content = null;
  316. if (api_get_setting('allow_social_tool') == 'true') {
  317. //Block Social Menu
  318. $social_menu_block = SocialManager::show_social_menu('messages');
  319. $social_right_content .= '<div class="row">';
  320. $social_right_content .= '<div class="col-md-12">';
  321. $social_right_content .= '<div class="actions">';
  322. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.
  323. Display::return_icon('back.png', get_lang('Back'), array(), 32).'</a>';
  324. $social_right_content .= '</div>';
  325. $social_right_content .= '</div>';
  326. $social_right_content .= '<div class="col-md-12">';
  327. }
  328. // MAIN CONTENT
  329. if (!isset($_POST['compose'])) {
  330. if (isset($_GET['re_id'])) {
  331. $social_right_content .= show_compose_reply_to_message(
  332. $_GET['re_id'],
  333. api_get_user_id()
  334. );
  335. } elseif (isset($_GET['send_to_user'])) {
  336. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  337. } else {
  338. $social_right_content .= show_compose_to_any(api_get_user_id());
  339. }
  340. } else {
  341. $restrict = false;
  342. if (isset($_POST['users'])) {
  343. $restrict = true;
  344. } elseif (isset($_POST['group_id'])) {
  345. $restrict = true;
  346. } elseif (isset($_POST['hidden_user'])) {
  347. $restrict = true;
  348. }
  349. $default['title'] = $_POST['title'];
  350. $default['content'] = $_POST['content'];
  351. // comes from a reply button
  352. if (isset($_GET['re_id']) || isset($_GET['forward_id'])) {
  353. $social_right_content .= manageForm($default);
  354. } else {
  355. // post
  356. if ($restrict) {
  357. if (!isset($_POST['group_id'])) {
  358. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  359. } else {
  360. $default['group_id'] = $_POST['group_id'];
  361. }
  362. if (isset($_POST['hidden_user'])) {
  363. $default['users'] = array($_POST['hidden_user']);
  364. }
  365. $social_right_content .= manageForm($default);
  366. } else {
  367. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  368. }
  369. }
  370. }
  371. if (api_get_setting('allow_social_tool') === 'true') {
  372. $social_right_content .= '</div>';
  373. $social_right_content .= '</div>';
  374. }
  375. $tpl = new Template(get_lang('ComposeMessage'));
  376. // Block Social Avatar
  377. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  378. if (api_get_setting('allow_social_tool') === 'true') {
  379. $tpl->assign('social_menu_block', $social_menu_block);
  380. $tpl->assign('social_right_content', $social_right_content);
  381. $social_layout = $tpl->get_template('social/inbox.tpl');
  382. $tpl->display($social_layout);
  383. } else {
  384. $content = $social_right_content;
  385. $tpl->assign('content', $content);
  386. $tpl->display_one_col_template();
  387. }