Completed
Push — FVSv2 ( fdb1fd...10d7ab )
by Patrick
08:12
created

ShiftAPI::processEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
class ShiftAPI extends VolunteerAPI
3
{
4
    use Processor;
5
6
    public function __construct()
7
    {
8
        parent::__construct('shifts');
9
    }
10
11
    public function setup($app)
12
    {
13
        parent::setup($app);
14
        $app->post('/Actions/CreateGroup', array($this, 'createGroup'));
15
        $app->post('/Actions/NewGroup', array($this, 'newGroup'));
16
        $app->post('/{shift}/Actions/Signup[/]', array($this, 'signup'));
17
        $app->post('/{shift}/Actions/Abandon[/]', array($this, 'abandon'));
18
        $app->post('/{shift}/Actions/Approve[/]', array($this, 'approvePending'));
19
        $app->post('/{shift}/Actions/Disapprove[/]', array($this, 'disapprovePending')); 
20
    }
21
22
    protected function canCreate($request)
23
    {
24
        if($this->isVolunteerAdmin($request))
25
        {
26
            return true;
27
        }
28
        //TODO give access to department lead
29
        return false;
30
    }
31
32
    protected function canUpdate($request, $entity)
33
    {
34
 	if($this->isVolunteerAdmin($request))
35
        {
36
            return true;
37
        }
38
        return $this->isUserDepartmentLead($entity['departmentID'], $this->user);
39
    }
40
41
    protected function canDelete($request, $entity)
42
    {
43
        return $this->canUpdate($request, $entity);
44
    }
45
46
    protected function processEntry($entry, $request)
47
    {
48
        return $this->processShift($entry, $request);
49
    }
50
51
    protected function genUUID()
52
    {
53
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
54
            // 32 bits for "time_low"
55
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
56
57
            // 16 bits for "time_mid"
58
            mt_rand(0, 0xffff),
59
60
            // 16 bits for "time_hi_and_version",
61
            // four most significant bits holds version number 4
62
            mt_rand(0, 0x0fff) | 0x4000,
63
64
            // 16 bits, 8 bits for "clk_seq_hi_res",
65
            // 8 bits for "clk_seq_low",
66
            // two most significant bits holds zero and one for variant DCE1.1
67
            mt_rand(0, 0x3fff) | 0x8000,
68
69
            // 48 bits for "node"
70
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
71
        );
72
    }
73
74
    public function createGroup($request, $response)
75
    {
76
        $array = $request->getParsedBody();
77
        $count = count($array);
78
        $entArray = array();
79
        $uuid = $this->genUUID();
80
        $dataTable = $this->getDataTable();
81
        //User must be able to edit all shifts
82
        for($i = 0; $i < $count; $i++)
83
        {
84
            $filter = $this->getFilterForPrimaryKey($array[$i]);
85
            $entity = $dataTable->read($filter);
86
            if($entity === false || !isset($entity[0]))
87
            {
88
                return $response->withStatus(404);
89
            }
90
            $entity = $entity[0];
91
            if(!$this->canUpdate($request, $entity))
92
            {
93
                return $response->withStatus(401);
94
            }
95
            $entity['groupID'] = $uuid;
96
            array_push($entArray, $entity);
97
        }
98
        //If we got here we can update them all
99
        $myRet = true;
100
        $errors = array();
101
        for($i = 0; $i < $count; $i++)
102
        {
103
            $filter = $this->getFilterForPrimaryKey($array[$i]);
104
            $ret = $dataTable->update($filter, $entArray[$i]);
105
            if($ret === false)
106
            {
107
               $myRet = false;
108
               array_push($errors, $array[$i]);
109
            }
110
        }
111
        if($myRet)
112
        {
113
            return $response->withJson($myRet);
114
        }
115
        else
116
        {
117
            return $response->withJson(array('res'=>$myRet, 'errors'=>$errors));
118
        }
119
    }
120
121
    public function newGroup($request, $response)
122
    {
123
        if(!$this->canCreate($request))
124
        {
125
            return $response->withStatus(401);
126
        }
127
        $data = $request->getParsedBody();
128
        $shift = array();
129
        $shift['groupID'] = $this->genUUID();
130
        $shift['departmentID'] = $data['groupDepartmentID'];
131
        $shift['earlyLate'] = $data['groupEarlyLate'];
132
        $shift['enabled'] = $data['groupEnabled'];
133
        $shift['endTime'] = $data['groupEndTime'];
134
        $shift['eventID'] = $data['groupEvent'];
135
        $shift['name'] = $data['groupName'];
136
        $shift['startTime'] = $data['groupStartTime'];
137
        $dataTable = $this->getDataTable();
138
        $ret = true;
139
        foreach($data['roles'] as $role=>$count)
140
        {
141
            $count = intval($count);
142
            for($i = 0; $i < $count; $i++)
143
            {
144
                $shift['roleID'] = $role;
145
                if($dataTable->create($shift) === false)
146
                {
147
                    $ret = false;
148
                }
149
            }
150
        }
151
        return $response->withJSON($ret);
152
    }
