ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Copyright;
22 
29 
30 class Renderer implements RendererInterface
31 {
32  protected const string FALLBACK_IMG = 'copyrights\all_rights_reserved.svg';
33 
34  protected Factory $factory;
35  protected IRSS $irss;
36 
37  public function __construct(
38  Factory $factory,
39  IRSS $irss
40  ) {
41  $this->factory = $factory;
42  $this->irss = $irss;
43  }
44 
48  public function toUIComponents(CopyrightDataInterface $copyright): array
49  {
50  $res = [];
51  $has_link = false;
52  if (!is_null($image = $this->buildIcon($copyright))) {
53  $res[] = $image;
54  }
55  if (!is_null($link = $this->buildLink($copyright))) {
56  $res[] = $link;
57  $has_link = true;
58  }
59  if ($copyright->fullName() && !$has_link) {
60  $res[] = $this->textInLegacy($copyright->fullName());
61  }
62  return $res;
63  }
64 
65  public function toString(CopyrightDataInterface $copyright): string
66  {
67  $full_name = $copyright->fullName();
68  $link = $copyright->link();
69 
70  $res = [];
71  if ($full_name !== '') {
72  $res[] = $full_name;
73  }
74  if ($link !== null) {
75  $res[] = (string) $link;
76  }
77 
78  return implode(' ', $res);
79  }
80 
81  protected function buildIcon(CopyrightDataInterface $copyright): ?Icon
82  {
83  if (!$copyright->hasImage()) {
84  if ($copyright->fallBackToDefaultImage()) {
85  return $this->buildFallBackIcon($copyright);
86  }
87  return null;
88  }
89  if ($copyright->isImageLink()) {
90  return $this->buildIconFromLink($copyright);
91  } else {
92  return $this->buildIconFromFile($copyright);
93  }
94  }
95 
96  protected function buildIconFromLink(CopyrightDataInterface $copyright): Icon
97  {
98  return $this->customIcon(
99  (string) $copyright->imageLink(),
100  $copyright->altText()
101  );
102  }
103 
104  protected function buildIconFromFile(CopyrightDataInterface $copyright): ?Icon
105  {
106  if ($from_irss = $this->getSourceFromIRSS($copyright->imageFile())) {
107  $src = $from_irss;
108  } else {
109  return null;
110  }
111 
112  return $this->customIcon(
113  $src,
114  $copyright->altText()
115  );
116  }
117 
118  protected function buildFallBackIcon(CopyrightDataInterface $copyright): ?Icon
119  {
120  return $this->customIcon(
121  $this->getFallBackSrc(),
122  $copyright->altText()
123  );
124  }
125 
126  protected function getFallBackSrc(): string
127  {
128  return \ilUtil::getImagePath(self::FALLBACK_IMG);
129  }
130 
131  protected function buildLink(CopyrightDataInterface $copyright): ?Link
132  {
133  if (!$copyright->link()) {
134  return null;
135  }
136  return $this->standardLink(
137  $copyright->fullName() !== '' ? $copyright->fullName() : (string) $copyright->link(),
138  (string) $copyright->link()
139  )->withAdditionalRelationshipToReferencedResource(Relationship::LICENSE);
140  }
141 
142  protected function customIcon(string $src, string $alt): Icon
143  {
144  return $this->factory->symbol()->icon()->custom($src, $alt, Icon::MEDIUM);
145  }
146 
147  protected function standardLink(string $label, string $action): Link
148  {
149  return $this->factory->link()->standard($label, $action);
150  }
151 
152  protected function textInLegacy(string $text): Content
153  {
154  return $this->factory->legacy()->content($text);
155  }
156 
157  protected function getSourceFromIRSS(string $string_id): string
158  {
159  if ($identifier = $this->irss->manage()->find($string_id)) {
160  return $this->irss->consume()->src($identifier)->getSrc();
161  }
162  return '';
163  }
164 }
toString(CopyrightDataInterface $copyright)
Definition: Renderer.php:65
$res
Definition: ltiservices.php:66
buildFallBackIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:118
buildIconFromLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:96
factory()
buildIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:81
getSourceFromIRSS(string $string_id)
Definition: Renderer.php:157
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(Factory $factory, IRSS $irss)
Definition: Renderer.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is how the factory for UI elements looks.
Definition: Factory.php:37
buildIconFromFile(CopyrightDataInterface $copyright)
Definition: Renderer.php:104
buildLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:131
customIcon(string $src, string $alt)
Definition: Renderer.php:142
standardLink(string $label, string $action)
Definition: Renderer.php:147
toUIComponents(CopyrightDataInterface $copyright)
Definition: Renderer.php:48