ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
34 
36 {
40  public function render(Component $component, RendererInterface $default_renderer): string
41  {
42  $this->checkComponent($component);
43 
44  if ($component instanceof Notification) {
45  return $this->renderNotification($component, $default_renderer);
46  } elseif ($component instanceof Group) {
47  return $this->renderGroup($component, $default_renderer);
48  } elseif ($component instanceof Standard) {
49  return $this->renderStandard($component, $default_renderer);
50  } elseif ($component instanceof Shy) {
51  return $this->renderShy($component, $default_renderer);
52  }
53  return "";
54  }
55 
56  protected function renderGroup(Group $component, RendererInterface $default_renderer): string
57  {
58  $tpl = $this->getTemplate("tpl.group.html", true, true);
59  $title = $component->getTitle();
60  $items = $component->getItems();
61 
62  // items
63  foreach ($items as $item) {
64  $tpl->setCurrentBlock("item");
65  $tpl->setVariable("ITEM", $default_renderer->render($item));
66  $tpl->parseCurrentBlock();
67  }
68 
69  if ($title != "") {
70  $tpl->setCurrentBlock("title");
71  $tpl->setVariable("TITLE", $title);
72  $tpl->parseCurrentBlock();
73  }
74 
75  // actions
76  $actions = $component->getActions();
77  if ($actions !== null) {
78  $tpl->setVariable("ACTIONS", $default_renderer->render($actions));
79  } else {
80  $tpl->setVariable("ACTIONS", "");
81  }
82 
83  return $tpl->get();
84  }
85 
86  protected function renderStandard(Item $component, RendererInterface $default_renderer): string
87  {
88  $tpl = $this->getTemplate("tpl.item_standard.html", true, true);
89 
90  $this->renderTitle($component, $default_renderer, $tpl);
91  $this->renderDescription($component, $tpl);
92  $this->renderProperties($component, $default_renderer, $tpl);
93  $this->renderAudioPlayer($component, $default_renderer, $tpl);
94  // color
95  $color = $component->getColor();
96  if ($color !== null) {
97  $tpl->setCurrentBlock("color");
98  $tpl->setVariable("COLOR", $color->asHex());
99  $tpl->parseCurrentBlock();
100  }
101 
102  // lead
103  $lead = $component->getLead();
104  $progress = $component->getProgress();
105  if ($lead != null) {
106  if (is_string($lead)) {
107  if ($progress != null) {
108  $tpl->setCurrentBlock("lead_text_with_progress");
109  $tpl->touchBlock("item_with_lead_and_progress");
110  } else {
111  $tpl->setCurrentBlock("lead_text");
112  $tpl->touchBlock("item_with_lead_only");
113  }
114  $tpl->setVariable("LEAD_TEXT", $lead);
115  $tpl->parseCurrentBlock();
116  }
117  if ($lead instanceof Image) {
118  if ($progress != null) {
119  $tpl->setCurrentBlock("lead_image_with_progress");
120  $tpl->touchBlock("item_with_lead_and_progress");
121  } else {
122  $tpl->setCurrentBlock("lead_image");
123  $tpl->touchBlock("item_with_lead_only");
124  }
125  $tpl->setVariable("LEAD_IMAGE", $default_renderer->render($lead));
126  $tpl->parseCurrentBlock();
127  }
128  if ($lead instanceof Icon || $lead instanceof Avatar) {
129  $tpl->setCurrentBlock("lead_icon");
130  $tpl->setVariable("LEAD_ICON", $default_renderer->render($lead));
131  $tpl->parseCurrentBlock();
132  $tpl->setCurrentBlock("lead_start_icon");
133  $tpl->parseCurrentBlock();
134  } else {
135  $tpl->setCurrentBlock("lead_start");
136  $tpl->parseCurrentBlock();
137  }
138  if ($progress != null && ($lead instanceof Icon || $lead instanceof Avatar)) {
139  $tpl->setCurrentBlock("progress_end_with_lead_icon");
140  $tpl->setVariable("PROGRESS", $default_renderer->render($progress));
141  $tpl->parseCurrentBlock();
142  } elseif ($progress != null) {
143  $tpl->setCurrentBlock("progress_end");
144  $tpl->setVariable("PROGRESS", $default_renderer->render($progress));
145  $tpl->parseCurrentBlock();
146  } else {
147  $tpl->touchBlock("lead_end");
148  }
149  } elseif ($progress != null) {
150  $tpl->touchBlock("item_with_progress_only");
151  $tpl->setCurrentBlock("progress_end");
152  $tpl->setVariable("PROGRESS", $default_renderer->render($progress));
153  $tpl->parseCurrentBlock();
154  }
155 
156  // actions
157  $actions = $component->getActions();
158  if ($actions !== null) {
159  $tpl->setVariable("ACTIONS", $default_renderer->render($actions));
160  }
161 
162  return $tpl->get();
163  }
164 
165  protected function renderShy(Shy $component, RendererInterface $default_renderer): string
166  {
167  $tpl = $this->getTemplate("tpl.item_shy.html", true, true);
168 
169  $this->renderTitle($component, $default_renderer, $tpl);
170  $this->renderDescription($component, $tpl);
171 
172  if ($component->getProperties() !== []) {
173  foreach ($component->getProperties() as $name => $value) {
174  $name = htmlentities($name);
175  if ($value instanceof Button\Shy) {
176  $value = $default_renderer->render($value);
177  } else {
178  $value = htmlentities($value);
179  }
180  $tpl->setCurrentBlock("property_row");
181  $tpl->setVariable("PROP_NAME_A", $name);
182  $tpl->setVariable("PROP_VAL_A", $value);
183  $tpl->parseCurrentBlock();
184  }
185  $tpl->setCurrentBlock("properties");
186  $tpl->parseCurrentBlock();
187  }
188 
189  if ($component->getLeadIcon() !== null) {
190  $tpl->setCurrentBlock("lead_icon");
191  $tpl->setVariable("LEAD_ICON", $default_renderer->render($component->getLeadIcon()));
192  $tpl->parseCurrentBlock();
193  }
194 
195  if ($component->getClose() !== null) {
196  $tpl->setCurrentBlock("close");
197  $tpl->setVariable("CLOSE", $default_renderer->render($component->getClose()));
198  $tpl->parseCurrentBlock();
199  }
200 
201  if ($component->getOnLoadCode() !== null) {
202  $tpl->setCurrentBlock("id");
203  $tpl->setVariable('ID', $this->bindJavaScript($component));
204  $tpl->parseCurrentBlock();
205  }
206 
207  return $tpl->get();
208  }
209 
210  protected function renderNotification(Notification $component, RendererInterface $default_renderer): string
211  {
212  $tpl = $this->getTemplate("tpl.item_notification.html", true, true);
213  $this->renderTitle($component, $default_renderer, $tpl);
214  $desc = $component->getDescription();
215  if (!is_null($desc) && trim($desc) != "") {
216  $tpl->setCurrentBlock("desc");
217  $tpl->setVariable("DESC", $desc);
218  $tpl->parseCurrentBlock();
219  }
220  $this->renderProperties($component, $default_renderer, $tpl);
221  $tpl->setVariable("LEAD_ICON", $default_renderer->render($component->getLeadIcon()));
222 
223  // actions
224  $actions = $component->getActions();
225  if ($actions !== null) {
226  $tpl->setVariable("ACTIONS", $default_renderer->render($actions));
227  }
228 
229  // additional content
230  if ($component->getAdditionalContent()) {
231  $tpl->setCurrentBlock("additional_content");
232  $tpl->setVariable("ADDITIONAL_CONTENT", $default_renderer->render($component->getAdditionalContent()));
233  $tpl->parseCurrentBlock();
234  }
235 
236  // aggregate notification
237  $title = $this->getUIFactory()->button()->bulky($this->getUIFactory()->symbol()->glyph()->back(), $this->txt("back"), "");
238  $aggregates_html = $default_renderer->render(
239  $this->getUIFactory()->mainControls()->slate()->notification($default_renderer->render($title), $component->getAggregateNotifications())
240  );
241 
242  $toggleable = true;
243  if (!empty($component->getAggregateNotifications())) {
244  $toggleable = false;
245  }
246 
250  $component = $component->withAdditionalOnLoadCode(
251  fn ($id) => "il.UI.item.notification.getNotificationItemObject($($id)).registerAggregates($toggleable);"
252  );
253 
254  //Bind id
255  $item_id = $this->bindJavaScript($component);
256 
257  $tpl->setCurrentBlock("aggregate_notifications");
258  $tpl->setVariable("AGGREGATES", $aggregates_html);
259  $tpl->setVariable("PARENT_ID", $item_id);
260  $tpl->parseCurrentBlock();
261 
262  // close action
263  if ($component->getCloseAction()) {
264  $url = $component->getCloseAction();
268  $close_action = $this->getUIFactory()->button()->close()->withAdditionalOnLoadCode(
269  fn ($id) => "il.UI.item.notification.getNotificationItemObject($($id)).registerCloseAction('$url',1);"
270  );
271  $tpl->setVariable("CLOSE_ACTION", $default_renderer->render($close_action));
272  }
273 
274  $tpl->setCurrentBlock("id");
275  $tpl->setVariable('ID', $item_id);
276  $tpl->parseCurrentBlock();
277 
278  return $tpl->get();
279  }
280 
281  protected function renderTitle(Item $component, RendererInterface $default_renderer, Template $tpl): void
282  {
283  $title = $component->getTitle();
284  if ($title instanceof Button\Shy || $title instanceof Link) {
285  $title = $default_renderer->render($title);
286  } else {
287  $title = htmlentities($title);
288  }
289  $tpl->setVariable("TITLE", $title);
290  }
291 
292  protected function renderDescription(Item $component, Template $tpl): void
293  {
294  // description
295  $desc = $component->getDescription();
296  if (!is_null($desc) && trim($desc) != "") {
297  $tpl->setCurrentBlock("desc");
298  $tpl->setVariable("DESC", htmlentities($desc));
299  $tpl->parseCurrentBlock();
300  }
301  }
302 
303  protected function renderAudioPlayer(
304  Item $component,
305  RendererInterface $default_renderer,
306  Template $tpl
307  ): void {
308  // description
309  $audio = $component->getAudioPlayer();
310  if (!is_null($audio)) {
311  $tpl->setCurrentBlock("audio");
312  $tpl->setVariable("AUDIO", $default_renderer->render($audio));
313  $tpl->parseCurrentBlock();
314  }
315  }
316 
317  protected function renderProperties(Item $component, RendererInterface $default_renderer, Template $tpl): void
318  {
319  $props = $component->getProperties();
320  if (count($props) > 0) {
321  $cnt = 0;
322  foreach ($props as $name => $value) {
323  $name = htmlentities($name);
324  if ($value instanceof Button\Shy || $value instanceof \ILIAS\UI\Component\Symbol\Icon\Icon) {
325  $value = $default_renderer->render($value);
326  } else {
327  //Note, as soon as we got rid of all legacy ListGUI needing to render LP Icons as string, we
328  //should introduce here htmlentities
329  $value = $value;
330  }
331  $cnt++;
332  if ($cnt % 2 == 1) {
333  $tpl->setCurrentBlock("property_row");
334  $tpl->setVariable("PROP_NAME_A", $name);
335  $tpl->setVariable("PROP_VAL_A", $value);
336  } else {
337  $tpl->setVariable("PROP_NAME_B", $name);
338  $tpl->setVariable("PROP_VAL_B", $value);
339  $tpl->parseCurrentBlock();
340  }
341  }
342  if ($cnt % 2 == 1) {
343  $tpl->parseCurrentBlock();
344  }
345  $tpl->setCurrentBlock("properties");
346  $tpl->parseCurrentBlock();
347  }
348  }
349 
353  public function registerResources(ResourceRegistry $registry): void
354  {
355  parent::registerResources($registry);
356  $registry->register('./src/UI/templates/js/Item/dist/notification.js');
357  }
358 
362  protected function getComponentInterfaceName(): array
363  {
364  return [
365  Standard::class,
366  Shy::class,
367  Group::class,
368  Notification::class
369  ];
370  }
371 }
Registry for resources required by rendered output like Javascript or CSS.
getTitle()
Gets the title of the item.
getActions()
Get the actions of the item.
renderGroup(Group $component, RendererInterface $default_renderer)
Definition: Renderer.php:56
getAggregateNotifications()
Get the list of Notification Items, this Notification Item aggregates or an empty list...
Class Factory.
This describes a symbol.
Definition: Symbol.php:29
getTitle()
Gets the title of the group.
getAdditionalContent()
Get the additional content of the item or null.
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
renderShy(Shy $component, RendererInterface $default_renderer)
Definition: Renderer.php:165
Class ChatMainBarProvider .
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $id)
Get a text from the language file.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
Common interface to item groups.
Definition: Group.php:29
renderAudioPlayer(Item $component, RendererInterface $default_renderer, Template $tpl)
Definition: Renderer.php:303
renderTitle(Item $component, RendererInterface $default_renderer, Template $tpl)
Definition: Renderer.php:281
setCurrentBlock(string $name)
Set the block to work on.
setVariable(string $name, $value)
Set a variable in the current block.
if($format !==null) $name
Definition: metadata.php:247
This describes how a letter or a picture avatar could be modified during construction of UI...
Definition: Avatar.php:28
render(Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:40
Common interface to all items.
Definition: Item.php:31
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
getActions()
Get the actions Dropdown of the group.
getOnLoadCode()
Get the currently bound on load code.
getItems()
Gets item of the group.
renderStandard(Item $component, RendererInterface $default_renderer)
Definition: Renderer.php:86
This describes a standard button.
Definition: Standard.php:26
getDescription()
Get the description of the item.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:353
register(string $name)
Add a dependency.
renderDescription(Item $component, Template $tpl)
Definition: Renderer.php:292
parseCurrentBlock()
Parse the block that is currently worked on.
getProperties()
Get the properties of the appointment.
renderProperties(Item $component, RendererInterface $default_renderer, Template $tpl)
Definition: Renderer.php:317
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getCloseAction()
Get the url attached to this Notification Item.
$url
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.