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