ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilOrgUnitSimpleImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
12{
13 public function simpleImport($file_path)
14 {
15 global $DIC;
16 $lng = $DIC['lng'];
17 $this->stats = array("created" => 0, "updated" => 0, "deleted" => 0);
18 $a = file_get_contents($file_path, "r");
19 $xml = new SimpleXMLElement($a);
20
21 if (!count($xml->OrgUnit)) {
22 $this->addError("no_orgunit", null, null);
23
24 return;
25 }
26
27 foreach ($xml->OrgUnit as $o) {
28 $this->simpleImportElement($o);
29 }
30 }
31
32
33 public function simpleImportElement(SimpleXMLElement $o)
34 {
35 global $DIC;
36 $tree = $DIC['tree'];
37 $tpl = $DIC['tpl'];
38 $ilUser = $DIC['ilUser'];
39 $title = (string) $o->title;
40 $description = (string) $o->description;
41 $external_id = (string) $o->external_id;
42 $create_mode = true;
43 $attributes = $o->attributes();
44 $action = (string) $attributes->action;
45 $ou_id = (string) $attributes->ou_id;
46 $ou_id_type = (string) $attributes->ou_id_type;
47 $ou_parent_id = (string) $attributes->ou_parent_id;
48 $ou_parent_id_type = (string) $attributes->ou_parent_id_type;
49
50 if ($ou_id == ilObjOrgUnit::getRootOrgRefId()) {
51 $this->addWarning("cannot_change_root_node", $ou_id ? $ou_id : $external_id, $action);
52
53 return;
54 }
55
56 if ($ou_parent_id == "__ILIAS") {
57 $ou_parent_id = ilObjOrgUnit::getRootOrgRefId();
58 $ou_parent_id_type = "reference_id";
59 }
60
61 //see mantis 0024601
62 if ($ou_id_type == 'external_id') {
63
64 if(strlen($external_id) == 0) {
65 $external_id = $ou_id;
66 }
67
68 if ($this->hasMoreThanOneMatch($external_id)) {
69 $this->addError("ou_more_than_one_match_found", $external_id, $action);
70
71 return;
72 }
73 }
74
75 $ref_id = $this->buildRef($ou_id, $ou_id_type);
76 $parent_ref_id = $this->buildRef($ou_parent_id, $ou_parent_id_type);
77
78 if ($action == "delete") {
79 if (!$parent_ref_id) {
80 $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
81
82 return;
83 }
84 if (!$ref_id) {
85 $this->addError("ou_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
86
87 return;
88 }
89 $ru = new ilRepUtil($this);
90 try {
91 $ru->deleteObjects($parent_ref_id, array($ref_id)) !== false;
92 $this->stats["deleted"]++;
93 } catch (Exception $e) {
94 $this->addWarning("orgu_already_deleted", $ou_id ? $ou_id : $external_id, $action);
95 }
96
97 return;
98 } elseif ($action == "update") {
99 if (!$parent_ref_id) {
100 $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
101
102 return;
103 }
104 if (!$ref_id) {
105 $this->addError("ou_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
106
107 return;
108 }
109 $object = new ilObjOrgUnit($ref_id);
110 $object->setTitle($title);
111
112 $object->updateTranslation($title, $description, $ilUser->getLanguage(), "");
113
114 $object->setDescription($description);
115 $object->update();
116 $object->setImportId($external_id);
117 $this->moveObject($ref_id, $parent_ref_id, $ou_id, $external_id);
118
119 $this->stats["updated"]++;
120 } elseif ($action == "create") {
121 if (!$parent_ref_id) {
122 $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
123
124 return;
125 }
126 if ($external_id) {
127 $obj_id = ilObject::_lookupObjIdByImportId($external_id);
128 if (ilObject::_hasUntrashedReference($obj_id) && ilObject::_lookupType($obj_id) == 'orgu') {
129 $this->addError("ou_external_id_exists", $ou_id ? $ou_id : $external_id, $action);
130
131 return;
132 }
133 }
134 $object = new ilObjOrgUnit();
135 $object->setTitle($title);
136 $object->setDescription($description);
137 $object->setImportId($external_id);
138 $object->create();
139 $object->createReference();
140 $object->putInTree($parent_ref_id);
141 $object->setPermissions($ou_parent_id);
142 $this->stats["created"]++;
143 } else {
144 $this->addError("no_valid_action_given", $ou_id, $action);
145 }
146 }
147
148
155 protected function moveObject($ref_id, $parent_ref_id, $ou_id, $external_id)
156 {
157 global $DIC;
158 $tree = $DIC['tree'];
159 if ($parent_ref_id != $tree->getParentId($ref_id)) {
160 try {
161 $path = $tree->getPathId($parent_ref_id);
162 if (in_array($ref_id, $path)) {
163 $this->addWarning("not_movable_to_subtree", $ou_id ? $ou_id : $external_id, "update");
164 } else {
165 $tree->moveTree($ref_id, $parent_ref_id);
166 }
167 } catch (Exception $e) {
168 global $DIC;
169 $ilLog = $DIC['ilLog'];
170 $this->addWarning("not_movable", $ou_id ? $ou_id : $external_id, "update");
171 $ilLog->write($e->getMessage() . "\\n" . $e->getTraceAsString());
172 error_log($e->getMessage() . "\\n" . $e->getTraceAsString());
173 }
174 }
175 }
176}
An exception for terminatinating execution or to throw for unit testing.
Class ilObjOrgUnit.
static getRootOrgRefId()
static _hasUntrashedReference($a_obj_id)
checks wether an object has at least one reference that is not in trash
static _lookupObjIdByImportId($a_import_id)
static _lookupType($a_id, $a_reference=false)
lookup object type
Class ilOrgUnitImporter.
hasMoreThanOneMatch($external_id)
addError($lang_var, $import_id, $action=null)
addWarning($lang_var, $import_id, $action=null)
Class ilOrgUnitSimpleImport.
simpleImportElement(SimpleXMLElement $o)
moveObject($ref_id, $parent_ref_id, $ou_id, $external_id)
Repository Utilities (application layer, put GUI related stuff into ilRepUtilGUI)
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
global $DIC
Definition: goto.php:24
$ilUser
Definition: imgupload.php:18
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$attributes
Definition: metadata.php:231
$xml
Definition: metadata.php:332
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$lng