message.lib.php 56 KB

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