Completed
Pull Request — master (#338)
by Leny
06:17
created

WidgetRenderer::computeOptions()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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\Helper\WidgetMapHelper;
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 = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
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
     * Compute slot options.
126
     *
127
     * @param Slot  $slotId
128
     * @param array $options
129
     *
130
     * @return string
131
     */
132
    public function computeOptions($slotId, $options = [])
133
    {
134
        $slots = $this->container->getParameter('victoire_core.slots');
135
136
        $availableWidgets = $this->container->getParameter('victoire_core.widgets');
137
        $widgets = [];
138
139
        //If the slot is declared in config
140
        if (!empty($slots[$slotId]) && !empty($slots[$slotId]['widgets'])) {
141
            //parse declared widgets
142
            $slotWidgets = array_keys($slots[$slotId]['widgets']);
143
        } elseif (!empty($options['availableWidgets'])) {
144
            $slotWidgets = $options['availableWidgets'];
145
        } else {
146
            //parse all widgets
147
            $slotWidgets = array_keys($availableWidgets);
148
        }
149
150
        foreach ($slotWidgets as $slotWidget) {
151
            $widgetParams = $availableWidgets[$slotWidget];
152
            $widgets[$slotWidget]['params'] = $widgetParams;
153
        }
154
        $slots[$slotId]['availableWidgets'] = $widgets;
155
        if (isset($options['max'])) {
156
            $slots[$slotId]['max'] = $options['max'];
157
        }
158
159
        return $slots[$slotId];
160
    }
161
162
    /**
163
     * Get the extra classes for the css.
164
     *
165
     * @return string The classes
166
     */
167
    public function getExtraCssClass(Widget $widget)
168
    {
169
        $cssClass = 'vic-widget-'.strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget));
170
171
        return $cssClass;
172
    }
173
174
    /**
175
     * Render the CSS style for a Widget.
176
     *
177
     * @param Widget $widget
178
     *
179
     * @return mixed
180
     */
181
    public function renderStyle(Widget $widget)
182
    {
183
        return $this->container->get('victoire_templating')->render(
184
            'VictoireCoreBundle:Widget:style/style.html.twig',
185
            [
186
                'widget'                   => $widget,
187
                'victoire_twig_responsive' => $this->victoireTwigResponsive,
188
            ]
189
        );
190
    }
191
}
192