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