messages.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 project_comment($project,$message){
  47. 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))){
  48. return true;
  49. }
  50. return false;
  51. }
  52. function issue_comment($issue,$message){
  53. 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))){
  54. return true;
  55. }
  56. return false;
  57. }
  58. function personal_message($toId,$message){
  59. 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))){
  60. return true;
  61. }
  62. return false;
  63. }
  64. ?>