Completed
Push — FVSv2 ( 7038c9...fdb1fd )
by Patrick
01:39
created

ParticipantAPI::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
class ParticipantAPI extends VolunteerAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('participants', 'uid');
7
    }
8
9
    public function setup($app)
10
    {
11
        parent::setup($app);
12
        $app->get('/me/shifts[/]', array($this, 'getMyShifts'));
13
    }
14
15
    protected function canCreate($request)
16
    {
17
        $this->validateLoggedIn($request);
18
        return true;
19
    }
20
21
    protected function canRead($request)
22
    {
23
        if($this->isVolunteerAdmin($request))
24
        {
25
            return true;
26
        }
27
        //TODO give access to department leads
28
        return true;
29
    }
30
31
    protected function canUpdate($request, $entity)
32
    {
33
 	if($this->isVolunteerAdmin($request))
34
        {
35
            return true;
36
        }       
37
        //TODO give access to department lead
38
        return false;
39
    }
40
41
    protected function canDelete($request, $entity)
42
    {
43
        return $this->canUpdate($request, $entity);
44
    }
45
46
    protected function validateCreate(&$obj, $request)
47
    {
48
        if(isset($obj['uid']))
49
        {
50
            return false;
51
        }
52
        $uid = $this->user->uid;
53
        $dataTable = $this->getDataTable();
54
        $filter = $this->getFilterForPrimaryKey($uid);
55
        $users = $dataTable->read($filter);
56
        if(!empty($users))
57
        {
58
            //User is already created...
59
            return false;
60
        }
61
        $obj['uid'] = $uid;
62
        return true;
63
    }
64
65
    protected function getFilterForPrimaryKey($value)
66
    {
67
        if($value === 'me')
68
        {
69
            $value = $this->user->uid;
70
        }
71
        return parent::getFilterForPrimaryKey($value);
72
    }
73
74
    public function readEntry($request, $response, $args)
75
    {
76
        $this->validateLoggedIn($request);
77
        $uid = $args['name'];
78
        if($uid === 'me')
79
        {
80
            $uid = $this->user->uid;
81
        }
82
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
83
        {
84
            return $response->withStatus(401);
85
        }
86
        $dataTable = $this->getDataTable();
87
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
88
        $filter = $this->getFilterForPrimaryKey($uid);
89
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
90
                                  $odata->skip, $odata->orderby);
91
        if(empty($areas))
92
        {
93
            return $response->withStatus(404);
94
        }
95
        return $response->withJson($areas[0]);
96
    }
97
98
    public function getMyShifts($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...
99
    {
100
        $this->validateLoggedIn($request);
101
        $uid = $this->user->uid;
102
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
103
        $filter = new \Data\Filter("participant eq '$uid'");
104
        $shifts = $dataTable->read($filter);
105
        $text = "BEGIN:VCALENDAR\r\n";
106
        $text.= "VERSION:2.0\r\n";
107
        $text.= "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
108
        $count = count($shifts);
109
        for($i = 0; $i < $count; $i++)
110
        {
111
            $text.= "BEGIN:VEVENT\r\n";
112
            $text.= "UID:".$this->user->mail."\r\n";
113
            $d = new DateTime($shifts[$i]['startTime']);
114
            $d->setTimezone(new \DateTimeZone('UTC'));
115
            $text.= "DTSTAMP:".$d->format('Ymd\THis\Z')."\r\n";
116
            $text.= "DTSTART:".$d->format('Ymd\THis\Z')."\r\n";
117
            $d = new DateTime($shifts[$i]['endTime']);
118
            $d->setTimezone(new \DateTimeZone('UTC'));
119
            $text.= "DTEND:".$d->format('Ymd\THis\Z')."\r\n";
120
            $text.= "SUMMARY:".$shifts[$i]['roleID'].' '.$shifts[$i]['name']."\r\n";
121
            $text.= "END:VEVENT\r\n";
122
        }
123
        $text.= "END:VCALENDAR\r\n";
124
        $response = $response->withHeader('Content-type', 'text/calendar');
125
        $response = $response->withHeader('Content-Disposition', 'attachment; filename="MyShifts.ics"');
126
        $body = $response->getBody();
127
        $body->write($text);
128
        return $response;
129
    }
130
}
131