1
|
|
|
<?php |
2
|
|
|
class EventAPI extends VolunteerAPI |
3
|
|
|
{ |
4
|
|
|
use Processor; |
|
|
|
|
5
|
|
|
|
6
|
|
|
public function __construct() |
7
|
|
|
{ |
8
|
|
|
parent::__construct('events'); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function setup($app) |
12
|
|
|
{ |
13
|
|
|
parent::setup($app); |
14
|
|
|
$app->get('/{event}/shifts[/]', array($this, 'getShiftsForEvent')); |
15
|
|
|
$app->post('/{event}/shifts[/]', array($this, 'createShiftForEvent')); |
16
|
|
|
$app->get('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent')); |
17
|
|
|
$app->post('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent')); |
18
|
|
|
$app->post('/{event}/Actions/ApproveEE', array($this, 'approveEEForEvent')); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function getFilterForPrimaryKey($value) |
22
|
|
|
{ |
23
|
|
|
return new \Data\Filter($this->primaryKeyName." eq '$value' or alias eq '$value'"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function canUpdate($request, $entity) |
27
|
|
|
{ |
28
|
|
|
if($this->isVolunteerAdmin($request)) |
29
|
|
|
{ |
30
|
|
|
return true; |
31
|
|
|
} |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function canDelete($request, $entity) |
36
|
|
|
{ |
37
|
|
|
if($this->isVolunteerAdmin($request)) |
38
|
|
|
{ |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function processEntry($entry, $request) |
45
|
|
|
{ |
46
|
|
|
$entry['available'] = true; |
47
|
|
|
$endTime = new DateTime($entry['endTime']); |
48
|
|
|
$now = new DateTime(); |
49
|
|
|
if($endTime < $now) |
50
|
|
|
{ |
51
|
|
|
$entry['available'] = false; |
52
|
|
|
$entry['why'] = 'Event is in the past'; |
53
|
|
|
} |
54
|
|
|
if(isset($entry['volList']) && !is_array($entry['volList'])) |
55
|
|
|
{ |
56
|
|
|
$entry['volList'] = explode(',', $entry['volList']); |
57
|
|
|
$count = count($entry['volList']); |
58
|
|
|
for($i = 0; $i < $count; $i++) |
59
|
|
|
{ |
60
|
|
|
$entry['volList'][$i] = trim($entry['volList'][$i]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
if($entry['private'] && !in_array($this->user->mail, $entry['volList'])) |
64
|
|
|
{ |
65
|
|
|
$entry['available'] = false; |
66
|
|
|
$entry['why'] = 'Event is private and you are not invited'; |
67
|
|
|
} |
68
|
|
|
if(!$entry['available'] && !$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user)) |
69
|
|
|
{ |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
if(!$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user) && isset($entry['eeLists'])) |
73
|
|
|
{ |
74
|
|
|
unset($entry['eeLists']); |
75
|
|
|
} |
76
|
|
|
return $entry; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getShiftsForEvent($request, $response, $args) |
80
|
|
|
{ |
81
|
|
|
$this->validateLoggedIn($request); |
82
|
|
|
$eventId = $args['event']; |
83
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
84
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
85
|
|
|
$filter = $this->addRequiredFilter('eventID', $eventId, $odata); |
86
|
|
|
if($filter === false) |
87
|
|
|
{ |
88
|
|
|
return $response->withStatus(409); |
89
|
|
|
} |
90
|
|
|
$shifts = $dataTable->read($filter, $odata->select, $odata->top, |
91
|
|
|
$odata->skip, $odata->orderby); |
92
|
|
|
if($shifts === false) |
93
|
|
|
{ |
94
|
|
|
$shifts = array(); |
95
|
|
|
} |
96
|
|
|
$count = count($shifts); |
|
|
|
|
97
|
|
|
for($i = 0; $i < $count; $i++) |
98
|
|
|
{ |
99
|
|
|
$shifts[$i] = $this->processShift($shifts[$i], $request); |
100
|
|
|
} |
101
|
|
|
$shifts = array_values(array_filter($shifts)); |
|
|
|
|
102
|
|
|
return $response->withJson($shifts); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function createShiftForEvent($request, $response, $args) |
106
|
|
|
{ |
107
|
|
|
$eventId = $args['event']; |
108
|
|
|
if($this->canUpdate($request, null) === false) |
109
|
|
|
{ |
110
|
|
|
return $response->withStatus(401); |
111
|
|
|
} |
112
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
113
|
|
|
$obj = $request->getParsedBody(); |
114
|
|
|
if($obj == NULL) |
115
|
|
|
{ |
116
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
117
|
|
|
} |
118
|
|
|
$obj['eventID'] = $eventId; |
119
|
|
|
$ret = $dataTable->create($obj); |
120
|
|
|
return $response->withJson($ret); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getEEShiftReportForEvent($request, $response, $args) |
124
|
|
|
{ |
125
|
|
|
$eventId = $args['event']; |
126
|
|
|
if($this->canUpdate($request, null) === false) |
127
|
|
|
{ |
128
|
|
|
return $response->withStatus(401); |
129
|
|
|
} |
130
|
|
|
$shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
131
|
|
|
$obj = $request->getParsedBody(); |
132
|
|
|
if($obj == NULL) |
133
|
|
|
{ |
134
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
135
|
|
|
} |
136
|
|
|
$filterStr = 'eventID eq '.$eventId.' and status eq filled'; |
137
|
|
|
if(isset($obj['earlyLate'])) |
138
|
|
|
{ |
139
|
|
|
$filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\''; |
140
|
|
|
} |
141
|
|
|
else |
142
|
|
|
{ |
143
|
|
|
$filterStr .= " and earlyLate ne '-1'"; |
144
|
|
|
} |
145
|
|
|
$filter = new \Data\Filter($filterStr); |
146
|
|
|
$shifts = $shiftDataTable->read($filter); |
147
|
|
|
$ret = array(); |
148
|
|
|
$count = count($shifts); |
|
|
|
|
149
|
|
|
for($i = 0; $i < $count; $i++) |
150
|
|
|
{ |
151
|
|
|
$shift = new \VolunteerShift(false, $shifts[$i]); |
152
|
|
|
$vol = $shift->participantObj; |
|
|
|
|
153
|
|
|
$role = $shift->role; |
|
|
|
|
154
|
|
|
$entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate); |
155
|
|
|
array_push($ret, $entry); |
156
|
|
|
} |
157
|
|
|
return $response->withJson($ret); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function userCanAuth($type) |
161
|
|
|
{ |
162
|
|
|
switch($type) |
163
|
|
|
{ |
164
|
|
|
case 'aar': |
165
|
|
|
return $this->user->isInGroupNamed('AAR'); |
166
|
|
|
case 'af': |
167
|
|
|
return $this->user->isInGroupNamed('AFs'); |
168
|
|
|
case 'lead': |
169
|
|
|
return $this->user->isInGroupNamed('Leads'); |
170
|
|
|
default: |
171
|
|
|
error_log('Unknown auth type: '.$type); |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function approveEEForEvent($request, $response, $args) |
177
|
|
|
{ |
178
|
|
|
$eventId = $args['event']; |
179
|
|
|
if($this->canUpdate($request, null) === false) |
180
|
|
|
{ |
181
|
|
|
return $response->withStatus(401); |
182
|
|
|
} |
183
|
|
|
$event = new \VolunteerEvent($eventId); |
184
|
|
|
$obj = $this->getParsedBody($request); |
185
|
|
|
//First make sure the current user can do the auth they are trying... |
186
|
|
|
if($this->userCanAuth($obj['approvalType']) === false) |
187
|
|
|
{ |
188
|
|
|
return $response->withStatus(401); |
189
|
|
|
} |
190
|
|
|
$eeList = $event->eeLists[intval($obj['eeList'])]; |
|
|
|
|
191
|
|
|
if(!isset($eeList[$obj['uid']])) |
192
|
|
|
{ |
193
|
|
|
return $response->withStatus(404); |
194
|
|
|
} |
195
|
|
|
$ret = $event->approveEE($obj['uid'], intval($obj['eeList']), $obj['approvalType']); |
196
|
|
|
return $response->withJson($ret); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
200
|
|
|
|