ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Renderer.php
Go to the documentation of this file.
1 <?php
3 
8 
13 {
14 
18  public function render(Component\Component $component, RendererInterface $default_renderer)
19  {
20  $this->checkComponent($component);
21 
22  // If the modal is rendered async, we just create a fake container which will be
23  // replaced by the modal upon successful ajax request
25  if ($component->getAsyncRenderUrl()) {
26  return $this->renderAsync($component);
27  }
28 
29  if ($component instanceof Component\Modal\Interruptive) {
30  return $this->renderInterruptive($component, $default_renderer);
31  } elseif ($component instanceof Component\Modal\RoundTrip) {
32  return $this->renderRoundTrip($component, $default_renderer);
33  } elseif ($component instanceof Component\Modal\Lightbox) {
34  return $this->renderLightbox($component, $default_renderer);
35  }
36  return '';
37  }
38 
42  public function registerResources(ResourceRegistry $registry)
43  {
44  parent::registerResources($registry);
45  $registry->register('./src/UI/templates/js/Modal/modal.js');
46  }
47 
48 
53  protected function registerSignals(Component\Modal\Modal $modal)
54  {
55  $show = $modal->getShowSignal();
56  $close = $modal->getCloseSignal();
57  $options = json_encode(array(
58  'ajaxRenderUrl' => $modal->getAsyncRenderUrl(),
59  'keyboard' => $modal->getCloseWithKeyboard(),
60  ));
61  // ATTENTION, ATTENTION:
62  // with(Additional)OnLoadCode opens a wormhole into the future, where some unspecified
63  // entity magically created an id for the component that can be used to refer to it
64  // via javascript.
65  // This replaced a pattern, where an id was created manually and the java script
66  // code was manually inserted to the (now internal) js-binding of the
67  // AbstractComponentRenderer. (see commit 192144fd1f0e040cadc0149c3dc15fbc4b67858e).
68  // The wormhole solution is considered superior over the manual creation of ids because:
69  // * withAdditionalOnLoadCode introduces no new principles to the UI framework but reuses
70  // an existing one
71  // * withAdditionalOnLoadCode does not require it to expose internals (js-binding) from
72  // the AbstractComponentRenderer and thus does have less coupling
73  // * withAdditionalOnLoadCode allows the framework to decide, when ids are actually
74  // created
75  // * since withAdditionalOnLoadCode refers to some yet unknown future, it disencourages
76  // tempering with the id _here_.
77  return $modal->withAdditionalOnLoadCode(function ($id) use ($show, $close, $options) {
78  return
79  "$(document).on('{$show}', function() { il.UI.modal.showModal('{$id}', {$options}); return false; });" .
80  "$(document).on('{$close}', function() { il.UI.modal.closeModal('{$id}'); return false; });";
81  });
82  }
83 
88  protected function renderAsync(Component\Modal\Modal $modal)
89  {
90  $modal = $this->registerSignals($modal);
91  $id = $this->bindJavaScript($modal);
92  return "<span id='{$id}'></span>";
93  }
94 
101  protected function renderInterruptive(Component\Modal\Interruptive $modal, RendererInterface $default_renderer)
102  {
103  $tpl = $this->getTemplate('tpl.interruptive.html', true, true);
104  $modal = $this->registerSignals($modal);
105  $id = $this->bindJavaScript($modal);
106  $tpl->setVariable('ID', $id);
107  $tpl->setVariable('FORM_ACTION', $modal->getFormAction());
108  $tpl->setVariable('TITLE', $modal->getTitle());
109  $tpl->setVariable('MESSAGE', $modal->getMessage());
110  if (count($modal->getAffectedItems())) {
111  $tpl->setCurrentBlock('with_items');
112  foreach ($modal->getAffectedItems() as $item) {
113  $tpl->setCurrentBlock('item');
114  $icon = ($item->getIcon()) ? $default_renderer->render($item->getIcon()) : '';
115  $desc = ($item->getDescription()) ? '<br>' . $item->getDescription() : '';
116  $tpl->setVariable('ITEM_ICON', $icon);
117  $tpl->setVariable('ITEM_ID', $item->getId());
118  $tpl->setVariable('ITEM_TITLE', $item->getTitle());
119  $tpl->setVariable('ITEM_DESCRIPTION', $desc);
120  $tpl->parseCurrentBlock();
121  }
122  }
123  $tpl->setVariable('ACTION_BUTTON_LABEL', $this->txt($modal->getActionButtonLabel()));
124  $tpl->setVariable('CANCEL_BUTTON_LABEL', $this->txt($modal->getCancelButtonLabel()));
125  return $tpl->get();
126  }
127 
128 
135  protected function renderRoundTrip(Component\Modal\RoundTrip $modal, RendererInterface $default_renderer)
136  {
137  $tpl = $this->getTemplate('tpl.roundtrip.html', true, true);
138  $modal = $this->registerSignals($modal);
139  $id = $this->bindJavaScript($modal);
140  $tpl->setVariable('ID', $id);
141  $tpl->setVariable('TITLE', $modal->getTitle());
142  foreach ($modal->getContent() as $content) {
143  $tpl->setCurrentBlock('with_content');
144  $tpl->setVariable('CONTENT', $default_renderer->render($content));
145  $tpl->parseCurrentBlock();
146  }
147  foreach ($modal->getActionButtons() as $button) {
148  $tpl->setCurrentBlock('with_buttons');
149  $tpl->setVariable('BUTTON', $default_renderer->render($button));
150  $tpl->parseCurrentBlock();
151  }
152  $tpl->setVariable('CANCEL_BUTTON_LABEL', $this->txt($modal->getCancelButtonLabel()));
153  return $tpl->get();
154  }
155 
156 
163  protected function renderLightbox(Component\Modal\Lightbox $modal, RendererInterface $default_renderer)
164  {
165  $tpl = $this->getTemplate('tpl.lightbox.html', true, true);
166  $modal = $this->registerSignals($modal);
167  $id = $this->bindJavaScript($modal);
168  $tpl->setVariable('ID', $id);
169  $id_carousel = "{$id}_carousel";
170  $pages = $modal->getPages();
171  $tpl->setVariable('TITLE', $pages[0]->getTitle());
172  $tpl->setVariable('ID_CAROUSEL', $id_carousel);
173  if (count($pages) > 1) {
174  $tpl->setCurrentBlock('has_indicators');
175  foreach ($pages as $index => $page) {
176  $tpl->setCurrentBlock('indicators');
177  $tpl->setVariable('INDEX', $index);
178  $tpl->setVariable('CLASS_ACTIVE', ($index == 0) ? 'active' : '');
179  $tpl->setVariable('ID_CAROUSEL2', $id_carousel);
180  $tpl->parseCurrentBlock();
181  }
182  }
183  foreach ($pages as $i => $page) {
184  $tpl->setCurrentBlock('pages');
185  $tpl->setVariable('CLASS_ACTIVE', ($i == 0) ? ' active' : '');
186  $tpl->setVariable('TITLE2', htmlentities($page->getTitle(), ENT_QUOTES, 'UTF-8'));
187  $tpl->setVariable('CONTENT', $default_renderer->render($page->getComponent()));
188  $tpl->setVariable('DESCRIPTION', $page->getDescription());
189  $tpl->parseCurrentBlock();
190  }
191  if (count($pages) > 1) {
192  $tpl->setCurrentBlock('controls');
193  $tpl->setVariable('ID_CAROUSEL3', $id_carousel);
194  $tpl->parseCurrentBlock();
195  }
196  $tpl->setVariable('ID_CAROUSEL4', $id_carousel);
197  return $tpl->get();
198  }
199 
200 
204  protected function getComponentInterfaceName()
205  {
206  return array(
207  Component\Modal\Interruptive::class,
208  Component\Modal\RoundTrip::class,
209  Component\Modal\Lightbox::class,
210  );
211  }
212 }
Registry for resources required by rendered output like Javascript or CSS.
renderRoundTrip(Component\Modal\RoundTrip $modal, RendererInterface $default_renderer)
Definition: Renderer.php:135
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
$tpl
Definition: ilias.php:10
if(!array_key_exists('StateId', $_REQUEST)) $id
$index
Definition: metadata.php:60
renderLightbox(Component\Modal\Lightbox $modal, RendererInterface $default_renderer)
Definition: Renderer.php:163
registerSignals(Component\Modal\Modal $modal)
Definition: Renderer.php:53
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.null
Definition: Renderer.php:42
renderInterruptive(Component\Modal\Interruptive $modal, RendererInterface $default_renderer)
Definition: Renderer.php:101
Create styles array
The data for the language used.
render(Component $component, Renderer $default_renderer)
Render the component if possible and delegate additional rendering to the default_renderer.
renderAsync(Component\Modal\Modal $modal)
Definition: Renderer.php:88
$i
Definition: disco.tpl.php:19
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.