ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilContainerImporter.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Export/classes/class.ilXmlImporter.php");
5
16{
21
25 protected $cont_log;
26
27 public function init()
28 {
29 $this->cont_log = ilLoggerFactory::getLogger('cont');
30 }
31
37 public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
38 {
39 include_once './Services/Container/classes/class.ilContainerXmlParser.php';
40
41 $this->structure_xml = $a_xml;
42 $this->cont_log->debug('Import xml: ' . $a_xml);
43 $this->cont_log->debug('Using id: ' . $a_id);
44
45 $parser = new ilContainerXmlParser($a_mapping, trim($a_xml));
46 $parser->parse($a_id);
47 }
48
52 public function finalProcessing($a_mapping)
53 {
54 $this->handleOfflineStatus($this->structure_xml, $a_mapping);
55 // pages
56 include_once('./Services/COPage/classes/class.ilPageObject.php');
57 $page_map = $a_mapping->getMappingsOfEntity('Services/COPage', 'pg');
58 foreach ($page_map as $old_pg_id => $new_pg_id) {
59 $parts = explode(':', $old_pg_id);
60 $pg_type = $parts[0];
61 $old_obj_id = $parts[1];
62 $new_pg_id = array_pop(explode(':', $new_pg_id));
63 $new_obj_id = $a_mapping->getMapping('Services/Container', 'objs', $old_obj_id);
64 // see bug #22718, this missed a check for the pg type
65 if (in_array($pg_type, array("crs", "grp", "fold", "cont"))) {
66 if ($new_obj_id > 0) {
67 ilPageObject::_writeParentId($pg_type, $new_pg_id, $new_obj_id);
68 $this->cont_log->debug('write parent id, type: ' . $pg_type . ", page id: " . $new_pg_id . ", parent id: " . $new_obj_id);
69 }
70 }
71 }
72
73 // style
74 include_once('./Services/Style/Content/classes/class.ilObjStyleSheet.php');
75 $sty_map = $a_mapping->getMappingsOfEntity('Services/Style', 'sty');
76 foreach ($sty_map as $old_sty_id => $new_sty_id) {
77 if (is_array(ilContainerXmlParser::$style_map[$old_sty_id])) {
78 foreach (ilContainerXmlParser::$style_map[$old_sty_id] as $obj_id) {
79 ilObjStyleSheet::writeStyleUsage($obj_id, $new_sty_id);
80 }
81 }
82 }
83
84 // skills
85 $new_crs_obj_id = end($a_mapping->getMappingsOfEntity('Modules/Course', 'crs'));
86 $new_crs_ref_id = ilObject::_getAllReferences($new_crs_obj_id);
87 $new_crs_ref_id = end($new_crs_ref_id);
88
89 $skl_local_prof_map = $a_mapping->getMappingsOfEntity('Services/Skill', 'skl_local_prof');
90 foreach ($skl_local_prof_map as $old_prof_id => $new_prof_id) {
91 $prof = new ilSkillProfile($new_prof_id);
92 $prof->updateRefIdAfterImport((int) $new_crs_ref_id);
93 $prof->addRoleToProfile(ilParticipants::getDefaultMemberRole($new_crs_ref_id));
94 }
95 }
96
100 protected function handleOfflineStatus(string $xml, ilImportMapping $mapping)
101 {
102 libxml_use_internal_errors(true);
103 $root = simplexml_load_string($xml);
104 if ($root === false) {
105 $errors = '';
106 foreach (libxml_get_errors() as $err) {
107 $errors .= $err->code . '<br/>';
108 }
109 $this->cont_log->error($xml);
110 $this->cont_log->error('Cannot parse xml: ' . $errors);
111 }
112 foreach ($root->xpath('//Item') as $item) {
113 $ref_id = 0;
114 $offline = null;
115 foreach ($item->attributes() as $name => $value) {
116 if ((string) $name == 'Offline') {
117 $offline = $value;
118 }
119 if ((string) $name == 'RefId') {
120 $ref_id = (string) $value;
121 }
122 }
123 if (is_null($offline)) {
124 $this->cont_log->debug('No offline handling for ref_id: ' . $ref_id);
125 continue;
126 }
127 $new_ref_id = $mapping->getMapping('Services/Container', 'refs', $ref_id);
128 $obj = ilObjectFactory::getInstanceByRefId($new_ref_id, false);
129 if (!$obj instanceof ilObject) {
130 $this->cont_log->warning('Cannot create instance for ref_id: ' . $new_ref_id);
131 continue;
132 }
133 if ($obj->supportsOfflineHandling()) {
134 if ($this->isRootNode($obj->getRefId(), $mapping)) {
135 $obj->setOfflineStatus(true);
136 } else {
137 // use the offline status of the imported XML file and set the offline status for the new container objects accordingly
138 $obj->setOfflineStatus(!((string) $offline === "0"));
139 }
140 $obj->update();
141 }
142 }
143 }
144
150 protected function isRootNode(int $ref_id, ilImportMapping $mapping) : bool
151 {
152 global $DIC;
153
154 $tree = $DIC->repositoryTree();
155 $parent_id = $tree->getParentId($ref_id);
156 if ($parent_id) {
157 return (int) $parent_id === (int) $mapping->getTargetId();
158 }
159 return false;
160 }
161}
An exception for terminatinating execution or to throw for unit testing.
container xml importer
finalProcessing($a_mapping)
Final processing.
isRootNode(int $ref_id, ilImportMapping $mapping)
handleOfflineStatus(string $xml, ilImportMapping $mapping)
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
XML parser for container structure.
getMapping($a_comp, $a_entity, $a_old_id)
Get a mapping.
getTargetId()
get target id
static getLogger($a_component_id)
Get component logger.
static writeStyleUsage($a_obj_id, $a_style_id)
Write style usage.
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
Class ilObject Basic functions for all objects.
static _getAllReferences($a_id)
get all reference ids of object
static _writeParentId($a_parent_type, $a_pg_id, $a_par_id)
Write parent id.
static getDefaultMemberRole($a_ref_id)
Xml importer class.
global $DIC
Definition: goto.php:24
$errors
Definition: imgupload.php:49
if($format !==null) $name
Definition: metadata.php:230
$xml
Definition: metadata.php:332