ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilBadgeImageTemplateTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
28use ilLanguage;
31use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Message\RequestInterface;
35use Generator;
40
42{
43 private readonly Factory $factory;
44 private readonly Renderer $renderer;
45 private readonly \ILIAS\Refinery\Factory $refinery;
46 private readonly ServerRequestInterface|RequestInterface $request;
47 private readonly Services $http;
48 private readonly ilLanguage $lng;
51 private ?array $cached_records = null;
52
53 public function __construct(protected bool $has_write = false)
54 {
55 global $DIC;
56 $this->lng = $DIC->language();
57 $this->tpl = $DIC->ui()->mainTemplate();
58 $this->factory = $DIC->ui()->factory();
59 $this->renderer = $DIC->ui()->renderer();
60 $this->refinery = $DIC->refinery();
61 $this->request = $DIC->http()->request();
62 $this->http = $DIC->http();
63 }
64
68 private function getRecords(): array
69 {
70 if ($this->cached_records !== null) {
72 }
73
74 $modal_container = new ModalBuilder();
75 $rows = [];
76
77 foreach (ilBadgeImageTemplate::getInstances() as $template) {
78 $image = '';
79 $title = $template->getTitle();
80
81 $image_src = $template->getImageFromResourceId();
82 if ($image_src !== '') {
83 $image_component = $this->factory->image()->responsive(
84 $image_src,
85 $template->getTitle()
86 );
87 $image_html = $this->renderer->render($image_component);
88
89 $image_src_large = $template->getImageFromResourceId(
91 );
92 $large_image_component = $this->factory->image()->responsive(
93 $image_src_large,
94 $template->getTitle()
95 );
96
97 $modal = $modal_container->constructModal($large_image_component, $template->getTitle());
98
99 $image = implode('', [
100 $modal_container->renderShyButton($image_html, $modal),
101 $modal_container->renderModal($modal)
102 ]);
103 $title = $modal_container->renderShyButton($template->getTitle(), $modal);
104 }
105
106 $rows[] = [
107 'id' => $template->getId(),
108 'image' => $image,
109 'title' => $title,
110 'title_sortable' => $template->getTitle()
111 ];
112 }
113
114 $this->cached_records = $rows;
115
116 return $rows;
117 }
118
119 public function getRows(
120 DataRowBuilder $row_builder,
121 array $visible_column_ids,
123 Order $order,
124 mixed $additional_viewcontrol_data,
125 mixed $filter_data,
126 mixed $additional_parameters
127 ): Generator {
128 $records = $this->getRecords();
129
130 if ($order) {
131 [$order_field, $order_direction] = $order->join(
132 [],
133 fn($ret, $key, $value) => [$key, $value]
134 );
135
136 usort($records, static function (array $left, array $right) use ($order_field): int {
137 if ($order_field === 'title') {
138 return \ilStr::strCmp(
139 $left[$order_field . '_sortable'],
140 $right[$order_field . '_sortable']
141 );
142 }
143
144 return $left[$order_field] <=> $right[$order_field];
145 });
146
147 if ($order_direction === Order::DESC) {
148 $records = array_reverse($records);
149 }
150 }
151
152 if ($range) {
153 $records = \array_slice($records, $range->getStart(), $range->getLength());
154 }
155
156 foreach ($records as $record) {
157 yield $row_builder->buildDataRow((string) $record['id'], $record);
158 }
159 }
160
161 public function getTotalRowCount(
162 mixed $additional_viewcontrol_data,
163 mixed $filter_data,
164 mixed $additional_parameters
165 ): ?int {
166 return \count($this->getRecords());
167 }
168
172 public function getColumns(): array
173 {
174 return [
175 'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
176 'title' => $this->factory->table()->column()->text($this->lng->txt('title'))
177 ];
178 }
179
183 private function getActions(
184 URLBuilder $url_builder,
185 URLBuilderToken $action_parameter_token,
186 URLBuilderToken $row_id_token
187 ): array {
188 return $this->has_write ? [
189 'badge_image_template_edit' => $this->factory->table()->action()->single(
190 $this->lng->txt('edit'),
191 $url_builder->withParameter($action_parameter_token, 'badge_image_template_editImageTemplate'),
192 $row_id_token
193 ),
194 'badge_image_template_delete' =>
195 $this->factory->table()->action()->standard(
196 $this->lng->txt('delete'),
197 $url_builder->withParameter($action_parameter_token, 'badge_image_template_delete'),
198 $row_id_token
199 )
200 ] : [];
201 }
202
203 public function renderTable(string $url): void
204 {
205 $df = new \ILIAS\Data\Factory();
206
207 $table_uri = $df->uri($url);
208 $url_builder = new URLBuilder($table_uri);
209 $query_params_namespace = ['tid'];
210
211 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
212 $query_params_namespace,
213 'table_action',
214 'id',
215 );
216
217 $table = $this->factory
218 ->table()
219 ->data($this, $this->lng->txt('badge_image_templates'), $this->getColumns())
220 ->withId(self::class)
221 ->withOrder(new Order('title', Order::ASC))
222 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
223 ->withRequest($this->request);
224
225 $out = [$table];
226 $query = $this->http->wrapper()->query();
227 if ($query->has('tid')) {
228 $query_values = $query->retrieve(
229 'tid',
230 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
231 );
232
233 $items = [];
234 if ($query_values === ['ALL_OBJECTS']) {
235 foreach (ilBadgeImageTemplate::getInstances() as $template) {
236 if ($template->getId() !== null) {
237 $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
238 (string) $template->getId(),
239 (string) $template->getId(),
240 $template->getTitle()
241 );
242 }
243 }
244 } elseif (\is_array($query_values)) {
245 foreach ($query_values as $id) {
246 $badge = new ilBadgeImageTemplate((int) $id);
247 $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
248 (string) $id,
249 (string) $badge->getId(),
250 $badge->getTitle()
251 );
252 }
253 } else {
254 $badge = new ilBadgeImageTemplate($query_values);
255 $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
256 (string) $badge->getId(),
257 (string) $badge->getId(),
258 $badge->getTitle()
259 );
260 }
261 if ($query->has($action_parameter_token->getName())) {
262 $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->kindlyTo()->string());
263 if ($action === 'badge_image_template_delete') {
264 $this->http->saveResponse(
265 $this->http
266 ->response()
267 ->withBody(
268 Streams::ofString($this->renderer->renderAsync([
269 $this->factory->modal()->interruptive(
270 $this->lng->txt('badge_deletion'),
271 $this->lng->txt('badge_deletion_confirmation'),
272 '#'
273 )->withAffectedItems($items)
274 ]))
275 )
276 );
277 $this->http->sendResponse();
278 $this->http->close();
279 }
280 }
281 }
282
283 $this->tpl->setContent($this->renderer->render($out));
284 }
285}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderer()
factory()
$out
Definition: buildRTE.php:24
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
readonly ServerRequestInterface RequestInterface $request
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Class Services.
Definition: Services.php:38
language handling
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
buildDataRow(string $id, array $record)
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.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68