messages.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. require_once(PATH_PHP.'database.php');
  4. function messages($id,$type,$start=0,$amount=10){
  5. switch($type){
  6. case 'project':
  7. if($res = query("SELECT m.id, u.name, m.message, UNIX_TIMESTAMP(m.timestamp) as timestamp FROM `messages` m JOIN `users` u ON u.id = m.from_id WHERE m.p_id='%d' ORDER BY m.timestamp DESC LIMIT %d,%d",array($id,$start,$amount))){
  8. $arr = array();
  9. while($row = $res->fetch_assoc()){
  10. array_push($arr,$row);
  11. }
  12. return $arr;
  13. }
  14. break;
  15. case 'user':
  16. if($res = query("SELECT m.id, uf.name as \"from\", ut.name as \"to\", m.message, UNIX_TIMESTAMP(m.timestamp) as timestamp, m.timestamp > ut.last_pm_check as is_new FROM `messages` m JOIN `users` uf ON uf.id = m.from_id JOIN `users` ut ON ut.id = m.to_id WHERE (m.from_id='%d' AND m.to_id IS NOT NULL) OR m.to_id='%d' ORDER BY m.timestamp DESC LIMIT %d,%d",array($id,$id,$start,$amount))){
  17. $arr = array();
  18. while($row = $res->fetch_assoc()){
  19. $msg = array(
  20. 'id'=>$row['id'],
  21. 'from'=>$row['from'],
  22. 'to'=>$row['to'],
  23. 'message'=>$row['message'],
  24. 'timestamp'=>$row['timestamp']
  25. );
  26. if($row['is_new']){
  27. $msg['is_new'] = true;
  28. }
  29. array_push($arr,$msg);
  30. }
  31. return $arr;
  32. }
  33. break;
  34. case 'issue':
  35. if($res = query("SELECT m.id, u.name, m.message, UNIX_TIMESTAMP(m.timestamp) as timestamp FROM `messages` m JOIN `users` u ON u.id = m.from_id WHERE m.i_id='%d' ORDER BY m.timestamp DESC LIMIT %d,%d",array($id,$start,$amount))){
  36. $arr = array();
  37. while($row = $res->fetch_assoc()){
  38. array_push($arr,$row);
  39. }
  40. return $arr;
  41. }
  42. break;
  43. }
  44. return array();
  45. }
  46. function messageObj($id){
  47. $message = array();
  48. return $message;
  49. }
  50. function project_comment($project,$message){
  51. if(query("INSERT INTO `bugs`.`messages` (`id`,`timestamp`,`from_id`,`to_id`,`p_id`,`s_id`,`i_id`,`message`) VALUES(NULL,CURRENT_TIMESTAMP,'%d',NULL,'%d',NULL,NULL,'%s');",array(userId($_SESSION['username']),$project,$message))){
  52. alog('p',$project,'Comment added');
  53. return true;
  54. }
  55. return false;
  56. }
  57. function issue_comment($issue,$message){
  58. if(query("INSERT INTO `bugs`.`messages` (`id`,`timestamp`,`from_id`,`to_id`,`p_id`,`s_id`,`i_id`,`message`) VALUES(NULL,CURRENT_TIMESTAMP,'%d',NULL,NULL,NULL,'%d','%s');",array(userId($_SESSION['username']),$issue,$message))){
  59. alog('i',$issue,'Comment added');
  60. return true;
  61. }
  62. return false;
  63. }
  64. function personal_message($toId,$message){
  65. if(query("INSERT INTO `bugs`.`messages` (`id`,`timestamp`,`from_id`,`to_id`,`p_id`,`s_id`,`i_id`,`message`) VALUES(NULL,CURRENT_TIMESTAMP,'%d','%d',NULL,NULL,NULL,'%s');",array(userId($_SESSION['username']),$toId,$message))){
  66. return true;
  67. }
  68. return false;
  69. }
  70. ?>