Completed
Pull Request — master (#433)
by Paul
06:40
created

TemplateBundle/Controller/TemplateController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Victoire\Bundle\TemplateBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Victoire\Bundle\TemplateBundle\Entity\Template;
12
use Victoire\Bundle\TemplateBundle\Event\Menu\TemplateMenuContextualEvent;
13
use Victoire\Bundle\TemplateBundle\Form\TemplateType;
14
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
15
16
/**
17
 * Template Controller.
18
 *
19
 * @Route("/victoire-dcms/template")
20
 */
21
class TemplateController extends Controller
22
{
23
    /**
24
     * list of all templates.
25
     *
26
     * @Route("/index", name="victoire_template_index")
27
     * @Configuration\Template()
28
     *
29
     * @return JsonResponse
30
     */
31
    public function indexAction()
32
    {
33
        $templates = $this->get('doctrine.orm.entity_manager')->getRepository('VictoireTemplateBundle:Template')->findByTemplate(null, ['position' => 'ASC']);
34
35
        return new JsonResponse(
36
            [
37
                'success' => true,
38
                'html'    => $this->container->get('templating')->render(
39
                    'VictoireTemplateBundle:Template:index.html.twig',
40
                    ['templates' => $templates]
41
                ),
42
            ]
43
        );
44
    }
45
46
    /**
47
     * list of all templates.
48
     *
49
     * @param Template $template The template
50
     *
51
     * @Route("/show/{slug}", name="victoire_template_show")
52
     * @ParamConverter("template", class="VictoireTemplateBundle:Template", options={"mapping": {"slug": "slug"}})
53
     *
54
     * @return Response
55
     */
56
    public function showAction(Template $template)
57
    {
58
        //add the view to twig
59
        $this->get('twig')->addGlobal('view', $template);
60
        $template->setReference(new ViewReference($template->getId()));
61
        $event = new TemplateMenuContextualEvent($template);
62
63
        //TODO : il serait bon de faire des constantes pour les noms d'évents
64
        $eventName = 'victoire_core.'.Template::TYPE.'_menu.contextual';
65
66
        $this->get('event_dispatcher')->dispatch($eventName, $event);
67
68
        //the victoire templating
69
        $templating = $this->container->get('templating');
70
        $layout = $template->getLayout().'.html.twig';
71
72
        $parameters = [
73
            'view'   => $template,
74
            'id'     => $template->getId(),
75
            'locale' => $template->getLocale(),
0 ignored issues
show
The method getLocale() does not seem to exist on object<Victoire\Bundle\T...Bundle\Entity\Template>.

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...
76
        ];
77
78
        $this->get('victoire_widget_map.builder')->build($template);
79
        $this->get('victoire_widget_map.widget_data_warmer')->warm(
80
            $this->get('doctrine.orm.entity_manager'),
81
            $template
82
        );
83
84
        $this->container->get('victoire_core.current_view')->setCurrentView($template);
85
86
        //create the response
87
        $response = $templating->renderResponse(
88
            'VictoireCoreBundle:Layout:'.$layout,
89
            $parameters
90
        );
91
92
        return $response;
93
    }
94
95
    /**
96
     * create a new Template.
97
     *
98
     * @return JsonResponse
99
     * @Route("/new", name="victoire_template_new")
100
     * @Configuration\Template()
101
     */
102
    public function newAction()
103
    {
104
        $em = $this->getDoctrine()->getManager();
105
        $template = new Template();
106
        $form = $this->container->get('form.factory')->create($this->getNewTemplateType(), $template); //@todo utiliser un service
107
108
        $form->handleRequest($this->get('request'));
109 View Code Duplication
        if ($form->isValid()) {
110
            $em->persist($template);
111
            $em->flush();
112
113
            return new JsonResponse([
114
                'success'  => true,
115
                'url'      => $this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]),
116
            ]);
117
        }
118
119
        return new JsonResponse(
120
            [
121
                'success' => true,
122
                'html'    => $this->container->get('templating')->render(
123
                    'VictoireTemplateBundle:Template:new.html.twig',
124
                    ['form' => $form->createView()]
125
                ),
126
            ]
127
        );
128
    }
129
130
    /**
131
     * define settings of the template.
132
     *
133
     * @param Template $template
134
     *
135
     * @return JsonResponse
136
     * @Route("/{slug}/parametres", name="victoire_template_settings")
137
     * @ParamConverter("template", class="VictoireTemplateBundle:Template", options={"mapping": {"slug": "slug"}})
138
     */
139
    public function settingsAction(Request $request, $template)
140
    {
141
        $em = $this->getDoctrine()->getManager();
142
143
        $form = $this->createForm(TemplateType::class, $template);
144
        $form->handleRequest($request);
145 View Code Duplication
        if ($form->isValid()) {
146
            $em->persist($template);
147
            $em->flush();
148
149
            return new JsonResponse(
150
                    [
151
                        'success' => true,
152
                        'url'     => $this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]),
153
                    ]
154
                );
155
        }
156
157
        return new JsonResponse(
158
            [
159
                'success' => true,
160
                'html'    => $this->container->get('templating')->render(
161
                    'VictoireTemplateBundle:Template:settings.html.twig',
162
                    ['template' => $template, 'form' => $form->createView()]
163
                ),
164
            ]
165
        );
166
    }
167
168
    /**
169
     * edit a Template.
170
     *
171
     * @param Template $template The Template to edit
172
     *
173
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
174
     * @Route("/edit/{slug}", name="victoire_template_edit")
175
     * @Configuration\Template()
176
     * @ParamConverter("template", class="VictoireTemplateBundle:Template")
177
     */
178
    public function editAction(Template $template)
179
    {
180
        $em = $this->getDoctrine()->getManager();
181
        $form = $this->container->get('form.factory')->create($this->getNewTemplateType(), $template);
182
183
        $form->handleRequest($this->get('request'));
184
        if ($form->isValid()) {
185
            $em->persist($template);
186
            $em->flush();
187
188
            return $this->redirect($this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]));
189
        }
190
191
        return $this->redirect($this->generateUrl('victoire_template_settings', ['slug' => $template->getSlug()]));
192
    }
193
}
194