Completed
Push — master ( e3793e...0dd56b )
by Quentin
23:42
created

TemplateAdminController::addComponentAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 4
nop 4
1
<?php
2
3
namespace Synapse\Admin\Bundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Synapse\Cmf\Bundle\Form\Type\Theme\TemplateType;
10
use Synapse\Cmf\Framework\Theme\Template\Model\TemplateInterface;
11
use Synapse\Cmf\Framework\Theme\Theme\Model\ThemeInterface;
12
13
/**
14
 * Controller for template management use cases action.
15
 */
16
class TemplateAdminController extends Controller
17
{
18
    /**
19
     * Templates listing action.
20
     *
21
     * @param Request $request
22
     *
23
     * @return Response
24
     */
25
    public function listAction(Request $request)
26
    {
27
        $theme = $request->attributes->get(
28
            'synapse_theme',
29
            $this->container->get('synapse')
30
                ->enableDefaultTheme()
31
                ->getCurrentTheme()
32
        );
33
34
        $templateCollection = $this->container->get('synapse.template.loader')
35
            ->retrieveAll(array('scope' => TemplateInterface::GLOBAL_SCOPE))
36
        ;
37
        $templateMap = array();
38
        foreach ($templateCollection as $template) {
39
            $templateMap[$template->getTemplateType()->getName()][$template->getContentType()->getName()] = $template;
40
        }
41
42
        return $this->render('SynapseAdminBundle:Template:list.html.twig', array(
43
            'theme' => $theme,
44
            'content_types' => $this->container->get('synapse.content_type.loader')
45
                ->retrieveAll(),
46
            'templates' => $templateMap,
47
        ));
48
    }
49
50
    /**
51
     * Template init action.
52
     *
53
     * @param Request $request
54
     * @param string  $templateType
55
     * @param string  $contentType
56
     * @param int     $contentId
57
     *
58
     * @return Response
59
     */
60
    public function initAction(Request $request, $templateType, $contentType, $contentId = null)
61
    {
62
        $templateDomain = $this->container->get('synapse.template.domain');
63
64
        $template = empty($contentId)
65
            ? $templateDomain->createGlobal(
66
                $contentType,
67
                $templateType
68
            )
69
            : $templateDomain->createLocal(
70
                $this->container->get('synapse.content.resolver')->resolveContentId($contentType, $contentId),
71
                $templateType
72
            )
73
        ;
74
75
        return new RedirectResponse(empty($contentId)
76
            ? $this->container->get('router')->generate('synapse_admin_template_edition', array(
77
                'id' => $template->getId(),
78
            ))
79
            : $request->server->get('HTTP_REFERER')
80
        );
81
    }
82
83
    /**
84
     * Global template edition action.
85
     * Requires an activated or activable theme.
86
     *
87
     * @param Request $request
88
     *
89
     * @return Response
90
     */
91
    public function editAction($id, Request $request)
92
    {
93
        $template = $this->container->get('synapse.template.orm_loader')
94
            ->retrieveOne(array(
95
                'id' => $id,
96
                'scope' => TemplateInterface::GLOBAL_SCOPE
97
            ))
98
        ;
99
        if (!$template) {
100
            throw new NotFoundHttpException(sprintf('No global template found for id "%s"', $id));
101
        }
102
103
        $form = $this->container->get('form.factory')->createNamed(
104
            'template',
105
            TemplateType::class,
106
            $template,
107
            array(
108
                'theme' => $request->attributes->get(
109
                    'synapse_theme',
110
                    $this->container->get('synapse')
111
                        ->enableDefaultTheme()
112
                        ->getCurrentTheme()
113
                ),
114
                'content_type' => $template->getContentType(),
115
                'template_type' => $template->getTemplateType(),
116
                'action' => $formUrl = $this->container->get('router')->generate(
117
                    'synapse_admin_template_edition',
118
                    array('id' => $template->getId())
119
                ),
120
                'method' => 'POST',
121
                'csrf_protection' => false,
122
            )
123
        );
124
        if ($request->request->has('template')) {
125
            $form->handleRequest($request);
126
            if ($form->isValid()) {
127
                return $this->redirect(
128
                    $this->container->get('router')->generate(
129
                        'synapse_admin_template_edition',
130
                        array('id' => $template->getId())
131
                    )
132
                );
133
            }
134
        }
135
136
        return $this->render('SynapseAdminBundle:Template:edit.html.twig', array(
137
            'template' => $template,
138
            'form' => $form->createView(),
139
        ));
140
    }
141
142
    /**
143
     * Adds a component of given type id into given template id and zone type id.
144
     *
145
     * @param int     $id
146
     * @param string  $zoneTypeId
147
     * @param string  $componentTypeId
148
     * @param Request $request
149
     *
150
     * @return Response
151
     */
152
    public function addComponentAction($id, $zoneTypeId, $componentTypeId, Request $request)
153
    {
154
        if (!$template = $this->container->get('synapse.template.loader')->retrieve($id)) {
155
            throw new NotFoundHttpException(sprintf('No template found under id "%s"', $id));
156
        }
157
        if (!$zoneType = $template->getTemplateType()
158
            ->getZoneTypes()
159
            ->search(array('id' => $zoneTypeId))
160
            ->first()
161
        ) {
162
            throw new NotFoundHttpException(sprintf(
163
                'Zone type "%s" is not activated for template "%s". Please check theme configuration.',
164
                $zoneTypeId,
165
                $templateType->getId()
166
            ));
167
        }
168
        if (!$componentType = $this->container->get('synapse.component_type.loader')->retrieve($componentTypeId)) {
169
            throw new NotFoundHttpException(sprintf(
170
                'No defined component type found under id "%s". Please check theme configuration.',
171
                $componentTypeId
172
            ));
173
        }
174
175
        $this->container->get('synapse.zone.domain')->addComponent(
176
            $template->getZones()->search(array('zoneType' => $zoneType))->first(),
177
            $componentType
178
        );
179
180
        return new RedirectResponse(
181
            $request->server->get('HTTP_REFERER')
182
        );
183
    }
184
}
185