1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DepartmentAPI extends VolunteerAPI |
4
|
|
|
{ |
5
|
|
|
use Processor; |
6
|
|
|
|
7
|
|
|
public function __construct() |
8
|
|
|
{ |
9
|
|
|
parent::__construct('departments', 'departmentID'); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function setup($app) |
13
|
|
|
{ |
14
|
|
|
parent::setup($app); |
15
|
|
|
$app->get('/{dept}/roles[/]', array($this, 'getRolesForDepartment')); |
16
|
|
|
$app->post('/{dept}/roles[/]', array($this, 'createRoleForDepartment')); |
17
|
|
|
$app->patch('/{dept}/roles/{roleName}[/]', array($this, 'updateRoleForDepartment')); |
18
|
|
|
$app->get('/{dept}/shifts[/]', array($this, 'getShiftsForDepartment')); |
19
|
|
|
$app->post('/{dept}/shifts[/]', array($this, 'createShiftForDepartment')); |
20
|
|
|
$app->get('/{dept}/shifts/Actions/GenerateShiftSchedule', array($this, 'generateShiftSchedule')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function canEditDept($request, $deptId, $dept = null) |
24
|
|
|
{ |
25
|
|
|
if($this->isVolunteerAdmin($request)) |
26
|
|
|
{ |
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
if($dept !== null) |
30
|
|
|
{ |
31
|
|
|
return $this->isUserDepartmentLead2($dept, $this->user); |
32
|
|
|
} |
33
|
|
|
return $this->isUserDepartmentLead($deptId, $this->user); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function canUpdate($request, $entity) |
37
|
|
|
{ |
38
|
|
|
return $this->canEditDept($request, false); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function canDelete($request, $entity) |
42
|
|
|
{ |
43
|
|
|
if($this->isVolunteerAdmin($request)) |
44
|
|
|
{ |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function processEntry($entry, $request) |
51
|
|
|
{ |
52
|
|
|
$entry['available'] = true; |
53
|
|
|
$entry['isAdmin'] = $this->canEditDept($request, null, $entry); |
54
|
|
|
if(isset($entry['public']) && $entry['public'] === false) |
55
|
|
|
{ |
56
|
|
|
if(!$this->isUserDepartmentLead2($entry, $this->user)) |
57
|
|
|
{ |
58
|
|
|
$entry['available'] = false; |
59
|
|
|
$entry['why'] = 'Not lead of department'; |
60
|
|
|
} |
61
|
|
|
if(!$entry['available'] && !$entry['isAdmin']) |
62
|
|
|
{ |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $entry; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getRolesForDepartment($request, $response, $args) |
70
|
|
|
{ |
71
|
|
|
$deptId = $args['dept']; |
72
|
|
|
if($this->canEditDept($request, $deptId) === false) |
73
|
|
|
{ |
74
|
|
|
return $response->withStatus(401); |
75
|
|
|
} |
76
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles'); |
77
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
78
|
|
|
$filter = new \Data\Filter("departmentID eq '$deptId'"); |
79
|
|
|
$roles = $dataTable->read($filter, $odata->select, $odata->top, |
80
|
|
|
$odata->skip, $odata->orderby); |
81
|
|
|
if($roles === false) |
82
|
|
|
{ |
83
|
|
|
$roles = array(); |
84
|
|
|
} |
85
|
|
|
$count = count($roles); |
86
|
|
|
for($i = 0; $i < $count; $i++) |
87
|
|
|
{ |
88
|
|
|
$roles[$i] = $this->processRole($roles[$i], $request); |
89
|
|
|
} |
90
|
|
|
return $response->withJson($roles); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getShiftsForDepartment($request, $response, $args) |
94
|
|
|
{ |
95
|
|
|
$deptId = $args['dept']; |
96
|
|
|
if($this->canEditDept($request, $deptId) === false) |
97
|
|
|
{ |
98
|
|
|
return $response->withStatus(401); |
99
|
|
|
} |
100
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
101
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
102
|
|
|
$filter = $this->addRequiredFilter('departmentID', $deptId, $odata); |
103
|
|
|
if($filter === false) |
104
|
|
|
{ |
105
|
|
|
return $response->withStatus(409); |
106
|
|
|
} |
107
|
|
|
$shifts = $dataTable->read($filter, $odata->select, $odata->top, |
108
|
|
|
$odata->skip, $odata->orderby); |
109
|
|
|
if($shifts === false) |
110
|
|
|
{ |
111
|
|
|
$shifts = array(); |
112
|
|
|
} |
113
|
|
|
return $response->withJson($shifts); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function createEntityForDepartment($request, $response, $args, $table) |
117
|
|
|
{ |
118
|
|
|
$deptId = $args['dept']; |
119
|
|
|
if($this->canEditDept($request, $deptId) === false) |
120
|
|
|
{ |
121
|
|
|
return $response->withStatus(401); |
122
|
|
|
} |
123
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', $table); |
124
|
|
|
$obj = $request->getParsedBody(); |
125
|
|
|
if($obj === null) |
126
|
|
|
{ |
127
|
|
|
$obj = json_decode($request->getBody()->getContents(), true); |
128
|
|
|
} |
129
|
|
|
$obj['departmentID'] = $deptId; |
130
|
|
|
if($table === 'shifts') |
131
|
|
|
{ |
132
|
|
|
if(isset($obj['unbounded']) && $obj['unbounded']) |
133
|
|
|
{ |
134
|
|
|
if(!isset($obj['minShifts']) || $obj['minShifts'] === 0 || $obj['minShifts'] === '') |
135
|
|
|
{ |
136
|
|
|
$obj['minShifts'] = '1'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
$ret = $dataTable->create($obj); |
141
|
|
|
return $response->withJson($ret); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function createRoleForDepartment($request, $response, $args) |
145
|
|
|
{ |
146
|
|
|
return $this->createEntityForDepartment($request, $response, $args, 'roles'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function createShiftForDepartment($request, $response, $args) |
150
|
|
|
{ |
151
|
|
|
return $this->createEntityForDepartment($request, $response, $args, 'shifts'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function updateRoleForDepartment($request, $response, $args) |
155
|
|
|
{ |
156
|
|
|
$deptId = $args['dept']; |
157
|
|
|
$roleId = $args['roleName']; |
158
|
|
|
if($this->canEditDept($request, $deptId) === false) |
159
|
|
|
{ |
160
|
|
|
return $response->withStatus(401); |
161
|
|
|
} |
162
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles'); |
163
|
|
|
$filter = new \Data\Filter("departmentID eq '$deptId' and short_name eq '$roleId'"); |
164
|
|
|
$entry = $dataTable->read($filter); |
165
|
|
|
if(empty($entry)) |
166
|
|
|
{ |
167
|
|
|
return $response->withStatus(404); |
168
|
|
|
} |
169
|
|
|
$obj = $this->getParsedBody($request); |
170
|
|
|
$ret = $dataTable->update($filter, $obj); |
171
|
|
|
return $response->withJson($ret); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function generateShiftSchedule($request, $response, $args) |
175
|
|
|
{ |
176
|
|
|
$deptId = $args['dept']; |
177
|
|
|
if($this->canEditDept($request, $deptId) === false) |
178
|
|
|
{ |
179
|
|
|
return $response->withStatus(401); |
180
|
|
|
} |
181
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments'); |
182
|
|
|
$depts = $dataTable->read(new \Data\Filter('departmentID eq '.$deptId)); |
183
|
|
|
if(empty($depts)) |
184
|
|
|
{ |
185
|
|
|
return $response->withStatus(404); |
186
|
|
|
} |
187
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
188
|
|
|
$eventId = $request->getParam('eventID'); |
189
|
|
|
$filter = new \Data\Filter('eventID eq '.$eventId.' and departmentID eq '.$deptId); |
190
|
|
|
$shifts = $dataTable->read($filter); |
191
|
|
|
if(empty($shifts)) |
192
|
|
|
{ |
193
|
|
|
return $response->withStatus(404); |
194
|
|
|
} |
195
|
|
|
switch($request->getParam('type')) |
196
|
|
|
{ |
197
|
|
|
case 'simplePDF': |
198
|
|
|
return $this->generateSimplePDFSchedule($depts[0], $shifts, $response); |
199
|
|
|
case 'gridXLSX': |
200
|
|
|
return $this->generateGridSchedule($depts[0], $shifts, $response, 'XLSX'); |
201
|
|
|
case 'gridPDF': |
202
|
|
|
return $this->generateGridSchedule($depts[0], $shifts, $response, 'PDF'); |
203
|
|
|
} |
204
|
|
|
return $response->withJson($shifts); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function generateSimplePDFSchedule($dept, $shifts, $response) |
208
|
|
|
{ |
209
|
|
|
$pdf = new \Schedules\SimplePDF($dept, $shifts); |
210
|
|
|
$response = $response->withHeader('Content-Type', 'application/pdf'); |
211
|
|
|
$response->getBody()->write($pdf->toPDFBuffer()); |
212
|
|
|
return $response; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function generateGridSchedule($dept, $shifts, $response, $type) |
216
|
|
|
{ |
217
|
|
|
$ss = new \Schedules\GridSchedule($dept, $shifts); |
218
|
|
|
$data = $ss->getBuffer($type); |
219
|
|
|
$response = $response->withHeader('Content-Type', $data['content-type']); |
220
|
|
|
$response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$data['extension']); |
221
|
|
|
$response->getBody()->write($data['buffer']); |
222
|
|
|
return $response; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
226
|
|
|
|