messages.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. require_once(PATH_PHP.'database.php');
  4. function messages($id,$type){
  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 ASC",array($id))){
  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",array($id,$id))){
  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. }
  35. return array();
  36. }
  37. function project_comment($project,$message){
  38. 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))){
  39. return true;
  40. }
  41. return false;
  42. }
  43. function personal_message($toId,$message){
  44. 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))){
  45. return true;
  46. }
  47. return false;
  48. }
  49. ?>