new_message.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.messages
  6. */
  7. /**
  8. * This script shows a compose area (wysiwyg editor if supported, otherwise
  9. * a simple textarea) where the user can type a message.
  10. * There are three modes
  11. * - standard: type a message, select a user to send it to, press send
  12. * - reply on message (when pressing reply when viewing a message)
  13. * - send to specific user (when pressing send message in the who is online list)
  14. */
  15. $cidReset = true;
  16. //require_once '../inc/global.inc.php';
  17. api_block_anonymous_users();
  18. if (api_get_setting('message.allow_message_tool') != 'true') {
  19. api_not_allowed();
  20. }
  21. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  22. /* Constants and variables */
  23. $htmlHeadXtra[]='
  24. <script language="javascript">
  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>
  35. var counter_image = 1;
  36. /*
  37. function remove_image_form(id_elem1) {
  38. var elem1 = document.getElementById(id_elem1);
  39. elem1.parentNode.removeChild(elem1);
  40. }
  41. */
  42. function add_image_form() {
  43. // Multiple filepaths for image form
  44. var filepaths = document.getElementById("filepaths");
  45. if (document.getElementById("filepath_"+counter_image)) {
  46. counter_image = counter_image + 1;
  47. } else {
  48. counter_image = counter_image;
  49. }
  50. var elem1 = document.createElement("div");
  51. elem1.setAttribute("id","filepath_"+counter_image);
  52. filepaths.appendChild(elem1);
  53. id_elem1 = "filepath_"+counter_image;
  54. id_elem1 = "\'"+id_elem1+"\'";
  55. 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 />";
  56. if (filepaths.childNodes.length == 6) {
  57. var link_attach = document.getElementById("link-more-attach");
  58. if (link_attach) {
  59. link_attach.innerHTML="";
  60. }
  61. }
  62. }
  63. </script>';
  64. $nameTools = get_lang('ComposeMessage');
  65. /* FUNCTIONS */
  66. /**
  67. * Shows the compose area + a list of users to select from.
  68. */
  69. function show_compose_to_any($user_id) {
  70. $online_user_list = MessageManager::get_online_user_list($user_id);
  71. $default['user_list'] = 0;
  72. $online_user_list=null;
  73. $html = manage_form($default, $online_user_list);
  74. return $html;
  75. }
  76. function show_compose_reply_to_message($message_id, $receiver_id)
  77. {
  78. $table_message = Database::get_main_table(TABLE_MESSAGE);
  79. $query = "SELECT user_sender_id FROM $table_message
  80. WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  81. $result = Database::query($query);
  82. $row = Database::fetch_array($result,'ASSOC');
  83. if (!isset($row['user_sender_id'])) {
  84. $html = get_lang('InvalidMessageId');
  85. return $html;
  86. }
  87. $userInfo = api_get_user_info($row['user_sender_id']);
  88. $default['users'] = array($row['user_sender_id']);
  89. $html = manage_form($default, null, $userInfo['complete_name']);
  90. return $html;
  91. }
  92. function show_compose_to_user ($receiver_id) {
  93. $userInfo = api_get_user_info($receiver_id);
  94. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  95. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  96. $default['users'] = array($receiver_id);
  97. $html .= manage_form($default);
  98. return $html;
  99. }
  100. function manage_form($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('compose_message', null, api_get_self().'?f='.$param_f, null, array('enctype'=>'multipart/form-data'));
  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('label', '', '<div id="filepaths" class="form-group">
  171. <div id="filepath_1">
  172. <label>'.get_lang('FilesAttachment').'</label>
  173. <input type="file" name="attach_1"/>
  174. <label>'.get_lang('Description').'</label>
  175. <input id="file-descrtiption" type="text" name="legend[]" class="form-control"/>
  176. </div>
  177. </div>'
  178. );
  179. $form->addElement(
  180. 'label',
  181. '',
  182. '<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.
  183. get_lang('AddOneMoreFile').'</a></span>&nbsp;('.
  184. sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message.message_max_upload_filesize'))).')'
  185. );
  186. }
  187. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  188. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  189. if (!empty($group_id) && !empty($message_id)) {
  190. $message_info = MessageManager::get_message_by_id($message_id);
  191. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  192. }
  193. $form->setDefaults($default);
  194. $html = '';
  195. if ($form->validate()) {
  196. $check = Security::check_token('post');
  197. if ($check) {
  198. $user_list = $default['users'];
  199. $file_comments = $_POST['legend'];
  200. $title = $default['title'];
  201. $content = $default['content'];
  202. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  203. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  204. if (is_array($user_list) && count($user_list)> 0) {
  205. //all is well, send the message
  206. foreach ($user_list as $user) {
  207. $res = MessageManager::send_message(
  208. $user,
  209. $title,
  210. $content,
  211. $_FILES,
  212. $file_comments,
  213. $group_id,
  214. $parent_id
  215. );
  216. if ($res) {
  217. $html .= MessageManager::display_success_message($user);
  218. }
  219. }
  220. } else {
  221. Display::display_error_message('ErrorSendingMessage');
  222. }
  223. }
  224. Security::clear_token();
  225. } else {
  226. $token = Security::get_token();
  227. $form->addElement('hidden','sec_token');
  228. $form->setConstants(array('sec_token' => $token));
  229. $html .= $form->returnForm();
  230. }
  231. return $html;
  232. }
  233. $socialToolIsActive = isset($_GET['f']) && $_GET['f'] == 'social';
  234. /* MAIN SECTION */
  235. if ($socialToolIsActive) {
  236. $this_section = SECTION_SOCIAL;
  237. $interbreadcrumb[] = array(
  238. 'url' => api_get_path(WEB_PATH).'main/social/home.php',
  239. 'name' => get_lang('SocialNetwork'),
  240. );
  241. } else {
  242. $this_section = SECTION_MYPROFILE;
  243. $interbreadcrumb[] = array(
  244. 'url' => Container::getRouter()->generate('fos_user_profile_edit'),
  245. 'name' => get_lang('Profile'),
  246. );
  247. }
  248. $group_id = isset($_REQUEST['group_id']) ? intval($_REQUEST['group_id']) : null;
  249. $social_right_content = null;
  250. if ($group_id != 0) {
  251. $social_right_content .= '<div class=actions>';
  252. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/group_view.php?id='.$group_id.'">'.
  253. Display::return_icon('back.png',api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  254. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php?group_id='.$group_id.'">'.
  255. Display::return_icon('message_new.png',api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  256. $social_right_content .= '</div>';
  257. } else {
  258. if ($socialToolIsActive) {
  259. } else {
  260. $social_right_content .= '<div class=actions>';
  261. if (api_get_setting(
  262. 'social.allow_social_tool'
  263. ) == 'true' && api_get_setting(
  264. 'message.allow_message_tool'
  265. ) == 'true'
  266. ) {
  267. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.
  268. Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'</a>';
  269. }
  270. if (api_get_setting('message.allow_message_tool') == 'true') {
  271. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.
  272. Display::return_icon('message_new.png',get_lang('ComposeMessage')).'</a>';
  273. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  274. Display::return_icon('inbox.png',get_lang('Inbox')).'</a>';
  275. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.
  276. Display::return_icon('outbox.png',get_lang('Outbox')).'</a>';
  277. }
  278. $social_right_content .= '</div>';
  279. }
  280. }
  281. // LEFT COLUMN
  282. $social_left_content = null;
  283. if (api_get_setting('social.allow_social_tool') == 'true') {
  284. //Block Social Menu
  285. $social_menu_block = SocialManager::show_social_menu('messages');
  286. $social_right_content .= '<div class="row">';
  287. $social_right_content .= '<div class="col-md-12">';
  288. $social_right_content .= '<div class="actions">';
  289. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.
  290. Display::return_icon('back.png', get_lang('Back'), array(), 32).'</a>';
  291. $social_right_content .= '</div>';
  292. $social_right_content .= '</div>';
  293. $social_right_content .= '<div class="col-md-12">';
  294. }
  295. // MAIN CONTENT
  296. if (!isset($_POST['compose'])) {
  297. if(isset($_GET['re_id'])) {
  298. $social_right_content .= show_compose_reply_to_message(
  299. $_GET['re_id'],
  300. api_get_user_id()
  301. );
  302. } elseif(isset($_GET['send_to_user'])) {
  303. $social_right_content .= show_compose_to_user($_GET['send_to_user']);
  304. } else {
  305. $social_right_content .= show_compose_to_any(api_get_user_id());
  306. }
  307. } else {
  308. $restrict = false;
  309. if (isset($_POST['users'])) {
  310. $restrict = true;
  311. } elseif (isset($_POST['group_id'])) {
  312. $restrict = true;
  313. } elseif(isset($_POST['hidden_user'])) {
  314. $restrict = true;
  315. }
  316. $default['title'] = $_POST['title'];
  317. $default['content'] = $_POST['content'];
  318. // comes from a reply button
  319. if (isset($_GET['re_id'])) {
  320. $social_right_content .= manage_form($default);
  321. } else {
  322. // post
  323. if ($restrict) {
  324. if (!isset($_POST['group_id'])) {
  325. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  326. } else {
  327. $default['group_id'] = $_POST['group_id'];
  328. }
  329. if (isset($_POST['hidden_user'])) {
  330. $default['users'] = array($_POST['hidden_user']);
  331. }
  332. $social_right_content .= manage_form($default);
  333. } else {
  334. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'),'error');
  335. }
  336. }
  337. }
  338. if (api_get_setting('social.allow_social_tool') == 'true') {
  339. $social_right_content .= '</div>';
  340. $social_right_content .= '</div>';
  341. }
  342. //$tpl = new Template(get_lang('ComposeMessage'));
  343. $tpl = \Chamilo\CoreBundle\Framework\Container::getTwig();
  344. // Block Social Avatar
  345. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  346. if (api_get_setting('social.allow_social_tool') == 'true') {
  347. $tpl->addGlobal('social_menu_block', $social_menu_block);
  348. $tpl->addGlobal('social_right_content', $social_right_content);
  349. echo $tpl->render('@template_style/social/inbox.html.twig');
  350. } else {
  351. $content = $social_right_content;
  352. echo $actions;
  353. echo $content;
  354. }