ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLinkInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 {
31  public const EXTERNAL_LINK_MAX_LENGTH = 200;
32  public const LIST = "list";
33  public const BOTH = "both";
34  public const INT = "int";
35  public const EXT = "ext";
36 
37  protected string $allowed_link_types = self::BOTH;
38  protected string $int_link_default_type = "RepositoryItem";
39  protected int $int_link_default_obj = 0;
40  protected array $int_link_filter_types = array("RepositoryItem");
41  protected bool $filter_white_list = true;
42  protected int $external_link_max_length = self::EXTERNAL_LINK_MAX_LENGTH;
43 
44  protected static array $iltypemap = array(
45  "page" => "PageObject",
46  "chap" => "StructureObject",
47  "term" => "GlossaryItem",
48  "wpage" => "WikiPage"
49  );
51  protected string $requested_postvar;
52  protected string $value = "";
53 
54  public function __construct(
55  string $a_title = "",
56  string $a_postvar = ""
57  ) {
58  global $DIC;
59 
60  $this->ctrl = $DIC->ctrl();
61  $this->lng = $DIC->language();
62 
63  parent::__construct($a_title, $a_postvar);
64  $this->setType("link");
65 
66  $this->obj_definition = $DIC["objDefinition"];
67 
68  $this->requested_postvar = $this->str("postvar");
69  }
70 
76  public function setAllowedLinkTypes(string $a_val): void
77  {
78  $this->allowed_link_types = $a_val;
79  }
80 
81  public function getAllowedLinkTypes(): string
82  {
84  }
85 
92  public function setInternalLinkDefault(
93  string $a_type,
94  int $a_obj = 0
95  ): void {
96  $this->int_link_default_type = $a_type;
97  $this->int_link_default_obj = $a_obj;
98  }
99 
105  public function setInternalLinkFilterTypes(array $a_val): void
106  {
107  $this->int_link_filter_types = $a_val;
108  }
109 
115  public static function getTypeToAttrType(): array
116  {
117  return self::$iltypemap;
118  }
119 
125  public static function getAttrTypeToType(): array
126  {
127  return array_flip(self::$iltypemap);
128  }
129 
135  public function setFilterWhiteList(bool $a_val): void
136  {
137  $this->filter_white_list = $a_val;
138  }
139 
140  public function getFilterWhiteList(): bool
141  {
143  }
144 
148  public function setExternalLinkMaxLength(int $a_max): void
149  {
150  $this->external_link_max_length = $a_max;
151  }
152 
153  public function getExternalLinkMaxLength(): int
154  {
156  }
157 
158  public function executeCommand()
159  {
160  $ilCtrl = $this->ctrl;
161  $lng = $this->lng;
162 
163  $next_class = $ilCtrl->getNextClass($this);
164  $cmd = $ilCtrl->getCmd();
165 
166  $ret = "";
167  switch ($next_class) {
168  case "ilinternallinkgui":
169  $lng->loadLanguageModule("content");
170  $link_gui = new ilInternalLinkGUI(
171  $this->int_link_default_type,
172  $this->int_link_default_obj
173  );
174  foreach ($this->int_link_filter_types as $t) {
175  $link_gui->filterLinkType($t);
176  }
177  $link_gui->setFilterWhiteList($this->getFilterWhiteList());
178 
179  $ret = $ilCtrl->forwardCommand($link_gui);
180  break;
181 
182  default:
183  var_dump($cmd);
184  //exit();
185  }
186 
187  return $ret;
188  }
189 
194  public function setValue(string $a_value): void
195  {
196  $this->value = $a_value;
197  }
198 
203  public function getValue(): string
204  {
205  return $this->value;
206  }
207 
208  public function setValueByArray(array $a_values): void
209  {
210  switch ($a_values[$this->getPostVar() . "_mode"] ?? null) {
211  case "int":
212  if ($a_values[$this->getPostVar() . "_ajax_type"] &&
213  $a_values[$this->getPostVar() . "_ajax_id"]) {
214  $val = $a_values[$this->getPostVar() . "_ajax_type"] . "|" .
215  $a_values[$this->getPostVar() . "_ajax_id"];
216  if ($a_values[$this->getPostVar() . "_ajax_target"] != "") {
217  $val .= "|" . $a_values[$this->getPostVar() . "_ajax_target"];
218  }
219  $this->setValue($val);
220  }
221  break;
222 
223  case "no":
224  break;
225 
226  default:
227  if ($a_values[$this->getPostVar()]) {
228  $this->setValue($a_values[$this->getPostVar()]);
229  }
230  break;
231  }
232  }
233 
238  public function checkInput(): bool
239  {
240  $lng = $this->lng;
241 
242  // debugging
243  // return false;
244 
245  $mode_type = $this->str($this->getPostVar() . "_mode_type");
246  $ajax_type = $this->str($this->getPostVar() . "_ajax_type");
247  $ajax_id = $this->str($this->getPostVar() . "_ajax_id");
248  $mode = $this->str($this->getPostVar() . "_mode");
249  $value = $this->str($this->getPostVar());
250 
251  if ($this->getRequired()) {
252  if ($mode_type == "list") {
253  return true;
254  }
255 
256  switch ($mode) {
257  case "ext":
258  if (!$value) {
259  $this->setAlert($lng->txt("msg_input_is_required"));
260  return false;
261  }
262  break;
263 
264  case "int":
265  if (!$ajax_type || !$ajax_id) {
266  $this->setAlert($lng->txt("msg_input_is_required"));
267  return false;
268  }
269  break;
270 
271  case "no":
272  default:
273  $this->setAlert($lng->txt("msg_input_is_required"));
274  return false;
275  }
276  }
277 
278  return true;
279  }
280 
281  public function getInput(): string
282  {
283  $ajax_type = $this->str($this->getPostVar() . "_ajax_type");
284  $ajax_id = $this->str($this->getPostVar() . "_ajax_id");
285  $ajax_target = $this->str($this->getPostVar() . "_ajax_target");
286  $mode = $this->str($this->getPostVar() . "_mode");
287  $value = $this->str($this->getPostVar());
288 
289  if ($mode == "int") {
290  // overwriting post-data so getInput() will work
291  $val = $ajax_type . "|" . $ajax_id;
292  if ($ajax_target != "") {
293  $val .= "|" . $ajax_target;
294  }
295  return $val;
296  } elseif ($mode == "no") {
297  return "";
298  }
299  return $value;
300  }
301 
302  public function render(): string
303  {
304  $lng = $this->lng;
305  $ilCtrl = $this->ctrl;
306 
307  $ti = null;
308  $ne = null;
309  $hidden_type = null;
310  $hidden_id = null;
311  $hidden_target = null;
312 
313  // parse settings
314  $has_int = $has_ext = $has_radio = $has_list = false;
315  switch ($this->getAllowedLinkTypes()) {
316  case self::EXT:
317  $has_ext = true;
318  break;
319 
320  case self::INT:
321  $has_int = true;
322  break;
323 
324  case self::BOTH:
325  $has_int = true;
326  $has_ext = true;
327  $has_radio = true;
328  break;
329 
330  case self::LIST:
331  $has_int = true;
332  $has_ext = true;
333  $has_radio = true;
334  $has_list = true;
335  break;
336  }
337  if (!$this->getRequired()) {
338  // see #0021274
339  $has_radio = true;
340  }
341 
342  // external
343  if ($has_ext) {
344  $title = $has_radio ? $lng->txt("url") : "";
345 
346  // external
347  $ti = new ilTextInputGUI($title, $this->getPostVar());
348  $ti->setMaxLength($this->getExternalLinkMaxLength());
349  }
350 
351  $itpl = new ilTemplate('tpl.prop_link.html', true, true, 'Services/Form');
352 
353  // internal
354  if ($has_int) {
355  $ilCtrl->setParameterByClass("ilformpropertydispatchgui", "postvar", $this->getPostVar());
356  $link = array(get_class($this->getParentForm()), "ilformpropertydispatchgui", get_class($this), "ilinternallinkgui");
357  $link = $ilCtrl->getLinkTargetByClass($link, "", '', true, false);
358  $ilCtrl->setParameterByClass("ilformpropertydispatchgui", "postvar", $this->requested_postvar);
359 
360  $no_disp_class = (strpos($this->getValue(), "|"))
361  ? ""
362  : " ilNoDisplay";
363 
364  $itpl->setVariable("VAL_ID", $this->getPostVar());
365  $itpl->setVariable("URL_EDIT", $link);
366  $itpl->setVariable("TXT_EDIT", $lng->txt("form_get_link"));
367  $itpl->setVariable("CSS_REMOVE", $no_disp_class);
368  $itpl->setVariable("TXT_REMOVE", $lng->txt("remove"));
369 
370  $ne = new ilNonEditableValueGUI($lng->txt("object"), $this->getPostVar() . "_val", true);
371 
372  // hidden field for selected value
373  $hidden_type = new ilHiddenInputGUI($this->getPostVar() . "_ajax_type");
374  $hidden_id = new ilHiddenInputGUI($this->getPostVar() . "_ajax_id");
375  $hidden_target = new ilHiddenInputGUI($this->getPostVar() . "_ajax_target");
376  }
377 
378  // mode
379  if ($has_radio) {
380  // BT 35578: link input might be required (so $has_radio = true), but might only be internal
381  if ($has_ext) {
382  $ext = new ilRadioOption($lng->txt("form_link_external"), "ext");
383  $ext->addSubItem($ti);
384  }
385 
386  if ($has_int) {
387  $int = new ilRadioOption($lng->txt("form_link_internal"), "int");
388  $int->addSubItem($ne);
389  }
390  $mode = new ilRadioGroupInputGUI("", $this->getPostVar() . "_mode");
391  $mode->setParentForm($this->getParentForm());
392  if (!$this->getRequired()) {
393  $no = new ilRadioOption($lng->txt("form_no_link"), "no");
394  $mode->addOption($no);
395  }
396  // BT 35578: link input might be required (so $has_radio = true), but might only be internal
397  if ($has_ext) {
398  $mode->addOption($ext);
399  }
400  if ($has_int) {
401  $mode->addOption($int);
402  }
403  } else {
404  $mode = new ilHiddenInputGUI($this->getPostVar() . "_mode");
405  if ($has_int) {
406  $mode->setValue("int");
407  } else {
408  $mode->setValue("ext");
409  }
410  }
411 
412  // list mode
413  if ($has_list) {
414  $mode_type = new ilRadioGroupInputGUI("", $this->getPostVar() . "_mode_type");
415  $mode_single = new ilRadioOption($lng->txt("webr_link_type_single"), "single");
416  $mode_type->addOption($mode_single);
417  $mode_list = new ilRadioOption($lng->txt("webr_link_type_list"), "list");
418  $mode_type->addOption($mode_list);
419  $mode = new ilRadioGroupInputGUI($lng->txt("webr_link_target"), $this->getPostVar() . "_mode");
420  if (!$this->getRequired()) {
421  $no = new ilRadioOption($lng->txt("form_no_link"), "no");
422  $mode->addOption($no);
423  }
424  $ext = new ilRadioOption($lng->txt("form_link_external"), "ext");
425  $ext->addSubItem($ti);
426  $int = new ilRadioOption($lng->txt("form_link_internal"), "int");
427  $int->addSubItem($ne);
428  $mode->addOption($ext);
429  $mode->addOption($int);
430  $mode_single->addSubItem($mode);
431  }
432 
433  // value
434  $value = $this->getValue();
435  if ($value) {
436  // #15647
437  if ($has_int && self::isInternalLink($value)) {
438  $mode->setValue("int");
439 
440  $value_trans = self::getTranslatedValue($value);
441 
442  $value = explode("|", $value);
443  $hidden_type->setValue($value[0]);
444  $hidden_id->setValue($value[1]);
445  $hidden_target->setValue($value[2] ?? "");
446 
447  $itpl->setVariable("VAL_OBJECT_TYPE", $value_trans["type"]);
448  $itpl->setVariable("VAL_OBJECT_NAME", $value_trans["name"]);
449  if (($value[2] ?? "") != "") {
450  $itpl->setVariable("VAL_TARGET_FRAME", "(" . $value[2] . ")");
451  }
452  } elseif ($has_ext) {
453  $mode->setValue("ext");
454 
455  $ti->setValue($value);
456  }
457  } elseif (!$this->getRequired()) {
458  $mode->setValue("no");
459  }
460 
461  // #10185 - default for external urls
462  if ($has_ext && !$ti->getValue()) {
463  $ti->setValue("https://");
464  }
465 
466  if ($has_int) {
467  $ne->setValue($itpl->get());
468  }
469 
470  // to html
471  if ($has_radio) {
472  $html = $mode->render();
473  } else {
474  $html = $mode->getToolbarHTML();
475 
476  if ($has_ext) {
477  $html .= $ti->getToolbarHTML();
478  } elseif ($has_int) {
479  $html .= $ne->render() .
480  '<div class="help-block">' . $ne->getInfo() . '</div>';
481  }
482  }
483  if ($has_list) {
484  $html = $mode_type->render();
485  }
486 
487  // js for internal link
488  if ($has_int) {
489  $html .= $hidden_type->getToolbarHTML() .
490  $hidden_id->getToolbarHTML() .
491  $hidden_target->getToolbarHTML();
492  }
493 
494  return $html;
495  }
496 
497  public function getContentOutsideFormTag(): string
498  {
499  if ($this->getAllowedLinkTypes() == self::INT ||
500  $this->getAllowedLinkTypes() == self::BOTH ||
501  $this->getAllowedLinkTypes() == self::LIST) {
502  // as the ajax-panel uses a form it has to be outside of the parent form!
504  }
505  return "";
506  }
507 
508  public static function isInternalLink(string $a_value): bool
509  {
510  if (strpos($a_value, "|")) {
511  $parts = explode("|", $a_value);
512  if (sizeof($parts) == 2 || sizeof($parts) == 3) {
513  // numeric id
514  if (is_numeric($parts[1])) {
515  // simple type
516  if (preg_match("/^[a-zA-Z_]+$/", $parts[0])) {
517  return true;
518  }
519  }
520  }
521  }
522  return false;
523  }
524 
525  public static function getTranslatedValue(string $a_value): array
526  {
527  global $DIC;
528 
529  $lng = $DIC->language();
530 
531  $value = explode("|", $a_value);
532  if ($value === false || $value === []) {
533  return [];
534  }
535  switch ($value[0]) {
536  case "media":
537  $type = $lng->txt("obj_mob");
538  $name = ilObject::_lookupTitle((int) $value[1]);
539  break;
540 
541  case "page":
542  $type = $lng->txt("obj_pg");
543  $name = ilLMPageObject::_lookupTitle((int) $value[1]);
544  break;
545 
546  case "chap":
547  $type = $lng->txt("obj_st");
548  $name = ilStructureObject::_lookupTitle((int) $value[1]);
549  break;
550 
551  case "term":
552  $type = $lng->txt("term");
553  $name = ilGlossaryTerm::_lookGlossaryTerm((int) $value[1]);
554  break;
555 
556  case "wpage":
557  $type = $lng->txt("cont_wiki_page");
558  $name = ilWikiPage::lookupTitle((int) $value[1]);
559  break;
560 
561  default:
562  $type = $lng->txt("obj_" . $value[0]);
564  break;
565  }
566  return array("type" => $type, "name" => $name);
567  }
568 
569  public function insert(ilTemplate $a_tpl): void
570  {
571  $html = $this->render();
572 
573  $a_tpl->setCurrentBlock("prop_generic");
574  $a_tpl->setVariable("PROP_GENERIC", $html);
575  $a_tpl->parseCurrentBlock();
576  }
577 
583  public function getIntLinkAttributes(): ?array
584  {
585  $val = explode("|", $this->getInput());
586  $ret = null;
587  $type = "";
588  $target = "";
589  if (self::isInternalLink($this->getInput())) {
590  $target_frame = $val[2] ?? "";
591  $map = self::getTypeToAttrType();
592  if (isset($map[$val[0]])) {
593  $type = $map[$val[0]];
594  $target_type = $val[0];
595  if ($val[0] == "chap") {
596  $target_type = "st";
597  }
598  if ($val[0] == "term") {
599  $target_type = "git";
600  }
601  if ($val[0] == "page") {
602  $target_type = "pg";
603  }
604  $target = "il__" . $target_type . "_" . $val[1];
605  } elseif ($this->obj_definition->isRBACObject($val[0])) {
606  $type = "RepositoryItem";
607  $target = "il__obj_" . $val[1];
608  }
609  if ($type != "") {
610  $ret = array(
611  "Target" => $target,
612  "Type" => $type,
613  "TargetFrame" => $target_frame
614  );
615  }
616  }
617  return $ret;
618  }
619 
621  string $a_type,
622  string $a_target,
623  string $a_target_frame = ""
624  ): void {
625  $t = explode("_", $a_target);
626  $target_id = ($t[3] ?? "");
627  $type = "";
628  $map = self::getAttrTypeToType();
629  if ($a_type == "RepositoryItem") {
630  $type = ilObject::_lookupType((int) $target_id, true);
631  } elseif (isset($map[$a_type])) {
632  $type = $map[$a_type];
633  }
634  if ($type != "" && $target_id != "") {
635  $val = $type . "|" . $target_id;
636  if ($a_target_frame != "") {
637  $val .= "|" . $a_target_frame;
638  }
639  $this->setValue($val);
640  }
641  }
642 
643  public function getOnloadCode(): array
644  {
645  return [
647  ];
648  }
649 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static array $iltypemap
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insert(ilTemplate $a_tpl)
setValueByArray(array $a_values)
static getInitHTML(string $a_url)
Get initialisation HTML to use internal link editing.
setAllowedLinkTypes(string $a_val)
Set allowed link types (LIST, BOTH, INT, EXT)
$target_type
Definition: goto.php:51
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
checkInput()
Check input, strip slashes etc.
static getAttrTypeToType()
Get internal types to xml attribute types map (reverse)
setInternalLinkFilterTypes(array $a_val)
Set internal link filter types.
static getTranslatedValue(string $a_value)
$target_id
Definition: goto.php:52
setFilterWhiteList(bool $a_val)
Set filter white list.
static getOnloadCode(string $a_url)
setValueByIntLinkAttributes(string $a_type, string $a_target, string $a_target_frame="")
loadLanguageModule(string $a_module)
Load language module.
Internal link selector.
setInternalLinkDefault(string $a_type, int $a_obj=0)
Set internal link default.
static _lookupObjId(int $ref_id)
static _lookupTitle(int $a_obj_id)
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
parses the objects.xml it handles the xml-description of all ilias objects
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getTypeToAttrType()
Get internal types to xml attribute types map.
This class represents a property in a property form.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
static _lookupTitle(int $obj_id)
__construct(string $a_title="", string $a_postvar="")
static _lookGlossaryTerm(int $term_id)
get glossary term
setValue(string $a_value)
Set Value.
getIntLinkAttributes()
Get value as internal link attributes.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static lookupTitle(int $a_page_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static isInternalLink(string $a_value)
This class represents a property in a property form.
__construct(Container $dic, ilPlugin $plugin)
static _lookupType(int $id, bool $reference=false)
ilObjectDefinition $obj_definition
setExternalLinkMaxLength(int $a_max)
This class represents a external and/or internal link in a property form.