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