ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAdvancedMDRecordGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
30 {
31  public const MODE_UNDEFINED = 0;
32  public const MODE_EDITOR = 1;
33  public const MODE_SEARCH = 2;
34  public const MODE_INFO = 3;
35  public const MODE_APP_PRESENTATION = 8;
36 
37  // glossary
38  public const MODE_REC_SELECTION = 4; // record selection (per object)
39  public const MODE_FILTER = 5; // filter (as used e.g. in tables)
40  public const MODE_TABLE_HEAD = 6; // table header (columns)
41  public const MODE_TABLE_CELLS = 7; // table cells
42 
43  private int $mode = self::MODE_UNDEFINED;
44  private string $obj_type = '';
45  private string $sub_type = '';
46  private int $sub_id = 0;
47  private int $obj_id = 0;
48  private ?int $ref_id = null;
49 
50  // mode specific parameters
52  private ?array $row_data = null;
53  private ?array $adt_search = null;
55  protected array $search_values = [];
56  protected ?array $search_form = null;
57  protected ?array $search_form_values = null;
58 
59  protected array $editor_form = [];
60 
61  protected ?ilInfoScreenGUI $info = null;
62 
63  // $adv_id - $adv_type - $adv_subtype:
64  // Object, that defines the adv md records being used. Default is $this->object, but the
65  // context may set another object (e.g. media pool for media objects)
66 
67  // $adv_id must be a ref id, if $in_repository is true,
68  // otherwise an object id
69  protected ?int $adv_id = null;
70  protected ?string $adv_type = null;
71  protected ?string $adv_subtype = null;
72 
73  // This is false e.g. for portfolios
74  protected bool $in_repository = true;
75 
76  protected ilObjUser $user;
77  protected ilLanguage $lng;
80 
81 
85  protected ?array $record_filter = null;
86 
87 
92  public function __construct(
93  int $a_mode,
94  string $a_obj_type = '',
95  int $a_obj_id = 0,
96  string $a_sub_type = '',
97  int $a_sub_id = 0,
98  bool $in_repository = true
99  ) {
100  global $DIC;
101 
102  $this->user = $DIC->user();
103  $this->lng = $DIC->language();
104  $this->mode = $a_mode;
105  $this->obj_type = $a_obj_type;
106  $this->obj_id = $a_obj_id;
107  $this->sub_type = $a_sub_type;
108  $this->sub_id = $a_sub_id;
109 
110  $this->in_repository = $in_repository;
111  if ($a_obj_id && $this->in_repository) {
112  $refs = ilObject::_getAllReferences($a_obj_id);
113  $this->ref_id = end($refs);
114  }
115 
116  $this->refinery = $DIC->refinery();
117  $this->http = $DIC->http();
118  }
119 
125  public function setAdvMdRecordObject(int $a_adv_id, string $a_adv_type, string $a_adv_subtype = "-"): void
126  {
127  $this->adv_id = $a_adv_id;
128  $this->adv_type = $a_adv_type;
129  $this->adv_subtype = $a_adv_subtype;
130  }
131 
136  public function getAdvMdRecordObject(): array
137  {
138  if ($this->adv_type == null) {
139  if ($this->in_repository) {
141  } else {
143  }
144  }
146  }
147 
152  public function setRefId(int $a_ref_id): void
153  {
154  $this->ref_id = $a_ref_id;
155  }
156 
157  public function setPropertyForm(ilPropertyFormGUI $form): void
158  {
159  $this->form = $form;
160  }
161 
165  public function setSearchValues(array $a_values): void
166  {
167  $this->search_values = $a_values;
168  }
169 
174  public function setInfoObject(ilInfoScreenGUI $info): void
175  {
176  $this->info = $info;
177  }
178 
183  public function setRecordFilter(?array $filter = null): void
184  {
185  $this->record_filter = $filter;
186  }
187 
191  protected function checkFilter($record_id): bool
192  {
193  return !(is_array($this->record_filter) && !in_array($record_id, $this->record_filter));
194  }
195 
206  public function parse()
207  {
208  switch ($this->mode) {
209  case self::MODE_EDITOR:
210  return $this->parseEditor();
211 
212  case self::MODE_SEARCH:
213  return $this->parseSearch();
214 
215  case self::MODE_INFO:
216  return $this->parseInfoPage();
217 
218  case self::MODE_APP_PRESENTATION:
219  return $this->parseAppointmentPresentationa();
220 
221  case self::MODE_REC_SELECTION:
222  return $this->parseRecordSelection();
223 
224  case self::MODE_FILTER:
225  return $this->parseFilter();
226 
227  case self::MODE_TABLE_HEAD:
228  return $this->parseTableHead();
229 
230  case self::MODE_TABLE_CELLS:
231  return $this->parseTableCells();
232 
233  default:
234  throw new InvalidArgumentException('Missing or wrong ADV mode given: ' . $this->mode);
235  }
236  }
237 
241  protected function parseEditor(): void
242  {
243  $this->editor_form = array();
244  foreach ($this->getActiveRecords() as $record_obj) {
245  $record_id = $record_obj->getRecordId();
246 
247  $values = new ilAdvancedMDValues($record_id, $this->obj_id, $this->sub_type, $this->sub_id);
248  $values->read();
249  $defs = $values->getDefinitions();
250 
251  // empty record?
252  if (!sizeof($defs)) {
253  continue;
254  }
255 
256  if (!$this->checkFilter($record_id)) {
257  continue;
258  }
259 
260  $translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($record_obj->getRecordId());
261  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record_obj->getRecordId());
262 
263  $adt_group_form = ilADTFactory::getInstance()->getFormBridgeForInstance($values->getADTGroup());
264  $adt_group_form->setForm($this->form);
265 
266  $adt_group_form->setTitle($translations->getTitleForLanguage($this->user->getLanguage()));
267  $adt_group_form->setInfo($translations->getDescriptionForLanguage($this->user->getLanguage()));
268 
269  foreach ($defs as $def) {
270  $element = $adt_group_form->getElement((string) $def->getFieldId());
271  $element->setTitle($field_translations->getTitleForLanguage(
272  $def->getFieldId(),
273  $this->user->getLanguage()
274  ));
275  $element->setInfo($field_translations->getDescriptionForLanguage(
276  $def->getFieldId(),
277  $this->user->getLanguage()
278  ));
279 
280  // definition may customize ADT form element
281  $def->prepareElementForEditor($element);
282 
283  if ($values->isDisabled((string) $def->getFieldId())) {
284  $element->setDisabled(true);
285  }
286  }
287 
288  $adt_group_form->addToForm();
289 
290  $this->editor_form[$record_id] = array("values" => $values, "form" => $adt_group_form);
291  }
292  }
293 
297  public function importEditFormPostValues(): bool
298  {
299  $valid = true;
300  foreach ($this->editor_form as $item) {
301  $item["form"]->importFromPost();
302  if (!$item["form"]->validate()) {
303  $valid = false;
304  }
305  }
306 
307  return $valid;
308  }
309 
314  public function writeEditForm(?int $a_obj_id = null, ?int $a_sub_id = null): bool
315  {
316  if (!count($this->editor_form)) {
317  return false;
318  }
319 
320  // switch ids?
321  if ($a_obj_id) {
322  $this->obj_id = $a_obj_id;
323  }
324  if ($a_sub_id) {
325  $this->sub_id = $a_sub_id;
326  }
327 
328  foreach ($this->editor_form as $item) {
329  if ($a_obj_id || $a_sub_id) {
330  // switch active record to updated primary keys, e.g. after creation
331  $item["values"]->setActiveRecordPrimary($this->obj_id, $this->sub_type, $this->sub_id);
332  }
333 
334  $item["values"]->write();
335  }
336  return true;
337  }
338 
342  private function parseSearch(): void
343  {
344  // this is NOT used for the global search, see ilLuceneAdvancedSearchFields::getFormElement()
345  // (so searchable flag is NOT relevant)
346  //
347  // current usage: wiki page element "[amd] page list"
348 
349  $this->lng->loadLanguageModule('search');
350 
351  $this->search_form = array();
352  foreach ($this->getActiveRecords() as $record) {
353  $fields = ilAdvancedMDFieldDefinition::getInstancesByRecordId($record->getRecordId(), true);
354 
355  // empty record?
356  if (!sizeof($fields)) {
357  continue;
358  }
359 
360  $record_translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($record->getRecordId());
361  $section = new ilFormSectionHeaderGUI();
362  $section->setTitle($record_translations->getTitleForLanguage($this->user->getLanguage()));
363  $section->setInfo($record_translations->getDescriptionForLanguage($this->user->getLanguage()));
364  $this->form->addItem($section);
365 
366  foreach ($fields as $field) {
367  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record->getRecordId());
368 
369  $field_form = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance(
370  $field->getADTDefinition(),
371  true,
372  false
373  );
374  $field_form->setForm($this->form);
375  $field_form->setElementId("advmd[" . $field->getFieldId() . "]");
376  $field_form->setTitle($field_translations->getTitleForLanguage(
377  $field->getFieldId(),
378  $this->user->getLanguage()
379  ));
380 
381  if (is_array($this->search_form_values) &&
382  isset($this->search_form_values[$field->getFieldId()])) {
383  $field->setSearchValueSerialized($field_form, $this->search_form_values[$field->getFieldId()]);
384  }
385 
386  $field->prepareElementForSearch($field_form);
387 
388  $field_form->addToForm();
389 
390  $this->search_form[$field->getFieldId()] = array("def" => $field, "value" => $field_form);
391  }
392  }
393  }
394 
398  public function importSearchForm(): ?array
399  {
400  if (!is_array($this->search_form)) {
401  return null;
402  }
403 
404  $valid = true;
405  $res = array();
406  foreach ($this->search_form as $field_id => $item) {
407  $item["value"]->importFromPost();
408  if (!$item["value"]->validate()) {
409  $valid = false;
410  }
411  $value = $item["def"]->getSearchValueSerialized($item["value"]);
412  if ($value !== null) {
413  $res[$field_id] = $value;
414  }
415  }
416 
417  if ($valid) {
418  return $res;
419  }
420  return null;
421  }
422 
423  public function setSearchFormValues(array $a_values): void
424  {
425  $this->search_form_values = $a_values;
426  }
427 
432  private function parseInfoPage(): void
433  {
435  $this->obj_id,
436  $this->obj_type,
437  $this->sub_type,
438  $this->sub_id
439  ) as $record_id => $a_values) {
440  // this correctly binds group and definitions
441  $a_values->read();
442 
443  $record_translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($record_id);
444  $this->info->addSection($record_translations->getTitleForLanguage($this->user->getLanguage()));
445 
446  $defs = $a_values->getDefinitions();
447  foreach ($a_values->getADTGroup()->getElements() as $element_id => $element) {
448  if (!$element->isNull()) {
449  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record_id);
450  $title = $field_translations->getTitleForLanguage($element_id, $this->user->getLanguage());
451 
452  $presentation_bridge = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
453 
454  if ($element instanceof ilADTLocation) {
455  $presentation_bridge->setSize('500px', '300px');
456  }
457 
458  $this->info->addProperty(
459  $title,
460  $presentation_bridge->getHTML()
461  );
462  }
463  }
464  }
465  }
466 
470  private function parseAppointmentPresentationa(): array
471  {
473 
474  $definitions = ilAdvancedMDFieldDefinition::getInstancesByObjType($this->obj_type);
475  $definitions = $sub->sortDefinitions($definitions);
476 
477  $positions = array();
478  foreach ($definitions as $position => $value) {
479  $positions[$value->getFieldId()] = $position;
480  }
481 
482  $array_elements = array();
484  $this->obj_id,
485  $this->obj_type,
486  $this->sub_type,
487  $this->sub_id
488  ) as $record_id => $a_values) {
489  // this correctly binds group and definitions
490  $a_values->read();
491 
492  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record_id);
493 
494  $defs = $a_values->getDefinitions();
495  foreach ($a_values->getADTGroup()->getElements() as $element_id => $element) {
496  if (!$element->isNull()) {
497  $presentation_bridge = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
498  #21615
499  if (get_class($element) == 'ilADTLocation') {
505  $presentation_value = $presentation_bridge->getSortable();
506  } elseif (get_class($element) == 'ilADTExternalLink' || get_class($element) == 'ilADTInternalLink') {
507  #22638
508  $presentation_value = $presentation_bridge->getHTML();
509  } else {
510  $presentation_value = strip_tags($presentation_bridge->getHTML());
511  }
512  $array_elements[$positions[$element_id]] =
513  [
514  "title" => $field_translations->getTitleForLanguage(
515  $element_id,
516  $this->user->getLanguage()
517  ),
518  "value" => $presentation_value
519  ];
520  }
521  }
522  }
523 
524  // already sorted by record positions
525  return $array_elements;
526  }
527 
531  private function handleECSDefinitions($a_definition): bool
532  {
533  if (ilECSServerSettings::getInstance()->activeServerExists() or
534  ($this->obj_type != 'crs' and $this->obj_type != 'rcrs')
535  ) {
536  return false;
537  }
538  return false;
539  }
540 
545  public function parseRecordSelection(string $a_sec_head = ""): void
546  {
547  $first = true;
549  $this->obj_type,
550  $this->sub_type
551  ) as $record_obj) {
552  $selected = ilAdvancedMDRecord::getObjRecSelection($this->obj_id, $this->sub_type);
553  if ($first) {
554  $first = false;
555  $section = new ilFormSectionHeaderGUI();
556  $sec_tit = ($a_sec_head == "")
557  ? $this->lng->txt("meta_adv_records")
558  : $a_sec_head;
559  $section->setTitle($sec_tit);
560  $this->form->addItem($section);
561  }
562 
563  // checkbox for each active record
564  $cb = new ilCheckboxInputGUI($record_obj->getTitle(), "amet_use_rec[]");
565  $cb->setInfo($record_obj->getDescription());
566  $cb->setValue((string) $record_obj->getRecordId());
567  if (in_array((string) $record_obj->getRecordId(), $selected)) {
568  $cb->setChecked(true);
569  }
570  $this->form->addItem($cb);
571  }
572  }
573 
577  public function saveSelection(): void
578  {
579  $post_amet_use_rec = [];
580  if ($this->http->wrapper()->post()->has('amet_use_rec')) {
581  $post_amet_use_rec = $this->http->wrapper()->post()->retrieve(
582  'amet_use_rec',
583  $this->refinery->kindlyTo()->dictOf(
584  $this->refinery->kindlyTo()->int()
585  )
586  );
587  }
588  ilAdvancedMDRecord::saveObjRecSelection($this->obj_id, $this->sub_type, $post_amet_use_rec);
589  }
590 
594  public function setTableGUI(ilTable2GUI $a_val): void
595  {
596  $this->table_gui = $a_val;
597  }
598 
599  public function getTableGUI(): ?ilTable2GUI
600  {
601  return $this->table_gui;
602  }
603 
608  public function setRowData(array $a_val): void
609  {
610  $this->row_data = $a_val;
611  }
612 
617  public function getRowData(): ?array
618  {
619  return $this->row_data;
620  }
621 
625  protected function getActiveRecords(): array
626  {
627  list($adv_id, $adv_type, $adv_subtype) = $this->getAdvMdRecordObject();
628  return ilAdvancedMDRecord::_getSelectedRecordsByObject($adv_type, $adv_id, $adv_subtype, $this->in_repository);
629  }
630 
634  private function parseFilter(): void
635  {
636  $this->adt_search = array();
637 
638  foreach ($this->getActiveRecords() as $record_obj) {
639  $record_id = $record_obj->getRecordId();
640 
641  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record_id);
642 
644  foreach ($defs as $def) {
645  // some input GUIs do NOT support filter rendering yet
646  if (!$def->isFilterSupported()) {
647  continue;
648  }
649 
650  $this->adt_search[$def->getFieldId()] = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance(
651  $def->getADTDefinition(),
652  true,
653  false
654  );
655  $this->adt_search[$def->getFieldId()]->setTableGUI($this->table_gui);
656  $this->adt_search[$def->getFieldId()]->setTitle(
657  $field_translations->getTitleForLanguage($def->getFieldId(), $this->user->getLanguage())
658  );
659  $this->adt_search[$def->getFieldId()]->setElementId('md_' . $def->getFieldId());
660 
661  $this->adt_search[$def->getFieldId()]->loadFilter();
662  $this->adt_search[$def->getFieldId()]->addToFilterForm();
663  }
664  }
665  }
666 
670  public function importFilter(): void
671  {
672  if (!is_array($this->adt_search)) {
673  return;
674  }
675 
676  foreach ($this->adt_search as $element) {
677  $element->importFromPost();
678  }
679  }
680 
684  public function getFilterElements(bool $a_only_non_empty = true): array
685  {
686  if (!is_array($this->adt_search)) {
687  return [];
688  }
689 
690  $res = [];
691  foreach ($this->adt_search as $def_id => $element) {
692  if (!$element->isNull() ||
693  !$a_only_non_empty) {
694  $res[$def_id] = $element;
695  }
696  }
697  return $res;
698  }
699 
703  private function parseTableHead(): void
704  {
705  foreach ($this->getActiveRecords() as $record_obj) {
706  $record_id = $record_obj->getRecordId();
707 
708  $field_translations = ilAdvancedMDFieldTranslations::getInstanceByRecordId($record_id);
709 
711  foreach ($defs as $def) {
712  if ($this->handleECSDefinitions($def)) {
713  continue;
714  }
715 
716  $this->table_gui->addColumn(
717  $field_translations->getTitleForLanguage($def->getFieldId(), $this->user->getLanguage()),
718  'md_' . $def->getFieldId()
719  );
720  }
721  }
722  }
723 
727  private function parseTableCells(): string
728  {
729  $data = $this->getRowData();
730  $html = "";
731 
732  foreach ($this->getActiveRecords() as $record_obj) {
733  $record_id = $record_obj->getRecordId();
734 
736  foreach ($defs as $def) {
737  if ($this->handleECSDefinitions($def)) {
738  continue;
739  }
740 
741  $res = '';
742  $res_raw = $data['md_' . $def->getFieldId()] ?? null;
743  $res_presentation = $data['md_' . $def->getFieldId() . '_presentation'] ?? null;
744  if ($res_raw) {
745  $res = $res_raw;
746  }
747  if (
748  $res_presentation instanceof ilADTPresentationBridge &&
749  !($res_presentation instanceof ilADTLocationPresentationBridge)
750  ) {
751  $res = $res_presentation->getHTML();
752  }
753 
754  $html .= "<td class='std'>" . $res . "</td>";
755  }
756  }
757  return $html;
758  }
759 }
static getInstancesForObjectId(int $a_obj_id, ?string $a_obj_type=null, string $a_sub_type="-", int $a_sub_id=0)
importSearchForm()
Load edit form values from post.
$res
Definition: ltiservices.php:66
setPropertyForm(ilPropertyFormGUI $form)
parseTableCells()
Parse table cells.
setRecordFilter(?array $filter=null)
Set advanced record filter.
importEditFormPostValues()
Load edit form values from post.
static _getActivatedRecordsByObjectType(string $a_obj_type, string $a_sub_type="", bool $a_only_optional=false)
Get activated records by object type.
static getInstance()
Get singleton instance.
static _getAllReferences(int $id)
get all reference ids for object ID
$valid
handleECSDefinitions($a_definition)
handle ecs definitions
__construct(int $a_mode, string $a_obj_type='', int $a_obj_id=0, string $a_sub_type='', int $a_sub_id=0, bool $in_repository=true)
Constructor.
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
parseTableHead()
Parse property for table head.
parseAppointmentPresentationa()
Presentation for calendar agenda list.
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
checkFilter($record_id)
Check filter.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
info()
description: > Example for rendering a info message box.
Definition: info.php:34
static getObjRecSelection(int $a_obj_id, string $a_sub_type="")
Get repository object record selection.
static http()
Fetches the global http state from ILIAS.
setAdvMdRecordObject(int $a_adv_id, string $a_adv_type, string $a_adv_subtype="-")
Set object, that defines the adv md records being used.
static getInstancesByObjType($a_obj_type, $a_active_only=true)
setRefId(int $a_ref_id)
Set ref_id for context.
global $DIC
Definition: shib_login.php:22
setRowData(array $a_val)
Set row data.
parseRecordSelection(string $a_sec_head="")
Parse property form in editor mode.
parseFilter()
Parse property for filter (table)
static saveObjRecSelection(int $a_obj_id, string $a_sub_type="", ?array $a_records=null, bool $a_delete_before=true)
Save repository object record selection.
setSearchValues(array $a_values)
Set values for search form.
getAdvMdRecordObject()
Get adv md record parameters.
form( $class_path, string $cmd, string $submit_caption="")
setInfoObject(ilInfoScreenGUI $info)
get info sections
setTableGUI(ilTable2GUI $a_val)
Set table for self::MODE_TABLE_FILTER.
ADT presentation bridge base class.
getFilterElements(bool $a_only_non_empty=true)
Get SQL conditions for current filter value(s)
parseEditor()
Parse property form in editor mode.
parseInfoPage()
Presentation for info page.
saveSelection()
Save selection per object.
writeEditForm(?int $a_obj_id=null, ?int $a_sub_id=null)
Write edit form values to db.
importFilter()
Import filter (post) values.
static _getInstanceByObjectType(string $a_type)