Completed
Push — master ( 806dd7...a6321e )
by Arnaud
01:25
created

PollController::commentsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 a form to edit an existing Poll entity.
129
     *
130
     * @Route("/{pollUrl}/edit", name="poll_edit")
131
     * @Method({"GET", "PUT"})
132
     * @Template()
133
     */
134
    public function editAction(Request $request, $pollUrl)
135
    {
136
        if ($this->poll->getAccessCode()) {
137
            return $this->redirect($this->generateUrl('poll_unlock', ['pollUrl' => $this->poll->getUrl()]));
138
        }
139
        $oldUrl = $this->poll->getUrl();
140
        $response = $this->doEditAction(PollType::class, $this->poll->getId(), $request);
141
        if ($request->isMethod('PUT') && $oldUrl != $this->poll->getUrl())
142
        {
143
            $baseUrl = $this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()], UrlGeneratorInterface::ABSOLUTE_URL);
144
            $flashMessage = $this->get('translator')->trans('poll.modified %url%', ['%url%' => $baseUrl]);
145
            $request->getSession()->getFlashBag()->add('success', $flashMessage);
146
        }
147
        return $response;
148
    }
149
150
    /**
151
     * Deletes a Poll entity.
152
     *
153
     * @Route("/{pollUrl}/", name="poll_delete")
154
     * @Method("DELETE")
155
     */
156
    public function deleteAction(Request $request, $pollUrl)
157
    {
158
        return $this->doDeleteAction($request, $this->poll->getId());
159
    }
160
161
    /**
162
     * Display a form to setup a lock on the Poll
163
     *
164
     * @Route("/{pollUrl}/lock", name="poll_lock")
165
     * @Method({"GET", "PUT"})
166
     * @Template()
167
     */
168
    public function lockAction(Request $request)
169
    {
170
        $form = $this->createForm(LockPollType::class, $this->poll, array(
171
            'method' => 'PUT',
172
        ));
173
174
        $form->add('actions', FormActionsType::class, [
175
            'buttons' => [
176
                'save' => ['type' => 'submit', 'options' => ['label' => 'save']],
177
                'cancel' => ['type' => 'submit', 'options' => ['label' => 'cancel', 'attr' => ['type' => 'default', 'novalidate' => true]]],
178
            ]
179
        ]);
180
181
        if ($request->isMethod('PUT') && $this->poll)
182
        {
183
            $em = $this->getDoctrine()->getManager();
184
            $form->handleRequest($request);
185
            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...
186
                $em->refresh($this->poll);
187
                return $this->redirect($this->generateUrl('poll_edit', ['pollUrl' => $this->poll->getUrl()]));
188
            }
189
            if ($form->isValid()) {
190
                $em->flush();
191
                $flashMessage = $this->get('translator')->trans('poll.locked %lock%', ['%lock%' => $this->poll->getAccessCode()]);
192
                $request->getSession()->getFlashBag()->add('success', $flashMessage);
193
                return $this->redirect($this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()]));
194
            }
195
            else {
196
                $em->refresh($this->poll);
197
            }
198
        }
199
200
        return [
201
            'poll'   => $this->poll,
202
            'form'   => $form->createView(),
203
        ];
204
    }
205
206
    /**
207
     * Display a form to unlock the Poll
208
     *
209
     * @Route("/{pollUrl}/unlock", name="poll_unlock")
210
     * @Method({"GET", "PUT"})
211
     * @Template()
212
     */
213
    public function unlockAction(Request $request)
214
    {
215
        $poll = (new Poll)->setTitle("dummy")->setUrl("dummy");
216
        $form = $this->createForm(LockPollType::class, $poll, array(
217
            'method' => 'PUT',
218
        ));
219
        $form->add('actions', FormActionsType::class, [
220
            'buttons' => [
221
                'save' => ['type' => SubmitType::class, 'options' => ['label' => 'save']],
222
                'cancel' => ['type' => SubmitType::class, 'options' => ['label' => 'cancel', 'attr' => ['type' => 'default', 'novalidate' => true]]],
223
            ]
224
        ]);
225
226
        if ($request->isMethod('PUT'))
227
        {
228
            $form->handleRequest($request);
229
            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...
230
                return $this->redirect($this->generateUrl('poll_show', ['pollUrl' => $this->poll->getUrl()]));
231
            }
232
            if ($form->isValid()) {
233
                if ($poll->getAccessCode() == $this->poll->getAccessCode()) {
234
                    $this->poll->setAccessCode('');
235
                    $em = $this->getDoctrine()->getManager();
236
                    $em->flush();
237
                    $flashMessage = $this->get('translator')->trans('poll.unlocked');
238
                    $request->getSession()->getFlashBag()->add('success', $flashMessage);
239
                    return $this->redirect($this->generateUrl('poll_edit', ['pollUrl' => $this->poll->getUrl()]));
240
                }
241
                else {
242
                    $flashMessage = $this->get('translator')->trans('unlock.failed');
243
                    $request->getSession()->getFlashBag()->add('success', $flashMessage);
244
                }
245
            }
246
        }
247
248
        return [
249
            'poll'   => $this->poll,
250
            'form'   => $form->createView(),
251
        ];
252
    }
253
}
254