ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Output.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ILIAS\GlobalScreen\GUI\Hasher;
31use ILIAS\GlobalScreen\GUI\I18n\Translator;
35use Psr\Http\Message\ResponseInterface;
38
43class Output
44{
45 use Hasher;
46
49 private \ilGlobalTemplateInterface $main_tpl;
50
51 public function __construct(
52 private UIServices $ui_services,
53 private Services $http,
54 private Translator $translator,
55 private Tabs $tabs,
56 private \ilToolbarGUI $toolbar,
57 ) {
58 $this->ui_factory = $this->ui_services->factory();
59 $this->ui_renderer = $this->ui_services->renderer();
60 $this->main_tpl = $this->ui_services->mainTemplate();
61 }
62
63 public function response(): ResponseInterface
64 {
65 return $this->http->response();
66 }
67
68 public function ui(): UIServices
69 {
70 return $this->ui_services;
71 }
72
73 public function toolbar(): \ilToolbarGUI
74 {
75 return $this->toolbar;
76 }
77
78 public function success(string $message, bool $keep = true): void
79 {
80 $this->message('success', $message, $keep);
81 }
82
83 public function info(string $message, bool $keep = true): void
84 {
85 $this->message('info', $message, $keep);
86 }
87
88 public function error(string $message, bool $keep = true): void
89 {
90 $this->message('failure', $message, $keep);
91 }
92
93 public function message(string $type, string $message, bool $keep = true): void
94 {
95 $this->main_tpl->setOnScreenMessage($type, $message, $keep);
96 }
97
98 public function outAsyncAsConfirmation(
99 string $title,
100 string $message,
101 string $button_text,
102 URI|string $post_url,
104 ): void {
105 if ($post_url instanceof URI) {
106 $post_url = (string) $post_url;
107 }
108 $modal = $this->ui_factory->modal()->interruptive(
109 $title,
110 $message,
111 $post_url
112 )->withAffectedItems(
114 )->withActionButtonLabel($button_text);
115
116 $this->outAsync($modal);
117 }
118
119 public function outAsyncAsModal(
120 string $title,
121 URI|string $post_url,
123 ): void {
124 if ($post_url instanceof URI) {
125 $post_url = (string) $post_url;
126 }
127
128 $is_form = count($components) === 1 && $components[0] instanceof Form;
129 $are_interruptive = array_filter(
131 fn($component): bool => $component instanceof InterruptiveItem
132 ) !== [];
133
134 $modal = match (true) {
135 $is_form => $this->ui_factory->modal()->roundtrip(
136 $title,
137 null,
139 $post_url
140 ),
141 $are_interruptive => $this->ui_factory->modal()->interruptive(
142 $title,
143 $this->translator->translate('confirm_delete'),
144 $post_url
145 )->withAffectedItems(
147 // array_map(
148 // fn(KeyValue $item): \ILIAS\UI\Component\Modal\InterruptiveItem\KeyValue => $this->ui_factory->modal(
149 // )->interruptiveItem()->keyValue(
150 // $this->hash($item->getId()),
151 // $item->getKey(),
152 // $item->getValue()
153 // ),
154 // $components
155 // )
156 ),
157 default => $this->ui_factory->modal()->roundtrip(
158 $title,
160 [],
161 $post_url
162 )
163 };
164
165 $this->outAsync($modal);
166 }
167
168 public function outAsync(?Component ...$components): void
169 {
170 $components = array_filter($components, fn($component): bool => $component !== null);
171 $string = $this->ui_renderer->renderAsync($components);
172 $response = $this->http->response()->withBody(
173 Streams::ofString(
174 $string
175 )
176 );
177 $this->http->saveResponse($response);
178 $this->http->sendResponse();
179 $this->http->close();
180 }
181
182 public function outString(string $string): void
183 {
184 $this->tabs->show();
185 $this->main_tpl->setContent($string);
186 }
187
188 public function out(?Component ...$components): void
189 {
190 $this->tabs->show();
191 $components = array_filter($components, fn($component): bool => $component !== null);
192
193 $this->main_tpl->setContent(
194 $this->ui_renderer->render($components)
195 );
196 }
197
198 public function render(?Component ...$components): string
199 {
200 $components = array_filter($components, fn($component): bool => $component !== null);
201
202 return $this->ui_renderer->render($components);
203 }
204
205 public function nok(bool $as_image = false): Icon|Image
206 {
207 if ($as_image) {
208 return $this->ui_factory->image()->standard(
209 'assets/images/standard/icon_unchecked.svg',
210 ''
211 );
212 }
213
214 return $this->ui_factory->symbol()->icon()->custom(
215 'assets/images/standard/icon_unchecked.svg',
216 '',
217 'small'
218 );
219 }
220
221 public function ok(bool $as_image = false): Icon|Image
222 {
223 if ($as_image) {
224 return $this->ui_factory->image()->standard(
225 'assets/images/standard/icon_checked.svg',
226 ''
227 );
228 }
229
230 return $this->ui_factory->symbol()->icon()->custom(
231 'assets/images/standard/icon_checked.svg',
232 '',
233 'small'
234 );
235 }
236
237}
$components
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
render(?Component ... $components)
Definition: Output.php:198
outAsyncAsModal(string $title, URI|string $post_url, ?Component ... $components)
Definition: Output.php:119
__construct(private UIServices $ui_services, private Services $http, private Translator $translator, private Tabs $tabs, private \ilToolbarGUI $toolbar,)
Definition: Output.php:51
out(?Component ... $components)
Definition: Output.php:188
outAsync(?Component ... $components)
Definition: Output.php:168
ok(bool $as_image=false)
Definition: Output.php:221
outAsyncAsConfirmation(string $title, string $message, string $button_text, URI|string $post_url, InterruptiveItem ... $components)
Definition: Output.php:98
message(string $type, string $message, bool $keep=true)
Definition: Output.php:93
info(string $message, bool $keep=true)
Definition: Output.php:83
success(string $message, bool $keep=true)
Definition: Output.php:78
ilGlobalTemplateInterface $main_tpl
Definition: Output.php:49
nok(bool $as_image=false)
Definition: Output.php:205
error(string $message, bool $keep=true)
Definition: Output.php:88
Class Services.
Definition: Services.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$http
Definition: deliver.php:30
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes commonalities between all forms.
Definition: Form.php:33
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
An entity that renders components to a string output.
Definition: Renderer.php:31
static http()
Fetches the global http state from ILIAS.
if(!file_exists('../ilias.ini.php'))
$message
Definition: xapiexit.php:31
$response
Definition: xapitoken.php:93