Completed
Pull Request — master (#325)
by Paul
08:40
created

WidgetRenderer::computeOptions()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 17
nc 12
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Renderer;
4
5
use Symfony\Component\DependencyInjection\Container;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\CoreBundle\Event\WidgetRenderEvent;
9
use Victoire\Bundle\CoreBundle\VictoireCmsEvents;
10
use Victoire\Bundle\WidgetBundle\Entity\Widget;
11
use Victoire\Bundle\WidgetMapBundle\Entity\Slot;
12
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
13
14
class WidgetRenderer
15
{
16
    private $container;
17
    private $victoireTwigResponsive;
18
19
    public function __construct(Container $container, $victoireTwigResponsive)
20
    {
21
        $this->container = $container;
22
        $this->victoireTwigResponsive = $victoireTwigResponsive;
23
    }
24
25
    /**
26
     * render the Widget.
27
     *
28
     * @param Widget $widget
29
     * @param View   $view
30
     *
31
     * @return widget show
32
     */
33
    public function render(Widget $widget, View $view)
34
    {
35
        //the mode of display of the widget
36
        $mode = $widget->getMode();
37
38
        //if entty is given and it's not the object, retrive it and set the entity for the widget
39
        if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) {
40
            $widget->setEntity($view->getBusinessEntity());
41
        }
42
43
        //the templating service
44
        $templating = $this->container->get('victoire_templating');
45
46
        //the content of the widget
47
        $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget);
48
49
        //the template displayed is in the widget bundle (with the potential theme)
50
        $showView = 'show'.ucfirst($widget->getTheme());
51
        $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget);
52
53
        return $templating->render(
54
            $templateName,
55
            $parameters
56
        );
57
    }
58
59
    /**
60
     * render a widget.
61
     *
62
     * @param Widget $widget
63
     * @param View   $view
64
     *
65
     * @return string
66
     */
67
    public function renderContainer(Widget $widget, View $view)
68
    {
69
        $dispatcher = $this->container->get('event_dispatcher');
70
71
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_PRE_RENDER, new WidgetRenderEvent($widget));
72
73
        $widgetMap = $view->getWidgetMapByWidget($widget);
74
75
        $directive = '';
76
        if ($this->container->get('security.context')->isGranted('ROLE_VICTOIRE')) {
77
            $directive = 'widget';
78
        }
79
80
        $html = sprintf('<div %s widget-map="%s" class="vic-widget-container" data-id="%s">', $directive, $widgetMap->getId(), $widget->getId());
81
        $html .= $this->render($widget, $view);
82
        $html .= '</div>';
83
84
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_RENDER, new WidgetRenderEvent($widget, $html));
85
86
        return $html;
87
    }
88
89
    /**
90
     * prepare a widget to be rendered asynchronously.
91
     *
92
     * @param int $widgetId
93
     *
94
     * @return string
95
     */
96
    public function prepareAsynchronousRender($widgetId)
97
    {
98
        $ngControllerName = 'widget'.$widgetId.'AsynchronousLoadCtrl';
99
        $ngDirectives = sprintf('ng-controller="WidgetAsynchronousLoadController as %s" class="vic-widget" ng-init="%s.init(%d)" ng-bind-html="html"', $ngControllerName, $ngControllerName, $widgetId);
100
        $html = sprintf('<div class="vic-widget-container vic-widget-asynchronous" data-id="%d" %s></div>', $widgetId, $ngDirectives);
101
102
        return $html;
103
    }
104
105
    /**
106
     * render widget unlink action.
107
     *
108
     * @param int  $widgetId
109
     * @param View $view
110
     *
111
     * @return string
112
     */
113
    public function renderUnlinkActionByWidgetId($widgetId, $view)
114
    {
115
        return $this->container->get('victoire_templating')->render(
116
            'VictoireCoreBundle:Widget:widgetUnlinkAction.html.twig',
117
            [
118
                'widgetId' => $widgetId,
119
                'view'     => $view,
120
            ]
121
        );
122
    }
123
124
    /**
125
     * render slot actions.
126
     *
127
     * @param Slot   $slot
128
     * @param string $options
129
     *
130
     * @return string
131
     */
132
    public function renderActions($slot, $options = [], $position = null, WidgetMap $widgetMapReference = null)
133
    {
134
        return $this->container->get('victoire_templating')->render(
135
            'VictoireCoreBundle:Widget:actions.html.twig',
136
            [
137
                'slot'                  => $slot,
138
                'options'               => $options,
139
                'position'              => $position,
140
                'widgetMapReferenceId'  => $widgetMapReference ? $widgetMapReference->getId() : null,
141
            ]
142
        );
143
    }
144
145
    /**
146
     * Compute slot options.
147
     *
148
     * @param Slot  $slotId
149
     * @param array $options
150
     *
151
     * @return string
152
     */
153
    public function computeOptions($slotId, $options = [])
154
    {
155
        $slots = $this->container->getParameter('victoire_core.slots');
156
157
        $availableWidgets = $this->container->getParameter('victoire_core.widgets');
158
        $widgets = [];
159
160
        //If the slot is declared in config
161
        if (!empty($slots[$slotId]) && !empty($slots[$slotId]['widgets'])) {
162
            //parse declared widgets
163
            $slotWidgets = array_keys($slots[$slotId]['widgets']);
164
        } elseif (!empty($options['availableWidgets'])) {
165
            $slotWidgets = $options['availableWidgets'];
166
        } else {
167
            //parse all widgets
168
            $slotWidgets = array_keys($availableWidgets);
169
        }
170
171
        foreach ($slotWidgets as $slotWidget) {
172
            $widgetParams = $availableWidgets[$slotWidget];
173
            $widgets[$slotWidget]['params'] = $widgetParams;
174
        }
175
        $slots[$slotId]['availableWidgets'] = $widgets;
176
        if (isset($options['max'])) {
177
            $slots[$slotId]['max'] = $options['max'];
178
        }
179
180
        return $slots[$slotId];
181
    }
182
183
    /**
184
     * Get the extra classes for the css.
185
     *
186
     * @return string The classes
187
     */
188
    public function getExtraCssClass(Widget $widget)
189
    {
190
        $cssClass = 'vic-widget-'.strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget));
191
192
        return $cssClass;
193
    }
194
195
    /**
196
     * Render the CSS style for a Widget.
197
     *
198
     * @param Widget $widget
199
     *
200
     * @return mixed
201
     */
202
    public function renderStyle(Widget $widget)
203
    {
204
        return $this->container->get('victoire_templating')->render(
205
            'VictoireCoreBundle:Widget:style/style.html.twig',
206
            [
207
                'widget'                   => $widget,
208
                'victoire_twig_responsive' => $this->victoireTwigResponsive,
209
            ]
210
        );
211
    }
212
}
213