new_message.php 15 KB

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