new_message.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = manage_form($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 = manage_form($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 .= manage_form($default);
  92. return $html;
  93. }
  94. function manage_form($default, $select_from_user_list = null, $sent_to = null)
  95. {
  96. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  97. $message_id = isset($_GET['message_id']) ? intval($_GET['message_id']) : null;
  98. $param_f = isset($_GET['f']) && $_GET['f'] == 'social' ? 'social' : null;
  99. $form = new FormValidator(
  100. 'compose_message',
  101. null,
  102. api_get_self().'?f='.$param_f,
  103. null,
  104. array('enctype' => 'multipart/form-data')
  105. );
  106. if (empty($group_id)) {
  107. if (isset($select_from_user_list)) {
  108. $form->addText(
  109. 'id_text_name',
  110. get_lang('SendMessageTo'),
  111. true,
  112. array(
  113. 'id'=>'id_text_name',
  114. 'onkeyup'=>'send_request_and_search()',
  115. 'autocomplete'=>'off'
  116. )
  117. );
  118. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  119. $form->addElement('html', '<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  120. $form->addElement('hidden', 'user_list', 0, array('id'=>'user_list'));
  121. } else {
  122. if (!empty($sent_to)) {
  123. $form->addLabel(get_lang('SendMessageTo'), $sent_to);
  124. }
  125. if (empty($default['users'])) {
  126. //fb select
  127. $form->addElement(
  128. 'select_ajax',
  129. 'users',
  130. get_lang('SendMessageTo'),
  131. array(),
  132. [
  133. 'multiple' => 'multiple',
  134. 'url' => api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users'
  135. ]
  136. );
  137. } else {
  138. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  139. }
  140. }
  141. } else {
  142. $userGroup = new UserGroup();
  143. $group_info = $userGroup->get($group_id);
  144. $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name']));
  145. $form->addElement('hidden','group_id',$group_id);
  146. $form->addElement('hidden','parent_id',$message_id);
  147. }
  148. $form->addText('title', get_lang('Subject'), true);
  149. $form->addHtmlEditor(
  150. 'content',
  151. get_lang('Message'),
  152. false,
  153. false,
  154. array('ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250')
  155. );
  156. if (isset($_GET['re_id'])) {
  157. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  158. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_reply_info['title'];
  159. $form->addElement('hidden', 're_id', intval($_GET['re_id']));
  160. $form->addElement('hidden', 'save_form', 'save_form');
  161. // Adding reply mail
  162. $user_reply_info = api_get_user_info($message_reply_info['user_sender_id']);
  163. $default['content'] = '<p><br/></p>'.sprintf(
  164. get_lang('XWroteY'),
  165. $user_reply_info['complete_name'],
  166. Security::filter_terms($message_reply_info['content'])
  167. );
  168. }
  169. if (empty($group_id)) {
  170. $form->addElement(
  171. 'label',
  172. '',
  173. '<div id="file_uploads"><div id="filepath_1">
  174. <div id="filepaths" class="form-horizontal">
  175. <div id="paths-file" class="form-group">
  176. <label class="col-sm-4">'.get_lang('FilesAttachment').'</label>
  177. <input class="col-sm-8" type="file" name="attach_1"/>
  178. </div>
  179. </div>
  180. <div id="paths-description" class="form-group">
  181. <label class="col-sm-4">'.get_lang('Description').'</label>
  182. <div class="col-sm-8">
  183. <input id="file-descrtiption" style="width:100%;" type="text" name="legend[]" />
  184. </div>
  185. </div>
  186. </div>
  187. </div>
  188. '
  189. );
  190. $form->addLabel(
  191. '',
  192. '<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'))).')'
  193. );
  194. }
  195. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  196. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  197. if (!empty($group_id) && !empty($message_id)) {
  198. $message_info = MessageManager::get_message_by_id($message_id);
  199. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  200. }
  201. if (isset($_GET['prefill'])) {
  202. switch ($_GET['prefill']) {
  203. case 'ofaj':
  204. $sendToUser = isset($_GET['send_to_user']) ? $_GET['send_to_user'] : 0;
  205. $sendToUserFullName = '';
  206. if ($sendToUser) {
  207. $sendToUserInfo = api_get_user_info($sendToUser);
  208. $sendToUserFirstname = $sendToUserInfo['firstname'];
  209. }
  210. $currentUserInfo = api_get_user_info();
  211. $default['title'] = get_lang('OfajEndLPSubject');
  212. $default['content'] = sprintf(
  213. get_lang('OfajEndLpDescription'),
  214. $sendToUserFirstname,
  215. $currentUserInfo['complete_name'],
  216. $currentUserInfo['complete_name']
  217. );
  218. break;
  219. }
  220. }
  221. $form->setDefaults($default);
  222. $html = '';
  223. if ($form->validate()) {
  224. $check = Security::check_token('post');
  225. if ($check) {
  226. $user_list = $default['users'];
  227. $file_comments = $_POST['legend'];
  228. $title = $default['title'];
  229. $content = $default['content'];
  230. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  231. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  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. );
  244. if ($res) {
  245. $userInfo = api_get_user_info($userId);
  246. Display::addFlash(Display::return_message(
  247. get_lang('MessageSentTo')."&nbsp;<b>".$userInfo['complete_name']."</b>",
  248. 'confirmation',
  249. false
  250. ));
  251. }
  252. }
  253. } else {
  254. Display::addFlash(Display::return_message('ErrorSendingMessage', 'error'));
  255. }
  256. }
  257. Security::clear_token();
  258. } else {
  259. $token = Security::get_token();
  260. $form->addElement('hidden', 'sec_token');
  261. $form->setConstants(array('sec_token' => $token));
  262. $html .= $form->returnForm();
  263. }
  264. return $html;
  265. }
  266. $socialToolIsActive = isset($_GET['f']) && $_GET['f'] == 'social';
  267. /* MAIN SECTION */
  268. if ($socialToolIsActive) {
  269. $this_section = SECTION_SOCIAL;
  270. $interbreadcrumb[] = array(
  271. 'url' => api_get_path(WEB_PATH).'main/social/home.php',
  272. 'name' => get_lang('SocialNetwork')
  273. );
  274. } else {
  275. $this_section = SECTION_MYPROFILE;
  276. $interbreadcrumb[] = array(
  277. 'url' => api_get_path(WEB_PATH).'main/auth/profile.php',
  278. 'name' => get_lang('Profile')
  279. );
  280. }
  281. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  282. $social_right_content = null;
  283. if ($group_id != 0) {
  284. $social_right_content .= '<div class=actions>';
  285. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/group_view.php?id='.$group_id.'">'.
  286. Display::return_icon('back.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  287. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php?group_id='.$group_id.'">'.
  288. Display::return_icon('message_new.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  289. $social_right_content .= '</div>';
  290. } else {
  291. if ($socialToolIsActive) {
  292. } else {
  293. $social_right_content .= '<div class=actions>';
  294. if (api_get_setting('allow_social_tool') === 'true' && api_get_setting('allow_message_tool') === 'true') {
  295. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  296. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  297. }
  298. if (api_get_setting('allow_message_tool') === 'true') {
  299. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.
  300. Display::return_icon('message_new.png', get_lang('ComposeMessage')).'</a>';
  301. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  302. Display::return_icon('inbox.png', get_lang('Inbox')).'</a>';
  303. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.
  304. Display::return_icon('outbox.png', get_lang('Outbox')).'</a>';
  305. }
  306. $social_right_content .= '</div>';
  307. }
  308. }
  309. // LEFT COLUMN
  310. $social_left_content = null;
  311. if (api_get_setting('allow_social_tool') == 'true') {
  312. //Block Social Menu
  313. $social_menu_block = SocialManager::show_social_menu('messages');
  314. $social_right_content .= '<div class="row">';
  315. $social_right_content .= '<div class="col-md-12">';
  316. $social_right_content .= '<div class="actions">';
  317. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.
  318. Display::return_icon('back.png', get_lang('Back'), array(), 32).'</a>';
  319. $social_right_content .= '</div>';
  320. $social_right_content .= '</div>';
  321. $social_right_content .= '<div class="col-md-12">';
  322. }
  323. // MAIN CONTENT
  324. if (!isset($_POST['compose'])) {
  325. if (isset($_GET['re_id'])) {
  326. $social_right_content .= show_compose_reply_to_message(
  327. $_GET['re_id'],
  328. api_get_user_id()
  329. );
  330. } elseif (isset($_GET['send_to_user'])) {
  331. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  332. } else {
  333. $social_right_content .= show_compose_to_any(api_get_user_id());
  334. }
  335. } else {
  336. $restrict = false;
  337. if (isset($_POST['users'])) {
  338. $restrict = true;
  339. } elseif (isset($_POST['group_id'])) {
  340. $restrict = true;
  341. } elseif (isset($_POST['hidden_user'])) {
  342. $restrict = true;
  343. }
  344. $default['title'] = $_POST['title'];
  345. $default['content'] = $_POST['content'];
  346. // comes from a reply button
  347. if (isset($_GET['re_id'])) {
  348. $social_right_content .= manage_form($default);
  349. } else {
  350. // post
  351. if ($restrict) {
  352. if (!isset($_POST['group_id'])) {
  353. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  354. } else {
  355. $default['group_id'] = $_POST['group_id'];
  356. }
  357. if (isset($_POST['hidden_user'])) {
  358. $default['users'] = array($_POST['hidden_user']);
  359. }
  360. $social_right_content .= manage_form($default);
  361. } else {
  362. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  363. }
  364. }
  365. }
  366. if (api_get_setting('allow_social_tool') === 'true') {
  367. $social_right_content .= '</div>';
  368. $social_right_content .= '</div>';
  369. }
  370. $tpl = new Template(get_lang('ComposeMessage'));
  371. // Block Social Avatar
  372. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  373. if (api_get_setting('allow_social_tool') === 'true') {
  374. $tpl->assign('social_menu_block', $social_menu_block);
  375. $tpl->assign('social_right_content', $social_right_content);
  376. $social_layout = $tpl->get_template('social/inbox.tpl');
  377. $tpl->display($social_layout);
  378. } else {
  379. $content = $social_right_content;
  380. $tpl->assign('content', $content);
  381. $tpl->display_one_col_template();
  382. }