Completed
Push — FVSv2 ( 1ec6b9...63e0fa )
by Patrick
01:36
created

ShiftAPI::isVolunteerAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 10
loc 10
rs 9.9332
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('events');
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
    }
19
20
    protected function canCreate($request)
21
    {
22
        if($this->isVolunteerAdmin($request))
23
        {
24
            return true;
25
        }
26
        //TODO give access to department lead
27
        return false;
28
    }
29
30
    protected function canUpdate($request, $entity)
31
    {
32
 	if($this->isVolunteerAdmin($request))
33
        {
34
            return true;
35
        }
36
        return $this->isUserDepartmentLead($entity['departmentID'], $this->user);
1 ignored issue
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    protected function canDelete($request, $entity)
40
    {
41
        return $this->canUpdate($request, $entity);
42
    }
43
44
    protected function processEntry($entry, $request)
45
    {
46
        return $this->processShift($entry, $request);
47
    }
48
49
    protected function genUUID()
50
    {
51
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
52
            // 32 bits for "time_low"
53
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
54
55
            // 16 bits for "time_mid"
56
            mt_rand(0, 0xffff),
57
58
            // 16 bits for "time_hi_and_version",
59
            // four most significant bits holds version number 4
60
            mt_rand(0, 0x0fff) | 0x4000,
61
62
            // 16 bits, 8 bits for "clk_seq_hi_res",
63
            // 8 bits for "clk_seq_low",
64
            // two most significant bits holds zero and one for variant DCE1.1
65
            mt_rand(0, 0x3fff) | 0x8000,
66
67
            // 48 bits for "node"
68
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
69
        );
70
    }
71
72
    public function createGroup($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        $array = $request->getParsedBody();
75
        $count = count($array);
76
        $entArray = array();
77
        $uuid = $this->genUUID();
78
        $dataTable = $this->getDataTable();
79
        //User must be able to edit all shifts
80
        for($i = 0; $i < $count; $i++)
81
        {
82
            $filter = $this->getFilterForPrimaryKey($array[$i]);
83
            $entity = $dataTable->read($filter);
84
            if($entity === false || !isset($entity[0]))
85
            {
86
                return $response->withStatus(404);
87
            }
88
            $entity = $entity[0];
89
            if(!$this->canUpdate($request, $entity))
90
            {
91
                return $response->withStatus(401);
92
            }
93
            $entity['groupID'] = $uuid;
94
            array_push($entArray, $entity);
95
        }
96
        //If we got here we can update them all
97
        $myRet = true;
98
        $errors = array();
99
        for($i = 0; $i < $count; $i++)
100
        {
101
            $filter = $this->getFilterForPrimaryKey($array[$i]);
102
            $ret = $dataTable->update($filter, $entArray[$i]);
103
            if($ret === false)
104
            {
105
               $myRet = false;
106
               array_push($errors, $array[$i]);
107
            }
108
        }
109
        if($myRet)
110
        {
111
            return $response->withJson($myRet);
112
        }
113
        else
114
        {
115
            return $response->withJson(array('res'=>$myRet, 'errors'=>$errors));
116
        }
117
    }
118
119
    public function newGroup($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        if(!$this->canCreate($request))
122
        {
123
            return $response->withStatus(401);
124
        }
125
        $data = $request->getParsedBody();
126
        $shift = array();
127
        $shift['groupID'] = $this->genUUID();
128
        $shift['departmentID'] = $data['groupDepartmentID'];
129
        $shift['earlyLate'] = $data['groupEarlyLate'];
130
        $shift['enabled'] = $data['groupEnabled'];
131
        $shift['endTime'] = $data['groupEndTime'];
132
        $shift['eventID'] = $data['groupEvent'];
133
        $shift['name'] = $data['groupName'];
134
        $shift['startTime'] = $data['groupStartTime'];
135
        $dataTable = $this->getDataTable();
136
        $ret = true;
137
        foreach($data['roles'] as $role=>$count)
138
        {
139
            $count = intval($count);
140
            for($i = 0; $i < $count; $i++)
141
            {
142
                $shift['roleID'] = $role;
143
                if($dataTable->create($shift) === false)
144
                {
145
                    $ret = false;
146
                }
147
            }
148
        }
149
        return $response->withJSON($ret);
150
    }
151
152
    public function signup($request, $response, $args)
153
    {
154
        $this->validateLoggedIn($request);
155
        $shiftId = $args['shift'];
156
        $dataTable = $this->getDataTable();
157
        $filter = $this->getFilterForPrimaryKey($shiftId);
158
        $entity = $dataTable->read($filter);
159
        if(empty($entity))
160
        {
161
            return $response->withStatus(404);
162
        }
163
        $entity = $entity[0];
164
        if(isset($entity['participant']) && strlen($entity['participant']) > 0)
165
        {
166
            return $response->withStatus(401);
167
        }
168
        $entity = $this->processShift($entity, $request);
169
        if(isset($entity['overlap']) && $entity['overlap'])
170
        {
171
            $overlaps = $this->findOverlaps($entity, $this->user->uid);
172
            $count = count($overlaps);
173
            $leads = array();
174
            for($i = 0; $i < $count; $i++)
175
            {
176
            	$leads = array_merge($leads, $this->getLeadForDepartment($overlaps[$i]['departmentID']));
177
                $overlaps[$i]['status'] = 'pending';
178
            }
179
            $leads = array_merge($leads, $this->getLeadForDepartment($entity['departmentID']));
180
            $leads = array_unique($leads);
181
            $entity['participant'] = $this->user->uid;
182
            $entity['status'] = 'pending';
183
            $email = new \Emails\TwoShiftsAtOnceEmail($profile);
0 ignored issues
show
Bug introduced by
The variable $profile does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
184
            $email->addLeads($leads);
185
            return $response->withJSON($email, 500);
186
        }
187
        if(isset($entity['available']) && $entity['available'])
188
        {
189
            $entity['participant'] = $this->user->uid;
190
            $entity['status'] = 'filled';
191
            return $response->withJSON($dataTable->update($filter, $entity));
192
        }
193
        print_r($entity); die();
194
    }
195
196
    public function abandon($request, $response, $args)
197
    {
198
        $this->validateLoggedIn($request);
199
        $shiftId = $args['shift'];
200
        $dataTable = $this->getDataTable();
201
        $filter = $this->getFilterForPrimaryKey($shiftId);
202
        $entity = $dataTable->read($filter);
203
        if(empty($entity))
204
        {
205
            return $response->withStatus(404);
206
        }
207
        $entity = $entity[0];
208
        if(!isset($entity['participant']) || $entity['participant'] !== $this->user->uid)
209
        {
210
            return $response->withStatus(401);
211
        }
212
        $entity['participant'] = '';
213
        $entity['status'] = 'unfilled';
214
        return $response->withJSON($dataTable->update($filter, $entity));
215
    }
216
}
217