ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Renderer.php
Go to the documentation of this file.
1<?php
2
4
11
23{
24
28 private $renderer;
29
30
34 protected function getComponentInterfaceName()
35 {
36 return array(
37 \ILIAS\UI\Component\Dropzone\File\Standard::class,
38 \ILIAS\UI\Component\Dropzone\File\Wrapper::class,
39 );
40 }
41
42
46 public function render(Component $component, \ILIAS\UI\Renderer $default_renderer)
47 {
48 $this->checkComponent($component);
49 $this->renderer = $default_renderer;
50 if ($component instanceof \ILIAS\UI\Component\Dropzone\File\Wrapper) {
51 return $this->renderWrapper($component);
52 }
53 if ($component instanceof \ILIAS\UI\Component\Dropzone\File\Standard) {
54 return $this->renderStandard($component);
55 }
56 }
57
58
62 public function registerResources(ResourceRegistry $registry)
63 {
64 parent::registerResources($registry);
65 $registry->register("./libs/bower/bower_components/jquery-dragster/jquery.dragster.js");
66 $registry->register("./libs/bower/bower_components/fine-uploader/dist/fine-uploader.core.min.js");
67 $registry->register("./src/UI/templates/js/Dropzone/File/uploader.js");
68 $registry->register("./src/UI/templates/js/Dropzone/File/dropzone.js");
69 }
70
71
77 private function renderStandard(\ILIAS\UI\Component\Dropzone\File\Standard $dropzone)
78 {
82 $dropzone = $this->registerSignals($dropzone);
83 $dropzoneId = $this->bindJavaScript($dropzone);
84 $f = $this->getUIFactory();
86
87 $tpl = $this->getTemplate("tpl.standard-dropzone.html", true, true);
88 $tpl->setVariable("ID", $dropzoneId);
89 // Set default message if empty
90 $message = ($dropzone->getMessage()) ? $dropzone->getMessage() : $this->txt('drag_files_here');
91 $tpl->setVariable("MESSAGE", $message);
92 $button = $dropzone->getUploadButton();
93
94 // Select-Button
95 $select_button = $f->button()->shy($this->txt('select_files_from_computer'), '#');
96 $tpl->setVariable('SHY_BUTTON', $r->render($select_button));
97
98 // Upload-Button
99 if ($button) {
100 $button = $button->withUnavailableAction()->withAdditionalOnLoadCode(function ($id) use ($dropzoneId) {
101 return "$ (function() {il.UI.uploader.bindUploadButton('{$dropzoneId}', $('#{$id}'));});";
102 });
103 $tpl->setCurrentBlock('with_upload_button');
104 $tpl->setVariable('BUTTON', $r->render($button));
105 $tpl->parseCurrentBlock();
106 }
107 $tplUploadFileList = $this->getFileListTemplate($dropzone);
108 $tpl->setVariable('FILELIST', $tplUploadFileList->get());
109
110 return $tpl->get();
111 }
112
113
119 private function renderWrapper(\ILIAS\UI\Component\Dropzone\File\Wrapper $dropzone)
120 {
121 // Create the roundtrip modal which displays the uploaded files
122 $tplUploadFileList = $this->getFileListTemplate($dropzone);
123 $uploadButton = $this->getUIFactory()->button()->primary($this->txt('upload'), '')->withUnavailableAction();
124 $modal = $this->getUIFactory()->modal()->roundtrip($this->txt('upload'), $this->getUIFactory()->legacy($tplUploadFileList->get()))->withActionButtons([ $uploadButton ]);
125
126 // Register JS
127 $dropzone = $dropzone->withAdditionalDrop($modal->getShowSignal());
128 $dropzone = $this->registerSignals($dropzone);
129 $dropzoneId = $this->bindJavaScript($dropzone);
130
131 // Render the Wrapper-Dropzone
132 $tpl = $this->getTemplate("tpl.wrapper-dropzone.html", true, true);
133 $tpl->setVariable('ID', $dropzoneId);
134 $tpl->setVariable('CONTENT', $this->renderer->render($dropzone->getContent()));
135 $tpl->setVariable('MODAL', $this->renderer->render($modal));
136
137 return $tpl->get();
138 }
139
140
146 protected function registerSignals(\ILIAS\UI\Component\Dropzone\File\File $dropzone)
147 {
148 $signals = array_map(function ($triggeredSignal) {
150 return array(
151 'id' => $triggeredSignal->getSignal()->getId(),
152 'options' => $triggeredSignal->getSignal()->getOptions(),
153 );
154 }, $dropzone->getTriggeredSignals());
155
156 return $dropzone->withAdditionalOnLoadCode(function ($id) use ($dropzone, $signals) {
157 $options = json_encode(
158 [
159 'id' => $id,
160 'registeredSignals' => $signals,
161 'uploadUrl' => $dropzone->getUploadUrl(),
162 'allowedFileTypes' => $dropzone->getAllowedFileTypes(),
163 'fileSizeLimit' => $dropzone->getFileSizeLimit() ? $dropzone->getFileSizeLimit()->getSize()
164 * $dropzone->getFileSizeLimit()->getUnit() : 0,
165 'maxFiles' => $dropzone->getMaxFiles(),
166 'identifier' => $dropzone->getParametername(),
167 'typeError' => $this->txt('msg_wrong_filetypes') . " " . implode(", ", $dropzone->getAllowedFileTypes()),
168 ]
169 );
170 $reflect = new \ReflectionClass($dropzone);
171 $type = $reflect->getShortName();
172
173 return "il.UI.dropzone.initializeDropzone('{$type}', JSON.parse('{$options}'));";
174 });
175 }
176
177
184 private function getFileListTemplate(\ILIAS\UI\Component\Dropzone\File\File $dropzone)
185 {
186 $f = $this->getUIFactory();
188 $tplUploadFileList = $this->getTemplate('tpl.upload-file-list.html', true, true);
189
190 // Actions
191 $items = array(
192 $f->button()->shy($this->txt("remove"), "")->withAriaLabel("delete_file"),
193 );
194
195 $tplUploadFileList->setVariable("REMOVE", $r->render([$f->button()->close()]));
196
197 if ($this->renderMetaData($dropzone)) {
198 $tplUploadFileList->setVariable("TOGGLE", $r->render([$f->glyph()->collapse(), $f->glyph()->expand()]));
199 $tplUploadFileList->setCurrentBlock("with_metadata");
200 $items[] = $f->button()->shy($this->txt("edit_metadata"), "")->withAriaLabel("edit_metadata");
201 if ($dropzone->allowsUserDefinedFileNames()) {
202 $tplUploadFileList->setVariable("LABEL_FILENAME", $this->txt("filename"));
203 }
204 if ($dropzone->allowsUserDefinedFileDescriptions()) {
205 $tplUploadFileList->setVariable("LABEL_DESCRIPTION", $this->txt("description"));
206 }
207 $tplUploadFileList->parseCurrentBlock();
208 }
209 $action = $f->dropdown()->standard($items);
210 $tplUploadFileList->setVariable("DROPDOWN", $r->render($action));
211
212 return $tplUploadFileList;
213 }
214
215
221 private function renderMetaData(\ILIAS\UI\Component\Dropzone\File\File $dropzone)
222 {
223 return ($dropzone->allowsUserDefinedFileNames()
224 || $dropzone->allowsUserDefinedFileDescriptions());
225 }
226}
$tpl
Definition: ilias.php:10
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
An exception for terminatinating execution or to throw for unit testing.
renderWrapper(\ILIAS\UI\Component\Dropzone\File\Wrapper $dropzone)
Definition: Renderer.php:119
render(Component $component, \ILIAS\UI\Renderer $default_renderer)
Definition: Renderer.php:46
renderMetaData(\ILIAS\UI\Component\Dropzone\File\File $dropzone)
Definition: Renderer.php:221
getComponentInterfaceName()
Get the name of the component-interface this renderer is supposed to render.ATTENTION: Fully qualifie...
Definition: Renderer.php:34
getFileListTemplate(\ILIAS\UI\Component\Dropzone\File\File $dropzone)
Definition: Renderer.php:184
registerResources(ResourceRegistry $registry)
@inheritDoc
Definition: Renderer.php:62
Renderer that dispatches rendering of UI components to a Renderer found in the same namespace as the ...
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.
checkComponent(Component $component)
Check if a given component fits this renderer and throw \LogicError if that is not the case.
$action
$r
Definition: example_031.php:79
if(!array_key_exists('StateId', $_REQUEST)) $id
A component is the most general form of an entity in the UI.
Definition: Component.php:14
Registry for resources required by rendered output like Javascript or CSS.
catch(Exception $e) $message
Class BaseForm.
Class Factory.
$type