2016-03-05 00:18:10 +00:00
< ? php
2016-02-29 15:59:36 +00:00
namespace App\Http\Controllers ;
use App\Models\Attendee ;
2016-03-05 00:18:10 +00:00
use App\Models\Event ;
2016-02-29 15:59:36 +00:00
use Carbon\Carbon ;
2016-03-05 00:18:10 +00:00
use DB ;
use Input ;
use Response ;
use View ;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
class EventCheckInController extends MyBaseController
{
2016-02-29 15:59:36 +00:00
/**
* @ param $event_id
2016-03-05 00:18:10 +00:00
*
2016-02-29 15:59:36 +00:00
* @ return mixed
*/
2016-03-05 00:18:10 +00:00
public function showCheckIn ( $event_id )
{
2016-02-29 15:59:36 +00:00
$data [ 'event' ] = Event :: scope () -> findOrFail ( $event_id );
$data [ 'attendees' ] = $data [ 'event' ] -> attendees ;
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
return View :: make ( 'ManageEvent.CheckIn' , $data );
}
2016-03-05 00:18:10 +00:00
public function postCheckInSearch ( $event_id )
{
2016-02-29 15:59:36 +00:00
$searchQuery = Input :: get ( 'q' );
$attendees = Attendee :: scope () -> withoutCancelled ()
-> join ( 'tickets' , 'tickets.id' , '=' , 'attendees.ticket_id' )
2016-03-05 00:18:10 +00:00
-> where ( function ( $query ) use ( $event_id ) {
2016-02-29 15:59:36 +00:00
$query -> where ( 'attendees.event_id' , '=' , $event_id );
2016-03-05 00:18:10 +00:00
}) -> where ( function ( $query ) use ( $searchQuery ) {
$query -> orWhere ( 'attendees.first_name' , 'like' , $searchQuery . '%' )
-> orWhere ( DB :: raw ( " CONCAT_WS(' ', first_name, last_name) " ), 'like' , $searchQuery . '%' )
2016-02-29 15:59:36 +00:00
//->orWhere('attendees.email', 'like', $searchQuery . '%')
2016-03-05 00:18:10 +00:00
-> orWhere ( 'attendees.reference' , 'like' , $searchQuery . '%' )
-> orWhere ( 'attendees.last_name' , 'like' , $searchQuery . '%' );
2016-02-29 15:59:36 +00:00
})
-> select ([
'attendees.id' ,
'attendees.first_name' ,
'attendees.last_name' ,
'attendees.email' ,
'attendees.reference' ,
'attendees.arrival_time' ,
'attendees.has_arrived' ,
2016-03-05 00:18:10 +00:00
'tickets.title as ticket' ,
2016-02-29 15:59:36 +00:00
])
-> orderBy ( 'attendees.first_name' , 'ASC' )
-> get ();
return Response :: json ( $attendees );
}
2016-03-05 00:18:10 +00:00
public function postCheckInAttendee ()
{
2016-02-29 15:59:36 +00:00
$attendee_id = Input :: get ( 'attendee_id' );
2016-03-05 00:18:10 +00:00
$checking = Input :: get ( 'checking' );
2016-02-29 15:59:36 +00:00
$attendee = Attendee :: scope () -> find ( $attendee_id );
/*
* Ugh
*/
if ((( $checking == 'in' ) && ( $attendee -> has_arrived == 1 )) || (( $checking == 'out' ) && ( $attendee -> has_arrived == 0 ))) {
return Response :: json ([
2016-03-05 00:18:10 +00:00
'status' => 'error' ,
'message' => 'Warning: This Attendee Has Already Been Checked ' . (( $checking == 'in' ) ? 'In (at ' . $attendee -> arrival_time -> format ( 'H:i A, F j' ) . ')' : 'Out' ) . '!' ,
2016-02-29 15:59:36 +00:00
'checked' => $checking ,
2016-03-05 00:18:10 +00:00
'id' => $attendee -> id ,
2016-02-29 15:59:36 +00:00
]);
}
$attendee -> has_arrived = ( $checking == 'in' ) ? 1 : 0 ;
$attendee -> arrival_time = Carbon :: now ();
$attendee -> save ();
return Response :: json ([
2016-03-05 00:18:10 +00:00
'status' => 'success' ,
2016-02-29 15:59:36 +00:00
'checked' => $checking ,
2016-03-05 00:18:10 +00:00
'message' => 'Attendee Successfully Checked ' . (( $checking == 'in' ) ? 'In' : 'Out' ),
'id' => $attendee -> id ,
2016-02-29 15:59:36 +00:00
]);
}
}