new_message.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/thickbox.js" type="text/javascript" language="javascript"></script>';
  48. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/thickbox.css" type="text/css" media="projection, screen">';
  49. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  50. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  51. $htmlHeadXtra[] = '<script type="text/javascript">
  52. $(document).ready(function (){
  53. $("#users").fcbkcomplete({
  54. json_url: "'.api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users",
  55. cache: false,
  56. filter_case: true,
  57. filter_hide: true,
  58. complete_text:"'.get_lang('StartToType').'",
  59. firstselected: true,
  60. //onremove: "testme",
  61. //onselect: "testme",
  62. filter_selected: true,
  63. newel: true
  64. });
  65. });
  66. var counter_image = 1;
  67. /*
  68. function remove_image_form(id_elem1) {
  69. var elem1 = document.getElementById(id_elem1);
  70. elem1.parentNode.removeChild(elem1);
  71. }
  72. */
  73. function add_image_form() {
  74. // Multiple filepaths for image form
  75. var filepaths = document.getElementById("filepaths");
  76. if (document.getElementById("filepath_"+counter_image)) {
  77. counter_image = counter_image + 1;
  78. } else {
  79. counter_image = counter_image;
  80. }
  81. var elem1 = document.createElement("div");
  82. elem1.setAttribute("id","filepath_"+counter_image);
  83. filepaths.appendChild(elem1);
  84. id_elem1 = "filepath_"+counter_image;
  85. id_elem1 = "\'"+id_elem1+"\'";
  86. //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>";
  87. document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" size=\"28\" />&nbsp;<input type=\"text\" name=\"legend[]\" size=\"28\" /></a>";
  88. if (filepaths.childNodes.length == 6) {
  89. var link_attach = document.getElementById("link-more-attach");
  90. if (link_attach) {
  91. link_attach.innerHTML="";
  92. }
  93. }
  94. }
  95. </script>';
  96. $nameTools = get_lang('ComposeMessage');
  97. /*
  98. ==============================================================================
  99. FUNCTIONS
  100. ==============================================================================
  101. */
  102. /**
  103. * Shows the compose area + a list of users to select from.
  104. */
  105. function show_compose_to_any ($user_id) {
  106. $online_user_list = MessageManager::get_online_user_list($user_id);
  107. $default['user_list'] = 0;
  108. $online_user_list=null;
  109. manage_form($default, $online_user_list);
  110. }
  111. function show_compose_reply_to_message ($message_id, $receiver_id) {
  112. global $charset;
  113. $table_message = Database::get_main_table(TABLE_MESSAGE);
  114. $query = "SELECT user_sender_id FROM $table_message WHERE user_receiver_id=".intval($receiver_id)." AND id='".intval($message_id)."';";
  115. $result = Database::query($query,__FILE__,__LINE__);
  116. $row = Database::fetch_array($result,'ASSOC');
  117. if (!isset($row['user_sender_id'])) {
  118. echo get_lang('InvalidMessageId');
  119. die();
  120. }
  121. $pre_html = '<div class="row">
  122. <div class="label">'.get_lang('SendMessageTo').'</div>
  123. <div class="formw">';
  124. $post = '</div></div>';
  125. $multi_select = '<select id="users" name="users">
  126. </select>';
  127. echo $pre_html.'<strong>'.GetFullUserName($row['user_sender_id']).'</strong>'.$post;
  128. //echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($row['user_sender_id']).'</strong>';
  129. //$default['title'] = get_lang('EnterTitle');
  130. $default['users'] = array($row['user_sender_id']);
  131. manage_form($default);
  132. }
  133. function show_compose_to_user ($receiver_id) {
  134. global $charset;
  135. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($receiver_id).'</strong>';
  136. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  137. $default['users'] = array($receiver_id);
  138. manage_form($default);
  139. }
  140. function manage_form ($default, $select_from_user_list = null) {
  141. global $charset;
  142. $table_message = Database::get_main_table(TABLE_MESSAGE);
  143. $group_id = intval($_REQUEST['group_id']);
  144. $message_id = intval($_GET['message_id']);
  145. $param_f = isset($_GET['f'])?Security::remove_XSS($_GET['f']):'';
  146. $form = new FormValidator('compose_message',null,api_get_self().'?f='.$param_f,null,array('enctype'=>'multipart/form-data'));
  147. if (empty($group_id)) {
  148. if (isset($select_from_user_list)) {
  149. $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'));
  150. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  151. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  152. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  153. } else {
  154. if (empty($default['users'])) {
  155. //the magic should be here
  156. $pre_html = '<div class="row">
  157. <div class="label">'.get_lang('SendMessageTo').'</div>
  158. <div class="formw">';
  159. $post = '</div></div>';
  160. $multi_select = '<select id="users" name="users">
  161. </select>';
  162. $form->addElement('html',$pre_html.$multi_select.$post );
  163. } else {
  164. $form->addElement('hidden','hidden_user',$default['users'][0],array('id'=>'hidden_user'));
  165. }
  166. }
  167. } else {
  168. $group_info = GroupPortalManager::get_group_data($group_id);
  169. $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>');
  170. $form->addElement('hidden','group_id',$group_id);
  171. $form->addElement('hidden','parent_id',$message_id);
  172. }
  173. $form->add_textfield('title', get_lang('Title'),true ,array('size' => 77));
  174. $form->add_html_editor('content', get_lang('Message'), false, false, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  175. //$form->addElement('textarea','content', get_lang('Message'), array('cols' => 75,'rows'=>8));
  176. if (isset($_GET['re_id'])) {
  177. $form->addElement('hidden','re_id',Security::remove_XSS($_GET['re_id']));
  178. $form->addElement('hidden','save_form','save_form');
  179. }
  180. if (empty($group_id)) {
  181. $form->addElement('html','<div class="row"><div class="label">'.get_lang('FilesAttachment').'</div><div class="formw">
  182. <span id="filepaths">
  183. <div id="filepath_1">
  184. <input type="file" name="attach_1" size="28" />
  185. <input type="text" name="legend[]" size="28" />
  186. </div></span></div></div>');
  187. $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>');
  188. }
  189. $form->addElement('style_submit_button','compose',api_xml_http_response_encode(get_lang('SendMessage')),'class="save"');
  190. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  191. if (!empty($group_id) && !empty($message_id)) {
  192. $message_info = MessageManager::get_message_by_id($message_id);
  193. $default['title']=get_lang('Re:').api_html_entity_decode($message_info['title'],ENT_QUOTES,$charset);
  194. }
  195. $form->setDefaults($default);
  196. if ($form->validate()) {
  197. $check = Security::check_token('post');
  198. if ($check) {
  199. $values = $default;
  200. $user_list = $values['users'];
  201. $file_comments = $_POST['legend'];
  202. $title = $values['title'];
  203. $content = $values['content'];
  204. $group_id = $values['group_id'];
  205. $parent_id = $values['parent_id'];
  206. if (is_array($user_list) && count($user_list)> 0) {
  207. //all is well, send the message
  208. foreach ($user_list as $user) {
  209. $res = MessageManager::send_message($user, $title, $content, $_FILES, $file_comments, $group_id, $parent_id);
  210. if ($res) {
  211. if (is_string($res)) {
  212. Display::display_error_message($res);
  213. } else {
  214. MessageManager::display_success_message($user);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. Security::clear_token();
  221. } else {
  222. $token = Security::get_token();
  223. $form->addElement('hidden','sec_token');
  224. $form->setConstants(array('sec_token' => $token));
  225. $form->display();
  226. }
  227. }
  228. /*
  229. ==============================================================================
  230. MAIN SECTION
  231. ==============================================================================
  232. */
  233. if ($_GET['f']=='social') {
  234. $this_section = SECTION_SOCIAL;
  235. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/social/home.php','name' => get_lang('Social'));
  236. $interbreadcrumb[]= array ('url' => '#','name' => $nameTools);
  237. } else {
  238. $this_section = SECTION_MYPROFILE;
  239. $interbreadcrumb[]= array ('url' => api_get_path(WEB_PATH).'main/auth/profile.php','name' => get_lang('Profile'));
  240. $interbreadcrumb[]= array ('url' => 'inbox.php','name' => get_lang('Inbox'));
  241. }
  242. Display::display_header('');
  243. $group_id = intval($_REQUEST['group_id']);
  244. if ($group_id != 0) {
  245. echo '<div class=actions>';
  246. 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>';
  247. 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>';
  248. echo '</div>';
  249. } else {
  250. if ($_GET['f']=='social') {
  251. } else {
  252. echo '<div class=actions>';
  253. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  254. 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>';
  255. }
  256. if (api_get_setting('allow_message_tool') == 'true') {
  257. echo '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.Display::return_icon('inbox.png').' '.get_lang('Messages').'</a>';
  258. }
  259. 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>';
  260. echo '</div>';
  261. }
  262. }
  263. echo '<div id="social-content" >';
  264. $id_content_right = '';
  265. //LEFT COLUMN
  266. if (api_get_setting('allow_social_tool') != 'true') {
  267. $id_content_right = 'inbox';
  268. echo '<div id="inbox-menu" class="actions">';
  269. echo '<ul>';
  270. 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>';
  271. 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>';
  272. 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>';
  273. echo '</ul>';
  274. echo '</div>';
  275. } else {
  276. require_once api_get_path(LIBRARY_PATH).'social.lib.php';
  277. echo '<div id="social-content-left">';
  278. //this include the social menu div
  279. SocialManager::show_social_menu('messages_compose');
  280. echo '</div>';
  281. $id_content_right = 'social-content-right';
  282. }
  283. echo '<div id="'.$id_content_right.'">';
  284. //MAIN CONTENT
  285. if (!isset($_POST['compose'])) {
  286. if(isset($_GET['re_id'])) {
  287. show_compose_reply_to_message($_GET['re_id'], api_get_user_id());
  288. } elseif(isset($_GET['send_to_user'])) {
  289. show_compose_to_user($_GET['send_to_user']);
  290. } else {
  291. show_compose_to_any($_user['user_id']);
  292. }
  293. } else {
  294. $restrict = false;
  295. if (isset($_POST['users'])) {
  296. $restrict = true;
  297. } elseif (isset($_POST['group_id'])) {
  298. $restrict = true;
  299. } elseif(isset($_POST['hidden_user'])) {
  300. $restrict = true;
  301. }
  302. $default['title'] = $_POST['title'];
  303. $default['content'] = $_POST['content'];
  304. // comes from a reply button
  305. if (isset($_GET['re_id'])) {
  306. manage_form($default);
  307. } else {
  308. // post
  309. if ($restrict) {
  310. if (!isset($_POST['group_id'])) {
  311. $default['users'] = $_POST['users'];
  312. } else {
  313. $default['group_id'] = $_POST['group_id'];
  314. }
  315. if (isset($_POST['hidden_user'])) {
  316. $default['users'] = array($_POST['hidden_user']);
  317. }
  318. manage_form($default);
  319. } else {
  320. Display::display_error_message(get_lang('ErrorSendingMessage'));
  321. }
  322. }
  323. }
  324. echo '</div>';
  325. echo '</div>';
  326. /*
  327. ==============================================================================
  328. FOOTER
  329. ==============================================================================
  330. */
  331. Display::display_footer();
  332. ?>