ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilBadgeImageTemplateTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge;
22 
28 use ilLanguage;
35 use Generator;
40 
42 {
43  private readonly Factory $factory;
44  private readonly Renderer $renderer;
45  private readonly \ILIAS\Refinery\Factory $refinery;
47  private readonly Services $http;
48  private readonly ilLanguage $lng;
49  private readonly ilGlobalTemplateInterface $tpl;
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) {
71  return $this->cached_records;
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,
122  Range $range,
123  Order $order,
124  ?array $filter_data,
125  ?array $additional_parameters
126  ): Generator {
127  $records = $this->getRecords();
128 
129  if ($order) {
130  [$order_field, $order_direction] = $order->join(
131  [],
132  fn($ret, $key, $value) => [$key, $value]
133  );
134 
135  usort($records, static function (array $left, array $right) use ($order_field): int {
136  if ($order_field === 'title') {
137  return \ilStr::strCmp(
138  $left[$order_field . '_sortable'],
139  $right[$order_field . '_sortable']
140  );
141  }
142 
143  return $left[$order_field] <=> $right[$order_field];
144  });
145 
146  if ($order_direction === Order::DESC) {
147  $records = array_reverse($records);
148  }
149  }
150 
151  if ($range) {
152  $records = \array_slice($records, $range->getStart(), $range->getLength());
153  }
154 
155  foreach ($records as $record) {
156  yield $row_builder->buildDataRow((string) $record['id'], $record);
157  }
158  }
159 
160  public function getTotalRowCount(
161  ?array $filter_data,
162  ?array $additional_parameters
163  ): ?int {
164  return \count($this->getRecords());
165  }
166 
170  public function getColumns(): array
171  {
172  return [
173  'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
174  'title' => $this->factory->table()->column()->text($this->lng->txt('title'))
175  ];
176  }
177 
181  private function getActions(
182  URLBuilder $url_builder,
183  URLBuilderToken $action_parameter_token,
184  URLBuilderToken $row_id_token
185  ): array {
186  return $this->has_write ? [
187  'badge_image_template_edit' => $this->factory->table()->action()->single(
188  $this->lng->txt('edit'),
189  $url_builder->withParameter($action_parameter_token, 'badge_image_template_editImageTemplate'),
190  $row_id_token
191  ),
192  'badge_image_template_delete' =>
193  $this->factory->table()->action()->standard(
194  $this->lng->txt('delete'),
195  $url_builder->withParameter($action_parameter_token, 'badge_image_template_delete'),
196  $row_id_token
197  )
198  ] : [];
199  }
200 
201  public function renderTable(): void
202  {
203  $df = new \ILIAS\Data\Factory();
204 
205  $table_uri = $df->uri($this->request->getUri()->__toString());
206  $url_builder = new URLBuilder($table_uri);
207  $query_params_namespace = ['tid'];
208 
209  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
210  $query_params_namespace,
211  'table_action',
212  'id',
213  );
214 
215  $table = $this->factory
216  ->table()
217  ->data($this, $this->lng->txt('badge_image_templates'), $this->getColumns())
218  ->withId(self::class)
219  ->withOrder(new Order('title', Order::ASC))
220  ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
221  ->withRequest($this->request);
222 
223  $out = [$table];
224  $query = $this->http->wrapper()->query();
225  if ($query->has('tid')) {
226  $query_values = $query->retrieve(
227  'tid',
228  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
229  );
230 
231  $items = [];
232  if ($query_values === ['ALL_OBJECTS']) {
233  foreach (ilBadgeImageTemplate::getInstances() as $template) {
234  if ($template->getId() !== null) {
235  $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
236  (string) $template->getId(),
237  (string) $template->getId(),
238  $template->getTitle()
239  );
240  }
241  }
242  } elseif (\is_array($query_values)) {
243  foreach ($query_values as $id) {
244  $badge = new ilBadgeImageTemplate((int) $id);
245  $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
246  (string) $id,
247  (string) $badge->getId(),
248  $badge->getTitle()
249  );
250  }
251  } else {
252  $badge = new ilBadgeImageTemplate($query_values);
253  $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
254  (string) $badge->getId(),
255  (string) $badge->getId(),
256  $badge->getTitle()
257  );
258  }
259  if ($query->has($action_parameter_token->getName())) {
260  $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->kindlyTo()->string());
261  if ($action === 'badge_image_template_delete') {
262  $this->http->saveResponse(
263  $this->http
264  ->response()
265  ->withBody(
266  Streams::ofString($this->renderer->renderAsync([
267  $this->factory->modal()->interruptive(
268  $this->lng->txt('badge_deletion'),
269  $this->lng->txt('badge_deletion_confirmation'),
270  '#'
271  )->withAffectedItems($items)
272  ]))
273  )
274  );
275  $this->http->sendResponse();
276  $this->http->close();
277  }
278  }
279  }
280 
281  $this->tpl->setContent($this->renderer->render($out));
282  }
283 }
join($init, callable $fn)
Definition: Order.php:75
factory()
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
renderer()
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static http()
Fetches the global http state from ILIAS.
$out
Definition: buildRTE.php:24
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35
readonly ServerRequestInterface RequestInterface $request
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
URLBuilder.
Definition: URLBuilder.php:40
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28