Completed
Push — master ( 939bf6...806dd7 )
by Arnaud
11:58
created

PollController::frameAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright 2014-2016 Arnaud Bienvenu
4
 *
5
 * This file is part of Kyela.
6
7
 * Kyela is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
12
 * Kyela is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with Kyela.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Abienvenu\KyelaBundle\Controller;
23
24
use Abienvenu\KyelaBundle\Form\Type\FormActionsType;
25
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
26
use Symfony\Component\HttpFoundation\Request;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
28
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
30
use Abienvenu\KyelaBundle\Entity\Poll;
31
use Abienvenu\KyelaBundle\Entity\Choice;
32
use Abienvenu\KyelaBundle\Entity\Participant;
33
use Abienvenu\KyelaBundle\Form\Type\PollType;
34
use Abienvenu\KyelaBundle\Form\Type\NewPollType;
35
use Abienvenu\KyelaBundle\Form\Type\LockPollType;
36
use Abienvenu\KyelaBundle\Form\Type\ParticipantType;
37
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
38
39
/**
40
 * Poll controller.
41
 *
42
 * @Route("/")
43
 */
44
class PollController extends CRUDController
45
{
46
    protected $entityName = 'KyelaBundle:Poll';
47
    protected $cancelRoute = 'poll_show';
48
    protected $successRoute = 'poll_show';
49
    protected $deleteRoute = 'poll_delete';
50
    protected $deleteSuccessRoute = 'poll_new';
51
52
    /**
53
     * Displays a form to create a new Poll entity.
54
     *
55
     * @Route("/", name="poll_new")
56
     * @Method({"GET", "POST"})
57
     * @Template()
58
     */
59
    public function newAction(Request $request)
60
    {
61
        $poll = new Poll();
62
63
        // Setup default (and hidden) values
64
        $poll->setUrl(uniqid());
65
        $poll->setHeadLines('');
66
        $poll->setBottomLines('');
67
        $poll->setAccessCode('');
68
        $t = $this->get('translator');
69
        $poll->addChoice((new Choice)->setName($t->trans('yes'))->setValue(1)->setColor('green')->setPriority(0)->setPoll($poll)->setIcon('ok'));
70
        $poll->addChoice((new Choice)->setName($t->trans('maybe'))->setValue(0)->setColor('orange')->setPriority(1)->setPoll($poll)->setIcon('time'));
71
        $poll->addChoice((new Choice)->setName($t->trans('no'))->setValue(0)->setColor('red')->setPriority(2)->setPoll($poll)->setIcon('remove'));
72
73
        $baseUrl = $this->generateUrl('poll_show', ['pollUrl' => $poll->getUrl()], UrlGeneratorInterface::ABSOLUTE_URL);
74
        $successMessage = $this->get('translator')->trans('poll.created %url%', ['%url%' => $baseUrl]);
75
76
        return $this->doNewAction(NewPollType::class, $poll, $request, $successMessage);
77
    }
78
79
    /**
80
     * Shows the poll
81
     *
82
     * @Route("/{pollUrl}/", name="poll_show")
83
     * @Method("GET")
84
     * @Template()
85
     */
86
    public function showAction()
87
    {
88
        $em = $this->getDoctrine()->getManager();
89
        $hasPastEvents = count($em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($this->poll, false));
90
91
        $participant_form = $this->createForm(ParticipantType::class, new Participant(), [
92
            'action' => $this->generateUrl('participant_new'),
93
            'method' => 'POST'
94
        ]);
95
96
        return [
97
            'poll' => $this->poll,
98
            'hasPastEvents' => $hasPastEvents,
99
            'participant_form' => $participant_form->createView(),
100
        ];
101
    }
102
103
    /**
104
     * Show a simplified poll to be used in iframe
105
     *
106
     * @Route("/{pollUrl}/frame", name="poll_showframe")
107
     * @Method("GET")
108
     * @Template()
109
     */
110
    public function frameAction()
111
    {
112
        return ['poll' => $this->poll];
113
    }
114
115
    /**
116
     * Shows the poll with past events only
117
     *
118
     * @Route("/{pollUrl}/archive", name="poll_archive")
119
     * @Method("GET")
120
     * @Template()
121
     */
122
    public function archiveAction()
123
    {
124
        return ['poll' => $this->poll];
125
    }
126
127
    /**
128
     * Displays poll events
129
     *
130
     * @Method("GET")
131
     * @Template()
132
     */
133
    public function eventsAction($isFuture)
134
    {
135
        $em = $this->getDoctrine()->getManager();
136
        $events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($this->poll, $isFuture);
137
        return [
138
            'poll' => $this->poll,
139
            'events' => $events,
140
        ];
141
    }
142
143
    /**
144
     * Displays interative participation table
145
     *
146
     * @Method("GET")
147
     * @Template()
148
     */
149
    public function participationsAction($isFuture)
150
    {
151
        $em = $this->getDoctrine()->getManager();
152
        $events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($this->poll, $isFuture);
153
        $choices = $em->getRepository("KyelaBundle:Choice")->getOrderedChoices($this->poll);
154
        $participationsArray = [];
155
        foreach ($events as $event)
156
        {
157
            foreach ($event->getParticipations() as $participation)
158
            {
159
                $accessKey = "{$event->getId()}-{$participation->getParticipant()->getId()}";
160
                $participationsArray[$accessKey] = $participation;
161
            }
162
        }
163
        return [
164
            'poll' => $this->poll,
165
            'choices' => $choices,
166
            'events' => $events,
167
            'participations' => $participationsArray,
168
        ];
169
    }
170
171
    /**
172
     * Displays latest comments
173
     *
174
     * @Method("GET")
175
     * @Template()
176
     */
177
    public function commentsAction()
178
    {
179
        $em = $this->getDoctrine()->getManager();
180
        $comments = $em->getRepository('KyelaBundle:Comment')->getLatestComments($this->poll);
181
        return ['poll' => $this->poll, 'comments' => $comments];
182
    }
183
184
    /**
185
     * Displays a form to edit an existing Poll entity.
186
     *
187
     * @Route("/{pollUrl}/edit", name="poll_edit")
188
     * @Method({"GET", "PUT"})
189
     * @Template()
190
     */
191
    public function editAction(Request $request, $pollUrl)
192
    {
193
        if ($this->poll->getAccessCode()) {
194
            return $this->redirect($this->generateUrl('poll_unlock', ['pollUrl' => $this->poll->getUrl()]));
195
        }
196
        $oldUrl = $this->poll->getUrl();
197
        $response = $this->doEditAction(PollType::class, $this->poll->getId(), $request);
198
        if ($request->isMethod('PUT') && $oldUrl != $this->poll->getUrl())
199
        {
200
            $baseUrl = $this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()], UrlGeneratorInterface::ABSOLUTE_URL);
201
            $flashMessage = $this->get('translator')->trans('poll.modified %url%', ['%url%' => $baseUrl]);
202
            $request->getSession()->getFlashBag()->add('success', $flashMessage);
203
        }
