activity.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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($activity['u_id']);
  58. $ret['url'] = '#~'.$ret['rel']['name'];
  59. break;
  60. case 'p':
  61. $ret['rel'] = projectObj($activity['p_id']);
  62. $ret['url'] = '#project-'.$ret['rel']['id'];
  63. $ret['rel']['title'] = 'Project - '.$ret['rel']['title'];
  64. break;
  65. case 'i':
  66. $ret['rel'] = issueObj($activity['i_id']);
  67. $ret['url'] = '#!'.$ret['rel']['id'];
  68. $ret['rel']['title'] = 'Issue - '.$ret['rel']['title'];
  69. break;
  70. case 'm':
  71. $ret['rel'] = messageObj($activity['i_id']);
  72. $ret['url'] = '#message-'.$ret['rel']['id'];
  73. break;
  74. default:
  75. $ret['rel'] = array();
  76. }
  77. return $ret;
  78. }
  79. }
  80. return false;
  81. }
  82. ?>