|
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
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function canUpdate($request, $entity) |
|
21
|
|
|
{ |
|
22
|
|
|
if($this->isVolunteerAdmin($request)) |
|
23
|
|
|
{ |
|
24
|
|
|
return true; |
|
25
|
|
|
} |
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function canDelete($request, $entity) |
|
30
|
|
|
{ |
|
31
|
|
|
if($this->isVolunteerAdmin($request)) |
|
32
|
|
|
{ |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function processEntry($entry, $request) |
|
39
|
|
|
{ |
|
40
|
|
|
$entry['available'] = true; |
|
41
|
|
|
$endTime = new DateTime($entry['endTime']); |
|
42
|
|
|
$now = new DateTime(); |
|
43
|
|
|
if($endTime < $now) |
|
44
|
|
|
{ |
|
45
|
|
|
$entry['available'] = false; |
|
46
|
|
|
$entry['why'] = 'Event is in the past'; |
|
47
|
|
|
} |
|
48
|
|
|
if($entry['private'] && !in_array($this->user->mail, $entry['volList'])) |
|
49
|
|
|
{ |
|
50
|
|
|
$entry['available'] = false; |
|
51
|
|
|
$entry['why'] = 'Event is private and you are not invited'; |
|
52
|
|
|
} |
|
53
|
|
|
if(!$entry['available'] && !$this->isVolunteerAdmin($request)) |
|
54
|
|
|
{ |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
return $entry; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getShiftsForEvent($request, $response, $args) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->validateLoggedIn($request); |
|
63
|
|
|
$eventId = $args['event']; |
|
64
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
65
|
|
|
$filter = new \Data\Filter("eventID eq '$eventId'"); |
|
66
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
|
67
|
|
View Code Duplication |
if($odata->filter !== false) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$clause = $odata->filter->getClause('eventID'); |
|
70
|
|
|
if($clause !== null) |
|
71
|
|
|
{ |
|
72
|
|
|
return $response->withStatus(409); |
|
73
|
|
|
} |
|
74
|
|
|
else |
|
75
|
|
|
{ |
|
76
|
|
|
$filter->appendChild('and'); |
|
77
|
|
|
$filter->appendChild($odata->filter); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
$shifts = $dataTable->read($filter, $odata->select, $odata->top, |
|
81
|
|
|
$odata->skip, $odata->orderby); |
|
82
|
|
|
if($shifts === false) |
|
83
|
|
|
{ |
|
84
|
|
|
$shifts = array(); |
|
85
|
|
|
} |
|
86
|
|
|
$count = count($shifts); |
|
87
|
|
|
for($i = 0; $i < $count; $i++) |
|
88
|
|
|
{ |
|
89
|
|
|
$shifts[$i] = $this->processShift($shifts[$i], $request); |
|
90
|
|
|
} |
|
91
|
|
|
$shifts = array_values(array_filter($shifts)); |
|
92
|
|
|
return $response->withJson($shifts); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function createShiftForEvent($request, $response, $args) |
|
96
|
|
|
{ |
|
97
|
|
|
$eventId = $args['event']; |
|
98
|
|
|
if($this->canUpdate($request, null) === false) |
|
99
|
|
|
{ |
|
100
|
|
|
return $response->withStatus(401); |
|
101
|
|
|
} |
|
102
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
103
|
|
|
$obj = $request->getParsedBody(); |
|
104
|
|
|
if($obj == NULL) |
|
105
|
|
|
{ |
|
106
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
|
107
|
|
|
} |
|
108
|
|
|
$obj['eventID'] = $eventId; |
|
109
|
|
|
$ret = $dataTable->create($obj); |
|
110
|
|
|
return $response->withJson($ret); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getEEShiftReportForEvent($request, $response, $args) |
|
114
|
|
|
{ |
|
115
|
|
|
$eventId = $args['event']; |
|
116
|
|
|
if($this->canUpdate($request, null) === false) |
|
117
|
|
|
{ |
|
118
|
|
|
return $response->withStatus(401); |
|
119
|
|
|
} |
|
120
|
|
|
$shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
121
|
|
|
$obj = $request->getParsedBody(); |
|
122
|
|
|
if($obj == NULL) |
|
123
|
|
|
{ |
|
124
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
|
125
|
|
|
} |
|
126
|
|
|
$filterStr = 'eventID eq '.$eventId.' and status eq filled'; |
|
127
|
|
|
if(isset($obj['earlyLate'])) |
|
128
|
|
|
{ |
|
129
|
|
|
$filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\''; |
|
130
|
|
|
} |
|
131
|
|
|
else |
|
132
|
|
|
{ |
|
133
|
|
|
$filterStr .= " and earlyLate ne '-1'"; |
|
134
|
|
|
} |
|
135
|
|
|
$filter = new \Data\Filter($filterStr); |
|
136
|
|
|
$shifts = $shiftDataTable->read($filter); |
|
137
|
|
|
$ret = array(); |
|
138
|
|
|
$count = count($shifts); |
|
139
|
|
|
for($i = 0; $i < $count; $i++) |
|
140
|
|
|
{ |
|
141
|
|
|
$shift = new \VolunteerShift(false, $shifts[$i]); |
|
142
|
|
|
$vol = $shift->participantObj; |
|
|
|
|
|
|
143
|
|
|
$role = $shift->role; |
|
|
|
|
|
|
144
|
|
|
$entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate); |
|
145
|
|
|
array_push($ret, $entry); |
|
146
|
|
|
} |
|
147
|
|
|
return $response->withJson($ret); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.