204
        return $response;
205
    }
206
207
    /**
208
     * Deletes a Poll entity.
209
     *
210
     * @Route("/{pollUrl}/", name="poll_delete")
211
     * @Method("DELETE")
212
     */
213
    public function deleteAction(Request $request, $pollUrl)
214
    {
215
        return $this->doDeleteAction($request, $this->poll->getId());
216
    }
217
218
    /**
219
     * Display a form to setup a lock on the Poll
220
     *
221
     * @Route("/{pollUrl}/lock", name="poll_lock")
222
     * @Method({"GET", "PUT"})
223
     * @Template()
224
     */
225
    public function lockAction(Request $request)
226
    {
227
        $form = $this->createForm(LockPollType::class, $this->poll, array(
228
            'method' => 'PUT',
229
        ));
230
231
        $form->add('actions', FormActionsType::class, [
232
            'buttons' => [
233
                'save' => ['type' => 'submit', 'options' => ['label' => 'save']],
234
                'cancel' => ['type' => 'submit', 'options' => ['label' => 'cancel', 'attr' => ['type' => 'default', 'novalidate' => true]]],
235
            ]
236
        ]);
237
238
        if ($request->isMethod('PUT') && $this->poll)
239
        {
240
            $em = $this->getDoctrine()->getManager();
241
            $form->handleRequest($request);
242
            if ($form->get('actions')->get('cancel')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
                $em->refresh($this->poll);
244
                return $this->redirect($this->generateUrl('poll_edit', ['pollUrl' => $this->poll->getUrl()]));
245
            }
246
            if ($form->isValid()) {
247
                $em->flush();
248
                $flashMessage = $this->get('translator')->trans('poll.locked %lock%', ['%lock%' => $this->poll->getAccessCode()]);
249
                $request->getSession()->getFlashBag()->add('success', $flashMessage);
250
                return $this->redirect($this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()]));
251
            }
252
            else {
253
                $em->refresh($this->poll);
254
            }
255
        }
256
257
        return [
258
            'poll'   => $this->poll,
259
            'form'   => $form->createView(),
260
        ];
261
    }
262
263
    /**
264
     * Display a form to unlock the Poll
265
     *
266
     * @Route("/{pollUrl}/unlock", name="poll_unlock")
267
     * @Method({"GET", "PUT"})
268
     * @Template()
269
     */
270
    public function unlockAction(Request $request)
271
    {
272
        $poll = (new Poll)->setTitle("dummy")->setUrl("dummy");
273
        $form = $this->createForm(LockPollType::class, $poll, array(
274
            'method' => 'PUT',
275
        ));
276
        $form->add('actions', FormActionsType::class, [
277
            'buttons' => [
278
                'save' => ['type' => SubmitType::class, 'options' => ['label' => 'save']],
279
                'cancel' => ['type' => SubmitType::class, 'options' => ['label' => 'cancel', 'attr' => ['type' => 'default', 'novalidate' => true]]],
280
            ]
281
        ]);
282
283
        if ($request->isMethod('PUT'))
284
        {
285
            $form->handleRequest($request);
286
            if ($form->get('actions')->get('cancel')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
287
                return $this->redirect($this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()]));
288
            }
289
            if ($form->isValid()) {
290
                if ($poll->getAccessCode() == $this->poll->getAccessCode()) {
291
                    $this->poll->setAccessCode('');
292
                    $em = $this->getDoctrine()->getManager();
293
                    $em->flush();
294
                    $flashMessage = $this->get('translator')->trans('poll.unlocked');
295
                    $request->getSession()->getFlashBag()->add('success', $flashMessage);
296
                    return $this->redirect($this->generateUrl('poll_edit', ['pollUrl' => $this->poll->getUrl()]));
297
                }
298
                else {
299
                    $flashMessage = $this->get('translator')->trans('unlock.failed');
300
                    $request->getSession()->getFlashBag()->add('success', $flashMessage);
301
                }
302
            }
303
        }
304
305
        return [
306
            'poll'   => $this->poll,
307
            'form'   => $form->createView(),
308
        ];
309
    }
310
}
311