Completed
Push — master ( a6321e...a24bbc )
by Arnaud
02:06
created

StaticController::thanksAction()   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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\RedirectResponse;
30
use Symfony\Component\Yaml\Yaml;
31
use Abienvenu\KyelaBundle\Form\Type\ContactType;
32
33
/**
34
 * Static content controller.
35
 *
36
 * @Route("/")
37
 */
38
class StaticController extends PollSetterController
39
{
40
    /**
41
     * Load additionnal translations
42
     *
43
     * @param string $domain
44
     * @param string $locale
45
     * @return array
46
     */
47
    protected function loadTranslations($domain, $locale)
48
    {
49
        $r = new \ReflectionClass($this);
50
        $dirName = dirname($r->getFileName());
51
        $fullFileName = "$dirName/../Resources/translations/$domain.$locale.yml";
52
        return Yaml::parse(file_get_contents($fullFileName));
53
    }
54
55
    /**
56
     * Displays the FAQ
57
     *
58
     * @Method("GET")
59
     * @Template()
60
     */
61
    public function faqAction(Request $request)
62
    {
63
        return ["poll" => $this->poll, "faq" => $this->loadTranslations("faq", $request->getLocale())];
64
    }
65
66
    /**
67
     * Displays the About page
68
     *
69
     * @Method("GET")
70
     * @Template()
71
     */
72
    public function aboutAction(Request $request)
73
    {
74
        return ["poll" => $this->poll, "about" => $this->loadTranslations("about", $request->getLocale())];
75
    }
76
77
    /**
78
     * Displays the Tips page
79
     *
80
     * @Method("GET")
81
     * @Template()
82
     */
83
    public function tipsAction(Request $request)
84
    {
85
        return ["poll" => $this->poll, "tips" => $this->loadTranslations("tips", $request->getLocale())];
86
    }
87
88
    /**
89
     * Displays the Thanks page
90
     *
91
     * @Method("GET")
92
     * @Template()
93
     */
94
    public function thanksAction()
95
    {
96
        return ["poll" => $this->poll];
97
    }
98
99
    /**
100
     * Switch locale
101
     *
102
     * @Method("GET")
103
     */
104
    public function switchAction(Request $request)
105
    {
106
        if ($request->headers->has('referer'))
107
        {
108
            $returnUrl = $request->headers->get('referer');
109
        }
110
        else
111
        {
112
            $returnUrl = $this->generateUrl("poll_new");
113
        }
114
        return new RedirectResponse($returnUrl, 302);
115
    }
116
117
    /**
118
     * Display the Contact form
119
     *
120
     * @Template()
121
     */
122
    public function contactAction(Request $request)
123
    {
124
        $form = $this->createForm(ContactType::class);
125
        $form->add('actions', FormActionsType::class, [
126
            'buttons' => [
127
                'send' => ['type' => 'submit', 'options' => ['label' => 'send']],
128
            ]
129
        ]);
130
131
        if ($request->isMethod('POST')) {
132
            $form->handleRequest($request);
133
134
            if ($form->isValid()) {
135
                $message = \Swift_Message::newInstance()
136
                    ->setSubject($form->get('subject')->getData())
137
                    ->setFrom($form->get('email')->getData())
138
                    ->setTo($this->container->hasParameter('contact.email') ? $this->container->getParameter('contact.email') : '[email protected]')
139
                    ->setBody(
140
                        $this->renderView(
141
                            'KyelaBundle:Mail:contact.html.twig',
142
                            array(
143
                                'ip' => $request->getClientIp(),
144
                                'name' => $form->get('name')->getData(),
145
                                'subject' => $form->get('subject')->getData(),
146
                                'message' => $form->get('message')->getData()
147
                            )
148
                        )
149
                    );
150
151
                $this->get('mailer')->send($message);
152
                $flashMessage = $this->get('translator')->trans('mail.sent');
153
                $request->getSession()->getFlashBag()->add('success', $flashMessage);
154
                return $this->redirect($this->generateUrl('poll_new'));
155
            }
156
        }
157
        return array(
158
            'poll' => $this->poll,
159
            'form' => $form->createView()
160
        );
161
    }
162
}
163