ILIAS  release_8 Revision v8.24
class.ilOrgUnitMultiLineInputGUI.php
Go to the documentation of this file.
1<?php
25{
26 public const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
27 public const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
28 public const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
29 protected array $cust_attr = array();
30 protected $value;
31 protected array $inputs = array();
32 protected array $input_options = array();
34 protected array $hooks = array();
35 protected array $line_values = array();
36 protected string $template_dir = '';
37 protected array $post_var_cache = array();
38 protected bool $show_label = false;
39 protected bool $show_label_once = false;
40 protected array $hidden_inputs = array();
41 protected bool $position_movable = false;
42 protected int $counter = 0;
43 protected bool $show_info = false;
44
50 public function __construct(string $a_title = "", string $a_postvar = "")
51 {
52 parent::__construct($a_title, $a_postvar);
53 $this->setType("line_select");
54 $this->setMulti(true);
55 $this->initCSSandJS();
56 }
57
58 public function getHook(string $key): ?string
59 {
60 if (isset($this->hooks[$key])) {
61 return $this->hooks[$key];
62 }
63
64 return false;
65 }
66
67 public function addHook(string $key, array $options): void
68 {
69 $this->hooks[$key] = $options;
70 }
71
72 public function removeHook(string $key): void
73 {
74 if (isset($this->hooks[$key])) {
75 unset($this->hooks[$key]);
76 }
77 }
78
79 public function addInput(\ilFormPropertyGUI $input, array $options = array()): void
80 {
81 $this->inputs[$input->getPostVar()] = $input;
82 $this->input_options[$input->getPostVar()] = $options;
83 $this->counter++;
84 }
85
86 public function getTemplateDir(): string
87 {
89 }
90
91 public function setTemplateDir(string $template_dir): void
92 {
93 $this->template_dir = $template_dir;
94 }
95
96 public function isShowLabel(): bool
97 {
98 return $this->show_label;
99 }
100
101 public function setShowLabel(bool $show_label)
102 {
103 $this->show_label = $show_label;
104 }
105
106 public function getInputs(): array
107 {
108 return $this->inputs;
109 }
110
111 public function setMulti(bool $a_multi, bool $a_sortable = false, bool $a_addremove = true): void
112 {
113 $this->multi = $a_multi;
114 }
115
116 public function setValue(string $a_value): void
117 {
118 foreach ($this->inputs as $key => $item) {
119 if (method_exists($item, 'setValue')) {
120 $item->setValue($a_value[$key]);
121 } elseif ($item instanceof \ilDateTimeInputGUI) {
122 $item->setDate(new \ilDate($a_value[$key]['date'], IL_CAL_DATE));
123 }
124 }
125 $this->value = $a_value;
126 }
127
128 public function getValue(): array
129 {
130 $out = array();
131 foreach ($this->inputs as $key => $item) {
132 $out[$key] = $item->getValue();
133 }
134
135 return $out;
136 }
137
142 public function setValueByArray(array $a_values): void
143 {
144 $data = $a_values[$this->getPostVar()];
145 if ($this->getMulti()) {
146 $this->line_values = $data;
147 } else {
148 $this->setValue($data);
149 }
150 }
151
156 public function checkInput(): bool
157 {
158 global $lng;
159 $valid = true;
160 // escape data
161 $out_array = array();
162 foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
163 foreach ($this->inputs as $input_key => $input) {
164 if (isset($item[$input_key])) {
165 $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? \ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
166 }
167 }
168 }
169 $_POST[$this->getPostVar()] = $out_array;
170 if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
171 $valid = false;
172 }
173 // validate
174 foreach ($this->inputs as $input_key => $inputs) {
175 if (!$inputs->checkInput()) {
176 $valid = false;
177 }
178 }
179 if (!$valid) {
180 $this->setAlert($lng->txt("msg_input_is_required"));
181
182 return false;
183 }
184
185 return $valid;
186 }
187
188 public function addCustomAttribute(string $key, string $value, bool $override = false): void
189 {
190 if (isset($this->cust_attr[$key]) && !$override) {
191 $this->cust_attr[$key] .= ' ' . $value;
192 } else {
193 $this->cust_attr[$key] = $value;
194 }
195 }
196
197 public function getCustomAttributes(): array
198 {
199 return (array) $this->cust_attr;
200 }
201
202 private function createInputPostVar(string $iterator_id, \ilFormPropertyGUI $input): string
203 {
204 if ($this->getMulti()) {
205 return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
206 } else {
207 return $this->getPostVar() . '[' . $input->getPostVar() . ']';
208 }
209 }
210
215 public function render(int $iterator_id = 0, bool $clean_render = false): string
216 {
217 $first_label = true;
218 $tpl = new \ilTemplate(
219 "tpl.multi_line_input.html",
220 true,
221 true,
222 'Customizing/global/plugins/Services/Repository/RepositoryObject/LiveVoting'
223 );
224 $class = 'multi_input_line';
225 $this->addCustomAttribute('class', $class, true);
226 foreach ($this->getCustomAttributes() as $key => $value) {
227 $tpl->setCurrentBlock('cust_attr');
228 $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
229 $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
230 $tpl->parseCurrentBlock();
231 }
233 foreach ($inputs as $key => $input) {
234 $input = clone $input;
235 $is_hidden = false;
236 $is_ta = false;
237 if (!method_exists($input, 'render')) {
238 switch (true) {
239 case ($input instanceof \ilHiddenInputGUI):
240 $is_hidden = true;
241 break;
242 case ($input instanceof \ilTextAreaInputGUI):
243 $is_ta = true;
244 break;
245 default:
246 throw new \ilException("Method " . get_class($input)
247 . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
248 }
249 }
250
251 $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
252 if ($is_disabled_hook !== false && !$clean_render) {
253 $input->setDisabled($is_disabled_hook($this->getValue()));
254 }
255 if ($this->getDisabled()) {
256 $input->setDisabled(true);
257 }
258 if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
259 $this->post_var_cache[$key] = $input->getPostVar();
260 } else {
261 // Reset post var
262 $input->setPostVar($this->post_var_cache[$key]);
263 }
264 $post_var = $this->createInputPostVar($iterator_id, $input);
265 $input->setPostVar($post_var);
266 $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
267 if ($before_render_hook !== false && !$clean_render) {
268 $input = $before_render_hook($this->getValue(), $key, $input);
269 }
270 switch (true) {
271 case $is_hidden:
272 $tpl->setCurrentBlock('hidden');
273 $tpl->setVariable('NAME', $post_var);
274 $tpl->setVariable('VALUE', ilLegacyFormElementsUtil::prepareFormOutput($input->getValue()));
275 break;
276 case $is_ta:
277 if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
278 $tpl->setCurrentBlock('input_label');
279 $tpl->setVariable('LABEL', $input->getTitle());
280 $tpl->setVariable('CONTENT', $input->getHTML());
281 $tpl->parseCurrentBlock();
282 $first_label = false;
283 } else {
284 $tpl->setCurrentBlock('input');
285 $tpl->setVariable('CONTENT', $input->getHTML());
286 }
287 break;
288 default:
289 if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
290 $tpl->setCurrentBlock('input_label');
291 $tpl->setVariable('LABEL', $input->getTitle());
292 $tpl->setVariable('CONTENT', $input->render());
293 $first_label = false;
294 } else {
295 $tpl->setCurrentBlock('input');
296 $tpl->setVariable('CONTENT', $input->render());
297 }
298 break;
299 }
300 if ($this->isShowInfo()) {
301 if ($this->isShowLabel()) {
302 $tpl->setCurrentBlock('input_info_label');
303 $tpl->setVariable('INFO_LABEL', $input->getInfo());
304 $tpl->parseCurrentBlock();
305 } else {
306 $tpl->setCurrentBlock('input_info');
307 $tpl->setVariable('INFO', $input->getInfo());
308 $tpl->parseCurrentBlock();
309 }
310 }
311 $tpl->parseCurrentBlock();
312 }
313 if ($this->getMulti() && !$this->getDisabled()) {
314 $image_plus = xlvoGlyphGUI::get('plus');
315 $show_remove = true;
316 $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
317 if ($is_removeable_hook !== false && !$clean_render) {
318 $show_remove = $is_removeable_hook($this->getValue());
319 }
320 $show_remove = true;
321 $image_minus = ($show_remove) ? xlvoGlyphGUI::get('minus') : '<span class="glyphicon glyphicon-minus hide"></span>';
322 $tpl->setCurrentBlock('multi_icons');
323 $tpl->setVariable('IMAGE_PLUS', $image_plus);
324 $tpl->setVariable('IMAGE_MINUS', $image_minus);
325 $tpl->parseCurrentBlock();
326 if ($this->isPositionMovable()) {
327 $tpl->setCurrentBlock('multi_icons_move');
328 $tpl->setVariable('IMAGE_UP', xlvoGlyphGUI::get(xlvoGlyphGUI::UP));
329 $tpl->setVariable('IMAGE_DOWN', xlvoGlyphGUI::get(xlvoGlyphGUI::DOWN));
330 $tpl->parseCurrentBlock();
331 }
332 }
333
334 return $tpl->get();
335 }
336
337 public function initCSSandJS(): void
338 {
339 global $tpl;
340 $tpl->addJavascript('./Modules/OrgUnit/templates/default/multi_line_input.js');
341 }
342
343 public function insert(ilTemplate $a_tpl): int
344 {
345 $output = "";
346
347 $output .= $this->render(0, true);
348 if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
349 foreach ($this->line_values as $run => $data) {
350 $object = $this;
351 $object->setValue($data);
352 $output .= $object->render($run);
353 }
354 } else {
355 $output .= $this->render(0, true);
356 }
357 if ($this->getMulti()) {
358 $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output
359 . '</div>';
360 $output .= '<script type="text/javascript">$("#' . $this->getFieldId()
361 . '").multi_line_input(' . json_encode($this->input_options) . ')</script>';
362 }
363 $a_tpl->setCurrentBlock("prop_generic");
364 $a_tpl->setVariable("PROP_GENERIC", $output);
365 $a_tpl->parseCurrentBlock();
366 }
367
371 public function getTableFilterHTML()
372 {
373 return $this->render();
374 }
375
379 public function getToolbarHTML(): string
380 {
381 return $this->render("toolbar");
382 }
383
384 public function isPositionMovable(): bool
385 {
387 }
388
390 {
391 $this->position_movable = $position_movable;
392 }
393
394 public function isShowLabelOnce(): bool
395 {
397 }
398
399 public function setShowLabelOnce(bool $show_label_once): void
400 {
401 $this->setShowLabel(false);
402 $this->show_label_once = $show_label_once;
403 }
404
405 public function isShowInfo(): bool
406 {
407 return $this->show_info;
408 }
409
410 public function setShowInfo(bool $show_info): void
411 {
412 $this->show_info = $show_info;
413 }
414}
$out
Definition: buildRTE.php:24
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
const IL_CAL_DATE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class for single dates.
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static prepareFormOutput($a_str, bool $a_strip=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createInputPostVar(string $iterator_id, \ilFormPropertyGUI $input)
addCustomAttribute(string $key, string $value, bool $override=false)
render(int $iterator_id=0, bool $clean_render=false)
getTableFilterHTML()
Get HTML for table filter.
checkInput()
Check input, strip slashes etc.
setMulti(bool $a_multi, bool $a_sortable=false, bool $a_addremove=true)
addInput(\ilFormPropertyGUI $input, array $options=array())
__construct(string $a_title="", string $a_postvar="")
Constructor.
setValueByArray(array $a_values)
Set value by array.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents a text area property in a property form.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$valid
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
get(string $key, Refinery\Transformation $t)
Get passed parameter, if not data passed, get key from http request.