ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.TermListTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
27
32{
33 protected \ilCtrl $ctrl;
34 protected \ilLanguage $lng;
35 protected \ilGlobalTemplateInterface $tpl;
36 protected UI\Factory $ui_fac;
37 protected UI\Renderer $ui_ren;
38 protected ServerRequestInterface $request;
39 protected Data\Factory $df;
40 protected \ilObjGlossary $glossary;
41 protected int $tax_node;
42 protected \ilGlossaryTermPermission $term_perm;
43 protected array $adv_cols_order = [];
44 protected \ILIAS\AdvancedMetaData\Services\SubObjectModes\DataTable\SupplierInterface $adv_term_mode;
46 protected string $requested_table_term_list_action = "";
47
51 protected array $requested_table_term_list_ids = [];
52
54 {
55 global $DIC;
56
57 $this->ctrl = $DIC->ctrl();
58 $this->lng = $DIC->language();
59 $this->tpl = $DIC["tpl"];
60 $this->ui_fac = $DIC->ui()->factory();
61 $this->ui_ren = $DIC->ui()->renderer();
62 $this->request = $DIC->http()->request();
63 $this->df = new Data\Factory();
64 $this->glossary = $glossary;
65 $this->tax_node = $tax_node;
66 $this->term_perm = \ilGlossaryTermPermission::getInstance();
67 $this->edit_gui_request = $DIC->glossary()->internal()->gui()->editing()->request();
68 $this->requested_table_term_list_action = $this->edit_gui_request->getTableGlossaryTermListAction();
69 $this->requested_table_term_list_ids = $this->edit_gui_request->getTableGlossaryTermListIds();
70
71 $adv_service = new \ILIAS\AdvancedMetaData\Services\Services();
72 $this->adv_term_mode = $adv_service->forSubObjects(
73 "glo",
74 $this->glossary->getRefId(),
75 "term"
76 )->inDataTable();
77 }
78
79 public function getComponent(): UI\Component\Table\Data
80 {
81 $columns = $this->getColumns();
82 $actions = $this->getActions();
83 $data_retrieval = $this->getDataRetrieval();
84
85 if ($this->requested_table_term_list_action === "deleteTerms") {
86 $items = [];
87 foreach ($this->requested_table_term_list_ids as $id) {
88 if ($id === "ALL_OBJECTS") {
89 $filter_term = "";
90 $filter_def = "";
91 $data = $this->glossary->getTermList(
92 $filter_term,
93 "",
94 $filter_def,
95 $this->tax_node,
96 true,
97 true,
98 null,
99 false,
100 true
101 );
102
103 $terms = [];
104 foreach ($data as $term) {
105 $term_id = $term["id"];
106 $add = $this->handleTermForModal((int) $term_id);
107 $items[] = $this->ui_fac->modal()->interruptiveItem()->standard(
108 (string) $term_id,
109 \ilGlossaryTerm::_lookGlossaryTerm((int) $term_id),
110 null,
111 $add
112 );
113 }
114 } else {
115 $add = $this->handleTermForModal((int) $id);
116 $items[] = $this->ui_fac->modal()->interruptiveItem()->standard(
117 $id,
119 null,
120 $add
121 );
122 }
123 }
124 echo($this->ui_ren->renderAsync([
125 $this->ui_fac->modal()->interruptive(
126 "",
127 empty($items) ? $this->lng->txt("no_checkbox") : $this->lng->txt("info_delete_sure"),
128 $this->ctrl->getFormActionByClass("ilobjglossarygui", "deleteTerms")
129 )
130 ->withAffectedItems($items)
131 ->withActionButtonLabel(empty($items) ? $this->lng->txt("ok") : $this->lng->txt("delete"))
132 ]));
133 exit();
134 }
135
136 $table = $this->ui_fac->table()
137 ->data($data_retrieval, $this->lng->txt("cont_terms"), $columns)
138 ->withId(
139 self::class . "_" .
140 $this->glossary->getRefId()
141 )
142 ->withActions($actions)
143 ->withRequest($this->request);
144
145 return $table;
146 }
147
148 protected function handleTermForModal(int $id): string
149 {
151 if ($term_glo_id != $this->glossary->getId()
152 && !\ilGlossaryTermReferences::isReferenced([$this->glossary->getId()], $id)
153 ) {
154 //TODO: How to handle redirects in modals?
155 $this->tpl->setOnScreenMessage("failure", $this->lng->txt("glo_term_must_belong_to_glo"), true);
156 $this->ctrl->redirectByClass("ilobjglossarygui", "listTerms");
157 }
158
159 $add = "";
161 if ($nr > 0) {
162 $this->ctrl->setParameterByClass(
163 "ilglossarytermgui",
164 "term_id",
165 $id
166 );
167
168 if (\ilGlossaryTermReferences::isReferenced([$this->glossary->getId()], $id)) {
169 $add = " (" . $this->lng->txt("glo_term_reference") . ")";
170 } else {
171 $link = $this->ui_fac->link()->standard(
172 $this->lng->txt("glo_list_usages"),
173 $this->ctrl->getLinkTargetByClass("ilglossarytermgui", "listUsages")
174 );
175 $add = sprintf($this->lng->txt("glo_term_is_used_n_times"), $nr)
176 . " [" . $this->ui_ren->render($link) . "]";
177 }
178 }
179
180 return $add;
181 }
182
183 protected function getColumns(): array
184 {
185 $columns = [];
186 $adv_columns = [];
187
188 $adv_ap = new \ilGlossaryAdvMetaDataAdapter($this->glossary->getRefId());
189 $this->adv_cols_order = $adv_ap->getColumnOrder();
190 foreach ($this->adv_cols_order as $c) {
191 $id = $c["id"];
192 if ($id == 0) {
193 $columns["term"] = $this->ui_fac->table()->column()->text($this->lng->txt("cont_term"));
194 } else {
195 if (empty($adv_columns)) {
196 $adv_columns = $this->adv_term_mode->getColumns();
197 }
198 }
199 }
200
201 foreach ($adv_columns as $k => $adv_column) {
202 $adv_column = $adv_column->withIsOptional(true, false);
203 $adv_columns[$k] = $adv_column;
204 }
205 $columns = array_merge($columns, $adv_columns);
206
207 $columns["language"] = $this->ui_fac->table()->column()->text($this->lng->txt("language"))
208 ->withIsSortable(false)
209 ->withIsOptional(true, true);
210 $columns["usage"] = $this->ui_fac->table()->column()->number($this->lng->txt("cont_usage"))
211 ->withIsSortable(false)
212 ->withIsOptional(true, true);
213 $columns["usage_link"] = $this->ui_fac->table()->column()->link($this->lng->txt("glo_usage_link"))
214 ->withIsSortable(false)
215 ->withIsOptional(true, true);
216 $columns["definition"] = $this->ui_fac->table()->column()->text($this->lng->txt("cont_definition"))
217 ->withIsSortable(false);
218
219 if ($this->glossary->getVirtualMode() === "coll"
220 || \ilGlossaryTermReferences::hasReferences($this->glossary->getId())
221 ) {
222 $columns["glossary"] = $this->ui_fac->table()->column()->text($this->lng->txt("obj_glo"))
223 ->withIsSortable(false);
224 }
225
226 return $columns;
227 }
228
229 protected function getActions(): array
230 {
231 $query_params_namespace = ["glo_term_list_table"];
232
233 $uri_copy = $this->df->uri(
234 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass("ilobjglossarygui", "copyTerms")
235 );
236 $url_builder_copy = new UI\URLBuilder($uri_copy);
237 list($url_builder_copy, $action_parameter_token_copy, $row_id_token_copy) =
238 $url_builder_copy->acquireParameters(
239 $query_params_namespace,
240 "action",
241 "term_ids"
242 );
243
244 $uri_reference = $this->df->uri(
245 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass("ilobjglossarygui", "referenceTerms")
246 );
247 $url_builder_reference = new UI\URLBuilder($uri_reference);
248 list($url_builder_reference, $action_parameter_token_reference, $row_id_token_reference) =
249 $url_builder_reference->acquireParameters(
250 $query_params_namespace,
251 "action",
252 "term_ids"
253 );
254
255 $url_builder_delete = new UI\URLBuilder($this->df->uri($this->request->getUri()->__toString()));
256 list($url_builder_delete, $action_parameter_token_delete, $row_id_token_delete) =
257 $url_builder_delete->acquireParameters(
258 $query_params_namespace,
259 "action",
260 "term_ids"
261 );
262
263 $uri_edit_definition = $this->df->uri(
264 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass(["ilglossarytermgui",
265 "iltermdefinitioneditorgui",
266 "ilglossarydefpagegui"], "edit")
267 );
268 $url_builder_edit_definition = new UI\URLBuilder($uri_edit_definition);
269 list($url_builder_edit_definition, $action_parameter_token_edit_definition, $row_id_token_edit_definition) =
270 $url_builder_edit_definition->acquireParameters(
271 $query_params_namespace,
272 "action",
273 "term_ids"
274 );
275
276 $uri_edit_term = $this->df->uri(
277 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass("ilglossarytermgui", "editTerm")
278 );
279 $url_builder_edit_term = new UI\URLBuilder($uri_edit_term);
280 list($url_builder_edit_term, $action_parameter_token_edit_term, $row_id_token_edit_term) =
281 $url_builder_edit_term->acquireParameters(
282 $query_params_namespace,
283 "action",
284 "term_ids"
285 );
286
287 $actions = [
288 "copy" => $this->ui_fac->table()->action()->multi(
289 $this->lng->txt("copy"),
290 $url_builder_copy->withParameter($action_parameter_token_copy, "copyTerms"),
291 $row_id_token_copy
292 ),
293 "reference" => $this->ui_fac->table()->action()->multi(
294 $this->lng->txt("glo_reference"),
295 $url_builder_reference->withParameter($action_parameter_token_reference, "referenceTerms"),
296 $row_id_token_reference
297 ),
298 "delete" => $this->ui_fac->table()->action()->multi(
299 $this->lng->txt("delete"),
300 $url_builder_delete->withParameter($action_parameter_token_delete, "deleteTerms"),
301 $row_id_token_delete
302 )
303 ->withAsync()
304 ];
305
306 $actions["edit_definition"] = $this->ui_fac->table()->action()->single(
307 $this->lng->txt("cont_edit_definition"),
308 $url_builder_edit_definition->withParameter($action_parameter_token_edit_definition, "editDefinition"),
309 $row_id_token_edit_definition
310 );
311
312 $actions["edit_term"] = $this->ui_fac->table()->action()->single(
313 $this->lng->txt("cont_edit_term"),
314 $url_builder_edit_term->withParameter($action_parameter_token_edit_term, "editTerm"),
315 $row_id_token_edit_term
316 );
317
318 return $actions;
319 }
320
321 protected function getDataRetrieval(): UI\Component\Table\DataRetrieval
322 {
323 $data_retrieval = new class (
330 $this->df,
333 ) implements UI\Component\Table\DataRetrieval {
334 use TableRecords;
335
336 protected \ilCtrl $ctrl;
337
338 public function __construct(
339 protected \ilObjGlossary $glossary,
340 protected int $tax_node,
341 protected array $adv_cols_order,
342 protected \ILIAS\AdvancedMetaData\Services\SubObjectModes\DataTable\SupplierInterface $adv_term_mode,
343 protected UI\Factory $ui_fac,
344 protected UI\Renderer $ui_ren,
345 protected Data\Factory $df,
346 protected \ilLanguage $lng,
348 ) {
349 global $DIC;
350
351 $this->ctrl = $DIC->ctrl();
352 }
353
354 public function getRows(
355 UI\Component\Table\DataRowBuilder $row_builder,
356 array $visible_column_ids,
358 Data\Order $order,
359 ?array $filter_data,
360 ?array $additional_parameters
361 ): \Generator {
362 $records = $this->getRecords($range, $order, $filter_data);
363 foreach ($records as $idx => $record) {
364 $row_id = (int) $record["term_id"];
365
366 $data_row = $row_builder->buildDataRow((string) $row_id, $record);
367 if (!($this->term_perm->checkPermission("write", $row_id)
368 || $this->term_perm->checkPermission("edit_content", $row_id))) {
369 if (!(\ilGlossaryTerm::_lookGlossaryID($row_id) == $this->glossary->getId()
370 || \ilGlossaryTermReferences::isReferenced([$this->glossary->getId()], $row_id))) {
371 $data_row = $data_row->withDisabledAction("edit_definition");
372 $data_row = $data_row->withDisabledAction("edit_term");
373 }
374 }
375 yield $data_row;
376 }
377 }
378
379 public function getTotalRowCount(
380 ?array $filter_data,
381 ?array $additional_parameters
382 ): ?int {
383 return count($this->getRecords());
384 }
385
386 protected function getRecords(?Data\Range $range = null, ?Data\Order $order = null, ?array $filter_data = null): array
387 {
388 $filter_term = "";
389 $filter_def = "";
390 //TODO: filter data when available in UI Data Table
391 if ($filter_data) {
392
393 }
394
395 $data = $this->glossary->getTermList(
396 $filter_term,
397 "",
398 $filter_def,
399 $this->tax_node,
400 true,
401 true,
402 null,
403 false,
404 true
405 );
406 $term_ids = [];
407 foreach ($data as $term) {
408 $term_ids[$term["id"]] = new \ILIAS\AdvancedMetaData\Services\SubObjectID(
409 $this->glossary->getId(),
410 (int) $term["id"],
411 "term"
412 );
413 }
414 $adv_md_data = $this->adv_term_mode->getData(...$term_ids);
415
416 $records = [];
417 $i = 0;
418 foreach ($data as $term) {
419 //TODO: Check if we need all these setParameterByClass calls
420 $term_id = (int) $term["id"];
421 $this->ctrl->setParameterByClass("ilobjglossarygui", "term_id", $term_id);
422 $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", $term_id);
423 $this->ctrl->setParameterByClass("ilglossarydefpagegui", "term_id", $term_id);
424 $records[$i]["term_id"] = $term_id;
425
426 // text
427 $short_str = \ilGlossaryTerm::_lookShortText($term_id);
428
429 try {
431 // #18022
432 $term_obj = new \ilGlossaryTerm($term_id);
433 $term_obj->updateShortText();
434 $short_str = $term_obj->getShortText();
435 }
436
437 $page = new \ilGlossaryDefPage($term_id);
438
439 // replace tex
440 // if a tex end tag is missing a tex end tag
441 $ltexs = strrpos($short_str, "[tex]");
442 $ltexe = strrpos($short_str, "[/tex]");
443 if ($ltexs > $ltexe) {
444 $page->buildDom();
445 $short_str = $page->getFirstParagraphText();
446 $short_str = strip_tags($short_str, "<br>");
447 $ltexe = strpos($short_str, "[/tex]", $ltexs);
448 $short_str = \ilStr::shortenTextExtended($short_str, $ltexe + 6, true);
449 }
450
451 $short_str = \ilPCParagraph::xml2output(
452 $short_str,
453 false,
454 true,
455 !$page->getPageConfig()->getPreventHTMLUnmasking()
456 );
457 } catch (\Exception $e) {
458 $short_str = "Error: Page is missing.";
459 }
460
461 $short_str = $this->ui_ren->render($this->ui_fac->legacy()->latexContent($short_str));
462
463 $records[$i]["definition"] = $short_str;
464
465 $this->ctrl->setParameterByClass("ilobjglossarygui", "term_id", $term_id);
466
467
468 // usage
469 $nr_usage = \ilGlossaryTerm::getNumberOfUsages($term_id);
470 if ($nr_usage > 0 && $this->glossary->getId() == $term["glo_id"]) {
471 $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", $term_id);
472 $records[$i]["usage"] = \ilGlossaryTerm::getNumberOfUsages($term_id);
473 $records[$i]["usage_link"] = $this->ui_fac->link()->standard(
474 $this->lng->txt("glo_link_to_usages"),
475 $this->ctrl->getLinkTargetByClass("ilglossarytermgui", "listUsages")
476 );
477 $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", "");
478 } else {
479 $records[$i]["usage"] = \ilGlossaryTerm::getNumberOfUsages($term_id);
480 }
481
482 // glossary title
483 if ($this->glossary->getVirtualMode() === "coll"
484 || \ilGlossaryTermReferences::hasReferences($this->glossary->getId())
485 ) {
486 $glo_title = \ilObject::_lookupTitle($term["glo_id"]);
487 $records[$i]["glossary"] = $glo_title;
488 }
489
490 // output language
491 $records[$i]["language"] = $this->lng->txt("meta_l_" . $term["language"]);
492
493 // advanced metadata
494 $sub_obj_id = $term_ids[$term_id];
495 $adv_data = $adv_md_data->dataForSubObject($sub_obj_id);
496 foreach ($adv_data as $key => $val) {
497 $records[$i][$key] = $val;
498 }
499
500 foreach ($this->adv_cols_order as $c) {
501 if ($c["id"] == 0) {
502 $records[$i]["term"] = $term["term"];
503 }
504 }
505
506 $i++;
507 }
508
509 if ($order) {
510 $records = $this->orderRecords($records, $order);
511 }
512
513 if ($range) {
514 $records = $this->limitRecords($records, $range);
515 }
516
517 return $records;
518 }
519 };
520
521 return $data_retrieval;
522 }
523}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Definition: UI.php:24
__construct(\ilObjGlossary $glossary, int $tax_node)
ILIAS AdvancedMetaData Services SubObjectModes DataTable SupplierInterface $adv_term_mode
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static hasReferences(int $a_glossary_id)
Check if a glossary uses references.
static isReferenced(array $a_glo_id, int $a_term_id)
Is a term referenced by a set of glossaries.
static _lookGlossaryTerm(int $term_id)
get glossary term
static getNumberOfUsages(int $a_term_id)
static _lookGlossaryID(int $term_id)
get glossary id form term id
static _lookShortTextDirty(int $term_id)
get definition short text dirty
static _lookShortText(int $term_id)
get definition short text
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
static xml2output(string $a_text, bool $a_wysiwyg=false, bool $a_replace_lists=true, bool $unmask=true)
Converts xml from DB to output in edit textarea.
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
$c
Definition: deliver.php:25
exit
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26