TemplateAdminController::addComponentAction()   B
last analyzed

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(
36
                'scope' => TemplateInterface::GLOBAL_SCOPE,
37
                'templateTypeId' => $theme->getTemplateTypes()->column('id')
38
            ))
39
        ;
40
        $templateMap = array();
41
        foreach ($templateCollection as $template) {
42
            $templateMap[$template->getTemplateType()->getName()][$template->getContentType()->getName()] = $template;
43
        }
44
45
        return $this->render('SynapseAdminBundle:Template:list.html.twig', array(
46
            'theme' => $theme,
47
            'content_types' => $this->container->get('synapse.content_type.loader')
48
                ->retrieveAll(),
49
            'templates' => $templateMap,
50
        ));
51
    }
52
53
    /**
54
     * Template init action.
55
     *
56
     * @param Request $request
57
     * @param string  $templateType
58
     * @param string  $contentType
59
     * @param int     $contentId
60
     *
61
     * @return Response
62
     */
63
    public function initAction(Request $request, $templateType, $contentType, $contentId = null)
64
    {
65
        $templateDomain = $this->container->get('synapse.template.domain');
66
67
        $template = empty($contentId)
68
            ? $templateDomain->createGlobal(
69
                $contentType,
70
                $templateType
71
            )
72
            : $templateDomain->createLocal(
73
                $this->container->get('synapse.content.resolver')->resolveContentId($contentType, $contentId),
74
                $templateType
75
            )
76
        ;
77
78
        return new RedirectResponse(empty($contentId)
79
            ? $this->container->get('router')->generate('synapse_admin_template_edition', array(
80
                'id' => $template->getId(),
81
            ))
82
            : $request->server->get('HTTP_REFERER')
83
        );
84
    }
85
86
    /**
87
     * Global template edition action.
88
     * Requires an activated or activable theme.
89
     *
90
     * @param Request $request
91
     *
92
     * @return Response
93
     */
94
    public function editAction($id, Request $request)
95
    {
96
        $template = $this->container->get('synapse.template.orm_loader')
97
            ->retrieveOne(array(
98
                'id' => $id,
99
                'scope' => TemplateInterface::GLOBAL_SCOPE
100
            ))
101
        ;
102
        if (!$template) {
103
            throw new NotFoundHttpException(sprintf('No global template found for id "%s"', $id));
104
        }
105
106
        $form = $this->container->get('form.factory')->createNamed(
107
            'template',
108
            TemplateType::class,
109
            $template,
110
            array(
111
                'theme' => $request->attributes->get(
112
                    'synapse_theme',
113
                    $this->container->get('synapse')
114
                        ->enableDefaultTheme()
115
                        ->getCurrentTheme()
116
                ),
117
                'content_type' => $template->getContentType(),
118
                'template_type' => $template->getTemplateType(),
119
                'action' => $formUrl = $this->container->get('router')->generate(
120
                    'synapse_admin_template_edition',
121
                    array('id' => $template->getId())
122
                ),
123
                'method' => 'POST',
124
                'csrf_protection' => false,
125
            )
126
        );
127
        if ($request->request->has('template')) {
128
            $form->handleRequest($request);
129
            if ($form->isValid()) {
130
                return $this->redirect(
131
                    $this->container->get('router')->generate(
132
                        'synapse_admin_template_edition',
133
                        array('id' => $template->getId())
134
                    )
135
                );
136
            }
137
        }
138
139
        return $this->render('SynapseAdminBundle:Template:edit.html.twig', array(
140
            'template' => $template,
141
            'form' => $form->createView(),
142
        ));
143
    }
144
145
    /**
146
     * Adds a component of given type id into given template id and zone type id.
147
     *
148
     * @param int     $id
149
     * @param string  $zoneTypeId
150
     * @param string  $componentTypeId
151
     * @param Request $request
152
     *
153
     * @return Response
154
     */
155
    public function addComponentAction($id, $zoneTypeId, $componentTypeId, Request $request)
156
    {
157
        if (!$template = $this->container->get('synapse.template.loader')->retrieve($id)) {
158
            throw new NotFoundHttpException(sprintf('No template found under id "%s"', $id));
159
        }
160
        if (!$zoneType = $template->getTemplateType()
161
            ->getZoneTypes()
162
            ->search(array('id' => $zoneTypeId))
163
            ->first()
164
        ) {
165
            throw new NotFoundHttpException(sprintf(
166
                'Zone type "%s" is not activated for template "%s". Please check theme configuration.',
167
                $zoneTypeId,
168
                $templateType->getId()
169
            ));
170
        }
171
        if (!$componentType = $this->container->get('synapse.component_type.loader')->retrieve($componentTypeId)) {
172
            throw new NotFoundHttpException(sprintf(
173
                'No defined component type found under id "%s". Please check theme configuration.',
174
                $componentTypeId
175
            ));
176
        }
177
178
        $this->container->get('synapse.zone.domain')->addComponent(
179
            $template->getZones()->search(array('zoneType' => $zoneType))->first(),
180
            $componentType
181
        );
182
183
        return new RedirectResponse(
184
            $request->server->get('HTTP_REFERER')
185
        );
186
    }
187
}
188