new_message.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /* For licensing terms, see /chamilo_license.txt */
  3. /**
  4. * This script shows a compose area (wysiwyg editor if supported, otherwise
  5. * a simple textarea) where the user can type a message.
  6. * There are three modes
  7. * - standard: type a message, select a user to send it to, press send
  8. * - reply on message (when pressing reply when viewing a message)
  9. * - send to specific user (when pressing send message in the who is online list)
  10. */
  11. /*
  12. ==============================================================================
  13. INIT SECTION
  14. ==============================================================================
  15. */
  16. // name of the language file that needs to be included
  17. $language_file= array('messages','userInfo');
  18. $cidReset=true;
  19. require_once '../inc/global.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).'text.lib.php';
  25. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  26. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  27. require_once api_get_path(LIBRARY_PATH).'message.lib.php';
  28. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  29. /*
  30. -----------------------------------------------------------
  31. Constants and variables
  32. -----------------------------------------------------------
  33. */
  34. $htmlHeadXtra[]='
  35. <script language="javascript">
  36. function validate(form,list) {
  37. if(list.selectedIndex<0)
  38. {
  39. alert("Please select someone to send the message to.")
  40. return false
  41. }
  42. else
  43. return true
  44. }
  45. </script>';
  46. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  47. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  48. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  49. $htmlHeadXtra[] = '<script type="text/javascript">
  50. $(document).ready(function (){
  51. $("#users").fcbkcomplete({
  52. json_url: "'.api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users",
  53. cache: false,
  54. filter_case: true,
  55. filter_hide: true,
  56. firstselected: true,
  57. //onremove: "testme",
  58. //onselect: "testme",
  59. filter_selected: true,
  60. newel: true
  61. });
  62. });
  63. var counter_image = 1;
  64. /*
  65. function remove_image_form(id_elem1) {
  66. var elem1 = document.getElementById(id_elem1);
  67. elem1.parentNode.removeChild(elem1);
  68. }
  69. */
  70. function add_image_form() {
  71. // Multiple filepaths for image form
  72. var filepaths = document.getElementById("filepaths");
  73. if (document.getElementById("filepath_"+counter_image)) {
  74. counter_image = counter_image + 1;
  75. } else {
  76. counter_image = counter_image;
  77. }
  78. var elem1 = document.createElement("div");
  79. elem1.setAttribute("id","filepath_"+counter_image);
  80. filepaths.appendChild(elem1);
  81. id_elem1 = "filepath_"+counter_image;
  82. id_elem1 = "\'"+id_elem1+"\'";
  83. //document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" size=\"20\" />&nbsp;<a href=\"javascript:remove_image_form("+id_elem1+")\"><img src=\"'.api_get_path(WEB_CODE_PATH).'img/delete.gif\"></a>";
  84. document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" size=\"28\" />&nbsp;<input type=\"text\" name=\"legend[]\" size=\"28\" /></a>";
  85. if (filepaths.childNodes.length == 6) {
  86. var link_attach = document.getElementById("link-more-attach");
  87. if (link_attach) {
  88. link_attach.innerHTML="";
  89. }
  90. }
  91. }
  92. </script>';
  93. $nameTools = get_lang('ComposeMessage');
  94. /*
  95. ==============================================================================
  96. FUNCTIONS
  97. ==============================================================================
  98. */
  99. /**
  100. * Shows the compose area + a list of users to select from.
  101. */
  102. function show_compose_to_any ($user_id) {
  103. $online_user_list = MessageManager::get_online_user_list($user_id);
  104. $default['user_list'] = 0;
  105. $online_user_list=null;
  106. manage_form($default, $online_user_list);
  107. }
  108. function show_compose_reply_to_message ($message_id, $receiver_id) {
  109. global $charset;
  110. $table_message = Database::get_main_table(TABLE_MESSAGE);
  111. $query = "SELECT user_sender_id FROM $table_message WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  112. $result = Database::query($query,__FILE__,__LINE__);
  113. $row = Database::fetch_array($result,'ASSOC');
  114. if (!isset($row['user_sender_id'])) {
  115. echo get_lang('InvalidMessageId');
  116. die();
  117. }
  118. $pre_html = '<div class="row">
  119. <div class="label">'.get_lang('SendMessageTo').'</div>
  120. <div class="formw">';
  121. $post = '</div></div>';
  122. $multi_select = '<select id="users" name="users">
  123. </select>';
  124. echo $pre_html.'<strong>'.GetFullUserName($row['user_sender_id']).'</strong>'.$post;
  125. //echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($row['user_sender_id']).'</strong>';
  126. //$default['title'] = get_lang('EnterTitle');
  127. $default['users'] = array($row['user_sender_id']);
  128. manage_form($default);
  129. }
  130. function show_compose_to_user ($receiver_id) {
  131. global $charset;
  132. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($receiver_id).'</strong>';
  133. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  134. $default['users'] = array($receiver_id);
  135. manage_form($default);
  136. }
  137. function manage_form ($default, $select_from_user_list = null) {
  138. global $charset;
  139. $table_message = Database::get_main_table(TABLE_MESSAGE);
  140. $group_id = intval($_REQUEST['group_id']);
  141. $message_id = intval($_GET['message_id']);
  142. $param_f = isset($_GET['f'])?Security::remove_XSS($_GET['f']):'';
  143. $form = new FormValidator('compose_message',null,api_get_self().'?f='.$param_f,null,array('enctype'=>'multipart/form-data'));
  144. if (empty($group_id)) {
  145. if (isset($select_from_user_list)) {
  146. $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'));
  147. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  148. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  149. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  150. } else {
  151. if (empty($default['users'])) {
  152. //the magic should be here
  153. $pre_html = '<div class="row">
  154. <div class="label">'.get_lang('SendMessageTo').'</div>
  155. <div class="formw">';
  156. $post = '</div></div>';
  157. $multi_select = '<select id="users" name="users">
  158. </select>';
  159. $form->addElement('html',$pre_html.$multi_select.$post );
  160. } else {
  161. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  162. }
  163. }
  164. } else {
  165. $group_info = GroupPortalManager::get_group_data($group_id);
  166. $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>');
  167. $form->addElement('hidden','group_id',$group_id);
  168. $form->addElement('hidden','parent_id',$message_id);
  169. }
  170. $form->add_textfield('title', get_lang('Title'),true ,array('size' => 77));
  171. $form->add_html_editor('content', get_lang('Message'), false, false, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  172. //$form->addElement('textarea','content', get_lang('Message'), array('cols' => 75,'rows'=>8));
  173. if (isset($_GET['re_id'])) {
  174. $form->addElement('hidden','re_id',Security::remove_XSS($_GET['re_id']));
  175. $form->addElement('hidden','save_form','save_form');
  176. }
  177. if (empty($group_id)) {
  178. $form->addElement('html','<div class="row"><div class="label">'.get_lang('FilesAttachment').'</div><div class="formw">
  179. <span id="filepaths">
  180. <div id="filepath_1">
  181. <input type="file" name="attach_1" size="28" />
  182. <input type="text" name="legend[]" size="28" />
  183. </div></span></div></div>');
  184. $form->addElement('html','<div class="row"><div class="formw"><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'))).')</div></div>');
  185. }
  186. $form->addElement('style_submit_button','compose',api_xml_http_response_encode(get_lang('SendMessage')),'class="save"');
  187. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  188. if (!empty($group_id) && !empty($message_id)) {
  189. $message_info = MessageManager::get_message_by_id($message_id);
  190. $default['title']=get_lang('Re:').api_html_entity_decode($message_info['title'],ENT_QUOTES,$charset);
  191. }
  192. $form->setDefaults($default);
  193. if ($form->validate()) {
  194. $check = Security::check_token('post');
  195. if ($check) {
  196. $values = $default;
  197. $user_list = $values['users'];
  198. $file_comments = $_POST['legend'];
  199. $title = $values['title'];
  200. $content = $values['content'];
  201. $group_id = $values['group_id'];
  202. $parent_id = $values['parent_id'];
  203. if (is_array($user_list) && count($user_list)> 0) {
  204. //all is well, send the message
  205. foreach ($user_list as $user) {
  206. $res = MessageManager::send_message($user, $title, $content, $_FILES, $file_comments, $group_id, $parent_id);
  207. if ($res) {
  208. if (is_string($res)) {
  209. Display::display_error_message($res);
  210. } else {
  211. MessageManager::display_success_message($user);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. Security::clear_token();
  218. } else {
  219. $token = Security::get_token();
  220. $form->addElement('hidden','sec_token');
  221. $form->setConstants(array('sec_token' => $token));
  222. $form->display();
  223. }
  224. }
  225. /*
  226. ==============================================================================
  227. MAIN SECTION
  228. ==============================================================================
  229. */
  230. if ($_GET['f']=='social') {
  231. $this_section = SECTION_SOCIAL;
  232. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/social/profile.php','name' => get_lang('Social'));
  233. $interbreadcrumb[]= array ('url' => 'outbox.php?f=social','name' => get_lang('Inbox'));
  234. } else {
  235. $this_section = SECTION_MYPROFILE;
  236. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/auth/profile.php','name' => get_lang('Profile'));
  237. $interbreadcrumb[]= array ('url' => 'outbox.php','name' => get_lang('Inbox'));
  238. }
  239. Display::display_header('');
  240. $group_id = intval($_REQUEST['group_id']);
  241. if ($group_id != 0) {
  242. echo '<div class=actions>';
  243. echo '<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'))).api_xml_http_response_encode(get_lang('BackToGroup')).'</a>';
  244. echo '<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'))).api_xml_http_response_encode(get_lang('ComposeMessage')).'</a>';
  245. echo '</div>';
  246. } else {
  247. if ($_GET['f']=='social') {
  248. $user_online_list = WhoIsOnline(api_get_setting('time_limit_whosonline'));
  249. $user_online_count = count($user_online_list);
  250. echo '<div class="actions-title-groups">';
  251. echo '<table width="100%"><tr><td width="150px" bgcolor="#32578b"><center><span class="menuTex1">'.strtoupper(get_lang('Menu')).'</span></center></td>
  252. <td width="15px">&nbsp;</td><td bgcolor="#32578b">'.Display::return_icon('whoisonline.png','',array('hspace'=>'6')).'<a href="#" ><span class="menuTex1">'.get_lang('FriendsOnline').' '.$user_online_count.'</span></a></td>
  253. </tr></table>';
  254. /*
  255. echo '<div class="menuTitle" align="center"><span class="menuTex1">'.get_lang('Menu').'</span></div>';
  256. echo '<div class="TitleRigth">'.Display::return_icon('whoisonline.png','',array('hspace'=>'6')).'<a href="#" ><span class="menuTex1">'.$who_is_on_line.'</span></a></div>';
  257. */
  258. echo '</div>';
  259. /*
  260. echo '<div class="actions-title">';
  261. echo get_lang('Messages');
  262. echo '</div>';
  263. */
  264. } else {
  265. echo '<div class=actions>';
  266. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  267. echo '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.Display::return_icon('shared_profile.png', get_lang('ViewSharedProfile')).'&nbsp;'.get_lang('ViewSharedProfile').'</a>';
  268. }
  269. if (api_get_setting('allow_message_tool') == 'true') {
  270. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png').' '.get_lang('Messages').'</a>';
  271. }
  272. echo '<a href="'.api_get_path(WEB_PATH).'main/auth/profile.php?type=reduced">'.Display::return_icon('edit.gif', get_lang('EditNormalProfile')).'&nbsp;'.get_lang('EditNormalProfile').'</a>';
  273. echo '</div>';
  274. }
  275. }
  276. echo '<div id="inbox-wrapper" >';
  277. $id_content_right = '';
  278. //LEFT COLUMN
  279. if (api_get_setting('allow_social_tool') != 'true') {
  280. $id_content_right = 'inbox';
  281. echo '<div id="inbox-menu" class="actions">';
  282. echo '<ul>';
  283. echo '<li><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png',get_lang('Inbox')).get_lang('Inbox').'</a>'.'</li>';
  284. echo '<li><a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php">'.Display::return_icon('message_new.png',get_lang('ComposeMessage')).get_lang('ComposeMessage').'</a>'.'</li>';
  285. echo '<li><a href="'.api_get_path(WEB_PATH).'main/messages/outbox.php">'.Display::return_icon('outbox.png',get_lang('Outbox')).get_lang('Outbox').'</a>'.'</li>';
  286. echo '</ul>';
  287. echo '</div>';
  288. } else {
  289. require_once api_get_path(LIBRARY_PATH).'social.lib.php';
  290. echo '<div id="socialContentLeft">';
  291. //this include the social menu div
  292. SocialManager::show_social_menu('messages');
  293. echo '</div>';
  294. $id_content_right = 'socialContentRigth';
  295. }
  296. echo '<div id="'.$id_content_right.'">';
  297. //MAIN CONTENT
  298. if (!isset($_POST['compose'])) {
  299. if(isset($_GET['re_id'])) {
  300. show_compose_reply_to_message($_GET['re_id'], api_get_user_id());
  301. } elseif(isset($_GET['send_to_user'])) {
  302. show_compose_to_user($_GET['send_to_user']);
  303. } else {
  304. show_compose_to_any($_user['user_id']);
  305. }
  306. } else {
  307. $restrict = false;
  308. if (isset($_POST['users'])) {
  309. $restrict = true;
  310. } elseif (isset($_POST['group_id'])) {
  311. $restrict = true;
  312. } elseif(isset($_POST['hidden_user'])) {
  313. $restrict = true;
  314. }
  315. $default['title'] = $_POST['title'];
  316. $default['content'] = $_POST['content'];
  317. // comes from a reply button
  318. if (isset($_GET['re_id'])) {
  319. manage_form($default);
  320. } else {
  321. // post
  322. if ($restrict) {
  323. if (!isset($_POST['group_id'])) {
  324. $default['users'] = $_POST['users'];
  325. } else {
  326. $default['group_id'] = $_POST['group_id'];
  327. }
  328. if (isset($_POST['hidden_user'])) {
  329. $default['users'] = array($_POST['hidden_user']);
  330. }
  331. manage_form($default);
  332. } else {
  333. Display::display_error_message(get_lang('ErrorSendingMessage'));
  334. }
  335. }
  336. }
  337. echo '</div>';
  338. echo '</div>';
  339. /*
  340. ==============================================================================
  341. FOOTER
  342. ==============================================================================
  343. */
  344. Display::display_footer();
  345. ?>