/api/1/restricted/eventinfo.php

https://github.com/dreamhackcrew/API · PHP · 151 lines · 111 code · 30 blank · 10 comment · 33 complexity · 5859a8eb21428da290b637569df42575 MD5 · raw file

  1. <?php
  2. class eventinfo extends service {
  3. function _get($events=null, $uid=null) {/*{{{*/
  4. if ( $events == null || $events == "current" ) {
  5. $events = array(db()->fetchOne("SELECT id FROM events WHERE active ='Y' AND end > CURRENT_DATE() ORDER BY start LIMIT 1"));
  6. } else {
  7. $events = explode('|',$events);
  8. // Only allow numbers
  9. $events = preg_grep('/^\d+$/',$events);
  10. }
  11. if ( $uid == null ) {
  12. $uid = $_SESSION['id'];
  13. }
  14. return $this->fetchEventinfo($events[0], $uid);
  15. }/*}}}*/
  16. function _checkin($event=null, $uid=null) {/*{{{*/
  17. // Check that the user have access
  18. $this->requireFlag('crewhantering');
  19. if ( $event == null || $event == "current" ) {
  20. $event = db()->fetchOne("SELECT id FROM events WHERE active ='Y' AND end > CURRENT_DATE() ORDER BY start LIMIT 1");
  21. } else {
  22. // Only allow numbers
  23. $event = intval($event);
  24. }
  25. if ( $uid == null ) {
  26. $uid = $_SESSION['id'];
  27. }
  28. $checkedinby = db()->fetchOne("SELECT username FROM users where uid = %d", $_SESSION['id']);
  29. db()->query("UPDATE user_eventinfo SET checkedin = now(), checkedinby = '%s' WHERE uid = %d AND event = %d", $checkedinby, $uid, $event);
  30. return $this->fetchEventinfo($event, $uid);
  31. }/*}}}*/
  32. function fetchEventinfo($event, $uid) {
  33. // Check that the user have access
  34. $this->requireFlag('crewhantering');
  35. if ( !$res = db()->fetchSingle('SELECT size, gsize, arrive, arrive_time, depart, depart_time, car, dinner, checkedin, checkedinby FROM user_eventinfo WHERE uid=%d and event in (%s)',$uid, $event) )
  36. return array(
  37. 'error' => 'The user have not completed the "Event information"-form'
  38. );
  39. $eventinfo = array(
  40. 'tshirt_size' => $res['size'],
  41. 'gift_tshirt_size' => $res['gsize'],
  42. 'arrival_date' => $res['arrive'],
  43. 'arrival_time' => $res['arrive_time'],
  44. 'departure_date' => $res['depart'],
  45. 'departure_time' => $res['depart_time'],
  46. 'dinner' => $res['dinner'] == 1,
  47. 'car_registration_number' => $res['car'] != '' ? $res['car'] : null
  48. );
  49. if ( $res['checkedin'] == '0000-00-00 00:00:00' ) {
  50. $eventinfo['checkedin'] = false;
  51. $eventinfo['checkedin_at'] = null;
  52. $eventinfo['checkedin_by'] = null;
  53. } else {
  54. $eventinfo['checkedin'] = true;
  55. $eventinfo['checkedin_at'] = $res['checkedin'];
  56. $eventinfo['checkedin_by'] = $res['checkedinby'];
  57. }
  58. return $eventinfo;
  59. }
  60. function _search( $events, $search ) {/*{{{*/
  61. // Check that the user have access
  62. //$this->requireFlag('crewhantering');
  63. if ( $events == null || $events == "current" ) {
  64. $events = array(db()->fetchOne("SELECT id FROM events WHERE active ='Y' AND end > CURRENT_DATE() ORDER BY start LIMIT 1"));
  65. } else {
  66. $events = explode('|',$events);
  67. // Only allow numbers
  68. $events = preg_grep('/^\d+$/',$events);
  69. }
  70. if ( !$events || !reset($events) )
  71. $events = array(0);
  72. $search = ltrim($search,'0');
  73. if ( !$search )
  74. return !trigger_error('The search string is to short',E_USER_ERROR);
  75. // Do the search
  76. if ( $u = db()->fetchAll("
  77. SELECT users.uid,username,firstname,lastname,city,car,allowed_arrive FROM users
  78. LEFT JOIN user_profile
  79. USING(uid)
  80. LEFT JOIN user_eventinfo
  81. ON user_eventinfo.uid=users.uid AND user_eventinfo.event IN (%s)
  82. WHERE
  83. ( concat(firstname,' ',lastname) LIKE '%%%2\$s%%'
  84. OR username LIKE '%%%2\$s%%'
  85. OR city LIKE '%%%2\$s%%'
  86. OR birthdate = '%2\$s'
  87. OR primaryphone LIKE '%%%2\$s%%'
  88. OR secondaryphone LIKE '%%%2\$s%%'
  89. OR user_profile.email LIKE '%%%2\$s%%'
  90. OR user_eventinfo.car LIKE '%%%2\$s%%'
  91. ) AND NOT level = 'disabled'
  92. ORDER BY firstname, lastname DESC LIMIT 20
  93. ",implode($events,','),$search) ) {
  94. foreach($u AS $key1=>$line1){
  95. // Get profile pictures
  96. if ( $pictures = db()->fetchAll("SELECT max(id) id,ident FROM images WHERE ident LIKE 'users.%%.%d' GROUP BY ident",$line1['uid']) )
  97. foreach($pictures as $key => $line) {
  98. switch(substr($line['ident'],0,11) ) {
  99. case 'users.badge':
  100. if ( $hash = db()->fetchOne("SELECT file FROM images WHERE id=%d LIMIT 1",$line['id']) )
  101. $u[$key1]['badge_picture'] = "api.crew.dreamhack.se/1/image/".$hash;
  102. break;
  103. case 'users.press':
  104. if ( $hash = db()->fetchOne("SELECT file FROM images WHERE id=%d LIMIT 1",$line['id']) )
  105. $u[$key1]['profile_picture'] = "api.crew.dreamhack.se/1/image/".$hash;
  106. break;
  107. }
  108. }
  109. // Get team memberships
  110. if ( $teams = db()->fetchAll("SELECT * FROM membership JOIN groups ON groups.gid=membership.gid AND groups.event IN (%s) WHERE uid=%d",implode($events,','),$line1['uid']) ) {
  111. foreach($teams as $key => $line) {
  112. $teams[$key] = db()->fetchAll("SELECT gid,name,is_team FROM groups WHERE lft <= %d AND rgt >= %d ORDER BY lft ASC",$line['lft'],$line['rgt']);
  113. }
  114. $u[$key1]['teams'] = $teams;
  115. }
  116. }
  117. }
  118. return $u;
  119. }/*}}}*/
  120. }
  121. ?>