ILIAS  release_8 Revision v8.24
class.ilLegacyFormElementsUtil.php
Go to the documentation of this file.
1<?php
2
33{
38 public static function prepareFormOutput($a_str, bool $a_strip = false): string
39 {
40 if ($a_strip) {
41 $a_str = ilUtil::stripSlashes($a_str);
42 }
43 $a_str = htmlspecialchars($a_str);
44 // Added replacement of curly brackets to prevent
45 // problems with PEAR templates, because {xyz} will
46 // be removed as unused template variable
47 $a_str = str_replace("{", "&#123;", $a_str);
48 $a_str = str_replace("}", "&#125;", $a_str);
49 // needed for LaTeX conversion \\ in LaTeX is a line break
50 // but without this replacement, php changes \\ to \
51 $a_str = str_replace("\\", "&#92;", $a_str);
52 return $a_str;
53 }
54
60 public static function period2String(ilDateTime $a_from, $a_to = null): string
61 {
62 global $DIC;
63
64 $lng = $DIC->language();
65
66 if (!$a_to) {
67 $a_to = new ilDateTime(time(), IL_CAL_UNIX);
68 }
69
70 $from = new DateTime($a_from->get(IL_CAL_DATETIME));
71 $to = new DateTime($a_to->get(IL_CAL_DATETIME));
72 $diff = $to->diff($from);
73
74 $periods = [];
75 $periods["years"] = $diff->format("%y");
76 $periods["months"] = $diff->format("%m");
77 $periods["days"] = $diff->format("%d");
78 $periods["hours"] = $diff->format("%h");
79 $periods["minutes"] = $diff->format("%i");
80 $periods["seconds"] = $diff->format("%s");
81
82 if (!array_sum($periods)) {
83 return '';
84 }
85 $array = [];
86 foreach ($periods as $key => $value) {
87 if ($value) {
88 $segment_name = ($value > 1)
89 ? $key
90 : substr($key, 0, -1);
91 $array[] = $value . ' ' . $lng->txt($segment_name);
92 }
93 }
94
95 if ($len = count($array) > 3) {
96 $array = array_slice($array, 0, (3 - $len));
97 }
98
99 return implode(', ', $array);
100 }
101
110 public static function prepareTextareaOutput(
111 string $txt_output,
112 bool $prepare_for_latex_output = false,
113 bool $omitNl2BrWhenTextArea = false
114 ) {
115 $result = $txt_output;
116 $is_html = ilUtil::isHTML($result);
117
118 // removed: did not work with magic_quotes_gpc = On
119 if (!$is_html) {
120 if (!$omitNl2BrWhenTextArea) {
121 // if the string does not contain HTML code, replace the newlines with HTML line breaks
122 $result = preg_replace("/[\n]/", "<br />", $result);
123 }
124 } else {
125 // patch for problems with the <pre> tags in tinyMCE
126 if (preg_match_all("/(<pre>.*?<\/pre>)/ims", $result, $matches)) {
127 foreach ($matches[0] as $found) {
128 $replacement = "";
129 if (strpos("\n", $found) === false) {
130 $replacement = "\n";
131 }
132 $removed = preg_replace("/<br\s*?\/>/ims", $replacement, $found);
133 $result = str_replace($found, $removed, $result);
134 }
135 }
136 }
137
138 // since server side mathjax rendering does include svg-xml structures that indeed have linebreaks,
139 // do latex conversion AFTER replacing linebreaks with <br>. <svg> tag MUST NOT contain any <br> tags.
140 if ($prepare_for_latex_output) {
141 $result = ilMathJax::getInstance()->insertLatexImages($result, "<span class\=\"latex\">", "<\/span>");
142 $result = ilMathJax::getInstance()->insertLatexImages($result, "\[tex\]", "\[\/tex\]");
143 }
144
145 if ($prepare_for_latex_output) {
146 // replace special characters to prevent problems with the ILIAS template system
147 // eg. if someone uses {1} as an answer, nothing will be shown without the replacement
148 $result = str_replace("{", "&#123;", $result);
149 $result = str_replace("}", "&#125;", $result);
150 $result = str_replace("\\", "&#92;", $result);
151 }
152
153 return $result;
154 }
155
171 public static function makeTimeSelect(
172 string $prefix,
173 bool $short = true,
174 int $hour = 0,
175 int $minute = 0,
176 int $second = 0,
177 bool $a_use_default = true,
178 array $a_further_options = []
179 ): string {
180 global $DIC;
181
182 $lng = $DIC->language();
183 $ilUser = $DIC->user();
184
185 $minute_steps = 1;
186 $disabled = '';
187 if (count($a_further_options)) {
188 if (isset($a_further_options['minute_steps'])) {
189 $minute_steps = $a_further_options['minute_steps'];
190 }
191 if (isset($a_further_options['disabled']) and $a_further_options['disabled']) {
192 $disabled = 'disabled="disabled" ';
193 }
194 }
195
196 if ($a_use_default and !strlen("$hour$minute$second")) {
197 $now = localtime();
198 $hour = $now[2];
199 $minute = $now[1];
200 $second = $now[0];
201 }
202
203 // build hour select
204 $sel_hour = '<select ';
205 if (isset($a_further_options['select_attributes'])) {
206 foreach ($a_further_options['select_attributes'] as $name => $value) {
207 $sel_hour .= $name . '=' . $value . ' ';
208 }
209 }
210 $sel_hour .= " " . $disabled . "name=\"" . $prefix . "[h]\" id=\"" . $prefix . "_h\" class=\"form-control\">\n";
211
212 $format = $ilUser->getTimeFormat();
213 for ($i = 0; $i <= 23; $i++) {
215 $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
216 } else {
217 $sel_hour .= "<option value=\"$i\">" . date("ga", mktime($i, 0, 0)) . "</option>\n";
218 }
219 }
220 $sel_hour .= "</select>\n";
221 $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
222
223 // build minutes select
224 $sel_minute = "<select " . $disabled . "name=\"" . $prefix . "[m]\" id=\"" . $prefix . "_m\" class=\"form-control\">\n";
225
226 for ($i = 0; $i <= 59; $i = $i + $minute_steps) {
227 $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
228 }
229 $sel_minute .= "</select>\n";
230 $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
231
232 if (!$short) {
233 // build seconds select
234 $sel_second = "<select " . $disabled . "name=\"" . $prefix . "[s]\" id=\"" . $prefix . "_s\" class=\"form-control\">\n";
235
236 for ($i = 0; $i <= 59; $i++) {
237 $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
238 }
239 $sel_second .= "</select>\n";
240 $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
241 }
242 $timeformat = ($lng->text["lang_timeformat"] ?? '');
243 if (strlen($timeformat) == 0) {
244 $timeformat = "H:i:s";
245 }
246 $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
247 $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
248 $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
249 $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
250 if ($short) {
251 $timeformat = preg_replace("/%%s/", "", $timeformat);
252 } else {
253 $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
254 }
255 return $timeformat;
256 }
257
261 public static function formCheckbox(
262 bool $checked,
263 string $varname,
264 string $value,
265 bool $disabled = false
266 ): string {
267 $str = "<input type=\"checkbox\" name=\"" . $varname . "\"";
268
269 if ($checked === true) {
270 $str .= " checked=\"checked\"";
271 }
272
273 if ($disabled === true) {
274 $str .= " disabled=\"disabled\"";
275 }
276
277 $array_var = false;
278
279 if (substr($varname, -2) == "[]") {
280 $array_var = true;
281 }
282
283 // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
284 if ($array_var) {
285 $varname_id = substr($varname, 0, -2) . "_" . $value;
286 } else {
287 $varname_id = $varname;
288 }
289
290 // dirty removal of other "[]" in string
291 $varname_id = str_replace("[", "_", $varname_id);
292 $varname_id = str_replace("]", "", $varname_id);
293
294 $str .= " value=\"" . $value . "\" id=\"" . $varname_id . "\" />\n";
295
296 return $str;
297 }
298
316 public static function formSelect(
317 $selected,
318 string $varname,
319 array $options,
320 bool $multiple = false,
321 bool $direct_text = false,
322 int $size = 0,
323 string $style_class = "",
324 array $attribs = [],
325 bool $disabled = false
326 ): string {
327 global $DIC;
328
329 $lng = $DIC->language();
330
331 if ($multiple == true) {
332 $multiple = " multiple=\"multiple\"";
333 } else {
334 $multiple = "";
335 $size = 0;
336 }
337
338 $class = " class=\" form-control " . $style_class . "\"";
339
340 // use form-inline!
341 // this is workaround the whole function should be set deprecated
342 // $attributes = " style='display:inline-block;' ";
343
344 $attributes = "";
345 if (is_array($attribs)) {
346 foreach ($attribs as $key => $val) {
347 $attributes .= " " . $key . "=\"" . $val . "\"";
348 }
349 }
350 if ($disabled) {
351 $disabled = ' disabled=\"disabled\"';
352 }
353
354 $size_str = "";
355 if ($size > 0) {
356 $size_str = ' size="' . $size . '" ';
357 }
358 $str = "<select name=\"" . $varname . "\"" . $multiple . " $class " . $size_str . " $attributes $disabled>\n";
359
360 foreach ($options as $key => $val) {
361 $style = "";
362 if (is_array($val)) {
363 $style = $val["style"];
364 $val = $val["text"]; // mus be last line, since we overwrite
365 }
366
367 $sty = ($style != "")
368 ? ' style="' . $style . '" '
369 : "";
370
371 if ($direct_text) {
372 $str .= " <option $sty value=\"" . $key . "\"";
373 } else {
374 $str .= " <option $sty value=\"" . $val . "\"";
375 }
376 if (is_array($selected)) {
377 if (in_array($key, $selected)) {
378 $str .= " selected=\"selected\"";
379 }
380 } elseif ($selected == $key) {
381 $str .= " selected=\"selected\"";
382 }
383
384 if ($direct_text) {
385 $str .= ">" . $val . "</option>\n";
386 } else {
387 $str .= ">" . $lng->txt($val) . "</option>\n";
388 }
389 }
390
391 $str .= "</select>\n";
392
393 return $str;
394 }
395
399 public static function formRadioButton(
400 bool $checked,
401 string $varname,
402 string $value,
403 string $onclick = null,
404 bool $disabled = false
405 ): string {
406 $str = '<input ';
407
408 if ($onclick !== null) {
409 $str .= ('onclick="' . $onclick . '"');
410 }
411
412 $str .= (" type=\"radio\" name=\"" . $varname . "\"");
413 if ($checked === true) {
414 $str .= " checked=\"checked\"";
415 }
416
417 if ($disabled === true) {
418 $str .= " disabled=\"disabled\"";
419 }
420
421 $str .= " value=\"" . $value . "\"";
422
423 $str .= " id=\"" . $value . "\" />\n";
424
425 return $str;
426 }
427}
const IL_CAL_UNIX
const IL_CAL_DATETIME
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static period2String(ilDateTime $a_from, $a_to=null)
Return a string of time period.
static makeTimeSelect(string $prefix, bool $short=true, int $hour=0, int $minute=0, int $second=0, bool $a_use_default=true, array $a_further_options=[])
Creates a combination of HTML selects for time inputs.
static formCheckbox(bool $checked, string $varname, string $value, bool $disabled=false)
static prepareTextareaOutput(string $txt_output, bool $prepare_for_latex_output=false, bool $omitNl2BrWhenTextArea=false)
Prepares a string for a text area output where latex code may be in it If the text is HTML-free,...
static prepareFormOutput($a_str, bool $a_strip=false)
static formRadioButton(bool $checked, string $varname, string $value, string $onclick=null, bool $disabled=false)
static formSelect( $selected, string $varname, array $options, bool $multiple=false, bool $direct_text=false, int $size=0, string $style_class="", array $attribs=[], bool $disabled=false)
Builds a select form field with options and shows the selected option first.
static getInstance()
Singleton: get instance for use in ILIAS requests with a config loaded from the settings.
static isHTML(string $a_text)
Checks if a given string contains HTML or not.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
if($format !==null) $name
Definition: metadata.php:247
$format
Definition: metadata.php:235
$i
Definition: metadata.php:41
$attributes
Definition: metadata.php:248
string $key
Consumer key/client ID value.
Definition: System.php:193
$lng