Completed
Push — master ( 4f2f75...b03ed7 )
by Christopher
06:52 queued 15s
created

EventController::postEvent()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 49
Code Lines 34

Duplication

Lines 7
Ratio 14.29 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 6
nop 3
dl 7
loc 49
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace TechWilk\Rota\Controller;
4
5
use DateTime;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use TechWilk\Rota\Authoriser\EventAuthoriser;
9
use TechWilk\Rota\Comment;
10
use TechWilk\Rota\Crypt;
11
use TechWilk\Rota\Event;
12
use TechWilk\Rota\EventPerson;
13
use TechWilk\Rota\EventPersonQuery;
14
use TechWilk\Rota\EventQuery;
15
use TechWilk\Rota\EventSubTypeQuery;
16
use TechWilk\Rota\EventTypeQuery;
17
use TechWilk\Rota\GroupQuery;
18
use TechWilk\Rota\LocationQuery;
19
use TechWilk\Rota\UserQuery;
20
use TechWilk\Rota\UserRoleQuery;
21
22
class EventController extends BaseController
23
{
24
    public function getAllEvents(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
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...
25
    {
26
        $this->logger->info("Fetch event GET '/events'");
27
        $events = EventQuery::create()->filterByDate(['min' => new DateTime()])->filterByRemoved(false)->orderByDate('asc')->find();
28
29
        return $this->view->render($response, 'events.twig', ['events' => $events]);
30
    }
31
32 View Code Duplication
    public function getAllEventsWithType(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $this->logger->info("Fetch event GET '/events/type/".$args['id']."'");
35
36
        $eventType = EventTypeQuery::create()->findPk($args['id']);
37
38
        $events = EventQuery::create()->filterByDate(['min' => new DateTime()])->filterByRemoved(false)->filterByEventType($eventType)->orderByDate('asc')->find();
39
40
        return $this->view->render($response, 'events.twig', ['events' => $events]);
41
    }
42
43 View Code Duplication
    public function getAllEventsWithSubType(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->logger->info("Fetch event GET '/events/type/".$args['id']."'");
46
47
        $eventType = EventSubTypeQuery::create()->findPk($args['id']);
48
49
        $events = EventQuery::create()->filterByDate(['min' => new DateTime()])->filterByRemoved(false)->filterByEventSubType($eventType)->orderByDate('asc')->find();
50
51
        return $this->view->render($response, 'events.twig', ['events' => $events]);
52
    }
53
54
    public function postEvent(ServerRequestInterface $request, ResponseInterface $response, $args)
55
    {
56
        $this->logger->info("Create event POST '/event'");
57
58
        $data = $request->getParsedBody();
59
60
        $data['name'] = filter_var(trim($data['name']), FILTER_SANITIZE_STRING);
61
        $data['comment'] = filter_var(trim($data['comment']), FILTER_SANITIZE_STRING);
62
63
        $e = new Event();
64
        if (isset($args['id'])) {
65
            // edit existing event
66
            $returnPath = 'user';
0 ignored issues
show
Unused Code introduced by
$returnPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
            $e = EventQuery::create()->findPK($args['id']);
68
            if (!$e->authoriser()->updatableBy($this->auth->currentUser())) {
69
                return $this->view->render($response, 'error.twig');
70
            }
71
        } else {
72
            // create new event
73
            if (!EventAuthoriser::createableBy($this->auth->currentUser())) {
74
                return $this->view->render($response, 'error.twig');
75
            }
76
            $returnPath = 'user-roles';
0 ignored issues
show
Unused Code introduced by
$returnPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
            $newIdFound = false;
78 View Code Duplication
            while (!$newIdFound) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                $id = Crypt::generateInt(0, 2147483648); // largest int in db column
80
                if (is_null(EventQuery::create()->findPK($id))) {
81
                    $newIdFound = true;
82
                    $e->setId($id);
83
                }
84
            }
85
            $e->setCreatedBy($this->auth->currentUser());
86
        }
87
        $e->setName($data['name']);
88
        $e->setDate(DateTime::createFromFormat('d/m/Y H:i', $data['date'].' '.$data['time']));
89
        $e->setEventTypeId($data['type']);
90
        $e->setEventSubTypeId($data['subtype']);
91
        $e->setLocationId($data['location']);
92
93
        if (!empty($data['comment'])) {
94
            $c = new Comment();
95
            $c->setEvent($e);
96
            $c->setUser($this->auth->currentUser());
97
            $c->setText($data['comment']);
98
        }
99
        $e->save();
100
101
        return $response->withStatus(303)->withHeader('Location', $this->router->pathFor('event', ['id' => $e->getId()]));
102
    }
103
104
    public function getNewEventForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
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...
105
    {
106
        $this->logger->info("Fetch event GET '/event/new'");
107
108
        if (!EventAuthoriser::createableBy($this->auth->currentUser())) {
109
            return $this->view->render($response, 'error.twig');
110
        }
111
112
        $l = LocationQuery::create()->orderByName()->find();
113
        $et = EventTypeQuery::create()->orderByName()->find();
114
        $est = EventSubTypeQuery::create()->orderByName()->find();
115
116
        return $this->view->render($response, 'event-edit.twig', ['locations' => $l, 'eventtypes' => $et, 'eventsubtypes' => $est]);
117
    }
118
119 View Code Duplication
    public function getEvent(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $this->logger->info("Fetch event GET '/event/".$args['id']."'");
122
        $e = EventQuery::create()->findPK($args['id']);
123
124
        if (!$e->authoriser()->readableBy($this->auth->currentUser())) {
125
            return $this->view->render($response, 'error.twig');
126
        }
127
128
        if (!is_null($e)) {
129
            return $this->view->render($response, 'event.twig', ['event' => $e]);
130
        } else {
131
            return $this->view->render($response, 'error.twig');
132
        }
133
    }
134
135 View Code Duplication
    public function getEventEditForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $this->logger->info("Fetch event GET '/event/".$args['id']."/edit'");
138
        $e = EventQuery::create()->findPK($args['id']);
139
        $l = LocationQuery::create()->orderByName()->find();
140
        $et = EventTypeQuery::create()->orderByName()->find();
141
        $est = EventSubTypeQuery::create()->orderByName()->find();
142
143
        if (!is_null($e)) {
144
            return $this->view->render($response, 'event-edit.twig', ['event' => $e, 'locations' => $l, 'eventtypes' => $et, 'eventsubtypes' => $est]);
145
        } else {
146
            return $this->view->render($response, 'error.twig');
147
        }
148
    }
