ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclTextRecordRepresentation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public const LINK_MAX_LENGTH = 40;
24 
25  public function getHTML(bool $link = true, array $options = []): string
26  {
27  $value = $this->getRecordField()->getValue();
28 
29  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
30 
31  $field = $this->getField();
32  $links = [];
33  if ($field->hasProperty(ilDclBaseFieldModel::PROP_URL)) {
34  $url = $value['link'];
35  $value = $value['title'] ?: $this->shortenLink($url);
36  if ($link) {
37  if (substr($url, 0, 3) === 'www') {
38  $url = 'https://' . $url;
39  } elseif (filter_var($url, FILTER_VALIDATE_EMAIL)) {
40  $url = "mailto:" . $url;
41  }
42  $links['dcl_open_url'] = $url;
43  }
44  }
45  if ($field->hasProperty(ilDclBaseFieldModel::PROP_LINK_DETAIL_PAGE_TEXT) && $link) {
46  if ($this->http->wrapper()->query()->has('tableview_id')) {
47  $tableview_id = $this->http->wrapper()->query()->retrieve('tableview_id', $this->refinery->kindlyTo()->int());
48  } else {
49  $tableview_id = $this->getRecord()->getTable()->getFirstTableViewId($this->user->getId());
50  }
51  if (ilDclDetailedViewDefinition::isActive($tableview_id)) {
52  $this->ctrl->clearParametersByClass("ilDclDetailedViewGUI");
53  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'table_id', $this->getRecord()->getTableId());
54  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'tableview_id', $tableview_id);
55  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'record_id', $this->getRecord()->getId());
56  $links['dcl_open_detail_view'] = $this->ctrl->getLinkTargetByClass(ilDclDetailedViewGUI::class, 'renderRecord');
57  }
58  }
59  $value = nl2br((string) $value);
60 
61  switch (count($links)) {
62  case 0:
63  return $value;
64  case 1:
65  return $this->renderer->render(
66  $this->factory->link()->standard($value, reset($links))->withOpenInNewViewport(true)
67  );
68  case 2:
69  default:
70  $ui_links = [];
71  foreach ($links as $key => $link) {
72  $ui_links[] = $this->factory->link()->standard($this->lng->txt($key), $link)->withOpenInNewViewport(true);
73  }
74  return $this->renderer->render(
75  $this->factory->dropdown()->standard($ui_links)->withLabel($value)
76  );
77  }
78  }
79 
80  protected function shortenLink(string $value): string
81  {
82  $value = preg_replace('/^(https?:\/\/)?(www\.)?/', '', $value);
83  $half = (int) ((self::LINK_MAX_LENGTH - 4) / 2);
84  $value = preg_replace('/^(.{' . ($half + 1) . '})(.{4,})(.{' . $half . '})$/', '\1...\3', $value);
85 
86  return $value;
87  }
88 
89  public function fillFormInput(ilPropertyFormGUI $form): void
90  {
91  $input_field = $form->getItemByPostVar('field_' . $this->getField()->getId());
92  $raw_input = $this->getFormInput();
93 
94  $value = is_array($raw_input) ? $raw_input['link'] : $raw_input;
95  $value = is_string($value) ? $value : "";
96  $field_values = [];
97  if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_URL)) {
98  $field_values["field_" . $this->getRecordField()->getField()->getId() . "_title"] = (isset($raw_input['title'])) ? $raw_input['title'] : '';
99  }
100 
101  $field_values["field_" . $this->getRecordField()->getField()->getId()] = $value;
102  $input_field->setValueByArray($field_values);
103  }
104 }
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.
string $key
Consumer key/client ID value.
Definition: System.php:193
$url
Definition: ltiregstart.php:35