ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
109 ->ui_factory
110 ->modal()
111 ->interruptive(
112 $title,
113 $message,
114 $post_url
115 )
116 ->withAffectedItems(
118 )
119 ->withActionButtonLabel($button_text);
120
121 $this->outAsync($modal);
122 }
123
124 public function outAsyncAsModal(
125 string $title,
126 URI|string $post_url,
128 ): void {
129 if ($post_url instanceof URI) {
130 $post_url = (string) $post_url;
131 }
132
133 $is_form = count($components) === 1 && $components[0] instanceof Form;
134 $are_interruptive = array_filter(
136 fn(?Component $component): bool => $component instanceof InterruptiveItem
137 ) !== [];
138
139 $modal = match (true) {
140 $is_form => $this
141 ->ui_factory
142 ->modal()
143 ->roundtrip(
144 $title,
145 null,
147 $post_url
148 ),
149 $are_interruptive => $this
150 ->ui_factory
151 ->modal()
152 ->interruptive(
153 $title,
154 $this->translator->translate('confirm_delete'),
155 $post_url
156 )
157 ->withAffectedItems(
159 ),
160 default => $this
161 ->ui_factory
162 ->modal()
163 ->roundtrip(
164 $title,
166 [],
167 $post_url
168 )
169 };
170
171 $this->outAsync($modal);
172 }
173
174 public function outAsync(?Component ...$components): void
175 {
176 $components = array_filter($components, fn(?Component $component): bool => $component !== null);
177 $string = $this->ui_renderer->renderAsync($components);
178 $response = $this->http->response()->withBody(
179 Streams::ofString(
180 $string
181 )
182 );
183 $this->http->saveResponse($response);
184 $this->http->sendResponse();
185 $this->http->close();
186 }
187
188 public function outString(string $string): void
189 {
190 $this->tabs->show();
191 $this->main_tpl->setContent($string);
192 }
193
194 public function out(?Component ...$components): void
195 {
196 $this->tabs->show();
197 $components = array_filter($components, fn(?Component $component): bool => $component !== null);
198
199 $this->main_tpl->setContent(
200 $this->ui_renderer->render($components)
201 );
202 }
203
204 public function render(?Component ...$components): string
205 {
206 $components = array_filter($components, fn(?Component $component): bool => $component !== null);
207
208 return $this->ui_renderer->render($components);
209 }
210
211 public function nok(bool $as_image = false): Icon|Image
212 {
213 if ($as_image) {
214 return $this->ui_factory->image()->standard(
215 'assets/images/standard/icon_unchecked.svg',
216 ''
217 );
218 }
219
220 return $this->ui_factory->symbol()->icon()->custom(
221 'assets/images/standard/icon_unchecked.svg',
222 '',
223 'small'
224 );
225 }
226
227 public function ok(bool $as_image = false): Icon|Image
228 {
229 if ($as_image) {
230 return $this->ui_factory->image()->standard(
231 'assets/images/standard/icon_checked.svg',
232 ''
233 );
234 }
235
236 return $this->ui_factory->symbol()->icon()->custom(
237 'assets/images/standard/icon_checked.svg',
238 '',
239 'small'
240 );
241 }
242
243}
$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:204
outAsyncAsModal(string $title, URI|string $post_url, ?Component ... $components)
Definition: Output.php:124
__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:194
outAsync(?Component ... $components)
Definition: Output.php:174
ok(bool $as_image=false)
Definition: Output.php:227
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:211
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:34
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'))
$response
Definition: xapitoken.php:90