ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
33  $links = [];
34  if ($field->hasProperty(ilDclBaseFieldModel::PROP_URL)) {
35  $url = $value['link'];
36  $value = $value['title'] ?: $this->shortenLink($url);
37  if ($link) {
38  if (substr($url, 0, 3) === 'www') {
39  $url = 'https://' . $url;
40  } elseif (filter_var($url, FILTER_VALIDATE_EMAIL)) {
41  $url = "mailto:" . $url;
42  }
43  $links['dcl_open_url'] = $url;
44  }
45  }
46  if ($field->hasProperty(ilDclBaseFieldModel::PROP_LINK_DETAIL_PAGE_TEXT) && $link) {
47  if ($this->http->wrapper()->query()->has('tableview_id')) {
48  $tableview_id = $this->http->wrapper()->query()->retrieve('tableview_id', $this->refinery->kindlyTo()->int());
49  } else {
50  $tableview_id = $this->getRecord()->getTable()->getFirstTableViewId($this->user->getId());
51  }
52  if (
53  ilDclDetailedViewDefinition::exists($tableview_id) &&
55  ) {
56  $this->ctrl->clearParametersByClass("ilDclDetailedViewGUI");
57  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'table_id', $this->getRecord()->getTableId());
58  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'tableview_id', $tableview_id);
59  $this->ctrl->setParameterByClass(ilDclDetailedViewGUI::class, 'record_id', $this->getRecord()->getId());
60  $links['dcl_open_detail_view'] = $this->ctrl->getLinkTargetByClass(ilDclDetailedViewGUI::class, 'renderRecord');
61  }
62  }
63 
64  $value = nl2br((string) $value);
65 
66  switch (count($links)) {
67  case 0:
68  return $value;
69  case 1:
70  return $this->renderer->render(
71  $this->factory->link()->standard(
72  $value,
73  reset($links)
74  )->withOpenInNewViewport(true)
75  );
76  case 2:
77  default:
78  $ui_links = [];
79  foreach ($links as $key => $link) {
80  $ui_links[] = $this->factory->link()->standard(
81  $this->lng->txt($key),
82  $link
83  )->withOpenInNewViewport(true);
84  }
85  return $this->renderer->render(
86  $this->factory->dropdown()->standard(
87  $ui_links
88  )->withLabel($value)
89  );
90  }
91 
92  }
93 
94  protected function shortenLink(string $value): string
95  {
96  $value = preg_replace('/^(https?:\/\/)?(www\.)?/', '', $value);
97  $half = (int) ((self::LINK_MAX_LENGTH - 4) / 2);
98  $value = preg_replace('/^(.{' . ($half + 1) . '})(.{4,})(.{' . $half . '})$/', '\1...\3', $value);
99 
100  return $value;
101  }
102 
103  public function fillFormInput(ilPropertyFormGUI $form): void
104  {
105  $input_field = $form->getItemByPostVar('field_' . $this->getField()->getId());
106  $raw_input = $this->getFormInput();
107 
108  $value = is_array($raw_input) ? $raw_input['link'] : $raw_input;
109  $value = is_string($value) ? $value : "";
110  $field_values = [];
111  if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_URL)) {
112  $field_values["field_" . $this->getRecordField()->getField()->getId() . "_title"] = (isset($raw_input['title'])) ? $raw_input['title'] : '';
113  }
114 
115  $field_values["field_" . $this->getRecordField()->getField()->getId()] = $value;
116  $input_field->setValueByArray($field_values);
117  }
118 }
static _lookupActive(int $a_id, string $a_parent_type, bool $a_check_scheduled_activation=false, string $a_lang="-")
lookup activation status
getHTML(bool $link=true, array $options=[])
getItemByPostVar(string $a_post_var)
factory()
renderer()
$url
Definition: shib_logout.php:68
static http()
Fetches the global http state from ILIAS.
$ref_id
Definition: ltiauth.php:65
getFormInput()
Gets the value from from the record field.