ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilDataCollectionDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
27  protected array $record_field_ids_2_storage = [];
44  protected array $caches
45  = [
46  'dcl' => [],
47  'il_dcl_table' => [],
48  'il_dcl_field' => [],
49  'il_dcl_field_prop' => [],
50  'il_dcl_sel_opts' => [],
51  'il_dcl_record' => [],
52  'il_dcl_record_field' => [],
53  'il_dcl_stloc1_value' => [],
54  'il_dcl_stloc2_value' => [],
55  'il_dcl_stloc3_value' => [],
56  'il_dcl_stloc1_default' => [],
57  'il_dcl_stloc2_default' => [],
58  'il_dcl_stloc3_default' => [],
59  'il_dcl_tfield_set' => [],
60  'il_dcl_tableview' => [],
61  'il_dcl_tview_set' => [],
62  ];
63 
65 
69  protected array $import_record_field_cache = [];
70  protected ilObjUser $user;
71  protected \ILIAS\Refinery\Factory $refinery;
72  protected array $import_temp_refs = [];
73  protected array $import_temp_refs_props = [];
74  protected array $import_temp_new_mob_ids = [];
75 
76  public function __construct()
77  {
78  global $DIC;
80  $this->db = $DIC->database();
81  $this->user = $DIC->user();
82  $this->refinery = $DIC->refinery();
83  }
84 
85  public function getSupportedVersions(): array
86  {
87  return ['4.5.0', '8.13'];
88  }
89 
94  public function getCache(string $a_entity): array
95  {
96  if (!in_array($a_entity, array_keys($this->caches))) {
97  throw new ilException("Entity '$a_entity' does not exist in Cache");
98  }
99 
100  return $this->caches[$a_entity];
101  }
102 
103  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
104  {
105  return 'https://www.ilias.de/xml/Modules/DataCollection/' . $a_entity;
106  }
107 
108  public function importRecord(
109  string $a_entity,
110  array $a_types,
111  array $a_rec,
112  ilImportMapping $a_mapping,
113  string $a_schema_version
114  ): void {
115  foreach ($a_rec as $key => &$value) {
116  $array = json_decode($value, true);
117  if (is_array($array)) {
118  $value = json_encode($this->escapeArray($array));
119  } else {
120  $value = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($value);
121  }
122  }
123  switch ($a_entity) {
124  case 'dcl':
125  if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['id'])) {
126  $new_obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
127  } else {
128  $new_obj = new ilObjDataCollection();
129  $new_obj->create(true);
130  }
131  $new_obj->setTitle($a_rec['title']);
132  $new_obj->setDescription($a_rec['description']);
133  $new_obj->setApproval($a_rec['approval'] === '1');
134  $new_obj->setPublicNotes($a_rec['public_notes'] === '1');
135  $new_obj->setNotification($a_rec['notification'] === '1');
136  $new_obj->setRating($a_rec['rating'] === '1');
137  $new_obj->update();
138  $this->import_dc_object = $new_obj;
139  $a_mapping->addMapping('components/ILIAS/DataCollection', 'dcl', $a_rec['id'], (string) $new_obj->getId());
140  $a_mapping->addMapping(
141  'components/ILIAS/MetaData',
142  'md',
143  $a_rec['id'] . ':0:dcl',
144  $new_obj->getId() . ':0:dcl'
145  );
146  break;
147  case 'il_dcl_table':
148  $table = new ilDclTable();
149  $table->setTitle($a_rec['title']);
150  $table->setObjId($this->import_dc_object->getId());
151  $table->setDescription($a_rec['description']);
152  $table->setAddPerm($a_rec['add_perm'] === '1');
153  $table->setEditPerm($a_rec['edit_perm'] === '1');
154  $table->setDeletePerm($a_rec['delete_perm'] === '1');
155  $table->setEditByOwner($a_rec['edit_by_owner'] === '1');
156  $table->setLimited($a_rec['limited'] === '1');
157  $table->setLimitStart($a_rec['limit_start']);
158  $table->setLimitEnd($a_rec['limit_end']);
159  $table->setIsVisible($a_rec['is_visible'] === '1');
160  $table->setExportEnabled($a_rec['export_enabled'] === '1');
161  $table->setImportEnabled($a_rec['import_enabled'] === '1');
162  $table->setDefaultSortField($a_rec['default_sort_field_id']);
163  $table->setDefaultSortFieldOrder($a_rec['default_sort_field_order']);
164  $table->setPublicCommentsEnabled($a_rec['public_comments'] === '1');
165  $table->setViewOwnRecordsPerm($a_rec['view_own_records_perm'] === '1');
166  $table->setDeleteByOwner($a_rec['delete_by_owner'] === '1');
167  $table->setSaveConfirmation($a_rec['save_confirmation'] === '1');
168  $table->setOrder((int) $a_rec['table_order']);
169  $table->doCreate(false, false); // false => Do not create views! They are imported later
170  $a_mapping->addMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['id'], (string) $table->getId());
171  break;
172  case 'il_dcl_tableview':
173  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
174  if ($new_table_id) {
175  $tableview = new ilDclTableView();
176  $tableview->setTitle($a_rec['title']);
177  $tableview->setTableId((int) $new_table_id);
178  $tableview->setDescription($a_rec['description']);
179  $tableview->setTableviewOrder((int) $a_rec['tableview_order']);
180  if (!is_array($a_rec['roles'])) {
181  $a_rec['roles'] = json_decode($a_rec['roles']);
182  }
183  $tableview->setRoles($a_rec['roles']);
184  $tableview->create(false); //do not create default setting as they are imported too
185 
186  $a_mapping->addMapping(
187  'components/ILIAS/DataCollection',
188  'il_dcl_tableview',
189  $a_rec['id'],
190  (string) $tableview->getId()
191  );
192  $a_mapping->addMapping('components/ILIAS/COPage', 'pg', 'dclf:' . $a_rec['id'], 'dclf:' . $tableview->getId());
193  }
194  break;
195  case 'il_dcl_field':
196  $new_table_id = (int) $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
197  if ($new_table_id > 0) {
198  $datatype_id = (int) $a_rec['datatype_id'];
199  $datatype = $a_rec['datatype_title'] ?? null;
200  $datatypes = ilDclDatatype::getAllDatatype();
201  if ($datatype !== null && ilDclFieldTypePlugin::isPluginDatatype($datatype)) {
202  $datatype_id = null;
203  foreach ($datatypes as $dt) {
204  if ($dt->getTitle() === $datatype) {
205  $datatype_id = $dt->getId();
206  }
207  }
208  }
209  if (in_array($datatype_id, array_keys($datatypes))) {
210  $field = new ilDclBaseFieldModel();
211  $field->setTableId($new_table_id);
212  $field->setDatatypeId($datatype_id);
213  $field->setTitle($a_rec['title']);
214  $field->setDescription($a_rec['description']);
215  $field->doCreate();
216  $a_mapping->addMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['id'], $field->getId());
217  }
218  }
219  break;
220  case 'il_dcl_tfield_set':
221  $new_table_id = (int) $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
222  $new_field_id = is_numeric($a_rec['field']) ? $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']) : $a_rec['field'];
223  if ($new_table_id > 0 && (is_string($new_field_id) || $new_field_id > 0)) {
225  $new_table_id,
226  $new_field_id
227  );
228  $setting->setFieldOrder((int) $a_rec['field_order']);
229  $setting->setExportable($a_rec['exportable'] === '1');
230  $setting->store();
231  }
232  break;
233  case 'il_dcl_tview_set':
234  $new_tableview_id = $a_mapping->getMapping(
235  'components/ILIAS/DataCollection',
236  'il_dcl_tableview',
237  $a_rec['tableview_id']
238  );
239  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']);
240  if ($new_tableview_id) {
241  $setting = new ilDclTableViewFieldSetting();
242  $setting->setTableviewId((int) $new_tableview_id);
243  $setting->setVisible($a_rec['visible'] === '1');
244  $setting->setField($new_field_id ?: $a_rec['field']);
245  $setting->setInFilter($a_rec['in_filter'] === '1');
246  $setting->setFilterValue($a_rec['filter_value']);
247  $setting->setFilterChangeable($a_rec['filter_changeable'] === '1');
248  $setting->setRequiredCreate($a_rec['required_create'] === '1');
249  $setting->setLockedCreate($a_rec['locked_create'] === '1');
250  $setting->setVisibleCreate($a_rec['visible_create'] === '1');
251  $setting->setVisibleEdit($a_rec['visible_edit'] === '1');
252  $setting->setRequiredEdit($a_rec['required_edit'] === '1');
253  $setting->setLockedEdit($a_rec['locked_edit'] === '1');
254  $setting->setDefaultValue($a_rec['default_value']);
255  $setting->create();
256  $a_mapping->addMapping(
257  'components/ILIAS/DataCollection',
258  'il_dcl_tview_set',
259  $a_rec['id'],
260  (string) $setting->getId()
261  );
262  }
263  break;
264  case 'il_dcl_record':
265  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
266  if ($new_table_id) {
267  $record = new ilDclBaseRecordModel();
268  $record->setTableId((int) $new_table_id);
269  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
270  $record->setCreateDate($datetime);
271  $record->setLastUpdate($datetime);
272  $record->setOwner($this->user->getId());
273  $record->setLastEditBy($this->user->getId());
274  $record->doCreate();
275  $a_mapping->addMapping(
276  'components/ILIAS/DataCollection',
277  'il_dcl_record',
278  $a_rec['id'],
279  (string) $record->getId()
280  );
281  }
282  break;
283  case 'il_dcl_view':
284  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
285  if ($new_table_id) {
286  //if import contains il_dcl_view, it must origin from an earlier ILIAS Version and therefore contains no tableviews
287  //->create standard view
288  $tableview = ilDclTableView::createOrGetStandardView((int) $new_table_id);
289  if ($a_rec['type'] == 0 && $a_rec['formtype'] == 0) { //set page_object to tableview
290  // This mapping is needed for the import handled by Services/COPage
291  $a_mapping->addMapping(
292  'components/ILIAS/COPage',
293  'pg',
294  'dclf:' . $a_rec['id'],
295  'dclf:' . $tableview->getId()
296  );
297  $a_mapping->addMapping(
298  'components/ILIAS/DataCollection',
299  'il_dcl_view',
300  $a_rec['id'],
301  (string) $tableview->getId()
302  );
303  } else {
304  $a_mapping->addMapping(
305  'components/ILIAS/DataCollection',
306  'il_dcl_view',
307  $a_rec['id'],
308  json_encode(['type' => $a_rec['type'],
309  'table_id' => $new_table_id,
310  'tableview_id' => $tableview->getId()
311  ])
312  );
313  }
314  }
315  break;
316  case 'il_dcl_viewdefinition':
317  $map = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_view', $a_rec['view_id']);
318  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']);
319  $field = ($new_field_id) ?: $a_rec['field'];
320  switch ($map['type']) {
321  case 1: //visible
322  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
323  $viewfield_setting->setVisible($a_rec['is_set']);
324  $viewfield_setting->store();
325  break;
326  case 3: //in_filter
327  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
328  $viewfield_setting->setInFilter($a_rec['is_set']);
329  $viewfield_setting->store();
330  break;
331  case 4: //exportable
332  $tablefield_setting = ilDclTableFieldSetting::getInstance($map['table_id'], $field);
333  $tablefield_setting->setExportable($a_rec['is_set']);
334  $tablefield_setting->setFieldOrder($a_rec['field_order']);
335  $tablefield_setting->store();
336  break;
337  }
338  break;
339  case 'il_dcl_sel_opts':
340  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
341  if ($new_field_id) {
342  $opt = new ilDclSelectionOption();
343  $opt->setFieldId((int) $new_field_id);
344  $opt->setOptId((int) $a_rec['opt_id']);
345  $opt->setSorting((int) $a_rec['sorting']);
346  $opt->setValue($a_rec['value']);
347  $opt->store();
348  }
349  break;
350  case 'il_dcl_field_prop':
351  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
352  if ($new_field_id) {
353  $prop = new ilDclFieldProperty();
354  $prop->setFieldId((int) $new_field_id);
355 
356  // OLD IMPORT! Backwards-compatibility
357  $name = $a_rec['name'];
358  if (!isset($name) && isset($a_rec['datatype_prop_id'])) {
359  $properties = [
360  1 => 'length',
361  2 => 'regex',
362  3 => 'table_id',
363  4 => 'url',
364  5 => 'reference_link',
365  6 => 'width',
366  7 => 'height',
367  8 => 'learning_progress',
368  9 => 'ILIAS_reference_link',
369  10 => 'multiple_selection',
370  11 => 'expression',
371  12 => 'display_action_menu',
372  13 => 'link_detail_page',
373  14 => 'link_detail_page',
374  ];
375 
376  $name = $properties[$a_rec['datatype_prop_id']];
377  }
378 
379  $prop->setName($name);
380  $prop->setValue($a_rec['value']);
381  $prop->save();
382  $a_mapping->addMapping(
383  'components/ILIAS/DataCollection',
384  'il_dcl_field_prop',
385  $a_rec['id'],
386  (string) $prop->getId()
387  );
388  $this->import_temp_refs_props[$prop->getId()] = $a_rec['value'];
389  }
390  break;
391  case 'il_dcl_record_field':
392  $record_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $a_rec['record_id']);
393  $field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
394  if ($record_id && $field_id) {
395  $record = ilDclCache::getRecordCache((int) $record_id);
396  $field = ilDclCache::getFieldCache((int) $field_id);
397  $record_field = new ilDclBaseRecordFieldModel($record, $field);
398  $a_mapping->addMapping(
399  'components/ILIAS/DataCollection',
400  'il_dcl_record_field',
401  $a_rec['id'],
402  (string) $record_field->getId()
403  );
404  $this->import_record_field_cache[$record_field->getId()] = $record_field;
405  }
406  break;
407  case 'il_dcl_stloc1_value':
408  case 'il_dcl_stloc2_value':
409  case 'il_dcl_stloc3_value':
410  $new_record_field_id = $a_mapping->getMapping(
411  'components/ILIAS/DataCollection',
412  'il_dcl_record_field',
413  $a_rec['record_field_id']
414  );
415  if ($new_record_field_id) {
417  $record_field = $this->import_record_field_cache[$new_record_field_id];
418  if (is_object($record_field)) {
419  // Need to rewrite internal references and lookup new objects if MOB or File
420  // For some fieldtypes it's better to reset the value, e.g. ILIAS_REF
421  switch ($record_field->getField()->getDatatypeId()) {
423  // Check if we got a mapping from old object
424  $new_mob_id = $a_mapping->getMapping('components/ILIAS/MediaObjects', 'mob', $a_rec['value']);
425  $value = ($new_mob_id) ? (int) $new_mob_id : null;
426  $this->import_temp_new_mob_ids[] = $new_mob_id;
427  break;
429  $new_file_id = $a_mapping->getMapping('components/ILIAS/File', 'file', $a_rec['value']);
430  $value = ($new_file_id) ? (int) $new_file_id : null;
431  break;
434  $value = $a_rec['value'];
435  $decode = json_decode($a_rec['value']);
436  if (is_array($decode)) {
437  foreach ($decode as $id) {
438  $this->import_temp_refs[$new_record_field_id][] = $id;
439  }
440  } else {
441  $this->import_temp_refs[$new_record_field_id] = $value;
442  }
443  break;
445  $value = null;
446  break;
449  $value = $a_rec['value'];
450  if ($value == '0000-00-00 00:00:00') {
451  $value = null;
452  }
453  break;
455  if (version_compare($a_schema_version, "8.13") < 0) {
456  $a_rec['value'] = str_replace('&lt;br /&gt;', '', $a_rec['value']);
457  }
458  // no break
459  default:
460  $value = $a_rec['value'];
461  if ($a_entity == 'il_dcl_stloc3_value' && empty($value)) {
462  $value = null;
463  }
464  }
465  $record_field->setValue($value, true);
466  $record_field->doUpdate();
467  }
468  }
469  break;
470  case 'il_dcl_stloc1_default':
471  case 'il_dcl_stloc2_default':
472  case 'il_dcl_stloc3_default':
473 
474  $tview_set_id = $a_mapping->getMapping(
475  'components/ILIAS/DataCollection',
476  'il_dcl_tview_set',
477  $a_rec['tview_set_id']
478  );
479 
480  if ($tview_set_id) {
481  $value = $a_rec['value'];
482  if ($value) {
483  $stloc_default = (new ilDclDefaultValueFactory())->createByTableName($a_entity);
485  $value = (int) $value;
486  }
487  $stloc_default->setValue($value);
488  $stloc_default->setTviewSetId((int) $tview_set_id);
489  $stloc_default->create();
490  }
491  }
492  break;
493  }
494  }
495 
496  protected function escapeArray(array $array): array
497  {
498  $new = [];
499  foreach ($array as $key => $value) {
500  $newkey = $key;
501  if (is_string($key)) {
502  $newkey = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($key);
503  }
504  $newvalue = $value;
505  if (is_string($value)) {
506  $newvalue = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($value);
507  }
508  if (is_array($value)) {
509  $newvalue = $this->escapeArray($value);
510  }
511  $new[$newkey] = $newvalue;
512  }
513  return $new;
514  }
515 
520  public function beforeFinishImport(ilImportMapping $a_mapping): void
521  {
522  foreach ($this->import_dc_object->getTables() as $table) {
523  if (is_numeric($table->getDefaultSortField())) {
524  $table->setDefaultSortField(
525  $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $table->getDefaultSortField())
526  );
527  $table->doUpdate();
528  }
529  }
530  foreach ($this->import_temp_new_mob_ids as $new_mob_id) {
531  if ($new_mob_id) {
532  ilObjMediaObject::_saveUsage((int) $new_mob_id, "dcl:html", $a_mapping->getTargetId());
533  }
534  }
535  foreach ($this->import_temp_refs as $record_field_id => $old_record_id) {
536  if (is_array($old_record_id)) {
537  $new_record_id = [];
538  foreach ($old_record_id as $id) {
539  $new_record_id[] = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $id);
540  }
541  $value = $new_record_id;
542  } else {
543  $value = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $old_record_id);
544  }
546  $record_field = $this->import_record_field_cache[$record_field_id];
547  $record_field->setValue($value, true);
548  $record_field->doUpdate();
549  }
550  foreach ($this->import_temp_refs_props as $field_prop_id => $prop_value) {
551  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $prop_value);
552  $value = ($new_field_id) ? (int) $new_field_id : $prop_value;
553 
554  $field_prop = new ilDclFieldProperty($field_prop_id);
555  $field_prop->setValue($value);
556  $field_prop->update();
557  }
558  }
559 
563  protected function getTypes(string $a_entity, string $a_version): array
564  {
565  switch ($a_entity) {
566  case 'dcl':
567  return [
568  "id" => "integer",
569  "title" => "text",
570  "description" => "text",
571  'is_online' => 'integer',
572  'rating' => 'integer',
573  'public_notes' => 'integer',
574  'approval' => 'integer',
575  'notification' => 'integer',
576  ];
577  case 'il_dcl_table':
578  return [
579  'id' => 'integer',
580  'obj_id' => 'integer',
581  'title' => 'text',
582  'add_perm' => 'integer',
583  'edit_perm' => 'integer',
584  'delete_perm' => 'integer',
585  'edit_by_owner' => 'integer',
586  'limited' => 'integer',
587  'limit_start' => 'text',
588  'limit_end' => 'text',
589  'is_visible' => 'integer',
590  'export_enabled' => 'integer',
591  'import_enabled' => 'integer',
592  'default_sort_field_id' => 'text',
593  'default_sort_field_order' => 'text',
594  'description' => 'text',
595  'public_comments' => 'integer',
596  'view_own_records_perm' => 'integer',
597  'delete_by_owner' => 'integer',
598  'save_confirmation' => 'integer',
599  'table_order' => 'integer',
600  ];
601  case 'il_dcl_tableview':
602  return [
603  'id' => 'integer',
604  'table_id' => 'integer',
605  'title' => 'text',
606  'roles' => 'text',
607  'description' => 'text',
608  'tableview_order' => 'integer',
609  ];
610  case 'il_dcl_field':
611  return [
612  'id' => 'integer',
613  'table_id' => 'integer',
614  'title' => 'text',
615  'description' => 'text',
616  'datatype_id' => 'integer',
617  'datatype_title' => 'text',
618  ];
619  case 'il_dcl_tview_set':
620  return [
621  'id' => 'integer',
622  'tableview_id' => 'integer',
623  'field' => 'text',
624  'visible' => 'integer',
625  'in_filter' => 'integer',
626  'filter_value' => 'text',
627  'filter_changeable' => 'integer',
628  'required_create' => 'integer',
629  'required_edit' => 'integer',
630  'locked_create' => 'integer',
631  'locked_edit' => 'integer',
632  'visible_create' => 'integer',
633  'visible_edit' => 'integer',
634  'default_value' => 'text',
635  ];
636  case 'il_dcl_tfield_set':
637  return [
638  'id' => 'integer',
639  'table_id' => 'integer',
640  'field' => 'text',
641  'field_order' => 'integer',
642  'exportable' => 'integer',
643  ];
644  case 'il_dcl_field_prop':
645  return [
646  'id' => 'integer',
647  'field_id' => 'integer',
648  'name' => 'text',
649  'value' => 'integer',
650  ];
651  case 'il_dcl_sel_opts':
652  return [
653  'id' => 'integer',
654  'field_id' => 'integer',
655  'opt_id' => 'integer',
656  'sorting' => 'integer',
657  'value' => 'text',
658  ];
659  case 'il_dcl_record':
660  return [
661  'id' => 'integer',
662  'table_id' => 'integer',
663  ];
664  case 'il_dcl_record_field':
665  return [
666  'id' => 'integer',
667  'record_id' => 'integer',
668  'field_id' => 'integer',
669  ];
670  case 'il_dcl_stloc1_value':
671  case 'il_dcl_stloc2_value':
672  case 'il_dcl_stloc3_value':
673  return [
674  'id' => 'integer',
675  'record_field_id' => 'integer',
676  'value' => 'text',
677  ];
678  case 'il_dcl_stloc1_default':
679  case 'il_dcl_stloc2_default':
680  case 'il_dcl_stloc3_default':
681  return [
682  'id' => 'integer',
683  'tview_set_id' => 'integer',
684  'value' => 'text',
685  ];
686  default:
687  return [];
688  }
689  }
690 
699  protected function getDependencies(
700  string $a_entity,
701  string $a_version,
702  ?array $a_rec = null,
703  ?array $a_ids = null
704  ): array {
705  if (!$a_rec && !$a_ids) {
706  return [];
707  }
708  switch ($a_entity) {
709  case 'dcl':
710  $set = $this->db->query('SELECT * FROM il_dcl_table WHERE obj_id = ' . $this->db->quote(
711  $a_rec['id'],
712  'integer'
713  ) . ' ORDER BY id');
714  $ids = $this->buildCache('il_dcl_table', $set);
715 
716  return [
717  'il_dcl_table' => ['ids' => $ids],
718  ];
719  case 'il_dcl_table':
720  $set = $this->db->query('SELECT * FROM il_dcl_record WHERE table_id = ' . $this->db->quote(
721  $a_rec['id'],
722  'integer'
723  ));
724  $ids_records = $this->buildCache('il_dcl_record', $set);
725  $set = $this->db->query('SELECT il_dcl_field.*, il_dcl_datatype.title as datatype_title FROM il_dcl_field INNER JOIN il_dcl_datatype ON il_dcl_field.datatype_id = il_dcl_datatype.id WHERE table_id = ' . $this->db->quote(
726  $a_rec['id'],
727  'integer'
728  ));
729  $ids_fields = $this->buildCache('il_dcl_field', $set);
730  $set = $this->db->query('SELECT * FROM il_dcl_tableview WHERE table_id = ' . $this->db->quote(
731  $a_rec['id'],
732  'integer'
733  ));
734  $ids_tableviews = $this->buildCache('il_dcl_tableview', $set);
735  $set = $this->db->query('SELECT * FROM il_dcl_tfield_set WHERE table_id = ' . $this->db->quote(
736  $a_rec['id'],
737  'integer'
738  ));
739  $ids_tablefield_settings = $this->buildCache('il_dcl_tfield_set', $set);
740 
741  return [
742  'il_dcl_field' => ['ids' => $ids_fields],
743  'il_dcl_record' => ['ids' => $ids_records],
744  'il_dcl_tableview' => ['ids' => $ids_tableviews],
745  'il_dcl_tfield_set' => ['ids' => $ids_tablefield_settings],
746  ];
747  case 'il_dcl_field':
748  $set = $this->db->query('SELECT * FROM il_dcl_field_prop WHERE field_id = ' . $this->db->quote(
749  $a_rec['id'],
750  'integer'
751  ));
752  $prop_ids = $this->buildCache('il_dcl_field_prop', $set);
753 
754  $set = $this->db->query('SELECT * FROM il_dcl_sel_opts WHERE field_id = ' . $this->db->quote(
755  $a_rec['id'],
756  'integer'
757  ));
758  $opt_ids = $this->buildCache('il_dcl_sel_opts', $set);
759 
760  return [
761  'il_dcl_field_prop' => ['ids' => $prop_ids],
762  'il_dcl_sel_opts' => ['ids' => $opt_ids],
763  ];
764  case 'il_dcl_record':
765  $sql = 'SELECT rf.*, d.storage_location FROM il_dcl_record_field AS rf' . ' INNER JOIN il_dcl_field AS f ON (f.id = rf.field_id)'
766  . ' INNER JOIN il_dcl_datatype AS d ON (f.datatype_id = d.id) ' . ' WHERE rf.record_id = '
767  . $this->db->quote($a_rec['id'], 'integer');
768  $set = $this->db->query($sql);
769  $ids = $this->buildCache('il_dcl_record_field', $set);
770 
771  $set = $this->db->query($sql);
772  while ($rec = $this->db->fetchObject($set)) {
773  $this->record_field_ids_2_storage[$rec->id] = ilDclCache::getFieldCache($rec->field_id)->getStorageLocation();
774  }
775  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
776  $sql
777  = 'SELECT rf.id AS record_field_id, st1.value AS value1, st2.value AS value2, st3.value AS value3 FROM il_dcl_record_field AS rf '
778  . 'LEFT JOIN il_dcl_stloc1_value AS st1 ON (st1.record_field_id = rf.id) '
779  . 'LEFT JOIN il_dcl_stloc2_value AS st2 ON (st2.record_field_id = rf.id) '
780  . 'LEFT JOIN il_dcl_stloc3_value AS st3 ON (st3.record_field_id = rf.id) ' . 'WHERE rf.record_id = '
781  . $this->db->quote($a_rec['id'], 'integer');
782  $set = $this->db->query($sql);
783 
784  while ($rec = $this->db->fetchObject($set)) {
785  $stloc = $this->record_field_ids_2_storage[$rec->record_field_id];
786  $value = null;
787  if ($stloc != 0) {
788  $value = "value$stloc";
789  $value = $rec->{$value};
790  }
791  // Save reocrd field id. Internal ID is not used currently
792  $this->caches["il_dcl_stloc{$stloc}_value"][$rec->record_field_id] = [
793  'record_field_id' => $rec->record_field_id,
794  'value' => $value,
795  ];
796  }
797 
798  return [
799  'il_dcl_record_field' => ['ids' => $ids],
800  ];
801  case 'il_dcl_tableview':
802  $set = $this->db->query('SELECT * FROM il_dcl_tview_set WHERE tableview_id = ' . $this->db->quote(
803  $a_rec['id'],
804  'integer'
805  ));
806  $ids = $this->buildCache('il_dcl_tview_set', $set);
807 
808  return [
809  'il_dcl_tview_set' => ['ids' => $ids],
810  ];
811  case 'il_dcl_tview_set':
812 
813  if (!(int) $a_rec['field'] > 0) {
814  break;
815  }
816  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
817  $sql
818  = '
819  SELECT tview_set.id AS tview_set_id, st1.value AS value1, st2.value AS value2, st3.value AS value3,
820  st1.id AS id1, st2.id AS id2, st3.id AS id3
821  FROM il_dcl_tview_set AS tview_set
822  LEFT JOIN il_dcl_stloc1_default AS st1 ON (st1.tview_set_id = tview_set.id)
823  LEFT JOIN il_dcl_stloc2_default AS st2 ON (st2.tview_set_id = tview_set.id)
824  LEFT JOIN il_dcl_stloc3_default AS st3 ON (st3.tview_set_id = tview_set.id)
825  WHERE tview_set.id = ' . $this->db->quote($a_rec['id'], 'integer');
826  $set = $this->db->query($sql);
827 
828  while ($rec = $this->db->fetchObject($set)) {
829  $stloc = ilDclCache::getFieldCache((int) $a_rec['field'])->getStorageLocation();
830  if ($stloc != 0) {
831  $value_str = "value$stloc";
832  $value = $rec->{$value_str};
833  $id_str = "id$stloc";
834  $id = $rec->{$id_str};
835  $tview_set_id = $rec->tview_set_id;
836 
837  // Save reocrd field id. Internal ID is not used currently
838  $this->caches["il_dcl_stloc" . "$stloc" . "_default"][$rec->tview_set_id] = [
839  'id' => $id,
840  'tview_set_id' => $rec->tview_set_id,
841  'value' => $value,
842  ];
843 
844  return [
845  "il_dcl_stloc{$stloc}_default" => ['ids' => [$tview_set_id]],
846  ];
847  }
848  }
849  break;
850  case 'il_dcl_record_field':
851  $record_field_id = $a_rec['id'];
852  $storage_loc = $this->record_field_ids_2_storage[$record_field_id];
853 
854  return [
855  "il_dcl_stloc{$storage_loc}_value" => ['ids' => [$record_field_id]],
856  ];
857  }
858 
859  return [];
860  }
861 
866  public function readData(string $a_entity, string $a_version, array $a_ids): void
867  {
868  $this->data = [];
869  $this->_readData($a_entity, $a_ids);
870  }
871 
877  protected function _readData(string $a_entity, array $a_ids): void
878  {
879  switch ($a_entity) {
880  case 'dcl':
881  foreach ($a_ids as $dcl_id) {
882  if (ilObject::_lookupType((int) $dcl_id) === 'dcl') {
883  $obj = new ilObjDataCollection((int) $dcl_id, false);
884  $data = [
885  'id' => $dcl_id,
886  'title' => $obj->getTitle(),
887  'description' => $obj->getDescription(),
888  'is_online' => $obj->getOnline() ? '1' : '0',
889  'rating' => $obj->getRating() ? '1' : '0',
890  'public_notes' => $obj->getPublicNotes() ? '1' : '0',
891  'approval' => $obj->getApproval() ? '1' : '0',
892  'notification' => $obj->getNotification() ? '1' : '0',
893  ];
894  $this->caches['dcl'][$dcl_id] = $data;
895  $this->data[] = $data;
896  }
897  }
898  break;
899  default:
900  $data = $this->getCache($a_entity);
901  foreach ($a_ids as $id) {
902  $this->data[] = $data[$id];
903  }
904  }
905  }
906 
907  protected function buildCache(string $a_entity, ilDBStatement $set): array
908  {
909  $fields = array_keys($this->getTypes($a_entity, ''));
910  $ids = [];
911  while ($rec = $this->db->fetchObject($set)) {
912  $data = [];
913  foreach ($fields as $field) {
914  $data[$field] = $rec->{$field};
915  }
916  $id = $rec->id;
917  $this->caches[$a_entity][$id] = $data;
918  $ids[] = $id;
919  }
920 
921  return $ids;
922  }
923 }
getXmlNamespace(string $a_entity, string $a_schema_version)
static createOrGetStandardView(int $table_id)
getTypes(string $a_entity, string $a_version)
Map XML attributes of entities to datatypes (text, integer...)
buildCache(string $a_entity, ilDBStatement $set)
static getFieldCache(int $field_id=0)
$datetime
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
static isPluginDatatype(string $datatype)
array $import_record_field_cache
Caches ilDclBaseRecordFieldModel objects.
const IL_CAL_UNIX
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getInstance(int $tableview_id, int $field_id)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
readData(string $a_entity, string $a_version, array $a_ids)
Read data from Cache for a given entity and ID(s)
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
getCache(string $a_entity)
Get cached data from a given entity.
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
global $DIC
Definition: shib_login.php:26
array ilObjDataCollection $import_dc_object
_readData(string $a_entity, array $a_ids)
Build data array, data is read from cache except dcl object itself.
static getInstance(int $table_id, string $field)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static getRecordCache(?int $record_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Return dependencies form entities to other entities (in our case these are all the DB relations) ...
static _lookupType(int $id, bool $reference=false)