TemplateController::newAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 9
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 9
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 0
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/{id}", name="victoire_template_show")
52
     * @ParamConverter("template", class="VictoireTemplateBundle:Template")
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->getCurrentLocale(),
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(TemplateType::class, $template); //@todo utiliser un service
107
108
        $form->handleRequest($this->get('request'));
109 View Code Duplication
        if ($form->isValid()) {
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...
110
            $em->persist($template);
111
            $em->flush();
112
113
            return new JsonResponse([
114
                'success'  => true,
115
                'url'      => $this->generateUrl('victoire_template_show', ['id' => $template->getId()]),
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
131
     * define settings of the template.
132
     *
133
     * @param Template $template
0 ignored issues
show
introduced by
Doc comment for parameter $template does not match actual variable name $request
Loading history...
134
     *
135
     * @return JsonResponse
136
     * @Route("/{id}/parametres", name="victoire_template_settings")
137
     * @ParamConverter("template", class="VictoireTemplateBundle:Template")
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()) {
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...
146
            $em->persist($template);
147
            $em->flush();
148
149
            return new JsonResponse(
150
                    [
151
                        'success' => true,
152
                        'url'     => $this->generateUrl('victoire_template_show', ['id' => $template->getId()]),
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(TemplateType::class, $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', ['id' => $template->getId()]));
189
        }
190
191
        return $this->redirect($this->generateUrl('victoire_template_settings', ['id' => $template->getId()]));
192
    }
193
}
194