new_message.php 15 KB

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