153
154
    public function signup($request, $response, $args)
155
    {
156
        $this->validateLoggedIn($request);
157
        $shiftId = $args['shift'];
158
        $dataTable = $this->getDataTable();
159
        $filter = $this->getFilterForPrimaryKey($shiftId);
160
        $entity = $dataTable->read($filter);
161
        if(empty($entity))
162
        {
163
            return $response->withStatus(404);
164
        }
165
        $entity = $entity[0];
166
        if(isset($entity['participant']) && strlen($entity['participant']) > 0)
167
        {
168
            return $response->withStatus(401);
169
        }
170
        $shift = new \VolunteerShift($shiftId, $entity);
171
        $entity = $this->processShift($entity, $request);
172
        if(isset($entity['overlap']) && $entity['overlap'])
173
        {
174
            $overlaps = $shift->findOverlaps($this->user->uid);
175
            $count = count($overlaps);
176
            $leads = array();
177
            for($i = 0; $i < $count; $i++)
178
            {
179
                $dept = new \VolunteerDepartment($overlaps[$i]->departmentID);
180
                $leads = array_merge($leads, $dept->getLeadEmails());
181
                $overlaps[$i]->status = 'pending';
182
                $tmp = new \Data\Filter('_id eq '.$overlaps[$i]->{'_id'});
183
                $res = $dataTable->update($tmp, $overlaps[$i]);
184
                if($res === false)
185
                {
186
                    return $response->withJSON(array('err'=>'Unable to update overlap with id '.$overlaps[$i]->{'_id'}));
187
                }
188
            }
189
            $dept = new \VolunteerDepartment($entity['departmentID']);
190
            $leads = array_merge($leads, $dept->getLeadEmails());
191
            $leads = array_unique($leads);
192
            $entity['participant'] = $this->user->uid;
193
            $entity['status'] = 'pending';
194
            $profile = new \VolunteerProfile($this->user->uid);
195
            $email = new \Emails\TwoShiftsAtOnceEmail($profile);
196
            $email->addLeads($leads);
197
            $emailProvider = \EmailProvider::getInstance();
198
            if($emailProvider->sendEmail($email) === false)
199
            {
200
                throw new \Exception('Unable to send duplicate email!');
201
            }
202
            return $response->withJSON($dataTable->update($filter, $entity));
203
        }
204
        if(isset($entity['available']) && $entity['available'])
205
        {
206
            $entity['participant'] = $this->user->uid;
207
            $entity['status'] = 'filled';
208
            return $response->withJSON($dataTable->update($filter, $entity));
209
        }
210
        print_r($entity); die();
211
    }
212
213
    public function abandon($request, $response, $args)
214
    {
215
        $this->validateLoggedIn($request);
216
        $shiftId = $args['shift'];
217
        $dataTable = $this->getDataTable();
218
        $filter = $this->getFilterForPrimaryKey($shiftId);
219
        $entity = $dataTable->read($filter);
220
        if(empty($entity))
221
        {
222
            return $response->withStatus(404);
223
        }
224
        $entity = $entity[0];
225
        if(!isset($entity['participant']) || $entity['participant'] !== $this->user->uid)
226
        {
227
            return $response->withStatus(401);
228
        }
229
        $entity['participant'] = '';
230
        $entity['status'] = 'unfilled';
231
        return $response->withJSON($dataTable->update($filter, $entity));
232
    }
233
234
    public function approvePending($request, $response, $args)
235
    {
236
        if(!$this->canCreate($request))
237
        {
238
            return $response->withStatus(401);
239
        }
240
        $shiftId = $args['shift'];
241
        $dataTable = $this->getDataTable();
242
        $filter = $this->getFilterForPrimaryKey($shiftId);
243
        $entity = $dataTable->read($filter);
244
        if(empty($entity))
245
        {
246
            return $response->withStatus(404);
247
        }
248
        $entity = $entity[0];
249
        $entity['status'] = 'filled';
250
        return $response->withJSON($dataTable->update($filter, $entity));
251
    }
252
253
    public function disapprovePending($request, $response, $args)
254
    {
255
        if(!$this->canCreate($request))
256
        {
257
            return $response->withStatus(401);
258
        }
259
        $shiftId = $args['shift'];
260
        $dataTable = $this->getDataTable();
261
        $filter = $this->getFilterForPrimaryKey($shiftId);
262
        $entity = $dataTable->read($filter);
263
        if(empty($entity))
264
        {
265
            return $response->withStatus(404);
266
        }
267
        $entity['participant'] = '';
268
        $entity['status'] = 'unfilled';
269
        $profile = new \VolunteerProfile($this->user->uid);
270
        $email = new \Emails\PendingRejectedEmail($profile);
271
        $email->setShift($entity);
272
        $emailProvider = \EmailProvider::getInstance();
273
        if($emailProvider->sendEmail($email) === false)
274
        {
275
            throw new \Exception('Unable to send duplicate email!');
276
        }
277
        return $response->withJSON($dataTable->update($filter, $entity));
278
    }
279
}
280