ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjectDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
34 {
35  protected ilObjectDIC $obj_dic;
38 
39  public function __construct()
40  {
42  global $DIC;
43  $this->storage = $DIC->resourceStorage();
44 
45  $obj_dic = ilObjectDIC::dic();
46  $this->object_properties_agregator = $obj_dic['object_properties_agregator'];
47 
49  }
50  public function getSupportedVersions(): array
51  {
52  return ['4.4.0', '5.1.0', '5.2.0', '5.4.0'];
53  }
54 
55  protected function getXmlNamespace(string $entity, string $schema_version): string
56  {
57  return 'http://www.ilias.de/xml/Services/Object/' . $entity;
58  }
59 
63  protected function getTypes(string $entity, string $version): array
64  {
65  if ($entity == 'transl_entry') {
66  switch ($version) {
67  case '4.4.0':
68  case '5.1.0':
69  case '5.2.0':
70  case '5.4.0':
71  return [
72  'ObjId' => 'integer',
73  'Title' => 'text',
74  'Description' => 'text',
75  'LangCode' => 'text',
76  'LangDefault' => 'integer'
77  ];
78  }
79  }
80  if ($entity == 'transl') {
81  switch ($version) {
82  case '4.4.0':
83  case '5.1.0':
84  case '5.2.0':
85  case '5.4.0':
86  return [
87  'ObjId' => 'integer',
88  'MasterLang' => 'text'
89  ];
90  }
91  }
92  if ($entity == 'service_settings') {
93  switch ($version) {
94  case '5.1.0':
95  case '5.2.0':
96  case '5.4.0':
97  return [
98  'ObjId' => 'integer',
99  'Setting' => 'text',
100  'Value' => 'text'
101  ];
102  }
103  }
104  if ($entity == 'common') {
105  if ($version == '5.4.0') {
106  return [
107  'ObjId' => 'integer'
108  ];
109  }
110  }
111  if ($entity == 'icon') {
112  if ($version == '5.4.0') {
113  return [
114  'ObjId' => 'integer',
115  'Filename' => 'text',
116  'Dir' => 'directory'
117  ];
118  }
119  }
120  if ($entity == 'tile') {
121  if ($version == '5.4.0') {
122  return [
123  'ObjId' => 'integer',
124  'Dir' => 'directory'
125  ];
126  }
127  }
128  return [];
129  }
130 
131  public function readData(string $entity, string $version, array $ids): void
132  {
134  global $DIC;
135 
136  if ($entity == 'transl_entry') {
137  switch ($version) {
138  case '4.4.0':
139  case '5.1.0':
140  case '5.2.0':
141  case '5.4.0':
142  $this->getDirectDataFromQuery(
143  'SELECT obj_id, title, description, lang_code, lang_default' . PHP_EOL
144  . 'FROM object_translation' . PHP_EOL
145  . 'WHERE ' . $this->db->in('obj_id', $ids, false, 'integer') . PHP_EOL
146  );
147  break;
148  }
149  }
150 
151  if ($entity == 'transl') {
152  switch ($version) {
153  case '4.4.0':
154  case '5.1.0':
155  case '5.2.0':
156  case '5.4.0':
157  $this->getDirectDataFromQuery(
158  'SELECT obj_id, master_lang' . PHP_EOL
159  . 'FROM obj_content_master_lng' . PHP_EOL
160  . 'WHERE ' . $this->db->in('obj_id', $ids, false, 'integer') . PHP_EOL
161  );
162  break;
163  }
164  }
165 
166  if ($entity == 'service_settings') {
167  switch ($version) {
168  case '5.1.0':
169  case '5.2.0':
170  case '5.4.0':
171  $this->data = [];
172  foreach ($ids as $id) {
173  // info, news, custom metadata, tags, taxonomies, auto rating (all stored in container settings)
174  $settings = [
182  ];
183  if ($version == '5.2.0') {
185  }
186  foreach ($settings as $s) {
187  if (ilContainer::_hasContainerSetting((int) $id, $s)) {
188  $val = ilContainer::_lookupContainerSetting((int) $id, $s);
189  $this->data[] = [
190  'ObjId' => $id,
191  'Setting' => $s,
192  'Value' => $val
193  ];
194  }
195  }
196  }
197  break;
198  }
199  }
200  // common
201  if ($entity == 'common') {
202  $this->data = [];
203  foreach ($ids as $id) {
204  $this->data[] = [
205  'ObjId' => $id
206  ];
207  }
208  }
209  // tile images
210  if ($entity == "tile") {
211  $this->data = [];
212  foreach ($ids as $id) {
213  $rid = $this->object_properties_agregator->getFor((int) $id)
214  ->getPropertyTileImage()->getTileImage()->getRid();
215  if ($rid === null) {
216  continue;
217  }
218 
219  $temp_dir = $this->copyTileToTempFolderForExport($rid);
220 
221  $this->data[] = [
222  "ObjId" => $id,
223  "Dir" => $temp_dir
224  ];
225  }
226  }
227 
228  // icons
229  if ($entity == 'icon') {
230  $customIconFactory = $DIC['object.customicons.factory'];
231  $this->data = [];
232  foreach ($ids as $id) {
234  $customIcon = $customIconFactory->getByObjId((int) $id, ilObject::_lookupType((int) $id));
235  if ($customIcon->exists()) {
236  $this->data[] = [
237  'ObjId' => $id,
238  'Filename' => pathinfo($customIcon->getFullPath(), PATHINFO_BASENAME),
239  'Dir' => dirname($customIcon->getFullPath())
240  ];
241  }
242  }
243  }
244  }
245 
246  private function copyTileToTempFolderForExport(string $rid): string
247  {
248  $i = $this->storage->manage()->find($rid);
249  $stream = $this->storage->consume()->stream(
250  $i
251  );
252  $title = $this->storage->manage()->getCurrentRevision($i)->getTitle();
253 
254  $temp_dir = implode(
255  DIRECTORY_SEPARATOR,
256  [ILIAS_DATA_DIR, CLIENT_ID, 'temp', uniqid('tmp')]
257  );
258  mkdir($temp_dir);
259  file_put_contents($temp_dir . DIRECTORY_SEPARATOR . $title, $stream->getStream()->getContents());
260  return $temp_dir;
261  }
265  protected function getDependencies(
266  string $entity,
267  string $version,
268  ?array $rec = null,
269  ?array $ids = null
270  ): array {
271  $rec['ObjId'] = $rec['ObjId'] ?? null;
272  switch ($entity) {
273  case 'common':
274  return [
275  'transl' => ['ids' => $rec['ObjId']],
276  'transl_entry' => ['ids' => $rec['ObjId']],
277  'service_settings' => ['ids' => $rec['ObjId']],
278  'tile' => ['ids' => $rec['ObjId']],
279  'icon' => ['ids' => $rec['ObjId']]
280  ];
281  }
282 
283  return [];
284  }
285 
286  public function importRecord(
287  string $entity,
288  array $types,
289  array $rec,
290  ilImportMapping $mapping,
291  string $schema_version
292  ): void {
294  global $DIC;
295 
296  switch ($entity) {
297  case 'transl_entry':
298  $new_id = $this->getNewObjId($mapping, $rec['ObjId']);
299  if ($new_id > 0) {
300  $transl = ilObjectTranslation::getInstance($new_id);
301  $transl->addLanguage(
302  $rec['LangCode'],
303  strip_tags(
304  $rec['Title'],
306  ),
307  strip_tags(
308  $rec['Description'],
310  ),
311  (bool) $rec['LangDefault'],
312  true
313  );
314  $transl->save();
315  }
316  break;
317 
318  case 'transl':
319  $new_id = $this->getNewObjId($mapping, $rec['ObjId']);
320  if ($new_id > 0) {
321  $transl = ilObjectTranslation::getInstance($new_id);
322  $transl->setMasterLanguage($rec['MasterLang']);
323  $transl->save();
324  }
325  break;
326 
327  case 'service_settings':
328  // info, news, custom metadata, tags, taxonomies, auto rating (all stored in container settings)
329  $settings = [
338  ];
339  $new_id = (int) $this->getNewObjId($mapping, $rec['ObjId']);
340  if ($new_id > 0) {
341  if (in_array($rec['Setting'], $settings)) {
342  ilContainer::_writeContainerSetting($new_id, $rec['Setting'], $rec['Value']);
343  }
344  }
345  break;
346 
347  case 'icon':
348  $new_id = (int) $this->getNewObjId($mapping, $rec['ObjId']);
349  $dir = str_replace('..', '', $rec['Dir']);
350  if ($dir != '' && $this->getImportDirectory() != '') {
351  $source_dir = $this->getImportDirectory() . '/' . $dir;
352 
353  $customIconFactory = $DIC['object.customicons.factory'];
354  $customIcon = $customIconFactory->getByObjId($new_id, ilObject::_lookupType($new_id));
355  $customIcon->createFromImportDir($source_dir);
356  }
357  break;
358 
359  case 'tile':
360  $new_id = (int) $this->getNewObjId($mapping, $rec['ObjId']);
361  $dir = str_replace('..', '', $rec['Dir']);
362  if ($new_id > 0 && $dir != '' && $this->getImportDirectory() != '') {
363  $source_dir = $this->getImportDirectory() . '/' . $dir;
364  $object_properties = $this->object_properties_agregator->getFor($new_id);
365  $ti = $object_properties->getPropertyTileImage()->getTileImage();
366  $ti->createFromImportDir($source_dir);
367  $object_properties->storePropertyTileImage(
368  $object_properties->getPropertyTileImage()->withTileImage($ti)
369  );
370  }
371  break;
372  }
373  }
374 
375  public function getNewObjId(ilImportMapping $mapping, string $old_id): int
376  {
377  global $DIC;
378 
380  $objDefinition = $DIC['objDefinition'];
381 
382  $new_id = $mapping->getMapping('Services/Container', 'objs', $old_id);
383  if (!$new_id) {
384  $new_id = $mapping->getMapping('Services/Object', 'objs', $old_id);
385  }
386  if (!$new_id) {
387  $new_id = $mapping->getMapping('Services/Object', 'obj', $old_id);
388  }
389  if (!$new_id) {
390  foreach ($mapping->getAllMappings() as $k => $m) {
391  if (substr($k, 0, 8) == 'Modules/') {
392  foreach ($m as $type => $map) {
393  if (!$new_id) {
394  if ($objDefinition->isRBACObject($type)) {
395  $new_id = $mapping->getMapping($k, $type, $old_id);
396  }
397  }
398  }
399  }
400  }
401  }
402  return (int) $new_id;
403  }
404 }
readData(string $a_entity, string $a_version, array $a_ids)
Read data from DB.
ResourceStorage $storage
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $schema_version
Object data set class.
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
global $DIC
Definition: feed.php:28
getTypes(string $entity, string $version)
Get field types for entity.
const ALLOWED_TAGS_IN_TITLE_AND_DESCRIPTION
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
copyTileToTempFolderForExport(string $rid)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_ID
Definition: constants.php:41
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.
static getInstance(int $obj_id)
static _hasContainerSetting(int $a_id, string $a_keyword)
getDependencies(string $entity, string $version, ?array $rec=null, ?array $ids=null)
Determine the dependent sets of data.
const ILIAS_DATA_DIR
Definition: constants.php:44
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
getDirectDataFromQuery(string $a_query, bool $a_convert_to_leading_upper=true, bool $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
string $version
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
string $entity
static _lookupType(int $id, bool $reference=false)
ilObjectPropertiesAgregator $object_properties_agregator
getXmlNamespace(string $entity, string $schema_version)