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