ILIAS  release_8 Revision v8.23
class.ilDclTextRecordRepresentation.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  public const LINK_MAX_LENGTH = 40;
22 
23  public function getHTML(bool $link = true, array $options = []): string
24  {
25  $value = $this->getRecordField()->getValue();
26 
27  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
28  $views = $this->getRecord()->getTable()->getVisibleTableViews($ref_id, true, $this->user->getId());
29 
30  //Property URL
31  $field = $this->getField();
32  if ($field->hasProperty(ilDclBaseFieldModel::PROP_URL)) {
33  if (is_array($value)) {
34  $link = (string) $value['link'];
35  $link_value = $value['title'] ?: $this->shortenLink($link);
36  } else {
37  $link = (string) $value;
38  $link_value = $this->shortenLink($link);
39  }
40 
41  if (substr($link, 0, 3) === 'www') {
42  $link = 'https://' . $link;
43  }
44 
45  if (preg_match(
46  "/^[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i",
47  $link
48  )) {
49  $link = "mailto:" . $link;
50  } elseif (!(preg_match('~(^(news|(ht|f)tp(s?)\://){1}\S+)~i', $link))) {
51  return $link;
52  }
53 
54  $html = "<a rel='noopener' target='_blank' href='" . htmlspecialchars(
55  $link,
56  ENT_QUOTES
57  ) . "'>" . htmlspecialchars($link_value, ENT_QUOTES) . "</a>";
58  } elseif ($field->hasProperty(ilDclBaseFieldModel::PROP_LINK_DETAIL_PAGE_TEXT) && $link && $views !== []) {
59  $view = array_shift($views);
60  if ($this->http->wrapper()->query()->has('tableview_id')) {
61  $tableview_id = $this->http->wrapper()->query()->retrieve('tableview_id', $this->refinery->kindlyTo()->int());
62  foreach ($views as $v) {
63  if ($v->getId() === $tableview_id) {
64  $view = $tableview_id;
65  break;
66  }
67  }
68  }
69 
70  $this->ctrl->clearParametersByClass("ilDclDetailedViewGUI");
71  $this->ctrl->setParameterByClass(
72  'ilDclDetailedViewGUI',
73  'record_id',
74  $this->getRecordField()->getRecord()->getId()
75  );
76 
77  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'table_id', $this->getRecord()->getTableId());
78  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'tableview_id', $view->getId());
79  $html = '<a href="' . $this->ctrl->getLinkTargetByClass(
80  "ilDclDetailedViewGUI",
81  'renderRecord'
82  ) . '">' . $value . '</a>';
83  } else {
84  $html = (is_array($value) && isset($value['link'])) ? $value['link'] : nl2br((string) $value);
85  }
86 
87  return $html;
88  }
89 
90  protected function shortenLink(string $value): string
91  {
92  $value = preg_replace('/^(https?:\/\/)?(www\.)?/', '', $value);
93  $half = (int) ((self::LINK_MAX_LENGTH - 4) / 2);
94  $value = preg_replace('/^(.{' . ($half + 1) . '})(.{4,})(.{' . $half . '})$/', '\1...\3', $value);
95 
96  return $value;
97  }
98 
99  public function fillFormInput(ilPropertyFormGUI $form): void
100  {
101  $input_field = $form->getItemByPostVar('field_' . $this->getField()->getId());
102  $raw_input = $this->getFormInput();
103 
104  $value = is_array($raw_input) ? $raw_input['link'] : $raw_input;
105  $field_values = [];
106  if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_URL)) {
107  $field_values["field_" . $this->getRecordField()->getField()->getId() . "_title"] = (isset($raw_input['title'])) ? $raw_input['title'] : '';
108  }
109 
110  if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_TEXTAREA)) {
111  $breaks = ["<br />"];
112  $value = str_ireplace($breaks, "", $value);
113  }
114 
115  $field_values["field_" . $this->getRecordField()->getField()->getId()] = $value;
116  $input_field->setValueByArray($field_values);
117  }
118 }
getHTML(bool $link=true, array $options=[])
getItemByPostVar(string $a_post_var)
static http()
Fetches the global http state from ILIAS.
$ref_id
Definition: ltiauth.php:67
getFormInput()
Gets the value from from the record field.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...