new_message.php 14 KB

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