ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilLegacyFormElementsUtil.php
Go to the documentation of this file.
1 <?php
2 
32 {
37  public static function prepareFormOutput($a_str, bool $a_strip = false): string
38  {
39  if ($a_strip) {
40  $a_str = ilUtil::stripSlashes($a_str);
41  }
42  $a_str = htmlspecialchars((string) $a_str);
43  // Added replacement of curly brackets to prevent
44  // problems with PEAR templates, because {xyz} will
45  // be removed as unused template variable
46  $a_str = str_replace("{", "&#123;", $a_str);
47  $a_str = str_replace("}", "&#125;", $a_str);
48  // needed for LaTeX conversion \\ in LaTeX is a line break
49  // but without this replacement, php changes \\ to \
50  $a_str = str_replace("\\", "&#92;", $a_str);
51  return $a_str;
52  }
53 
59  public static function period2String(ilDateTime $a_from, $a_to = null): string
60  {
61  global $DIC;
62 
63  $lng = $DIC->language();
64 
65  if (!$a_to) {
66  $a_to = new ilDateTime(time(), IL_CAL_UNIX);
67  }
68 
69  $from = new DateTime($a_from->get(IL_CAL_DATETIME));
70  $to = new DateTime($a_to->get(IL_CAL_DATETIME));
71  $diff = $to->diff($from);
72 
73  $periods = [];
74  $periods["years"] = $diff->format("%y");
75  $periods["months"] = $diff->format("%m");
76  $periods["days"] = $diff->format("%d");
77  $periods["hours"] = $diff->format("%h");
78  $periods["minutes"] = $diff->format("%i");
79  $periods["seconds"] = $diff->format("%s");
80 
81  if (!array_sum($periods)) {
82  return '';
83  }
84  $array = [];
85  foreach ($periods as $key => $value) {
86  if ($value) {
87  $segment_name = ($value > 1)
88  ? $key
89  : substr($key, 0, -1);
90  $array[] = $value . ' ' . $lng->txt($segment_name);
91  }
92  }
93 
94  if ($len = count($array) > 3) {
95  $array = array_slice($array, 0, (3 - $len));
96  }
97 
98  return implode(', ', $array);
99  }
100 
109  public static function prepareTextareaOutput(
110  string $txt_output,
111  bool $prepare_for_latex_output = false,
112  bool $omitNl2BrWhenTextArea = false
113  ) {
114  $result = $txt_output;
115  $is_html = ilUtil::isHTML($result);
116 
117  // removed: did not work with magic_quotes_gpc = On
118  if (!$is_html) {
119  if (!$omitNl2BrWhenTextArea) {
120  // if the string does not contain HTML code, replace the newlines with HTML line breaks
121  $result = preg_replace("/[\n]/", "<br />", $result);
122  }
123  } else {
124  // patch for problems with the <pre> tags in tinyMCE
125  if (preg_match_all("/(<pre>.*?<\/pre>)/ims", $result, $matches)) {
126  foreach ($matches[0] as $found) {
127  $replacement = "";
128  if (strpos("\n", $found) === false) {
129  $replacement = "\n";
130  }
131  $removed = preg_replace("/<br\s*?\/>/ims", $replacement, $found);
132  $result = str_replace($found, $removed, $result);
133  }
134  }
135  }
136 
137  if ($prepare_for_latex_output) {
138  $result = ilRTE::replaceLatexSpan($result);
139 
140  // replace special characters to prevent problems with the ILIAS template system
141  // eg. if someone uses {1} as an answer, nothing will be shown without the replacement
142  $result = str_replace("{", "&#123;", $result);
143  $result = str_replace("}", "&#125;", $result);
144  $result = str_replace("\\", "&#92;", $result);
145  }
146 
147  return $result;
148  }
149 
165  public static function makeTimeSelect(
166  string $prefix,
167  bool $short = true,
168  int $hour = 0,
169  int $minute = 0,
170  int $second = 0,
171  bool $a_use_default = true,
172  array $a_further_options = []
173  ): string {
174  global $DIC;
175 
176  $lng = $DIC->language();
177  $ilUser = $DIC->user();
178 
179  $minute_steps = 1;
180  $disabled = '';
181  if (count($a_further_options)) {
182  if (isset($a_further_options['minute_steps'])) {
183  $minute_steps = $a_further_options['minute_steps'];
184  }
185  if (isset($a_further_options['disabled']) and $a_further_options['disabled']) {
186  $disabled = 'disabled="disabled" ';
187  }
188  }
189 
190  if ($a_use_default and !strlen("$hour$minute$second")) {
191  $now = localtime();
192  $hour = $now[2];
193  $minute = $now[1];
194  $second = $now[0];
195  }
196 
197  // build hour select
198  $sel_hour = '<select ';
199  if (isset($a_further_options['select_attributes'])) {
200  foreach ($a_further_options['select_attributes'] as $name => $value) {
201  $sel_hour .= $name . '=' . $value . ' ';
202  }
203  }
204  $sel_hour .= " " . $disabled . "name=\"" . $prefix . "[h]\" id=\"" . $prefix . "_h\" class=\"form-control\">\n";
205 
206  $format = $ilUser->getTimeFormat();
207  for ($i = 0; $i <= 23; $i++) {
208  if ($format == ilCalendarSettings::TIME_FORMAT_24) {
209  $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
210  } else {
211  $sel_hour .= "<option value=\"$i\">" . date("ga", mktime($i, 0, 0)) . "</option>\n";
212  }
213  }
214  $sel_hour .= "</select>\n";
215  $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
216 
217  // build minutes select
218  $sel_minute = "<select " . $disabled . "name=\"" . $prefix . "[m]\" id=\"" . $prefix . "_m\" class=\"form-control\">\n";
219 
220  for ($i = 0; $i <= 59; $i = $i + $minute_steps) {
221  $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
222  }
223  $sel_minute .= "</select>\n";
224  $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
225 
226  if (!$short) {
227  // build seconds select
228  $sel_second = "<select " . $disabled . "name=\"" . $prefix . "[s]\" id=\"" . $prefix . "_s\" class=\"form-control\">\n";
229 
230  for ($i = 0; $i <= 59; $i++) {
231  $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
232  }
233  $sel_second .= "</select>\n";
234  $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
235  }
236  $timeformat = ($lng->text["lang_timeformat"] ?? '');
237  if (strlen($timeformat) == 0) {
238  $timeformat = "H:i:s";
239  }
240  $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
241  $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
242  $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
243  $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
244  if ($short) {
245  $timeformat = preg_replace("/%%s/", "", $timeformat);
246  } else {
247  $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
248  }
249  return $timeformat;
250  }
251 
255  public static function formCheckbox(
256  bool $checked,
257  string $varname,
258  string $value,
259  bool $disabled = false
260  ): string {
261  $str = "<input type=\"checkbox\" name=\"" . $varname . "\"";
262 
263  if ($checked === true) {
264  $str .= " checked=\"checked\"";
265  }
266 
267  if ($disabled === true) {
268  $str .= " disabled=\"disabled\"";
269  }
270 
271  $array_var = false;
272 
273  if (substr($varname, -2) == "[]") {
274  $array_var = true;
275  }
276 
277  // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
278  if ($array_var) {
279  $varname_id = substr($varname, 0, -2) . "_" . $value;
280  } else {
281  $varname_id = $varname;
282  }
283 
284  // dirty removal of other "[]" in string
285  $varname_id = str_replace("[", "_", $varname_id);
286  $varname_id = str_replace("]", "", $varname_id);
287 
288  $str .= " value=\"" . $value . "\" id=\"" . $varname_id . "\" />\n";
289 
290  return $str;
291  }
292 
310  public static function formSelect(
311  $selected,
312  string $varname,
313  array $options,
314  bool $multiple = false,
315  bool $direct_text = false,
316  int $size = 0,
317  string $style_class = "",
318  array $attribs = [],
319  bool $disabled = false
320  ): string {
321  global $DIC;
322 
323  $lng = $DIC->language();
324 
325  if ($multiple == true) {
326  $multiple = " multiple=\"multiple\"";
327  } else {
328  $multiple = "";
329  $size = 0;
330  }
331 
332  $class = " class=\" form-control " . $style_class . "\"";
333 
334  // use form-inline!
335  // this is workaround the whole function should be set deprecated
336  // $attributes = " style='display:inline-block;' ";
337 
338  $attributes = "";
339  if (is_array($attribs)) {
340  foreach ($attribs as $key => $val) {
341  $attributes .= " " . $key . "=\"" . $val . "\"";
342  }
343  }
344  if ($disabled) {
345  $disabled = ' disabled=\"disabled\"';
346  }
347 
348  $size_str = "";
349  if ($size > 0) {
350  $size_str = ' size="' . $size . '" ';
351  }
352  $str = "<select name=\"" . $varname . "\"" . $multiple . " $class " . $size_str . " $attributes $disabled>\n";
353 
354  foreach ($options as $key => $val) {
355  $style = "";
356  if (is_array($val)) {
357  $style = $val["style"];
358  $val = $val["text"]; // mus be last line, since we overwrite
359  }
360 
361  $sty = ($style != "")
362  ? ' style="' . $style . '" '
363  : "";
364 
365  if ($direct_text) {
366  $str .= " <option $sty value=\"" . $key . "\"";
367  } else {
368  $str .= " <option $sty value=\"" . $val . "\"";
369  }
370  if (is_array($selected)) {
371  if (in_array($key, $selected)) {
372  $str .= " selected=\"selected\"";
373  }
374  } elseif ($selected == $key) {
375  $str .= " selected=\"selected\"";
376  }
377 
378  if ($direct_text) {
379  $str .= ">" . $val . "</option>\n";
380  } else {
381  $str .= ">" . $lng->txt($val) . "</option>\n";
382  }
383  }
384 
385  $str .= "</select>\n";
386 
387  return $str;
388  }
389 
393  public static function formRadioButton(
394  bool $checked,
395  string $varname,
396  string $value,
397  ?string $onclick = null,
398  bool $disabled = false
399  ): string {
400  $str = '<input ';
401 
402  if ($onclick !== null) {
403  $str .= ('onclick="' . $onclick . '"');
404  }
405 
406  $str .= (" type=\"radio\" name=\"" . $varname . "\"");
407  if ($checked === true) {
408  $str .= " checked=\"checked\"";
409  }
410 
411  if ($disabled === true) {
412  $str .= " disabled=\"disabled\"";
413  }
414 
415  $str .= " value=\"" . $value . "\"";
416 
417  $str .= " id=\"" . $value . "\" />\n";
418 
419  return $str;
420  }
421 }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const IL_CAL_DATETIME
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="")
static period2String(ilDateTime $a_from, $a_to=null)
Return a string of time period.
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 prepareFormOutput($a_str, bool $a_strip=false)
const IL_CAL_UNIX
static replaceLatexSpan(string $text)
Replace the latex delimiters used by the rich text editor Unfortunately these can&#39;t be processed by M...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
static formCheckbox(bool $checked, string $varname, string $value, bool $disabled=false)
global $lng
Definition: privfeed.php:31
static formRadioButton(bool $checked, string $varname, string $value, ?string $onclick=null, 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 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.