activity.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. require_once(PATH_PHP.'database.php');
  4. function alog($type,$id,$action){
  5. $aid = create_action($action);
  6. if(query("INSERT INTO `activity` (%s_id,a_id) VALUES (%d,%d)",array($type,$id,$aid))){
  7. return true;
  8. }
  9. return false;
  10. }
  11. function create_action($action){
  12. if($aid = get_action_id($action)){
  13. return $aid;
  14. }elseif(query("INSERT INTO `actions` (name) VALUES ('%s')",array($action))){
  15. return mysqli_insert_id(get_sql());
  16. }
  17. return false;
  18. }
  19. function get_action_id($action){
  20. if($res = query("SELECT a.id,a.name FROM `actions` AS a WHERE a.name = '%s'",array($action))){
  21. if($res->num_rows == 1){
  22. $action = $res->fetch_assoc();
  23. return $action['id'];
  24. }
  25. }
  26. return false;
  27. }
  28. function activityObj($id){
  29. if($res = query("SELECT UNIX_TIMESTAMP(a.date) as timestamp,
  30. a.id,
  31. ac.name AS action,
  32. CASE
  33. WHEN a.u_id IS NOT NULL THEN 'u'
  34. WHEN a.p_id IS NOT NULL THEN 'p'
  35. WHEN a.i_id IS NOT NULL THEN 'i'
  36. WHEN a.m_id IS NOT NULL THEN 'm'
  37. ELSE 'other'
  38. END AS type,
  39. a.u_id,
  40. a.p_id,
  41. a.i_id,
  42. a.m_id
  43. FROM `activity` AS a
  44. JOIN `actions` AS ac ON ac.id = a.a_id
  45. WHERE a.id = %d
  46. ",array($id))){
  47. if($res->num_rows == 1){
  48. $activity = $res->fetch_assoc();
  49. $ret = array(
  50. 'id'=>$activity['id'],
  51. 'timestamp'=>$activity['timestamp'],
  52. 'action'=>$activity['action'],
  53. 'type'=>$activity['type']
  54. );
  55. switch($activity['type']){
  56. case 'u':
  57. $ret['rel'] = userObj((int)$activity['u_id']);
  58. if(isset($ret['rel']['name'])){
  59. $ret['rel']['title'] = $ret['rel']['name'];
  60. $ret['url'] = '#~'.$ret['rel']['name'];
  61. }
  62. break;
  63. case 'p':
  64. $ret['rel'] = projectObj($activity['p_id']);
  65. $ret['url'] = '#project-'.$ret['rel']['id'];
  66. $ret['rel']['title'] = 'Project - '.$ret['rel']['title'];
  67. break;
  68. case 'i':
  69. $ret['rel'] = issueObj($activity['i_id']);
  70. $ret['url'] = '#!'.$ret['rel']['id'];
  71. $ret['rel']['title'] = 'Issue - '.$ret['rel']['title'];
  72. break;
  73. case 'm':
  74. $ret['rel'] = messageObj($activity['i_id']);
  75. $ret['url'] = '#message-'.$ret['rel']['id'];
  76. break;
  77. default:
  78. $ret['rel'] = array();
  79. }
  80. return $ret;
  81. }
  82. }
  83. return false;
  84. }
  85. ?>