new_message.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 '../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 language="javascript">
  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. /*
  36. function remove_image_form(id_elem1) {
  37. var elem1 = document.getElementById(id_elem1);
  38. elem1.parentNode.removeChild(elem1);
  39. }
  40. */
  41. function add_image_form() {
  42. // Multiple filepaths for image form
  43. var filepaths = document.getElementById("filepaths");
  44. if (document.getElementById("filepath_"+counter_image)) {
  45. counter_image = counter_image + 1;
  46. } else {
  47. counter_image = counter_image;
  48. }
  49. var elem1 = document.createElement("div");
  50. elem1.setAttribute("id","filepath_"+counter_image);
  51. filepaths.appendChild(elem1);
  52. id_elem1 = "filepath_"+counter_image;
  53. id_elem1 = "\'"+id_elem1+"\'";
  54. 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 />";
  55. if (filepaths.childNodes.length == 6) {
  56. var link_attach = document.getElementById("link-more-attach");
  57. if (link_attach) {
  58. link_attach.innerHTML="";
  59. }
  60. }
  61. }
  62. </script>';
  63. $nameTools = get_lang('ComposeMessage');
  64. /* FUNCTIONS */
  65. /**
  66. * Shows the compose area + a list of users to select from.
  67. */
  68. function show_compose_to_any($user_id) {
  69. $online_user_list = MessageManager::get_online_user_list($user_id);
  70. $default['user_list'] = 0;
  71. $online_user_list=null;
  72. $html = manage_form($default, $online_user_list);
  73. return $html;
  74. }
  75. function show_compose_reply_to_message($message_id, $receiver_id)
  76. {
  77. $table_message = Database::get_main_table(TABLE_MESSAGE);
  78. $query = "SELECT user_sender_id FROM $table_message
  79. WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  80. $result = Database::query($query);
  81. $row = Database::fetch_array($result,'ASSOC');
  82. if (!isset($row['user_sender_id'])) {
  83. $html = get_lang('InvalidMessageId');
  84. return $html;
  85. }
  86. $userInfo = api_get_user_info($row['user_sender_id']);
  87. $default['users'] = array($row['user_sender_id']);
  88. $html = manage_form($default, null, $userInfo['complete_name']);
  89. return $html;
  90. }
  91. function show_compose_to_user ($receiver_id) {
  92. $html = get_lang('To').':&nbsp;<strong>'.GetFullUserName($receiver_id).'</strong>';
  93. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  94. $default['users'] = array($receiver_id);
  95. $html .= manage_form($default);
  96. return $html;
  97. }
  98. function manage_form($default, $select_from_user_list = null, $sent_to = null)
  99. {
  100. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  101. $message_id = isset($_GET['message_id']) ? intval($_GET['message_id']) : null;
  102. $param_f = isset($_GET['f']) && $_GET['f'] == 'social' ? 'social' : null;
  103. $form = new FormValidator('compose_message', null, api_get_self().'?f='.$param_f, null, array('enctype'=>'multipart/form-data'));
  104. if (empty($group_id)) {
  105. if (isset($select_from_user_list)) {
  106. $form->addText(
  107. 'id_text_name',
  108. get_lang('SendMessageTo'),
  109. true,
  110. array(
  111. 'id'=>'id_text_name',
  112. 'onkeyup'=>'send_request_and_search()',
  113. 'autocomplete'=>'off'
  114. )
  115. );
  116. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  117. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  118. $form->addElement('hidden','user_list', 0, array('id'=>'user_list'));
  119. } else {
  120. if (!empty($sent_to)) {
  121. $form->addLabel(get_lang('SendMessageTo'), $sent_to);
  122. }
  123. if (empty($default['users'])) {
  124. //fb select
  125. $form->addElement(
  126. 'select_ajax',
  127. 'users',
  128. get_lang('SendMessageTo'),
  129. array(),
  130. [
  131. 'multiple' => 'multiple',
  132. 'url' => api_get_path(WEB_AJAX_PATH) . 'message.ajax.php?a=find_users'
  133. ]
  134. );
  135. } else {
  136. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  137. }
  138. }
  139. } else {
  140. $userGroup = new UserGroup();
  141. $group_info = $userGroup->get($group_id);
  142. $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name']));
  143. $form->addElement('hidden','group_id',$group_id);
  144. $form->addElement('hidden','parent_id',$message_id);
  145. }
  146. $form->addText('title', get_lang('Subject'), true);
  147. $form->addHtmlEditor(
  148. 'content',
  149. get_lang('Message'),
  150. false,
  151. false,
  152. array('ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250')
  153. );
  154. if (isset($_GET['re_id'])) {
  155. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  156. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_reply_info['title'];
  157. $form->addElement('hidden','re_id', intval($_GET['re_id']));
  158. $form->addElement('hidden','save_form','save_form');
  159. //adding reply mail
  160. $user_reply_info = api_get_user_info($message_reply_info['user_sender_id']);
  161. $default['content'] = '<p><br/></p>'.sprintf(
  162. get_lang('XWroteY'),
  163. $user_reply_info['complete_name'],
  164. Security::filter_terms($message_reply_info['content'])
  165. );
  166. }
  167. if (empty($group_id)) {
  168. $form->addElement('label', '', '<div id="filepaths" class="form-group">
  169. <div id="filepath_1">
  170. <label>'.get_lang('FilesAttachment').'</label>
  171. <input type="file" name="attach_1"/>
  172. <label>'.get_lang('Description').'</label>
  173. <input id="file-descrtiption" type="text" name="legend[]" class="form-control"/>
  174. </div>
  175. </div>'
  176. );
  177. $form->addElement('label', '', '<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'))).')');
  178. }
  179. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  180. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  181. if (!empty($group_id) && !empty($message_id)) {
  182. $message_info = MessageManager::get_message_by_id($message_id);
  183. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  184. }
  185. $form->setDefaults($default);
  186. $html = '';
  187. if ($form->validate()) {
  188. $check = Security::check_token('post');
  189. if ($check) {
  190. $user_list = $default['users'];
  191. $file_comments = $_POST['legend'];
  192. $title = $default['title'];
  193. $content = $default['content'];
  194. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  195. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  196. if (is_array($user_list) && count($user_list)> 0) {
  197. //all is well, send the message
  198. foreach ($user_list as $user) {
  199. $res = MessageManager::send_message(
  200. $user,
  201. $title,
  202. $content,
  203. $_FILES,
  204. $file_comments,
  205. $group_id,
  206. $parent_id
  207. );
  208. if ($res) {
  209. $html .= MessageManager::display_success_message($user);
  210. }
  211. }
  212. } else {
  213. Display::display_error_message('ErrorSendingMessage');
  214. }
  215. }
  216. Security::clear_token();
  217. } else {
  218. $token = Security::get_token();
  219. $form->addElement('hidden','sec_token');
  220. $form->setConstants(array('sec_token' => $token));
  221. $html .= $form->returnForm();
  222. }
  223. return $html;
  224. }
  225. $socialToolIsActive = isset($_GET['f']) && $_GET['f'] == 'social';
  226. /* MAIN SECTION */
  227. if ($socialToolIsActive) {
  228. $this_section = SECTION_SOCIAL;
  229. $interbreadcrumb[] = array(
  230. 'url' => api_get_path(WEB_PATH).'main/social/home.php',
  231. 'name' => get_lang('SocialNetwork'),
  232. );
  233. } else {
  234. $this_section = SECTION_MYPROFILE;
  235. $interbreadcrumb[] = array(
  236. 'url' => api_get_path(WEB_PATH).'main/auth/profile.php',
  237. 'name' => get_lang('Profile'),
  238. );
  239. }
  240. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  241. $social_right_content = 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/group_view.php?id='.$group_id.'">'.
  245. Display::return_icon('back.png',api_xml_http_response_encode(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',api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  248. $social_right_content .= '</div>';
  249. } else {
  250. if ($socialToolIsActive) {
  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. // LEFT COLUMN
  269. $social_left_content = null;
  270. if (api_get_setting('allow_social_tool') == 'true') {
  271. //Block Social Menu
  272. $social_menu_block = SocialManager::show_social_menu('messages');
  273. $social_right_content .= '<div class="row">';
  274. $social_right_content .= '<div class="col-md-12">';
  275. $social_right_content .= '<div class="actions">';
  276. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.
  277. Display::return_icon('back.png', get_lang('Back'), array(), 32).'</a>';
  278. $social_right_content .= '</div>';
  279. $social_right_content .= '</div>';
  280. $social_right_content .= '<div class="col-md-12">';
  281. }
  282. // MAIN CONTENT
  283. if (!isset($_POST['compose'])) {
  284. if(isset($_GET['re_id'])) {
  285. $social_right_content .= show_compose_reply_to_message(
  286. $_GET['re_id'],
  287. api_get_user_id()
  288. );
  289. } elseif(isset($_GET['send_to_user'])) {
  290. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  291. } else {
  292. $social_right_content .= show_compose_to_any(api_get_user_id());
  293. }
  294. } else {
  295. $restrict = false;
  296. if (isset($_POST['users'])) {
  297. $restrict = true;
  298. } elseif (isset($_POST['group_id'])) {
  299. $restrict = true;
  300. } elseif(isset($_POST['hidden_user'])) {
  301. $restrict = true;
  302. }
  303. $default['title'] = $_POST['title'];
  304. $default['content'] = $_POST['content'];
  305. // comes from a reply button
  306. if (isset($_GET['re_id'])) {
  307. $social_right_content .= manage_form($default);
  308. } else {
  309. // post
  310. if ($restrict) {
  311. if (!isset($_POST['group_id'])) {
  312. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  313. } else {
  314. $default['group_id'] = $_POST['group_id'];
  315. }
  316. if (isset($_POST['hidden_user'])) {
  317. $default['users'] = array($_POST['hidden_user']);
  318. }
  319. $social_right_content .= manage_form($default);
  320. } else {
  321. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'),'error');
  322. }
  323. }
  324. }
  325. if (api_get_setting('allow_social_tool') == 'true') {
  326. $social_right_content .= '</div>';
  327. $social_right_content .= '</div>';
  328. }
  329. $tpl = new Template(get_lang('ComposeMessage'));
  330. // Block Social Avatar
  331. SocialManager::setSocialUserBlock($tpl, $user_id, 'messages');
  332. if (api_get_setting('allow_social_tool') == 'true') {
  333. $tpl->assign('social_menu_block', $social_menu_block);
  334. $tpl->assign('social_right_content', $social_right_content);
  335. $social_layout = $tpl->get_template('social/inbox.tpl');
  336. $tpl->display($social_layout);
  337. } else {
  338. $content = $social_right_content;
  339. //$tpl->assign('actions', $actions);
  340. //$tpl->assign('message', $show_message);
  341. $tpl->assign('content', $content);
  342. $tpl->display_one_col_template();
  343. }