message.lib.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for messages management.
  5. * Include/require it in your code to use its features.
  6. *
  7. * @package chamilo.library
  8. */
  9. /**
  10. * Code
  11. */
  12. require_once api_get_path(LIBRARY_PATH).'online.inc.php';
  13. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  15. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  16. /*
  17. * @todo use constants!
  18. */
  19. define('MESSAGE_STATUS_NEW', '0');
  20. define('MESSAGE_STATUS_UNREAD', '1');
  21. //2 ??
  22. define('MESSAGE_STATUS_DELETED', '3');
  23. define('MESSAGE_STATUS_OUTBOX', '4');
  24. define('MESSAGE_STATUS_INVITATION_PENDING', '5');
  25. define('MESSAGE_STATUS_INVITATION_ACCEPTED','6');
  26. define('MESSAGE_STATUS_INVITATION_DENIED', '7');
  27. /**
  28. * Class
  29. * @package chamilo.library
  30. */
  31. class MessageManager
  32. {
  33. public static function get_online_user_list($current_user_id) {
  34. $min=30;
  35. global $_configuration;
  36. $userlist = who_is_online($min, true);
  37. $online_user_list = array();
  38. foreach($userlist as $row) {
  39. $receiver_id = $row[0];
  40. $online_user_list[$receiver_id] = GetFullUserName($receiver_id).($current_user_id==$receiver_id?("&nbsp;(".get_lang('Myself').")"):(""));
  41. }
  42. return $online_user_list;
  43. }
  44. /**
  45. * Displays info stating that the message is sent successfully.
  46. */
  47. public static function display_success_message($uid) {
  48. global $charset;
  49. if ($_SESSION['social_exist']===true) {
  50. $redirect="#remote-tab-2";
  51. if (api_get_setting('allow_social_tool')=='true' && api_get_setting('allow_message_tool')=='true') {
  52. $success=get_lang('MessageSentTo').
  53. "&nbsp;<b>".
  54. GetFullUserName($uid).
  55. "</b>";
  56. }else {
  57. $success=get_lang('MessageSentTo').
  58. "&nbsp;<b>".
  59. GetFullUserName($uid).
  60. "</b>";
  61. }
  62. } else {
  63. $success=get_lang('MessageSentTo').
  64. "&nbsp;<b>".
  65. GetFullUserName($uid).
  66. "</b>";
  67. }
  68. Display::display_confirmation_message(api_xml_http_response_encode($success), false);
  69. }
  70. /**
  71. * Displays the wysiwyg html editor.
  72. */
  73. public static function display_html_editor_area($name, $resp) {
  74. api_disp_html_area($name, get_lang('TypeYourMessage'), '', '', null, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  75. }
  76. /**
  77. * Get the new messages for the current user from the database.
  78. */
  79. public static function get_new_messages() {
  80. $table_message = Database::get_main_table(TABLE_MESSAGE);
  81. if (!api_get_user_id()) {
  82. return false;
  83. }
  84. $i=0;
  85. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".api_get_user_id()." AND msg_status=".MESSAGE_STATUS_UNREAD;
  86. $result = Database::query($query);
  87. $i = Database::num_rows($result);
  88. return $i;
  89. }
  90. /**
  91. * Get the list of user_ids of users who are online.
  92. */
  93. public static function users_connected_by_id() {
  94. global $_configuration, $_user;
  95. $minute=30;
  96. $user_connect = who_is_online($minute, true);
  97. for ($i=0; $i<count($user_connect); $i++) {
  98. $user_id_list[$i]=$user_connect[$i][0];
  99. }
  100. return $user_id_list;
  101. }
  102. /**
  103. * Gets the total number of messages, used for the inbox sortable table
  104. */
  105. public static function get_number_of_messages ($unread = false) {
  106. $table_message = Database::get_main_table(TABLE_MESSAGE);
  107. $condition_msg_status = '';
  108. if ($unread) {
  109. $condition_msg_status = ' msg_status = '.MESSAGE_STATUS_UNREAD.' ';
  110. } else {
  111. $condition_msg_status = ' msg_status IN('.MESSAGE_STATUS_NEW.','.MESSAGE_STATUS_UNREAD.') ';
  112. }
  113. $sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE $condition_msg_status AND user_receiver_id=".api_get_user_id();
  114. $sql_result = Database::query($sql_query);
  115. $result = Database::fetch_array($sql_result);
  116. return $result['number_messages'];
  117. }
  118. /**
  119. * Gets information about some messages, used for the inbox sortable table
  120. * @param int $from
  121. * @param int $number_of_items
  122. * @param string $direction
  123. */
  124. public static function get_message_data($from, $number_of_items, $column, $direction) {
  125. global $charset;
  126. $from = intval($from);
  127. $number_of_items = intval($number_of_items);
  128. //forcing this order
  129. if (!isset($direction)) {
  130. $column = 3;
  131. $direction = 'DESC';
  132. } else {
  133. $column = intval($column);
  134. if (!in_array($direction, array('ASC', 'DESC')))
  135. $direction = 'ASC';
  136. }
  137. $table_message = Database::get_main_table(TABLE_MESSAGE);
  138. $request=api_is_xml_http_request();
  139. $sql_query = "SELECT id as col0, user_sender_id as col1, title as col2, send_date as col3, msg_status as col4 FROM $table_message " .
  140. " WHERE user_receiver_id=".api_get_user_id()." AND msg_status IN (0,1)" .
  141. " ORDER BY col$column $direction LIMIT $from,$number_of_items";
  142. $sql_result = Database::query($sql_query);
  143. $i = 0;
  144. $message_list = array ();
  145. while ($result = Database::fetch_row($sql_result)) {
  146. if ($request===true) {
  147. $message[0] = '<input type="checkbox" value='.$result[0].' name="id[]">';
  148. } else {
  149. $message[0] = ($result[0]);
  150. }
  151. $result[2] = Security::remove_XSS($result[2]);
  152. $result[2] = cut($result[2], 80,true);
  153. if ($request===true) {
  154. /*if($result[4]==0) {
  155. $message[1] = Display::return_icon('mail_open.png',get_lang('AlreadyReadMessage'));//Message already read
  156. } else {
  157. $message[1] = Display::return_icon('mail.png',get_lang('UnReadMessage'));//Message without reading
  158. }*/
  159. $message[1] = '<a onclick="get_action_url_and_show_messages(1,'.$result[0].')" href="javascript:void(0)">'.GetFullUserName($result[1]).'</a>';
  160. $message[2] = '<a onclick="get_action_url_and_show_messages(1,'.$result[0].')" href="javascript:void(0)">'.str_replace("\\","",$result[3]).'</a>';
  161. $message[3] = '<a onclick="reply_to_messages(\'show\','.$result[0].',\'\')" href="javascript:void(0)">'.Display::return_icon('message_reply.png',get_lang('ReplyToMessage')).'</a>'.
  162. '&nbsp;&nbsp;<a onclick="delete_one_message('.$result[0].')" href="javascript:void(0)" >'.Display::return_icon('delete.png',get_lang('DeleteMessage')).'</a>';
  163. } else {
  164. if($result[4]==1) {
  165. $class = 'class = "unread"';
  166. } else {
  167. $class = 'class = "read"';
  168. }
  169. $link = '';
  170. if ($_GET['f']=='social') {
  171. $link = '&f=social';
  172. }
  173. $message[1] = '<a '.$class.' href="view_message.php?id='.$result[0].$link.'">'.$result[2].'</a><br />'.GetFullUserName(($result[1]));
  174. //$message[2] = '<a '.$class.' href="view_message.php?id='.$result[0].$link.'">'.$result[2].'</a>';
  175. $message[3] = '<a href="new_message.php?re_id='.$result[0].'&f='.Security::remove_XSS($_GET['f']).'">'.Display::return_icon('message_reply.png',get_lang('ReplyToMessage')).'</a>'.
  176. '&nbsp;&nbsp;<a delete_one_message('.$result[0].') href="inbox.php?action=deleteone&id='.$result[0].'&f='.Security::remove_XSS($_GET['f']).'">'.Display::return_icon('delete.png',get_lang('DeleteMessage')).'</a>';
  177. }
  178. $message[2] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  179. foreach($message as $key => $value) {
  180. $message[$key] = api_xml_http_response_encode($value);
  181. }
  182. $message_list[] = $message;
  183. $i++;
  184. }
  185. return $message_list;
  186. }
  187. /**
  188. * Sends a message to a user/group
  189. *
  190. * @param int receiver user id
  191. * @param string subject
  192. * @param string content
  193. * @param array attachment files array($_FILES) (optional)
  194. * @param array comments about attachment files (optional)
  195. * @param int group id (optional)
  196. * @param int parent id (optional)
  197. * @param int message id for updating the message (optional)
  198. * @param bool sent an email or not (@todo)
  199. * @return bool
  200. */
  201. public static function send_message($receiver_user_id, $subject, $content, $file_attachments = array(), $file_comments = array(), $group_id = 0, $parent_id = 0, $edit_message_id = 0, $sent_email = false) {
  202. global $charset;
  203. $table_message = Database::get_main_table(TABLE_MESSAGE);
  204. $group_id = intval($group_id);
  205. $receiver_user_id = intval($receiver_user_id);
  206. $parent_id = intval($parent_id);
  207. $edit_message_id = intval($edit_message_id);
  208. $user_sender_id = api_get_user_id();
  209. $total_filesize = 0;
  210. if (is_array($file_attachments)) {
  211. foreach ($file_attachments as $file_attach) {
  212. $total_filesize += $file_attach['size'];
  213. }
  214. }
  215. // validating fields
  216. if (empty($subject) && empty($group_id) ) {
  217. return get_lang('YouShouldWriteASubject');
  218. } else if ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) {
  219. return sprintf(get_lang("FilesSizeExceedsX"),format_file_size(api_get_setting('message_max_upload_filesize')));
  220. }
  221. $inbox_last_id = null;
  222. //Just in case we replace the and \n and \n\r while saving in the DB
  223. $content = str_replace(array("\n", "\n\r"), '<br />', $content);
  224. $now = api_get_utc_datetime();
  225. if (!empty($receiver_user_id) || !empty($group_id)) {
  226. // message for user friend
  227. $clean_subject = Database::escape_string($subject);
  228. $clean_content = Database::escape_string($content);
  229. //message in inbox for user friend
  230. //@todo it's possible to edit a message? yes, only for groups
  231. if ($edit_message_id) {
  232. $query = " UPDATE $table_message SET update_date = '".$now."', content = '$clean_content' WHERE id = '$edit_message_id' ";
  233. $result = Database::query($query);
  234. $inbox_last_id = $edit_message_id;
  235. } else {
  236. $query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  237. "VALUES ('$user_sender_id', '$receiver_user_id', '1', '".$now."','$clean_subject','$clean_content','$group_id','$parent_id', '".$now."')";
  238. $result = Database::query($query);
  239. $inbox_last_id = Database::insert_id();
  240. }
  241. // Save attachment file for inbox messages
  242. if (is_array($file_attachments)) {
  243. $i = 0;
  244. foreach ($file_attachments as $file_attach) {
  245. if ($file_attach['error'] == 0) {
  246. self::save_message_attachment_file($file_attach,$file_comments[$i],$inbox_last_id,null,$receiver_user_id,$group_id);
  247. }
  248. $i++;
  249. }
  250. }
  251. if (empty($group_id)) {
  252. //message in outbox for user friend or group
  253. $sql = "INSERT INTO $table_message (user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  254. " VALUES ('$user_sender_id', '$receiver_user_id', '4', '".$now."','$clean_subject','$clean_content', '$group_id', '$parent_id', '".$now."')";
  255. $rs = Database::query($sql);
  256. $outbox_last_id = Database::insert_id();
  257. // save attachment file for outbox messages
  258. if (is_array($file_attachments)) {
  259. $o = 0;
  260. foreach ($file_attachments as $file_attach) {
  261. if ($file_attach['error'] == 0) {
  262. self::save_message_attachment_file($file_attach,$file_comments[$o],$outbox_last_id,$user_sender_id);
  263. }
  264. $o++;
  265. }
  266. }
  267. }
  268. //Load user settings
  269. $notification = new Notification();
  270. if (empty($group_id)) {
  271. $sender_info = api_get_user_info($user_sender_id);
  272. $notification->save_notification(NOTIFICATION_TYPE_MESSAGE, array($receiver_user_id), $subject, $content, $sender_info);
  273. } else {
  274. $group_info = GroupPortalManager::get_group_data($group_id);
  275. $user_list = GroupPortalManager::get_users_by_group($group_id, false, array(),0, 1000);
  276. //Adding more sens to the message group
  277. $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
  278. $new_user_list = array();
  279. foreach($user_list as $user_data) {
  280. $new_user_list[]= $user_data['user_id'];
  281. }
  282. $notification->save_notification(NOTIFICATION_TYPE_GROUP, $new_user_list, $subject, $content, $group_info);
  283. }
  284. return $inbox_last_id;
  285. } else {
  286. return get_lang('UserDoesNotExist');
  287. }
  288. }
  289. /**
  290. * Update parent ids for other receiver user from current message in groups
  291. * @author Christian Fasanando Flores
  292. * @param int parent id
  293. * @param int receiver user id
  294. * @param int message id
  295. * @return void
  296. */
  297. public static function update_parent_ids_from_reply($parent_id,$receiver_user_id,$message_id) {
  298. $table_message = Database::get_main_table(TABLE_MESSAGE);
  299. $parent_id = intval($parent_id);
  300. $receiver_user_id = intval($receiver_user_id);
  301. $message_id = intval($message_id);
  302. // first get data from message id (parent)
  303. $sql_message= "SELECT * FROM $table_message WHERE id = '$parent_id'";
  304. $rs_message = Database::query($sql_message);
  305. $row_message= Database::fetch_array($rs_message);
  306. // get message id from data found early for other receiver user
  307. $sql_msg_id = " SELECT id FROM $table_message WHERE user_sender_id ='{$row_message[user_sender_id]}'
  308. AND title='{$row_message[title]}' AND content='{$row_message[content]}' AND group_id='{$row_message[group_id]}' AND user_receiver_id='$receiver_user_id'";
  309. $rs_msg_id = Database::query($sql_msg_id);
  310. $row = Database::fetch_array($rs_msg_id);
  311. // update parent_id for other user receiver
  312. $sql_upd = "UPDATE $table_message SET parent_id = '{$row[id]}' WHERE id = '$message_id'";
  313. Database::query($sql_upd);
  314. }
  315. public static function delete_message_by_user_receiver ($user_receiver_id,$id) {
  316. $table_message = Database::get_main_table(TABLE_MESSAGE);
  317. if ($id != strval(intval($id))) return false;
  318. $user_receiver_id = intval($user_receiver_id);
  319. $id = Database::escape_string($id);
  320. $sql="SELECT * FROM $table_message WHERE id=".$id." AND msg_status<>4;";
  321. $rs=Database::query($sql);
  322. if (Database::num_rows($rs) > 0 ) {
  323. $row = Database::fetch_array($rs);
  324. // delete attachment file
  325. $res = self::delete_message_attachment_file($id,$user_receiver_id);
  326. // delete message
  327. $query = "UPDATE $table_message SET msg_status=3 WHERE user_receiver_id=".$user_receiver_id." AND id=".$id;
  328. //$query = "DELETE FROM $table_message WHERE user_receiver_id=".Database::escape_string($user_receiver_id)." AND id=".$id;
  329. $result = Database::query($query);
  330. return $result;
  331. } else {
  332. return false;
  333. }
  334. }
  335. /**
  336. * Set status deleted
  337. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  338. * @param integer
  339. * @param integer
  340. * @return array
  341. */
  342. public static function delete_message_by_user_sender ($user_sender_id,$id) {
  343. if ($id != strval(intval($id))) return false;
  344. $table_message = Database::get_main_table(TABLE_MESSAGE);
  345. $id = intval($id);
  346. $user_sender_id = intval($user_sender_id);
  347. $sql="SELECT * FROM $table_message WHERE id='$id'";
  348. $rs=Database::query($sql);
  349. if (Database::num_rows($rs) > 0 ) {
  350. $row = Database::fetch_array($rs);
  351. // delete attachment file
  352. $res = self::delete_message_attachment_file($id,$user_sender_id);
  353. // delete message
  354. $query = "UPDATE $table_message SET msg_status=3 WHERE user_sender_id='$user_sender_id' AND id='$id'";
  355. //$query = "DELETE FROM $table_message WHERE user_sender_id='$user_sender_id' AND id='$id'";
  356. $result = Database::query($query);
  357. return $result;
  358. }
  359. return false;
  360. }
  361. /**
  362. * Saves a message attachment files
  363. * @param array $_FILES['name']
  364. * @param string a comment about the uploaded file
  365. * @param int message id
  366. * @param int receiver user id (optional)
  367. * @param int sender user id (optional)
  368. * @param int group id (optional)
  369. * @return void
  370. */
  371. public static function save_message_attachment_file($file_attach,$file_comment,$message_id,$receiver_user_id=0,$sender_user_id=0,$group_id=0) {
  372. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  373. // Try to add an extension to the file if it hasn't one
  374. $new_file_name = add_ext_on_mime(stripslashes($file_attach['name']), $file_attach['type']);
  375. // user's file name
  376. $file_name =$file_attach['name'];
  377. if (!filter_extension($new_file_name)) {
  378. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  379. } else {
  380. $new_file_name = uniqid('');
  381. $message_user_id = '';
  382. if (!empty($receiver_user_id)) {
  383. $message_user_id = $receiver_user_id;
  384. } else {
  385. $message_user_id = $sender_user_id;
  386. }
  387. // User-reserved directory where photos have to be placed.
  388. if (!empty($group_id)) {
  389. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($group_id, 'system', true);
  390. } else {
  391. $path_user_info = UserManager::get_user_picture_path_by_id($message_user_id, 'system', true);
  392. }
  393. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  394. // If this directory does not exist - we create it.
  395. if (!file_exists($path_message_attach)) {
  396. @mkdir($path_message_attach, api_get_permissions_for_new_directories(), true);
  397. }
  398. $new_path=$path_message_attach.$new_file_name;
  399. if (is_uploaded_file($file_attach['tmp_name'])) {
  400. $result= @copy($file_attach['tmp_name'], $new_path);
  401. }
  402. $safe_file_comment= Database::escape_string($file_comment);
  403. $safe_file_name = Database::escape_string($file_name);
  404. $safe_new_file_name = Database::escape_string($new_file_name);
  405. // Storing the attachments if any
  406. $sql="INSERT INTO $tbl_message_attach(filename,comment, path,message_id,size)
  407. VALUES ( '$safe_file_name', '$safe_file_comment', '$safe_new_file_name' , '$message_id', '".$file_attach['size']."' )";
  408. $result=Database::query($sql);
  409. }
  410. }
  411. /**
  412. * Delete message attachment files (logically updating the row with a suffix _DELETE_id)
  413. * @param int message id
  414. * @param int message user id (receiver user id or sender user id)
  415. * @param int group id (optional)
  416. * @return void
  417. */
  418. public static function delete_message_attachment_file($message_id,$message_uid,$group_id=0) {
  419. $message_id = intval($message_id);
  420. $message_uid = intval($message_uid);
  421. $table_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  422. $sql= "SELECT * FROM $table_message_attach WHERE message_id = '$message_id'";
  423. $rs = Database::query($sql);
  424. $new_paths = array();
  425. while ($row = Database::fetch_array($rs)) {
  426. $path = $row['path'];
  427. $attach_id = $row['id'];
  428. $new_path = $path.'_DELETED_'.$attach_id;
  429. if (!empty($group_id)) {
  430. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($group_id, 'system', true);
  431. } else {
  432. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  433. }
  434. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  435. if (is_file($path_message_attach.$path)) {
  436. if(rename($path_message_attach.$path, $path_message_attach.$new_path)) {
  437. $sql_upd = "UPDATE $table_message_attach set path='$new_path' WHERE id ='$attach_id'";
  438. $rs_upd = Database::query($sql_upd);
  439. }
  440. }
  441. }
  442. }
  443. /**
  444. * update messages by user id and message id
  445. * @param int user id
  446. * @param int message id
  447. * @return resource
  448. */
  449. public static function update_message($user_id, $message_id) {
  450. if ($message_id != strval(intval($message_id)) || $user_id != strval(intval($user_id))) return false;
  451. $table_message = Database::get_main_table(TABLE_MESSAGE);
  452. $query = "UPDATE $table_message SET msg_status = '0' WHERE msg_status<>4 AND user_receiver_id=".intval($user_id)." AND id='".intval($message_id)."'";
  453. $result = Database::query($query);
  454. }
  455. /**
  456. * get messages by user id and message id
  457. * @param int user id
  458. * @param int message id
  459. * @return array
  460. */
  461. public static function get_message_by_user($user_id,$message_id) {
  462. if ($message_id != strval(intval($message_id)) || $user_id != strval(intval($user_id))) return false;
  463. $table_message = Database::get_main_table(TABLE_MESSAGE);
  464. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".intval($user_id)." AND id='".intval($message_id)."'";
  465. $result = Database::query($query);
  466. return $row = Database::fetch_array($result);
  467. }
  468. /**
  469. * get messages by group id
  470. * @param int group id
  471. * @return array
  472. */
  473. public static function get_messages_by_group($group_id) {
  474. if ($group_id != strval(intval($group_id))) return false;
  475. $table_message = Database::get_main_table(TABLE_MESSAGE);
  476. $current_uid = api_get_user_id();
  477. $group_id = intval($group_id);
  478. $query = "SELECT * FROM $table_message WHERE group_id=$group_id AND msg_status <> ".MESSAGE_STATUS_OUTBOX." ORDER BY id";
  479. $rs = Database::query($query);
  480. $data = array();
  481. if (Database::num_rows($rs) > 0) {
  482. while ($row = Database::fetch_array($rs,'ASSOC')) {
  483. $data[] = $row;
  484. }
  485. }
  486. return $data;
  487. }
  488. /**
  489. * get messages by group id
  490. * @param int group id
  491. * @return array
  492. */
  493. public static function get_messages_by_group_by_message($group_id, $message_id) {
  494. if ($group_id != strval(intval($group_id))) return false;
  495. $table_message = Database::get_main_table(TABLE_MESSAGE);
  496. $current_uid = api_get_user_id();
  497. $group_id = intval($group_id);
  498. $query = "SELECT * FROM $table_message WHERE group_id=$group_id AND msg_status <> ".MESSAGE_STATUS_OUTBOX." ORDER BY id";
  499. $rs = Database::query($query);
  500. $data = array();
  501. $parents = array();
  502. if (Database::num_rows($rs) > 0) {
  503. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  504. if ($message_id == $row['parent_id'] || in_array($row['parent_id'], $parents)) {
  505. $parents[]= $row['id'];
  506. $data[] = $row;
  507. }
  508. }
  509. }
  510. return $data;
  511. }
  512. /**
  513. * get messages by parent id optionally with limit
  514. * @param int parent id
  515. * @param int group id (optional)
  516. * @param int offset (optional)
  517. * @param int limit (optional)
  518. * @return array
  519. */
  520. public static function get_messages_by_parent($parent_id,$group_id = '',$offset = 0,$limit = 0) {
  521. if ($parent_id != strval(intval($parent_id))) return false;
  522. $table_message = Database::get_main_table(TABLE_MESSAGE);
  523. $current_uid = api_get_user_id();
  524. $parent_id = intval($parent_id);
  525. $condition_group_id = "";
  526. if ($group_id !== '') {
  527. $group_id = intval($group_id);
  528. $condition_group_id = " AND group_id = '$group_id' ";
  529. }
  530. $condition_limit = "";
  531. if ($offset && $limit) {
  532. $offset = ($offset - 1) * $limit;
  533. $condition_limit = " LIMIT $offset,$limit ";
  534. }
  535. $query = "SELECT * FROM $table_message WHERE parent_id='$parent_id' AND msg_status <> ".MESSAGE_STATUS_OUTBOX." $condition_group_id ORDER BY send_date DESC $condition_limit ";
  536. $rs = Database::query($query);
  537. $data = array();
  538. if (Database::num_rows($rs) > 0) {
  539. while ($row = Database::fetch_array($rs)) {
  540. $data[$row['id']] = $row;
  541. }
  542. }
  543. return $data;
  544. }
  545. /**
  546. * Gets information about if exist messages
  547. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  548. * @param integer
  549. * @param integer
  550. * @return boolean
  551. */
  552. public static function exist_message ($user_id, $id) {
  553. if ($id != strval(intval($id)) || $user_id != strval(intval($user_id))) return false;
  554. $table_message = Database::get_main_table(TABLE_MESSAGE);
  555. $query = "SELECT id FROM $table_message WHERE user_receiver_id=".Database::escape_string($user_id)." AND id='".Database::escape_string($id)."'";
  556. $result = Database::query($query);
  557. $num = Database::num_rows($result);
  558. if ($num>0)
  559. return true;
  560. else
  561. return false;
  562. }
  563. /**
  564. * Gets information about messages sent
  565. * @param integer
  566. * @param integer
  567. * @param string
  568. * @return array
  569. */
  570. public static function get_message_data_sent($from, $number_of_items, $column, $direction) {
  571. global $charset;
  572. $from = intval($from);
  573. $number_of_items = intval($number_of_items);
  574. if (!isset($direction)) {
  575. $column = 3;
  576. $direction = 'DESC';
  577. } else {
  578. $column = intval($column);
  579. if (!in_array($direction, array('ASC', 'DESC')))
  580. $direction = 'ASC';
  581. }
  582. $table_message = Database::get_main_table(TABLE_MESSAGE);
  583. $request=api_is_xml_http_request();
  584. $sql_query = "SELECT id as col0, user_sender_id as col1, title as col2, send_date as col3, user_receiver_id as col4, msg_status as col5 FROM $table_message " .
  585. "WHERE user_sender_id=".api_get_user_id()." AND msg_status=".MESSAGE_STATUS_OUTBOX." " .
  586. "ORDER BY col$column $direction LIMIT $from,$number_of_items";
  587. $sql_result = Database::query($sql_query);
  588. $i = 0;
  589. $message_list = array ();
  590. while ($result = Database::fetch_row($sql_result)) {
  591. if ($request===true) {
  592. $message[0] = '<input type="checkbox" value='.$result[0].' name="out[]">';
  593. } else {
  594. $message[0] = ($result[0]);
  595. }
  596. $class = 'class = "read"';
  597. $result[2] = Security::remove_XSS($result[2]);
  598. if ($request===true) {
  599. $message[1] = '<a onclick="show_sent_message('.$result[0].')" href="javascript:void(0)">'.GetFullUserName($result[4]).'</a>';
  600. $message[2] = '<a onclick="show_sent_message('.$result[0].')" href="javascript:void(0)">'.str_replace("\\","",$result[2]).'</a>';
  601. $message[3] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  602. $message[4] = '&nbsp;&nbsp;<a onclick="delete_one_message_outbox('.$result[0].')" href="javascript:void(0)" >'.Display::return_icon('delete.png',get_lang('DeleteMessage')).'</a>';
  603. } else {
  604. $link = '';
  605. if ($_GET['f']=='social') {
  606. $link = '&f=social';
  607. }
  608. $message[1] = '<a '.$class.' onclick="show_sent_message ('.$result[0].')" href="../messages/view_message.php?id_send='.$result[0].$link.'">'.$result[2].'</a><br />'.GetFullUserName($result[4]);
  609. //$message[2] = '<a '.$class.' onclick="show_sent_message ('.$result[0].')" href="../messages/view_message.php?id_send='.$result[0].$link.'">'.$result[2].'</a>';
  610. $message[2] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  611. $message[3] = '<a href="outbox.php?action=deleteone&id='.$result[0].'&f='.Security::remove_XSS($_GET['f']).'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmDeleteMessage')))."'".')) return false;">'.Display::return_icon('delete.png',get_lang('DeleteMessage')).'</a>';
  612. }
  613. foreach($message as $key => $value) {
  614. $message[$key] = $value;
  615. }
  616. $message_list[] = $message;
  617. $i++;
  618. }
  619. return $message_list;
  620. }
  621. /**
  622. * Gets information about number messages sent
  623. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  624. * @param void
  625. * @return integer
  626. */
  627. public static function get_number_of_messages_sent () {
  628. $table_message = Database::get_main_table(TABLE_MESSAGE);
  629. $sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE msg_status=".MESSAGE_STATUS_OUTBOX." AND user_sender_id=".api_get_user_id();
  630. $sql_result = Database::query($sql_query);
  631. $result = Database::fetch_array($sql_result);
  632. return $result['number_messages'];
  633. }
  634. /**
  635. * display message box in the inbox
  636. * @param int the message id
  637. * @param string inbox or outbox strings are available
  638. * @todo replace numbers with letters in the $row array pff...
  639. * @return string html with the message content
  640. */
  641. public static function show_message_box($message_id, $source = 'inbox') {
  642. $table_message = Database::get_main_table(TABLE_MESSAGE);
  643. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  644. $message_id = intval($message_id);
  645. if ($source == 'outbox') {
  646. if (isset($message_id) && is_numeric($message_id)) {
  647. $query = "SELECT * FROM $table_message WHERE user_sender_id=".api_get_user_id()." AND id=".$message_id." AND msg_status=4;";
  648. $result = Database::query($query);
  649. $path = 'outbox.php';
  650. }
  651. } else {
  652. if (is_numeric($message_id) && !empty($message_id)) {
  653. $query = "UPDATE $table_message SET msg_status = '".MESSAGE_STATUS_NEW."' WHERE user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
  654. $result = Database::query($query);
  655. $query = "SELECT * FROM $table_message WHERE msg_status<>4 AND user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
  656. $result = Database::query($query);
  657. }
  658. $path='inbox.php';
  659. }
  660. $row = Database::fetch_array($result);
  661. // get file attachments by message id
  662. $files_attachments = self::get_links_message_attachment_files($message_id,$source);
  663. $user_con = self::users_connected_by_id();
  664. $band=0;
  665. $reply='';
  666. for ($i=0;$i<count($user_con);$i++)
  667. if ($row[1]==$user_con[$i])
  668. $band=1;
  669. $row[5] = Security::remove_XSS($row[5]);
  670. $from_user = UserManager::get_user_info_by_id($row[1]);
  671. $name = api_get_person_name($from_user['firstname'], $from_user['lastname']);
  672. $user_image = UserManager::get_picture_user($row[1], $from_user['picture_uri'],80);
  673. $user_image = Display::img($user_image['file'], $name, array('title'=>$name));
  674. $message_content = '<table>
  675. <tr>
  676. <td width="10px">&nbsp; </td>
  677. <td width="100%">
  678. <table>
  679. <tr>
  680. <td valign="top" width="100%">
  681. <h1>'.str_replace("\\","",$row[5]).'</h1>
  682. </td>';
  683. if (api_get_setting('allow_social_tool') == 'true') {
  684. $message_content .='<td width="100%">'.$user_image.'</td>';
  685. }
  686. $message_content .='<tr>';
  687. if (api_get_setting('allow_social_tool') == 'true') {
  688. if ($source == 'outbox') {
  689. $message_content .='<td>'.get_lang('From').' <a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$row[1].'">'.$name.'</a> '.api_strtolower(get_lang('To')).'&nbsp;<b>'.GetFullUserName($row[2]).'</b> </td>';
  690. } else {
  691. $message_content .='<td>'.get_lang('From').' <a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$row[1].'">'.$name.'</a> '.api_strtolower(get_lang('To')).'&nbsp;<b>'.get_lang('Me').'</b> </td>';
  692. }
  693. } else {
  694. if ($source == 'outbox') {
  695. $message_content .='<td>'.get_lang('From').'&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.GetFullUserName($row[2]).'</b> </td>';
  696. } else {
  697. $message_content .='<td>'.get_lang('From').'&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.get_lang('Me').'</b> </td>';
  698. }
  699. }
  700. $message_content .='</tr>
  701. <tr>
  702. <td>'.get_lang('Date').'&nbsp; '.$row[4].'</td>
  703. </tr>
  704. </tr>
  705. </table>
  706. <br />
  707. <hr style="color:#ddd" />
  708. <table height="209px" width="100%">
  709. <tr>
  710. <td valign=top class="view-message-content">'.str_replace("\\","",$row['content']).'</td>
  711. </tr>
  712. </table>
  713. <div id="message-attach">'.(!empty($files_attachments)?implode('<br />',$files_attachments):'').'</div>
  714. <div style="padding: 15px 0px 5px 0px">';
  715. $social_link = '';
  716. if ($_GET['f'] == 'social') {
  717. $social_link = 'f=social';
  718. }
  719. if ($source == 'outbox') {
  720. $message_content .= '<a href="outbox.php?'.$social_link.'">'.Display::return_icon('back.png',get_lang('ReturnToOutbox')).'</a> &nbsp';
  721. } else {
  722. $message_content .= '<a href="inbox.php?'.$social_link.'">'.Display::return_icon('back.png',get_lang('ReturnToInbox')).'</a> &nbsp';
  723. $message_content .= '<a href="new_message.php?re_id='.$message_id.'&'.$social_link.'">'.Display::return_icon('message_reply.png',get_lang('ReplyToMessage')).'</a> &nbsp';
  724. }
  725. $message_content .= '<a href="inbox.php?action=deleteone&id='.$message_id.'&'.$social_link.'" >'.Display::return_icon('delete.png',get_lang('DeleteMessage')).'</a>&nbsp';
  726. $message_content .='</div></td>
  727. <td width=10></td>
  728. </tr>
  729. </table>';
  730. return $message_content;
  731. }
  732. /**
  733. * display message box sent showing it into outbox
  734. * @return void
  735. */
  736. public static function show_message_box_sent () {
  737. global $charset;
  738. $table_message = Database::get_main_table(TABLE_MESSAGE);
  739. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  740. $message_id = '';
  741. if (is_numeric($_GET['id_send'])) {
  742. $query = "SELECT * FROM $table_message WHERE user_sender_id=".api_get_user_id()." AND id=".intval(Database::escape_string($_GET['id_send']))." AND msg_status=4;";
  743. $result = Database::query($query);
  744. $message_id = intval($_GET['id_send']);
  745. }
  746. $path='outbox.php';
  747. // get file attachments by message id
  748. $files_attachments = self::get_links_message_attachment_files($message_id,'outbox');
  749. $row = Database::fetch_array($result);
  750. $user_con = self::users_connected_by_id();
  751. $band=0;
  752. $reply='';
  753. for ($i=0;$i<count($user_con);$i++)
  754. if ($row[1]==$user_con[$i])
  755. $band=1;
  756. echo '<div class=actions>';
  757. echo '<a onclick="close_and_open_outbox()" href="javascript:void(0)">'.Display::return_icon('folder_up.gif',api_xml_http_response_encode(get_lang('BackToOutbox'))).api_xml_http_response_encode(get_lang('BackToOutbox')).'</a>';
  758. echo '<a onclick="delete_one_message_outbox('.$row[0].')" href="javascript:void(0)" >'.Display::return_icon('delete.png',api_xml_http_response_encode(get_lang('DeleteMessage'))).api_xml_http_response_encode(get_lang('DeleteMessage')).'</a>';
  759. echo '</div><br />';
  760. echo '
  761. <table class="message_view_table" >
  762. <TR>
  763. <TD width=10>&nbsp; </TD>
  764. <TD vAlign=top width="100%">
  765. <TABLE>
  766. <TR>
  767. <TD width="100%">
  768. <TR> <h1>'.str_replace("\\","",api_xml_http_response_encode($row[5])).'</h1></TR>
  769. </TD>
  770. <TR>
  771. <TD>'.api_xml_http_response_encode(get_lang('From').'&nbsp;<b>'.GetFullUserName($row[1]).'</b> '.api_strtolower(get_lang('To')).'&nbsp; <b>'.GetFullUserName($row[2])).'</b> </TD>
  772. </TR>
  773. <TR>
  774. <TD >'.api_xml_http_response_encode(get_lang('Date').'&nbsp; '.$row[4]).'</TD>
  775. </TR>
  776. </TR>
  777. </TABLE>
  778. <br />
  779. <TABLE height="209px" width="100%" bgColor=#ffffff>
  780. <TBODY>
  781. <TR>
  782. <TD vAlign=top>'.str_replace("\\","",api_xml_http_response_encode($row[6])).'</TD>
  783. </TR>
  784. </TBODY>
  785. </TABLE>
  786. <div id="message-attach">'.(!empty($files_attachments)?implode('<br />',$files_attachments):'').'</div>
  787. <DIV class=HT style="PADDING-BOTTOM: 5px"> </DIV></TD>
  788. <TD width=10>&nbsp;</TD>
  789. </TR>
  790. </TABLE>';
  791. }
  792. /**
  793. * get user id by user email
  794. * @param string $user_email
  795. * @return int user id
  796. */
  797. public static function get_user_id_by_email($user_email) {
  798. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  799. $sql='SELECT user_id FROM '.$tbl_user.' WHERE email="'.Database::escape_string($user_email).'";';
  800. $rs=Database::query($sql);
  801. $row=Database::fetch_array($rs,'ASSOC');
  802. if (isset($row['user_id'])) {
  803. return $row['user_id'];
  804. } else {
  805. return null;
  806. }
  807. }
  808. /**
  809. * Displays messages of a group with nested view
  810. * @param int group id
  811. */
  812. public static function display_messages_for_group($group_id) {
  813. global $my_group_role;
  814. $rows = self::get_messages_by_group($group_id);
  815. //$rows = self::calculate_children($rows, 1);
  816. $group_info = GroupPortalManager::get_group_data($group_id);
  817. $current_user_id = api_get_user_id();
  818. $topics_per_page = 10;
  819. $count_items = 0;
  820. $html_messages = '';
  821. $query_vars = array('id'=>$group_id, 'topics_page_nr'=>0);
  822. if (is_array($rows) && count($rows) > 0) {
  823. // prepare array for topics with its items
  824. $topics = array();
  825. $x = 0;
  826. $my_items = array();
  827. foreach($rows as $index => $value) {
  828. if (empty($value['parent_id'])) {
  829. $topics[$value['id']] = $value;
  830. }
  831. }
  832. $new_topics = array();
  833. foreach($topics as $id => $value) {
  834. $rows = null;
  835. $rows = self::get_messages_by_group_by_message($group_id, $value['id']);
  836. if (!empty($rows)) {
  837. $count = count(self::calculate_children($rows, $value['id']));
  838. } else {
  839. $count = 0;
  840. }
  841. $value['count'] = $count;
  842. $new_topics[$id] = $value;
  843. }
  844. //$new_topics = sort_column($new_topics,'count');
  845. $param_names = array_keys($_GET);
  846. $array_html = array();
  847. foreach ($new_topics as $index => $topic) {
  848. $html = '';
  849. // topics
  850. $indent = 0;
  851. $user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
  852. $files_attachments = self::get_links_message_attachment_files($topic['id']);
  853. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  854. $html .= '<div class="rounded_div" style="width:620px">';
  855. $items = $topic['count'];
  856. $reply_label = ($items == 1) ? get_lang('Reply'): get_lang('Replies');
  857. $html .= '<table width="100%"><tr><td width="20px" valign="top">';
  858. $html .= Display::div(Display::tag('span', $items).$reply_label, array('class' =>'group_discussions_replies'));
  859. $html .= '</td><td valign="top">';
  860. $topic['title'] = trim($topic['title']);
  861. if (empty($topic['title'])) {
  862. $topic['title'] = get_lang('Untitled');
  863. }
  864. $title = Display::url('<h2>'.Security::remove_XSS($topic['title']).'</h2>', 'group_topics.php?id='.$group_id.'&topic_id='.$topic['id']);
  865. $date = '';
  866. $link = '';
  867. if ($topic['send_date']!=$topic['update_date']) {
  868. if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00' ) {
  869. $date .= '<div class="message-group-date" > <i>'.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</i></div>';
  870. }
  871. } else {
  872. $date .= '<div class="message-group-date"> <i>'.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'</i></div>';
  873. }
  874. $image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
  875. $image_repository = $image_path['dir'];
  876. $existing_image = $image_path['file'];
  877. $user = '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a>';
  878. $user_image = '<div class="clear"></div><div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  879. $user = '<div class="message-group-author">'.$user.'</div>';
  880. //$date.
  881. $html .= Display::div($title.cut($topic['content'], 350).$user_image.$user.$link, array('class'=>'group_discussions_info')).'</td></table>';
  882. $html .= '</div>'; //rounded_div
  883. $array_html[] = array($html);
  884. }
  885. // grids for items and topics with paginations
  886. $html_messages .= Display::return_sortable_grid('topics', array(), $array_html, array('hide_navigation'=>false, 'per_page' => $topics_per_page), $query_vars, false, array(true, true, true,false), false);
  887. }
  888. return $html_messages;
  889. }
  890. /**
  891. * Displays messages of a group with nested view
  892. * @param int group id
  893. */
  894. public static function display_message_for_group($group_id, $message_id, $is_member) {
  895. global $my_group_role;
  896. $main_message = self::get_message_by_id($message_id);
  897. $group_info = GroupPortalManager::get_group_data($group_id);
  898. $rows = self::get_messages_by_group_by_message($group_id, $message_id);
  899. $rows = self::calculate_children($rows, $message_id);
  900. $current_user_id = api_get_user_id();
  901. $topics_per_page = 5;
  902. $items_per_page = 50;
  903. $count_items = 0;
  904. $html_messages = '';
  905. $query_vars = array('id'=>$group_id, 'topic_id'=>$message_id , 'topics_page_nr'=>0);
  906. // Main message
  907. $html = '';
  908. $user_link = '';
  909. $links = '';
  910. $main_content = '';
  911. //$items_page_nr = intval($_GET['items_'.$topic['id'].'_page_nr']);
  912. $items_page_nr = null;
  913. echo Display::tag('h1', $main_message['title']);
  914. $user_sender_info = UserManager::get_user_info_by_id($main_message['user_sender_id']);
  915. $files_attachments = self::get_links_message_attachment_files($main_message['id']);
  916. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  917. $topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null;
  918. $links.= '<div id="message-reply-link">';
  919. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $main_message['user_sender_id'] == $current_user_id) {
  920. $links.= '<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.$current_user_id.'&group_id='.$group_id.'&message_id='.$main_message['id'].'&action=edit_message_group&anchor_topic=topic_'.$main_message['id'].'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$main_message['id'].'" class="thickbox" title="'.get_lang('Edit').'">'.Display :: return_icon('edit.png', get_lang('Edit'), array(), 22).'</a>';
  921. }
  922. $links.= '&nbsp;&nbsp;<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&message_id='.$main_message['id'].'&action=reply_message_group&anchor_topic=topic_'.$main_message['id'].'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$main_message['id'].'" class="thickbox" title="'.get_lang('Reply').'">'.Display :: return_icon('talk.png', get_lang('Reply')).'</a>';
  923. $links.= '</div>';
  924. $image_path = UserManager::get_user_picture_path_by_id($main_message['user_sender_id'], 'web', false, true);
  925. $image_repository = $image_path['dir'];
  926. $existing_image = $image_path['file'];
  927. $main_content.= '<div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  928. $user_link = '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$main_message['user_sender_id'].'">'.$name.'&nbsp;</a>';
  929. $date = '';
  930. if ($main_message['send_date'] != $main_message['update_date']) {
  931. if (!empty($main_message['update_date']) && $main_message['update_date'] != '0000-00-00 00:00:00' ) {
  932. $date = '<div class="message-group-date"> '.get_lang('LastUpdate').' '.date_to_str_ago($main_message['update_date']).'</div>';
  933. }
  934. } else {
  935. $date = '<div class="message-group-date"> '.get_lang('Created').' '.date_to_str_ago($main_message['send_date']).'</div>';
  936. }
  937. $attachment = '<div class="message-attach">'.(!empty($files_attachments)?implode('<br />',$files_attachments):'').'</div>';
  938. $main_content.= '<div class="message-group-content">'.$links.$user_link.' '.$date.$main_message['content'].$attachment.'</div>';
  939. $html = '';
  940. $html .= Display::div(Display::div(Display::div($main_content, array('class'=>'group_social_sub_item', 'style'=>'background-color:#fff;')), array('class' => 'group_social_item')), array('class' => 'group_social_grid'));
  941. $topic_id = $main_message['id'];
  942. if (is_array($rows) && count($rows)> 0) {
  943. $topics = $rows;
  944. $array_html = array();
  945. foreach ($topics as $index => $topic) {
  946. if (empty($topic['id'])) {
  947. continue;
  948. }
  949. $items_page_nr = isset($_GET['items_'.$topic['id'].'_page_nr']) ? intval($_GET['items_'.$topic['id'].'_page_nr']) : null;
  950. $user_link = '';
  951. $links = '';
  952. $html_items = '';
  953. $user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
  954. $files_attachments = self::get_links_message_attachment_files($topic['id']);
  955. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  956. $links.= '<div id="message-reply-link">';
  957. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $topic['user_sender_id'] == $current_user_id) {
  958. $links.= '<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.$current_user_id.'&group_id='.$group_id.'&message_id='.$topic['id'].'&action=edit_message_group&anchor_topic=topic_'.$topic_id.'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$topic_id.'" class="thickbox" title="'.get_lang('Edit').'">'.Display :: return_icon('edit.png', get_lang('Edit'), array(), 22).'</a>';
  959. }
  960. $links.= '&nbsp;&nbsp;<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&message_id='.$topic['id'].'&action=reply_message_group&anchor_topic=topic_'.$topic_id.'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$topic_id.'" class="thickbox" title="'.get_lang('Reply').'">'.Display :: return_icon('talk.png', get_lang('Reply')).'</a>';
  961. $links.= '</div>';
  962. $image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
  963. $image_repository = $image_path['dir'];
  964. $existing_image = $image_path['file'];
  965. $html_items.= '<div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  966. $user_link = '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a>';
  967. $date = '';
  968. if ($topic['send_date'] != $topic['update_date']) {
  969. if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00' ) {
  970. $date = '<div class="message-group-date"> '.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</div>';
  971. }
  972. } else {
  973. $date = '<div class="message-group-date"> '.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'</div>';
  974. }
  975. $attachment = '<div class="message-attach">'.(!empty($files_attachments)?implode('<br />',$files_attachments):'').'</div>';
  976. $html_items.= '<div class="message-group-content">'.$links.$user_link.' '.$date.$topic['content'].$attachment.'</div>';
  977. $base_padding = 20;
  978. if ($topic['indent_cnt'] == 0) {
  979. $indent = $base_padding;
  980. } else {
  981. $indent = intval($topic['indent_cnt'])*$base_padding + $base_padding;
  982. }
  983. $html_items = Display::div($html_items, array('class' => 'group_social_sub_item'));
  984. $html_items = Display::div($html_items, array('class' => '', 'style'=>'margin-left:'.$indent.'px'));
  985. $array_html_items[] = array($html_items);
  986. }
  987. // grids for items with paginations
  988. $options = array('hide_navigation'=>false, 'per_page' => $items_per_page);
  989. $order = array(true, true, true,false);
  990. $style_class = array('item' => array('class'=>'group_social_item'), 'main' => array('class'=>'group_social_grid'));
  991. if (!empty($array_html_items)) {
  992. $html .= Display::return_sortable_grid('items_'.$topic['id'], array(), $array_html_items, $options, $query_vars, false, $order, false, $style_class);
  993. }
  994. }
  995. $html .= '</div>';
  996. $html = Display::div($html, array('class'=>'rounded_div', 'style'=>'width:638px'));
  997. return $html;
  998. }
  999. /**
  1000. * Add children to messages by id is used for nested view messages
  1001. * @param array rows of messages
  1002. * @return array new list adding the item children
  1003. */
  1004. public static function calculate_children($rows, $first_seed) {
  1005. $rows_with_children = array();
  1006. foreach($rows as $row) {
  1007. $rows_with_children[$row["id"]]=$row;
  1008. $rows_with_children[$row["parent_id"]]["children"][]=$row["id"];
  1009. }
  1010. $rows = $rows_with_children;
  1011. $sorted_rows = array(0=>array());
  1012. self::message_recursive_sort($rows, $sorted_rows, $first_seed);
  1013. unset($sorted_rows[0]);
  1014. return $sorted_rows;
  1015. }
  1016. /**
  1017. * Sort recursively the messages, is used for for nested view messages
  1018. * @param array original rows of messages
  1019. * @param array list recursive of messages
  1020. * @param int seed for calculate the indent
  1021. * @param int indent for nested view
  1022. * @return void
  1023. */
  1024. public static function message_recursive_sort($rows, &$messages, $seed=0, $indent=0) {
  1025. if ($seed > 0 && isset($rows[$seed]["id"])) {
  1026. $messages[$rows[$seed]["id"]]=$rows[$seed];
  1027. $messages[$rows[$seed]["id"]]["indent_cnt"]=$indent;
  1028. $indent++;
  1029. }
  1030. if (isset($rows[$seed]["children"])) {
  1031. foreach($rows[$seed]["children"] as $child) {
  1032. self::message_recursive_sort($rows, $messages, $child, $indent);
  1033. }
  1034. }
  1035. }
  1036. /**
  1037. * Sort date by desc from a multi-dimensional array
  1038. * @param array1 first array to compare
  1039. * @param array2 second array to compare
  1040. * @return bool
  1041. */
  1042. public function order_desc_date($array1,$array2) {
  1043. return strcmp($array2['send_date'],$array1['send_date']);
  1044. }
  1045. /**
  1046. * Get array of links (download) for message attachment files
  1047. * @param int message id
  1048. * @param string type message list (inbox/outbox)
  1049. * @return array
  1050. */
  1051. public static function get_links_message_attachment_files($message_id,$type='') {
  1052. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1053. $message_id = intval($message_id);
  1054. // get file attachments by message id
  1055. $links_attach_file = array();
  1056. if (!empty($message_id)) {
  1057. $sql = "SELECT * FROM $tbl_message_attach WHERE message_id = '$message_id'";
  1058. $rs_file = Database::query($sql);
  1059. if (Database::num_rows($rs_file) > 0) {
  1060. $attach_icon = Display::return_icon('attachment.gif', '');
  1061. $archiveURL=api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
  1062. while ($row_file = Database::fetch_array($rs_file)) {
  1063. $archiveFile= $row_file['path'];
  1064. $filename = $row_file['filename'];
  1065. $filesize = format_file_size($row_file['size']);
  1066. $filecomment= $row_file['comment'];
  1067. $links_attach_file[] = $attach_icon.'&nbsp;<a href="'.$archiveURL.$archiveFile.'">'.$filename.'</a>&nbsp;('.$filesize.')'.(!empty($filecomment)?'&nbsp;-&nbsp;<i>'.$filecomment.'</i>':'');
  1068. }
  1069. }
  1070. }
  1071. return $links_attach_file;
  1072. }
  1073. /**
  1074. * Get message list by id
  1075. * @param int message id
  1076. * @return array
  1077. */
  1078. public static function get_message_by_id($message_id) {
  1079. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  1080. $message_id = intval($message_id);
  1081. $sql = "SELECT * FROM $tbl_message WHERE id = '$message_id'";
  1082. $res = Database::query($sql);
  1083. $item = array();
  1084. if (Database::num_rows($res)>0) {
  1085. $item = Database::fetch_array($res,'ASSOC');
  1086. }
  1087. return $item;
  1088. }
  1089. }
  1090. //@todo this functions should be in the message class
  1091. function inbox_display() {
  1092. global $charset;
  1093. $table_message = Database::get_main_table(TABLE_MESSAGE);
  1094. $success = get_lang('SelectedMessagesDeleted');
  1095. if (isset ($_REQUEST['action'])) {
  1096. switch ($_REQUEST['action']) {
  1097. case 'delete' :
  1098. $number_of_selected_messages = count($_POST['id']);
  1099. foreach ($_POST['id'] as $index => $message_id) {
  1100. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
  1101. }
  1102. Display::display_normal_message(api_xml_http_response_encode($success),false);
  1103. break;
  1104. case 'deleteone' :
  1105. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1106. Display::display_confirmation_message(api_xml_http_response_encode($success),false);
  1107. echo '<br />';
  1108. break;
  1109. }
  1110. }
  1111. // display sortable table with messages of the current user
  1112. //$table = new SortableTable('messages', 'get_number_of_messages_mask', 'get_message_data_mask', 3, get_number_of_messages_mask(),'DESC');
  1113. $table = new SortableTable('message_inbox', array('MessageManager','get_number_of_messages'), array('MessageManager','get_message_data'),3,20,'DESC');
  1114. $table->set_header(0, '', false,array ('style' => 'width:15px;'));
  1115. $title=api_xml_http_response_encode(get_lang('Title'));
  1116. $action=api_xml_http_response_encode(get_lang('Modify'));
  1117. $table->set_header(1,api_xml_http_response_encode(get_lang('Messages')),false);
  1118. //$table->set_header(2,$title,true);
  1119. $table->set_header(2,api_xml_http_response_encode(get_lang('Date')),true, array('style' => 'width:160px;'));
  1120. $table->set_header(3,$action,false,array ('style' => 'width:70px;'));
  1121. if ($_REQUEST['f']=='social') {
  1122. $parameters['f'] = 'social';
  1123. $table->set_additional_parameters($parameters);
  1124. }
  1125. $table->set_form_actions(array ('delete' => get_lang('DeleteSelectedMessages')));
  1126. $table->display();
  1127. }
  1128. function get_number_of_messages_mask() {
  1129. return MessageManager::get_number_of_messages();
  1130. }
  1131. function get_message_data_mask($from, $number_of_items, $column, $direction) {
  1132. $column='3';
  1133. $direction='DESC';
  1134. //non set by SortableTable ?
  1135. $number_of_items=get_number_of_messages_mask();
  1136. return MessageManager::get_message_data($from, $number_of_items, $column, $direction);
  1137. }
  1138. function outbox_display() {
  1139. $table_message = Database::get_main_table(TABLE_MESSAGE);
  1140. $request=api_is_xml_http_request();
  1141. global $charset;
  1142. $social_link = false;
  1143. if ($_REQUEST['f']=='social') {
  1144. $social_link ='f=social';
  1145. }
  1146. $success= get_lang('SelectedMessagesDeleted').'&nbsp</b><br /><a href="outbox.php?'.$social_link.'">'.get_lang('BackToOutbox').'</a>';
  1147. if (isset ($_REQUEST['action'])) {
  1148. switch ($_REQUEST['action']) {
  1149. case 'delete' :
  1150. $number_of_selected_messages = count($_POST['id']);
  1151. if ($number_of_selected_messages!=0) {
  1152. foreach ($_POST['id'] as $index => $message_id) {
  1153. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
  1154. }
  1155. }
  1156. Display::display_normal_message(api_xml_http_response_encode($success),false);
  1157. break;
  1158. case 'deleteone' :
  1159. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1160. Display::display_confirmation_message(api_xml_http_response_encode($success),false);
  1161. echo '<br/>';
  1162. break;
  1163. }
  1164. }
  1165. // display sortable table with messages of the current user
  1166. //$table = new SortableTable('messages', 'get_number_of_messages_send_mask', 'get_message_data_send_mask', 3, get_number_of_messages_send_mask(), 'DESC');
  1167. $table = new SortableTable('message_outbox', array('MessageManager','get_number_of_messages_sent'), array('MessageManager','get_message_data_sent'),3,20,'DESC');
  1168. $parameters['f'] = Security::remove_XSS($_GET['f']);
  1169. $table->set_additional_parameters($parameters);
  1170. $table->set_header(0, '', false,array ('style' => 'width:15px;'));
  1171. $title = api_xml_http_response_encode(get_lang('Title'));
  1172. $action= api_xml_http_response_encode(get_lang('Modify'));
  1173. $table->set_header(1, api_xml_http_response_encode(get_lang('Messages')),false);
  1174. //$table->set_header(2, $title,true);
  1175. $table->set_header(2, api_xml_http_response_encode(get_lang('Date')),true,array ('style' => 'width:160px;'));
  1176. $table->set_header(3,$action, false,array ('style' => 'width:70px;'));
  1177. if ($request===true) {
  1178. echo '<form name="form_send_out" id="form_send_out" action="" method="post">';
  1179. echo '<input type="hidden" name="action" value="delete" />';
  1180. $table->display();
  1181. echo '</form>';
  1182. if (get_number_of_messages_send_mask() > 0) {
  1183. echo '<a href="javascript:void(0)" onclick="selectall_cheks()">'.api_xml_http_response_encode(get_lang('SelectAll')).'</a>&nbsp;&nbsp;&nbsp;';
  1184. echo '<a href="javascript:void(0)" onclick="unselectall_cheks()">'.api_xml_http_response_encode(get_lang('UnSelectAll')).'</a>&nbsp;&nbsp;&nbsp;';
  1185. echo '<button class="save" name="delete" type="button" value="'.api_xml_http_response_encode(get_lang('DeleteSelectedMessages')).'" onclick="submit_form(\'outbox\')">'.api_xml_http_response_encode(get_lang('DeleteSelectedMessages')).'</button>';
  1186. }
  1187. } else {
  1188. $table->set_form_actions(array ('delete' => get_lang('DeleteSelectedMessages')));
  1189. $table->display();
  1190. }
  1191. }
  1192. function get_number_of_messages_send_mask() {
  1193. return MessageManager::get_number_of_messages_sent();
  1194. }
  1195. function get_message_data_send_mask($from, $number_of_items, $column, $direction) {
  1196. $column='3';
  1197. $direction='desc';
  1198. //non set by SortableTable ?
  1199. $number_of_items=get_number_of_messages_send_mask();
  1200. return MessageManager::get_message_data_sent($from, $number_of_items, $column, $direction);
  1201. }