1 | <?php |
||
20 | class WidgetRenderer |
||
|
|||
21 | { |
||
22 | private $container; |
||
23 | /** |
||
24 | * @var WidgetCache |
||
25 | */ |
||
26 | private $widgetCache; |
||
27 | /** |
||
28 | * @var WidgetHelper |
||
29 | */ |
||
30 | private $widgetHelper; |
||
31 | /** |
||
32 | * @var VictoireCollector |
||
33 | */ |
||
34 | private $victoireCollector; |
||
35 | /** |
||
36 | * @var BusinessPageHelper |
||
37 | */ |
||
38 | private $bepHelper; |
||
39 | |||
40 | /** |
||
41 | * WidgetRenderer constructor. |
||
42 | * |
||
43 | * @param Container $container |
||
44 | * @param WidgetCache $widgetCache |
||
45 | * @param WidgetHelper $widgetHelper |
||
46 | * @param VictoireCollector $victoireCollector |
||
47 | * @param BusinessPageHelper $bepHelper |
||
48 | * |
||
49 | * @internal param Client $redis |
||
50 | */ |
||
51 | public function __construct(Container $container, WidgetCache $widgetCache, WidgetHelper $widgetHelper, VictoireCollector $victoireCollector, BusinessPageHelper $bepHelper) |
||
59 | |||
60 | /** |
||
61 | * render the Widget. |
||
62 | * |
||
63 | * @param Widget $widget |
||
64 | * @param View $view |
||
65 | * |
||
66 | * @return widget show |
||
67 | */ |
||
68 | public function render(Widget $widget, View $view) |
||
69 | { |
||
70 | //the mode of display of the widget |
||
71 | $mode = $widget->getMode(); |
||
72 | |||
73 | //if entity is given and it's not the object, retrieve it and set the entity for the widget |
||
74 | if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) { |
||
75 | $widget->setEntity($view->getEntity()); |
||
76 | } elseif ($view instanceof BusinessTemplate) { |
||
77 | //We'll try to find a sample entity to mock the widget behavior |
||
78 | /** @var EntityManager $entityManager */ |
||
79 | $entityManager = $this->container->get('doctrine.orm.entity_manager'); |
||
80 | if ($view->getBusinessEntity()->getType() === ORMBusinessEntity::TYPE) { |
||
81 | if ($mock = $this->bepHelper->getEntitiesAllowedQueryBuilder($view, $entityManager)->setMaxResults(1)->getQuery()->getOneOrNullResult()) { |
||
82 | $widget->setEntity($mock); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | //the templating service |
||
88 | $templating = $this->container->get('templating'); |
||
89 | |||
90 | //the content of the widget |
||
91 | |||
92 | $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget); |
||
93 | /* |
||
94 | * In some cases, for example, WidgetRender in BusinessEntity mode with magic variables {{entity.id}} transformed |
||
95 | * into the real business entity id, then if in the rendered action, we need to flush, it would persist the |
||
96 | * modified widget which really uncomfortable ;) |
||
97 | */ |
||
98 | if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY) { |
||
99 | $this->container->get('doctrine.orm.entity_manager')->refresh($widget); |
||
100 | } |
||
101 | |||
102 | //the template displayed is in the widget bundle (with the potential theme) |
||
103 | $showView = 'show'.ucfirst($widget->getTheme()); |
||
104 | $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget); |
||
105 | |||
106 | return $templating->render($templateName, $parameters); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * render a widget. |
||
111 | * |
||
112 | * @param Widget $widget |
||
113 | * @param View $view |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function renderContainer(Widget $widget, View $view) |
||
152 | |||
153 | /** |
||
154 | * prepare a widget to be rendered asynchronously. |
||
155 | * |
||
156 | * @param int $widgetId |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function prepareAsynchronousRender($widgetId) |
||
168 | |||
169 | /** |
||
170 | * render widget unlink action. |
||
171 | * |
||
172 | * @param int $widgetId |
||
173 | * @param View $view |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function renderUnlinkActionByWidgetId($widgetId, $view) |
||
187 | |||
188 | /** |
||
189 | * Compute slot options. |
||
190 | * |
||
191 | * @param int $slotId |
||
192 | * @param array $options |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function computeOptions($slotId, $options = []) |
||
225 | |||
226 | /** |
||
227 | * Get the extra classes for the css. |
||
228 | * |
||
229 | * @return string The classes |
||
230 | */ |
||
231 | public function getExtraCssClass(Widget $widget) |
||
237 | } |
||
238 |