149
150 View Code Duplication
    public function getEventCopyForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $this->logger->info("Fetch event GET '/event/".$args['id']."/copy'");
153
        $e = EventQuery::create()->findPK($args['id']);
154
        $l = LocationQuery::create()->orderByName()->find();
155
        $et = EventTypeQuery::create()->orderByName()->find();
156
        $est = EventSubTypeQuery::create()->orderByName()->find();
157
158
        if (!is_null($e)) {
159
            return $this->view->render($response, 'event-edit.twig', ['copy' => true, 'event' => $e, 'locations' => $l, 'eventtypes' => $et, 'eventsubtypes' => $est]);
160
        } else {
161
            return $this->view->render($response, 'error.twig');
162
        }
163
    }
164
165 View Code Duplication
    public function getEventAssignForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $this->logger->info("Fetch event GET '/event/".$args['id']."/assign'");
168
        $e = EventQuery::create()->findPK($args['id']);
169
        $ur = UserRoleQuery::create()->find();
170
171
        if (!is_null($e)) {
172
            return $this->view->render($response, 'event-assign.twig', ['event' => $e, 'userroles' => $ur]);
173
        } else {
174
            return $this->view->render($response, 'error.twig');
175
        }
176
    }
177
178 View Code Duplication
    public function postEventAssign(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $this->logger->info("Create event people POST '/event".$args['id']."/assign'");
181
182
        $eventId = filter_var($args['id'], FILTER_SANITIZE_NUMBER_INT);
183
        $existingUserRoles = UserRoleQuery::create()->useEventPersonQuery()->filterByEventId($eventId)->endUse()->find();
184
185
        $existing = [];
186
        foreach ($existingUserRoles as $ur) {
187
            $existing[] = $ur->getId();
188
        }
189
190
        $data = $request->getParsedBody();
191
192
        if (!is_array($data['userrole'])) {
193
            // delete all roles
194
            $eps = EventPersonQuery::create()->filterByEventId($eventId)->find();
195
            foreach ($eps as $ep) {
196
                $ep->delete();
197
            }
198
        } else {
199
            // sanitize data from user
200
            foreach ($data['userrole'] as $key => $userRole) {
201
                $data['userrole'][$key] = filter_var(trim($userRole), FILTER_SANITIZE_NUMBER_INT);
202
            }
203
204
            // add new roles
205
            $addArray = array_diff($data['userrole'], $existing);
206
            foreach ($addArray as $roleToAdd) {
207
                $ep = new EventPerson();
208
                $ep->setUserRoleId($roleToAdd);
209
                $ep->setEventId($eventId);
210
                $ep->save();
211
            }
212
213
            // remove existing roles
214
            $deleteArray = array_diff($existing, $data['userrole']);
215
            foreach ($deleteArray as $roleToRemove) {
216
                $ep = EventPersonQuery::create()->filterByEventId($eventId)->filterByUserRoleId($roleToRemove)->findOne();
217
                $ep->delete();
218
            }
219
        }
220
221
        return $response->withStatus(303)->withHeader('Location', $this->router->pathFor('event', ['id' => $eventId]));
222
    }
223
224
    public function getAllEventsToPrint(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
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...
225
    {
226
        $this->logger->info("Fetch event printable page GET '/events/print'");
227
228
        $events = EventQuery::create()
229
            ->filterByDate(['min' => new DateTime()])
230
            ->filterByRemoved(false)
231
            ->orderByDate('asc')
232
            ->find();
233
234
        $groups = GroupQuery::create()
235
            ->filterById(136)
236
            ->find();
237
238
        $users = UserQuery::create()
239
            ->useUserRoleQuery()
240
                ->filterByReserve(false)
241
                ->useRoleQuery()
242
                    ->filterByGroup($groups)
243
                ->endUse()
244
            ->endUse()
245
            ->distinct()
246
            ->find();
247
248
        return $this->view->render($response, 'events-print.twig', ['events' => $events, 'groups' => $groups, 'users' => $users]);
249
    }
250
251 View Code Duplication
    public function getAllEventInfoToPrint(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
    {
253
        $this->logger->info("Fetch event printable page GET '/events/print/info'");
254
255
        $events = EventQuery::create()
256
            ->filterByDate(['min' => new DateTime()])
257
            ->filterByRemoved(false)
258
            ->orderByDate('asc')
259
            ->find();
260
261
        $groups = GroupQuery::create()
262
            ->filterById(136)
263
            ->find();
264
265
        return $this->view->render($response, 'events-print-info.twig', ['events' => $events, 'groups' => $groups]);
266
    }
267
}
268