ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilStyleDataSet.php
Go to the documentation of this file.
1 <?php
2 
21 
43 {
44  protected ?ilObjStyleSheet $current_obj = null;
49  protected $log;
50 
54  protected $rbacsystem;
55 
59  protected $user;
60 
61  public function __construct()
62  {
63  global $DIC;
64 
65  $this->db = $DIC->database();
67  $this->log = ilLoggerFactory::getLogger('styl');
68  $this->log->debug("constructed");
69  $this->rbacsystem = $DIC->rbac()->system();
70  $this->user = $DIC->user();
71  $this->repo = $DIC->contentStyle()->internal()->repo();
72  }
73 
74 
79  public function getSupportedVersions(): array
80  {
81  return array("5.1.0", "8.0");
82  }
83 
89  public function getXmlNamespace(string $a_entity, string $a_schema_version): string
90  {
91  return "http://www.ilias.de/xml/Services/Style/" . $a_entity;
92  }
93 
100  protected function getTypes(string $a_entity, string $a_version): array
101  {
102  if ($a_entity == "sty") {
103  switch ($a_version) {
104  case "5.1.0":
105  case "8.0":
106  return array(
107  "Id" => "integer",
108  "Title" => "text",
109  "Description" => "text",
110  "ImagesDir" => "directory"
111  );
112  }
113  }
114 
115  if ($a_entity == "object_style") {
116  switch ($a_version) {
117  case "5.1.0":
118  case "8.0":
119  return array(
120  "ObjectId" => "integer"
121  );
122  }
123  }
124 
125  if ($a_entity == "sty_setting") {
126  switch ($a_version) {
127  case "5.1.0":
128  case "8.0":
129  return array(
130  "StyleId" => "integer",
131  "Name" => "test",
132  "Value" => "text"
133  );
134  }
135  }
136 
137  if ($a_entity == "sty_char") {
138  switch ($a_version) {
139  case "5.1.0":
140  return array(
141  "StyleId" => "integer",
142  "Type" => "text",
143  "Characteristic" => "text",
144  "Hide" => "integer"
145  );
146  case "8.0":
147  return array(
148  "StyleId" => "integer",
149  "Type" => "text",
150  "Characteristic" => "text",
151  "Hide" => "integer",
152  "OrderNr" => "integer",
153  "Outdate" => "integer"
154  );
155  }
156  }
157 
158  if ($a_entity == "sty_char_title") {
159  switch ($a_version) {
160  case "8.0":
161  return array(
162  "StyleId" => "integer",
163  "Type" => "text",
164  "Characteristic" => "text",
165  "Lang" => "text",
166  "Title" => "text"
167  );
168  }
169  }
170 
171  if ($a_entity == "sty_parameter") {
172  switch ($a_version) {
173  case "5.1.0":
174  case "8.0":
175  return array(
176  "StyleId" => "integer",
177  "Tag" => "text",
178  "Class" => "text",
179  "Parameter" => "text",
180  "Value" => "text",
181  "Type" => "text",
182  "MqId" => "integer",
183  "Custom" => "integer"
184  );
185  }
186  }
187 
188  if ($a_entity == "sty_color") {
189  switch ($a_version) {
190  case "5.1.0":
191  case "8.0":
192  return array(
193  "StyleId" => "integer",
194  "ColorName" => "text",
195  "ColorCode" => "text"
196  );
197  }
198  }
199 
200  if ($a_entity == "sty_media_query") {
201  switch ($a_version) {
202  case "5.1.0":
203  case "8.0":
204  return array(
205  "Id" => "integer",
206  "StyleId" => "integer",
207  "OrderNr" => "integer",
208  "MQuery" => "text"
209  );
210  }
211  }
212 
213  if ($a_entity == "sty_template") {
214  switch ($a_version) {
215  case "5.1.0":
216  case "8.0":
217  return array(
218  "Id" => "integer",
219  "StyleId" => "integer",
220  "Name" => "text",
221  "Preview" => "text",
222  "TempType" => "text"
223  );
224  }
225  }
226 
227  if ($a_entity == "sty_template_class") {
228  switch ($a_version) {
229  case "5.1.0":
230  case "8.0":
231  return array(
232  "TemplateId" => "integer",
233  "ClassType" => "text",
234  "Class" => "text"
235  );
236  }
237  }
238 
239  if ($a_entity == "sty_usage") {
240  switch ($a_version) {
241  case "5.1.0":
242  case "8.0":
243  return array(
244  "ObjId" => "integer",
245  "StyleId" => "integer"
246  );
247  }
248  }
249  }
250 
256  public function getXmlRecord(string $a_entity, string $a_version, array $a_set): array
257  {
258  if ($a_entity == "sty") {
259  $dir = ilObjStyleSheet::_getImagesDirectory($a_set["Id"]);
260  $a_set["ImagesDir"] = $dir;
261  }
262 
263  return $a_set;
264  }
265 
271  public function readData(string $a_entity, string $a_version, array $a_ids): void
272  {
273  $ilDB = $this->db;
274 
275  if (!is_array($a_ids)) {
276  $a_ids = array($a_ids);
277  }
278 
279  if ($a_entity == "object_style") {
280  switch ($a_version) {
281  case "5.1.0":
282  case "8.0":
283  foreach ($a_ids as $id) {
284  $this->data[] = array("ObjectId" => $id);
285  }
286  break;
287  }
288  }
289 
290  if ($a_entity == "sty") {
291  switch ($a_version) {
292  case "5.1.0":
293  case "8.0":
294  $this->getDirectDataFromQuery("SELECT o.title, o.description, o.obj_id id" .
295  " FROM object_data o " .
296  " WHERE " . $ilDB->in("o.obj_id", $a_ids, false, "integer"));
297  break;
298  }
299  }
300 
301  if ($a_entity == "sty_setting") {
302  switch ($a_version) {
303  case "5.1.0":
304  case "8.0":
305  $this->getDirectDataFromQuery("SELECT style_id, name, value" .
306  " FROM style_setting " .
307  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
308  break;
309  }
310  }
311 
312  if ($a_entity == "sty_char") {
313  switch ($a_version) {
314  case "5.1.0":
315  case "8.0":
316  $this->getDirectDataFromQuery("SELECT style_id, type, characteristic, hide, order_nr, outdated" .
317  " FROM style_char " .
318  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
319  break;
320  }
321  }
322 
323  if ($a_entity == "sty_char_title") {
324  switch ($a_version) {
325  case "8.0":
326  $this->getDirectDataFromQuery("SELECT style_id, type, characteristic, lang, title" .
327  " FROM style_char_title " .
328  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
329  break;
330  }
331  }
332 
333  if ($a_entity == "sty_parameter") {
334  switch ($a_version) {
335  case "5.1.0":
336  case "8.0":
337  $this->getDirectDataFromQuery("SELECT style_id, tag, class, parameter, value, type, mq_id, custom" .
338  " FROM style_parameter " .
339  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
340  break;
341  }
342  }
343 
344  if ($a_entity == "sty_color") {
345  switch ($a_version) {
346  case "5.1.0":
347  case "8.0":
348  $this->getDirectDataFromQuery("SELECT style_id, color_name, color_code" .
349  " FROM style_color " .
350  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
351  break;
352  }
353  }
354 
355  if ($a_entity == "sty_media_query") {
356  switch ($a_version) {
357  case "5.1.0":
358  case "8.0":
359  $this->getDirectDataFromQuery("SELECT id, style_id, order_nr, mquery m_query" .
360  " FROM sty_media_query " .
361  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
362  break;
363  }
364  }
365 
366  if ($a_entity == "sty_template") {
367  switch ($a_version) {
368  case "5.1.0":
369  case "8.0":
370  $this->getDirectDataFromQuery("SELECT id, style_id, name, preview, temp_type" .
371  " FROM style_template " .
372  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
373  break;
374  }
375  }
376 
377  if ($a_entity == "sty_template_class") {
378  switch ($a_version) {
379  case "5.1.0":
380  case "8.0":
381  $this->getDirectDataFromQuery("SELECT template_id, class_type, class" .
382  " FROM style_template_class " .
383  " WHERE " . $ilDB->in("template_id", $a_ids, false, "integer"));
384  break;
385  }
386  }
387 
388  if ($a_entity == "sty_usage") {
389  switch ($a_version) {
390  case "5.1.0":
391  case "8.0":
392  $this->getDirectDataFromQuery("SELECT obj_id, style_id" .
393  " FROM style_usage " .
394  " WHERE " . $ilDB->in("style_id", $a_ids, false, "integer"));
395  break;
396  }
397  }
398  }
399 
403  protected function getDependencies(
404  string $a_entity,
405  string $a_version,
406  ?array $a_rec = null,
407  ?array $a_ids = null
408  ): array {
409  $this->ds_log->debug("entity: " . $a_entity . ", rec: " . print_r($a_rec, true));
410  switch ($a_entity) {
411  case "object_style":
412  $this->ds_log->debug("object id: " . ($a_rec["ObjectId"] ?? null));
413  $style_id = ilObjStyleSheet::lookupObjectStyle($a_rec["ObjectId"] ?? 0);
414  $this->ds_log->debug("style id: " . $style_id);
415  //if ($style_id > 0 && !ilObjStyleSheet::_lookupStandard($style_id))
416  if ($style_id > 0 && ilObject::_lookupType($style_id) == "sty") { // #0019337 always export style, if valid
417  return array(
418  "sty" => array("ids" => $style_id));
419  }
420  return array();
421  break;
422 
423  case "sty":
424  return array(
425  "sty_setting" => array("ids" => $a_rec["Id"] ?? null),
426  "sty_media_query" => array("ids" => $a_rec["Id"] ?? null),
427  "sty_char" => array("ids" => $a_rec["Id"] ?? null),
428  "sty_char_title" => array("ids" => $a_rec["Id"] ?? null),
429  "sty_color" => array("ids" => $a_rec["Id"] ?? null),
430  "sty_parameter" => array("ids" => $a_rec["Id"] ?? null),
431  "sty_template" => array("ids" => $a_rec["Id"] ?? null),
432  "sty_usage" => array("ids" => $a_rec["Id"] ?? null)
433  );
434 
435  case "sty_template":
436  return array(
437  "sty_template_class" => array("ids" => $a_rec["Id"] ?? null)
438  );
439  }
440 
441  return [];
442  }
443 
444 
450  public function importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version): void
451  {
452  global $DIC;
453  $service = $DIC->contentStyle()->internal();
454  $access_manager = $service->domain()->access(
455  0,
456  $this->user->getId()
457  );
458  $access_manager->enableWrite(true);
459 
460  $style_id = (isset($this->current_obj))
461  ? $this->current_obj->getId()
462  : 0;
463  $characteristic_manager = $service->domain()->characteristic(
464  $style_id,
465  $access_manager
466  );
467 
468  $color_manager = $service->domain()->color(
469  $style_id,
470  $access_manager
471  );
472 
473  $a_rec = $this->stripTags($a_rec);
474  switch ($a_entity) {
475  case "sty":
476  $this->log->debug("Entity: " . $a_entity);
477  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_rec['Id'])) {
478  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
479  } else {
480  $newObj = new ilObjStyleSheet();
481  $newObj->create(0, true);
482  }
483 
484  $newObj->setTitle($a_rec["Title"]);
485  $newObj->setDescription($a_rec["Description"]);
486  $newObj->update(true);
487 
488  $this->current_obj = $newObj;
489  $a_mapping->addMapping("Services/Style", "sty", $a_rec["Id"], $newObj->getId());
490  $a_mapping->addMapping("Services/Object", "obj", $a_rec["Id"], $newObj->getId());
491  $this->log->debug("Added mapping Services/Style sty " . $a_rec["Id"] . " > " . $newObj->getId());
492 
493  $dir = str_replace("..", "", $a_rec["ImagesDir"]);
494  if ($dir != "" && $this->getImportDirectory() != "") {
495  $source_dir = $this->getImportDirectory() . "/" . $dir;
496  $target_dir = $dir = ilObjStyleSheet::_getImagesDirectory($newObj->getId());
497  ilFileUtils::rCopy($source_dir, $target_dir);
498  }
499  break;
500 
501  case "sty_setting":
502  $this->current_obj->writeStyleSetting($a_rec["Name"], $a_rec["Value"]);
503  break;
504 
505  case "sty_char":
506  $this->current_obj->addCharacteristic($a_rec["Type"], $a_rec["Characteristic"], $a_rec["Hide"], (int) ($a_rec["OrderNr"] ?? 0), (bool) ($a_rec["Outdated"] ?? false));
507  break;
508 
509  case "sty_char_title":
510  $char_repo = $this->repo->characteristic();
511  $char_repo->addTitle(
512  $this->current_obj->getId(),
513  $a_rec["Type"],
514  $a_rec["Characteristic"],
515  $a_rec["Lang"],
516  $a_rec["Title"],
517  );
518  break;
519 
520  case "sty_parameter":
521  $mq_id = (int) $a_mapping->getMapping("Services/Style", "media_query", $a_rec["MqId"]);
522  $characteristic_manager->replaceParameter($a_rec["Tag"], $a_rec["Class"], $a_rec["Parameter"], $a_rec["Value"], $a_rec["Type"], $mq_id, $a_rec["Custom"]);
523  break;
524 
525  case "sty_color":
526  $color_manager->addColor($a_rec["ColorName"], $a_rec["ColorCode"]);
527  break;
528 
529  case "sty_media_query":
530  $mq_id = $this->current_obj->addMediaQuery($a_rec["MQuery"], $a_rec["OrderNr"]);
531  $a_mapping->addMapping("Services/Style", "media_query", $a_rec["Id"], $mq_id);
532  break;
533 
534  case "sty_template":
535  $tid = $this->current_obj->addTemplate($a_rec["TempType"], $a_rec["Name"], array());
536  $a_mapping->addMapping("Services/Style", "template", $a_rec["Id"], $tid);
537  break;
538 
539  case "sty_template_class":
540  $tid = (int) $a_mapping->getMapping("Services/Style", "template", $a_rec["TemplateId"]);
541  $this->current_obj->addTemplateClass($tid, $a_rec["ClassType"], $a_rec["Class"]);
542  break;
543 
544  case "sty_usage":
545  $obj_id = (int) $a_mapping->getMapping("Services/Object", "obj", $a_rec["ObjId"]);
546  $style_id = (int) $a_mapping->getMapping("Services/Style", "sty", $a_rec["StyleId"]);
547  if ($obj_id > 0 && $style_id > 0) {
548  ilObjStyleSheet::writeStyleUsage($obj_id, $style_id);
549  ilObjStyleSheet::writeOwner($obj_id, $style_id);
550  }
551  break;
552  }
553  }
554 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Content InternalRepoService $repo
static _getImagesDirectory(int $a_style_id)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
ilObjStyleSheet $current_obj
getXmlRecord(string $a_entity, string $a_version, array $a_set)
Get xml record.
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static writeOwner($obj_id, $style_id)
global $DIC
Definition: feed.php:28
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Determine the dependent sets of data.
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
ilDBInterface $db
Content style internal repo service.
getSupportedVersions()
Get supported versions.
readData(string $a_entity, string $a_version, array $a_ids)
Read data.
getTypes(string $a_entity, string $a_version)
Get field types for entity.
getXmlNamespace(string $a_entity, string $a_schema_version)
Get xml namespace.
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 ...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Style Data set class.
static writeStyleUsage(int $a_obj_id, int $a_style_id)
Write style usage.
static lookupObjectStyle(int $a_obj_id)
Lookup object style.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
stripTags(array $rec, array $omit_keys=[])
static _lookupType(int $id, bool $reference=false)
$service
Definition: ltiservices.php:43
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Import record.