ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 */
3require_once("./Modules/OrgUnit/classes/class.ilOrgUnitImporter.php");
12
13 public function simpleImport($file_path){
14 global $lng;
15 $this->stats = array("created" => 0, "updated" => 0, "deleted" => 0);
16 $a = file_get_contents($file_path, "r");
17 $xml = new SimpleXMLElement($a);
18
19 if(!count($xml->OrgUnit)) {
20 $this->addError("no_orgunit",null,null);
21 return;
22 }
23
24 foreach($xml->OrgUnit as $o){
25 $this->simpleImportElement($o);
26 }
27 }
28
29 public function simpleImportElement(SimpleXMLElement $o){
30 global $tree, $tpl, $ilUser;
31 $title = $o->title;
32 $description = $o->description;
33 $external_id = $o->external_id;
34 $create_mode = true;
35 $attributes = $o->attributes();
36 $action = (string)$attributes->action;
37 $ou_id = (string)$attributes->ou_id;
38 $ou_id_type = (string)$attributes->ou_id_type;
39 $ou_parent_id = (string)$attributes->ou_parent_id;
40 $ou_parent_id_type = (string)$attributes->ou_parent_id_type;
41
42 if($ou_id == ilObjOrgUnit::getRootOrgRefId()){
43 $this->addWarning("cannot_change_root_node", $ou_id?$ou_id:$external_id, $action);
44 return;
45 }
46
47 if($ou_parent_id == "__ILIAS"){
48 $ou_parent_id = ilObjOrgUnit::getRootOrgRefId();
49 $ou_parent_id_type = "reference_id";
50 }
51
52 $ref_id = $this->buildRef($ou_id, $ou_id_type);
53 $parent_ref_id = $this->buildRef($ou_parent_id, $ou_parent_id_type);
54
55 if($action == "delete"){
56 if(!$parent_ref_id){
57 $this->addError("ou_parent_id_not_valid", $ou_id?$ou_id:$external_id, $action);
58 return;
59 }
60 if(!$ref_id){
61 $this->addError("ou_id_not_valid", $ou_id?$ou_id:$external_id, $action);
62 return;
63 }
64 include_once("./Services/Repository/classes/class.ilRepUtil.php");
65 $ru = new ilRepUtil($this);
66 try{
67 $ru->deleteObjects($parent_ref_id, array($ref_id)) !== false;
68 $this->stats["deleted"]++;
69 }catch(Exception $e){
70 $this->addWarning("orgu_already_deleted", $ou_id?$ou_id:$external_id, $action);
71 }
72 return;
73 }elseif($action == "update"){
74 if(!$parent_ref_id){
75 $this->addError("ou_parent_id_not_valid", $ou_id?$ou_id:$external_id, $action);
76 return;
77 }
78 if(!$ref_id){
79 $this->addError("ou_id_not_valid", $ou_id?$ou_id:$external_id, $action);
80 return;
81 }
82 $object = new ilObjOrgUnit($ref_id);
83 $object->setTitle($title);
84
85 $arrTranslations = $object->getTranslations();
86 $object->updateTranslation($title,$description,$ilUser->getLanguage(),"");
87
88 $object->setDescription($description);
89 $object->update();
90 $object->setImportId($external_id);
91 $this->moveObject($ref_id, $parent_ref_id, $ou_id, $external_id);
92
93 $this->stats["updated"]++;
94 }elseif($action == "create"){
95 if(!$parent_ref_id){
96 $this->addError("ou_parent_id_not_valid", $ou_id?$ou_id:$external_id, $action);
97 return;
98 }
99 if($external_id){
100 $obj_id = ilObject::_lookupObjIdByImportId($external_id);
101 if(ilObject::_hasUntrashedReference($obj_id) && ilObject::_lookupType($obj_id) == 'orgu')
102 {
103 $this->addError("ou_external_id_exists", $ou_id?$ou_id:$external_id, $action);
104 return;
105 }
106 }
107 $object = new ilObjOrgUnit();
108 $object->setTitle($title);
109 $object->setDescription($description);
110 $object->setImportId($external_id);
111 $object->create();
112 $object->createReference();
113 $object->putInTree($parent_ref_id);
114 $object->setPermissions($ou_parent_id);
115 $this->stats["created"]++;
116 }else{
117 $this->addError("no_valid_action_given", $ou_id, $action);
118 }
119 }
120
127 protected function moveObject($ref_id, $parent_ref_id, $ou_id, $external_id) {
128 global $tree;
129 if($parent_ref_id != $tree->getParentId($ref_id)){
130 try{
131 $path = $tree->getPathId($parent_ref_id);
132 if(in_array($ref_id, $path)) {
133 $this->addWarning("not_movable_to_subtree", $ou_id?$ou_id:$external_id, "update");
134 } else {
135 $tree->moveTree($ref_id, $parent_ref_id);
136 }
137 }catch(Exception $e){
138 global $ilLog;
139 $this->addWarning("not_movable", $ou_id?$ou_id:$external_id, "update");
140 $ilLog->write($e->getMessage()."\\n".$e->getTraceAsString());
141 error_log($e->getMessage()."\\n".$e->getTraceAsString());
142 }
143 }
144 }
145}
146?>
global $tpl
Definition: ilias.php:8
Class ilObjOrgUnit.
static getRootOrgRefId()
_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.
addWarning($lang_var, $import_id, $action=NULL)
addError($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)
global $lng
Definition: privfeed.php:40
$ref_id
Definition: sahs_server.php:39
$path
Definition: index.php:22
global $ilUser
Definition: imgupload.php:15