ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
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(
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, false))) {
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 toImageOnly(CopyrightDataInterface $copyright): ?Icon
66 {
67 return $this->buildIcon($copyright);
68 }
69
70 public function toLinkOnly(CopyrightDataInterface $copyright): ?Link
71 {
72 return $this->buildLink($copyright, true);
73 }
74
75 public function toString(CopyrightDataInterface $copyright): string
76 {
77 $full_name = $copyright->fullName();
78 $link = $copyright->link();
79
80 $res = [];
81 if ($full_name !== '') {
82 $res[] = $full_name;
83 }
84 if ($link !== null) {
85 $res[] = (string) $link;
86 }
87
88 return implode(' ', $res);
89 }
90
91 protected function buildIcon(CopyrightDataInterface $copyright): ?Icon
92 {
93 if (!$copyright->hasImage()) {
94 if ($copyright->fallBackToDefaultImage()) {
95 return $this->buildFallBackIcon($copyright);
96 }
97 return null;
98 }
99 if ($copyright->isImageLink()) {
100 return $this->buildIconFromLink($copyright);
101 } else {
102 return $this->buildIconFromFile($copyright);
103 }
104 }
105
106 protected function buildIconFromLink(CopyrightDataInterface $copyright): Icon
107 {
108 return $this->customIcon(
109 (string) $copyright->imageLink(),
110 $copyright->altText()
111 );
112 }
113
114 protected function buildIconFromFile(CopyrightDataInterface $copyright): ?Icon
115 {
116 if ($from_irss = $this->getSourceFromIRSS($copyright->imageFile())) {
117 $src = $from_irss;
118 } else {
119 return null;
120 }
121
122 return $this->customIcon(
123 $src,
124 $copyright->altText()
125 );
126 }
127
128 protected function buildFallBackIcon(CopyrightDataInterface $copyright): ?Icon
129 {
130 return $this->customIcon(
131 $this->getFallBackSrc(),
132 $copyright->altText()
133 );
134 }
135
136 protected function getFallBackSrc(): string
137 {
138 return \ilUtil::getImagePath(self::FALLBACK_IMG);
139 }
140
141 protected function buildLink(CopyrightDataInterface $copyright, bool $allow_empty_link): ?Link
142 {
143 if (
144 !$copyright->link() &&
145 (!$allow_empty_link || $copyright->fullName() === '')
146 ) {
147 return null;
148 }
149 return $this->standardLink(
150 $copyright->fullName() !== '' ? $copyright->fullName() : (string) $copyright->link(),
151 (string) $copyright->link(),
152 $copyright->link() !== null ? Relationship::LICENSE : null,
153 $copyright->link() === null
154 );
155 }
156
157 protected function customIcon(string $src, string $alt): Icon
158 {
159 return $this->factory->symbol()->icon()->custom($src, $alt, Icon::MEDIUM);
160 }
161
162 protected function standardLink(
163 string $label,
164 string $action,
165 ?Relationship $relationship,
166 bool $disabled
167 ): Link {
168 $link = $this->factory->link()->standard($label, $action);
169 if ($relationship !== null) {
170 $link = $link->withAdditionalRelationshipToReferencedResource($relationship);
171 }
172 if ($disabled) {
173 $link = $link->withDisabled();
174 }
175 return $link;
176 }
177
178 protected function textInLegacy(string $text): Content
179 {
180 return $this->factory->legacy()->content($text);
181 }
182
183 protected function getSourceFromIRSS(string $string_id): string
184 {
185 if ($identifier = $this->irss->manage()->find($string_id)) {
186 return $this->irss->consume()->src($identifier)->getSrc();
187 }
188 return '';
189 }
190}
factory()
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
getSourceFromIRSS(string $string_id)
Definition: Renderer.php:183
buildIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:91
toString(CopyrightDataInterface $copyright)
Definition: Renderer.php:75
toLinkOnly(CopyrightDataInterface $copyright)
Returns a string as a disabled link, if only a string can be returned, or null if the copyright is im...
Definition: Renderer.php:70
standardLink(string $label, string $action, ?Relationship $relationship, bool $disabled)
Definition: Renderer.php:162
toUIComponents(CopyrightDataInterface $copyright)
Definition: Renderer.php:48
__construct(Factory $factory, IRSS $irss)
Definition: Renderer.php:37
buildIconFromLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:106
buildIconFromFile(CopyrightDataInterface $copyright)
Definition: Renderer.php:114
buildLink(CopyrightDataInterface $copyright, bool $allow_empty_link)
Definition: Renderer.php:141
customIcon(string $src, string $alt)
Definition: Renderer.php:157
buildFallBackIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:128
toImageOnly(CopyrightDataInterface $copyright)
Definition: Renderer.php:65
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38
$res
Definition: ltiservices.php:69
link(string $caption, string $href, bool $new_viewport=false)
$text
Definition: xapiexit.php:21