|
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
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
protected function canUpdate($request, $entity) |
|
19
|
|
|
{ |
|
20
|
|
|
if($this->isVolunteerAdmin($request)) |
|
21
|
|
|
{ |
|
22
|
|
|
return true; |
|
23
|
|
|
} |
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function canDelete($request, $entity) |
|
28
|
|
|
{ |
|
29
|
|
|
if($this->isVolunteerAdmin($request)) |
|
30
|
|
|
{ |
|
31
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function processEntry($entry, $request) |
|
37
|
|
|
{ |
|
38
|
|
|
$entry['available'] = true; |
|
39
|
|
|
$endTime = new DateTime($entry['endTime']); |
|
40
|
|
|
$now = new DateTime(); |
|
41
|
|
|
if($endTime < $now) |
|
42
|
|
|
{ |
|
43
|
|
|
$entry['available'] = false; |
|
44
|
|
|
$entry['why'] = 'Event is in the past'; |
|
45
|
|
|
} |
|
46
|
|
|
if($entry['private'] && !in_array($this->user->mail, $entry['volList'])) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$entry['available'] = false; |
|
49
|
|
|
$entry['why'] = 'Event is private and you are not invited'; |
|
50
|
|
|
} |
|
51
|
|
|
if(!$entry['available'] && !$this->isVolunteerAdmin($request)) |
|
52
|
|
|
{ |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
return $entry; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getShiftsForEvent($request, $response, $args) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->validateLoggedIn($request); |
|
61
|
|
|
$eventId = $args['event']; |
|
62
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
63
|
|
|
$filter = new \Data\Filter("eventID eq '$eventId'"); |
|
64
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
|
65
|
|
|
if($odata->filter !== false) |
|
66
|
|
|
{ |
|
67
|
|
|
$clause = $odata->filter->getClause('eventID'); |
|
68
|
|
|
if($clause !== null) |
|
69
|
|
|
{ |
|
70
|
|
|
return $response->withStatus(409); |
|
71
|
|
|
} |
|
72
|
|
|
else |
|
73
|
|
|
{ |
|
74
|
|
|
$filter->appendChild('and'); |
|
75
|
|
|
$filter->appendChild($odata->filter); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
$shifts = $dataTable->read($filter, $odata->select, $odata->top, |
|
79
|
|
|
$odata->skip, $odata->orderby); |
|
80
|
|
|
if($shifts === false) |
|
81
|
|
|
{ |
|
82
|
|
|
$shifts = array(); |
|
83
|
|
|
} |
|
84
|
|
|
$count = count($shifts); |
|
85
|
|
|
for($i = 0; $i < $count; $i++) |
|
86
|
|
|
{ |
|
87
|
|
|
$shifts[$i] = $this->processShift($shifts[$i], $request); |
|
88
|
|
|
} |
|
89
|
|
|
$shifts = array_values(array_filter($shifts)); |
|
90
|
|
|
return $response->withJson($shifts); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
public function createShiftForEvent($request, $response, $args) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$eventId = $args['event']; |
|
96
|
|
|
if($this->canUpdate($request, null) === false) |
|
97
|
|
|
{ |
|
98
|
|
|
return $response->withStatus(401); |
|
99
|
|
|
} |
|
100
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
101
|
|
|
$obj = $request->getParsedBody(); |
|
102
|
|
|
if($obj == NULL) |
|
103
|
|
|
{ |
|
104
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
|
105
|
|
|
} |
|
106
|
|
|
$obj['eventID'] = $eventId; |
|
107
|
|
|
$ret = $dataTable->create($obj); |
|
108
|
|
|
return $response->withJson($ret); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: