Completed
Push — FVSv2 ( 2079dd...e85196 )
by Patrick
01:41
created

DepartmentAPI::updateRoleForDepartment()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 3
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
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
        $filter = new \Data\Filter("departmentID eq '$deptId'");
78
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
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
        $filter = new \Data\Filter("departmentID eq '$deptId'");
102
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
103
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
104
                                  $odata->skip, $odata->orderby);
105
        if($shifts === false)
106
        {
107
            $shifts = array();
108
        }
109
        return $response->withJson($shifts);
110
    }
111
112 View Code Duplication
    protected function createEntityForDepartment($request, $response, $args, $table)
113
    {
114
        $deptId = $args['dept'];
115
        if($this->canEditDept($request, $deptId) === false)
116
        {
117
            return $response->withStatus(401);
118
        }
119
        $dataTable = DataSetFactory::getDataTableByNames('fvs', $table);
120
        $obj = $request->getParsedBody();
121
        if($obj === null)
122
        {
123
            $obj = json_decode($request->getBody()->getContents(), true);
124
        }
125
        $obj['departmentID'] = $deptId;
126
        $ret = $dataTable->create($obj);
127
        return $response->withJson($ret);
128
    }
129
130
    public function createRoleForDepartment($request, $response, $args)
131
    {
132
        return $this->createEntityForDepartment($request, $response, $args, 'roles');
133
    }
134
135
    public function createShiftForDepartment($request, $response, $args)
136
    {
137
        return $this->createEntityForDepartment($request, $response, $args, 'shifts');
138
    }
139
140
    public function updateRoleForDepartment($request, $response, $args)
141
    {
142
        $deptId = $args['dept'];
143
        $roleId = $args['roleName'];
144
        if($this->canEditDept($request, $deptId) === false)
145
        {
146
            return $response->withStatus(401);
147
        }
148
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
149
        $filter = new \Data\Filter("departmentID eq '$deptId' and short_name eq '$roleId'");
150
        $entry = $dataTable->read($filter);
151
        if(empty($entry))
152
        {
153
            return $response->withStatus(404);
154
        }
155
        $obj = $request->getParsedBody();
156
        if($obj === null)
157
        {
158
            $request->getBody()->rewind();
159
            $obj = $request->getBody()->getContents();
160
            $tmp = json_decode($obj, true);
161
            if($tmp !== null)
162
            {
163
                $obj = $tmp;
164
            }
165
        }
166
        $ret = $dataTable->update($filter, $obj);
167
        return $response->withJson($ret);
168
    }
169
170
    public function generateShiftSchedule($request, $response, $args)
171
    {
172
        $deptId = $args['dept'];
173
        if($this->canEditDept($request, $deptId) === false)
174
        {
175
            return $response->withStatus(401);
176
        }
177
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
178
        $depts = $dataTable->read(new \Data\Filter('departmentID eq '.$deptId));
179
        if(empty($depts))
180
        {
181
            return $response->withStatus(404);
182
        }
183
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
184
        $eventId = $request->getParam('eventID');
185
        $filter = new \Data\Filter('eventID eq '.$eventId.' and departmentID eq '.$deptId);
186
        $shifts = $dataTable->read($filter);
187
        if(empty($shifts))
188
        {
189
            return $response->withStatus(404);
190
        }
191
        switch($request->getParam('type'))
192
        {
193
            case 'simplePDF':
194
               return $this->generateSimplePDFSchedule($depts[0], $shifts, $response);
195
            case 'gridXLSX':
196
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'XLSX');
197
            case 'gridPDF':
198
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'PDF');
199
        }
200
        return $response->withJson($shifts);
201
    }
202
203
    public function generateSimplePDFSchedule($dept, $shifts, $response)
204
    {
205
        $pdf = new \Schedules\SimplePDF($dept, $shifts);
206
        $response = $response->withHeader('Content-Type', 'application/pdf');
207
        $response->getBody()->write($pdf->toPDFBuffer());
208
        return $response;
209
    }
210
211
    public function generateGridSchedule($dept, $shifts, $response, $type)
212
    {
213
        $ss = new \Schedules\GridSchedule($dept, $shifts);
214
        $data = $ss->getBuffer($type);
215
        $response = $response->withHeader('Content-Type', $data['content-type']);
216
        $response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$data['extension']);
217
        $response->getBody()->write($data['buffer']);
218
        return $response;
219
    }
220
}
221
222