ILIAS  trunk Revision v11.0_alpha-1838-g59fc79e306b
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.TermListTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data;
24 use ILIAS\UI;
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;
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 
53  public function __construct(\ilObjGlossary $glossary, int $tax_node)
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  {
150  $term_glo_id = \ilGlossaryTerm::_lookGlossaryID($id);
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 (
329  $this->df,
330  $this->lng,
332  ) implements UI\Component\Table\DataRetrieval {
333  use TableRecords;
334 
335  protected \ilCtrl $ctrl;
336 
337  public function __construct(
338  protected \ilObjGlossary $glossary,
339  protected int $tax_node,
340  protected array $adv_cols_order,
341  protected \ILIAS\AdvancedMetaData\Services\SubObjectModes\DataTable\SupplierInterface $adv_term_mode,
342  protected UI\Factory $ui_fac,
343  protected Data\Factory $df,
344  protected \ilLanguage $lng,
345  protected \ilGlossaryTermPermission $term_perm
346  ) {
347  global $DIC;
348 
349  $this->ctrl = $DIC->ctrl();
350  }
351 
352  public function getRows(
353  UI\Component\Table\DataRowBuilder $row_builder,
354  array $visible_column_ids,
355  Data\Range $range,
356  Data\Order $order,
357  ?array $filter_data,
358  ?array $additional_parameters
359  ): \Generator {
360  $records = $this->getRecords($range, $order, $filter_data);
361  foreach ($records as $idx => $record) {
362  $row_id = (int) $record["term_id"];
363 
364  $data_row = $row_builder->buildDataRow((string) $row_id, $record);
365  if (!($this->term_perm->checkPermission("write", $row_id)
366  || $this->term_perm->checkPermission("edit_content", $row_id))) {
367  if (!(\ilGlossaryTerm::_lookGlossaryID($row_id) == $this->glossary->getId()
368  || \ilGlossaryTermReferences::isReferenced([$this->glossary->getId()], $row_id))) {
369  $data_row = $data_row->withDisabledAction("edit_definition");
370  $data_row = $data_row->withDisabledAction("edit_term");
371  }
372  }
373  yield $data_row;
374  }
375  }
376 
377  public function getTotalRowCount(
378  ?array $filter_data,
379  ?array $additional_parameters
380  ): ?int {
381  return count($this->getRecords());
382  }
383 
384  protected function getRecords(?Data\Range $range = null, ?Data\Order $order = null, ?array $filter_data = null): array
385  {
386  $filter_term = "";
387  $filter_def = "";
388  //TODO: filter data when available in UI Data Table
389  if ($filter_data) {
390 
391  }
392 
393  $data = $this->glossary->getTermList(
394  $filter_term,
395  "",
396  $filter_def,
397  $this->tax_node,
398  true,
399  true,
400  null,
401  false,
402  true
403  );
404  $term_ids = [];
405  foreach ($data as $term) {
406  $term_ids[$term["id"]] = new \ILIAS\AdvancedMetaData\Services\SubObjectID(
407  $this->glossary->getId(),
408  (int) $term["id"],
409  "term"
410  );
411  }
412  $adv_md_data = $this->adv_term_mode->getData(...$term_ids);
413 
414  $records = [];
415  $i = 0;
416  foreach ($data as $term) {
417  //TODO: Check if we need all these setParameterByClass calls
418  $term_id = (int) $term["id"];
419  $this->ctrl->setParameterByClass("ilobjglossarygui", "term_id", $term_id);
420  $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", $term_id);
421  $this->ctrl->setParameterByClass("ilglossarydefpagegui", "term_id", $term_id);
422  $records[$i]["term_id"] = $term_id;
423 
424  // text
425  $short_str = \ilGlossaryTerm::_lookShortText($term_id);
426 
427  if (\ilGlossaryTerm::_lookShortTextDirty($term_id)) {
428  // #18022
429  $term_obj = new \ilGlossaryTerm($term_id);
430  $term_obj->updateShortText();
431  $short_str = $term_obj->getShortText();
432  }
433 
434  try {
435  $page = new \ilGlossaryDefPage($term_id);
436 
437  // replace tex
438  // if a tex end tag is missing a tex end tag
439  $ltexs = strrpos($short_str, "[tex]");
440  $ltexe = strrpos($short_str, "[/tex]");
441  if ($ltexs > $ltexe) {
442  $page->buildDom();
443  $short_str = $page->getFirstParagraphText();
444  $short_str = strip_tags($short_str, "<br>");
445  $ltexe = strpos($short_str, "[/tex]", $ltexs);
446  $short_str = \ilStr::shortenTextExtended($short_str, $ltexe + 6, true);
447  }
448 
449  $short_str = \ilMathJax::getInstance()->insertLatexImages($short_str);
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  $records[$i]["definition"] = $short_str;
462 
463  $this->ctrl->setParameterByClass("ilobjglossarygui", "term_id", $term_id);
464 
465 
466  // usage
467  $nr_usage = \ilGlossaryTerm::getNumberOfUsages($term_id);
468  if ($nr_usage > 0 && $this->glossary->getId() == $term["glo_id"]) {
469  $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", $term_id);
470  $records[$i]["usage"] = \ilGlossaryTerm::getNumberOfUsages($term_id);
471  $records[$i]["usage_link"] = $this->ui_fac->link()->standard(
472  $this->lng->txt("glo_link_to_usages"),
473  $this->ctrl->getLinkTargetByClass("ilglossarytermgui", "listUsages")
474  );
475  $this->ctrl->setParameterByClass("ilglossarytermgui", "term_id", "");
476  } else {
477  $records[$i]["usage"] = \ilGlossaryTerm::getNumberOfUsages($term_id);
478  }
479 
480  // glossary title
481  if ($this->glossary->getVirtualMode() === "coll"
482  || \ilGlossaryTermReferences::hasReferences($this->glossary->getId())
483  ) {
484  $glo_title = \ilObject::_lookupTitle($term["glo_id"]);
485  $records[$i]["glossary"] = $glo_title;
486  }
487 
488  // output language
489  $records[$i]["language"] = $this->lng->txt("meta_l_" . $term["language"]);
490 
491  // advanced metadata
492  $sub_obj_id = $term_ids[$term_id];
493  $adv_data = $adv_md_data->dataForSubObject($sub_obj_id);
494  foreach ($adv_data as $key => $val) {
495  $records[$i][$key] = $val;
496  }
497 
498  foreach ($this->adv_cols_order as $c) {
499  if ($c["id"] == 0) {
500  $records[$i]["term"] = $term["term"];
501  }
502  }
503 
504  $i++;
505  }
506 
507  if ($order) {
508  $records = $this->orderRecords($records, $order);
509  }
510 
511  if ($range) {
512  $records = $this->limitRecords($records, $range);
513  }
514 
515  return $records;
516  }
517  };
518 
519  return $data_retrieval;
520  }
521 }
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.
__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:25
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:28
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _lookupTitle(int $obj_id)
static _lookGlossaryTerm(int $term_id)
get glossary term
global $DIC
Definition: shib_login.php:22
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.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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
exit
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS AdvancedMetaData Services SubObjectModes DataTable SupplierInterface $adv_term_mode
static _lookGlossaryID(int $term_id)
get glossary id form term id