ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilECSCmsTreeSynchronizer.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
6 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignments.php';
7 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
8 
16 {
17  private $server = null;
18  private $mid = null;
19  private $tree_id = null;
20  private $tree = null;
21 
22  private $default_settings = array();
23  private $global_settings = null;
24 
25 
30  {
31  $this->server = $server;
32  $this->mid = $mid;
33  $this->tree = new ilECSCmsTree($tree_id);
34  $this->tree_id = $tree_id;
35 
36  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingSettings.php';
37  $this->global_settings = ilECSNodeMappingSettings::getInstanceByServerMid($this->getServer()->getServerId(), $this->mid);
38  }
39 
44  public function getServer()
45  {
46  return $this->server;
47  }
48 
52  public function getTree()
53  {
54  return $this->tree;
55  }
56 
61  public function getDefaultSettings()
62  {
63  return (array) $this->default_settings;
64  }
65 
70  public function getGlobalSettings()
71  {
73  }
74 
80  public function sync()
81  {
82  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignments.php';
83  $this->default_settings = ilECSNodeMappingAssignments::lookupSettings(
84  $this->getServer()->getServerId(),
85  $this->mid,
86  $this->tree_id,
87  0
88  );
89 
90  // return if setting is false => no configuration done
91  if (!$this->getDefaultSettings()) {
92  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': No directory allocation settings. Aborting');
93  return true;
94  }
95 
96  // lookup obj id of root node
97  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
98 
99  $root_obj_id = ilECSCmsTree::lookupRootId($this->tree_id);
100  $this->syncNode($root_obj_id, 0);
101 
102  // Tree structure is up to date, now check node movements
103  $this->checkTreeUpdates($root_obj_id);
104  return true;
105  }
106 
112  protected function checkTreeUpdates($a_root_obj_id)
113  {
114  if ($this->default_settings['tree_update'] == false) {
115  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Tree update disabled for tree with id ' . $this->getTree()->getTreeId());
116  return false;
117  }
118 
119  // Start recursion
120  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
121  $mapping = new ilECSNodeMappingAssignment(
122  $this->getServer()->getServerId(),
123  $this->mid,
124  $this->tree_id,
125  $a_root_obj_id
126  );
127  $a_root_ref_id = $mapping->getRefId();
128  if ($a_root_ref_id) {
129  $this->handleTreeUpdate($a_root_ref_id, $a_root_obj_id);
130  }
131  }
132 
138  protected function handleTreeUpdate($a_parent_ref_id, $a_tnode_id)
139  {
140  global $DIC;
141 
142  $tree = $DIC['tree'];
143 
144  // Check if node is already imported at location "parent_ref_id"
145  // If not => move it
146  $cms_data = new ilECSCmsData($a_tnode_id);
147 
148  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
149  $import_obj_id = ilECSImport::lookupObjIdByContentId(
150  $this->getServer()->getServerId(),
151  $this->mid,
152  $cms_data->getCmsId()
153  );
154  if (!$import_obj_id) {
155  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': cms tree node not imported. tnode_id: ' . $a_tnode_id);
156  return false;
157  }
158 
159  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ' parent ref:' . $a_parent_ref_id . ' tnode:' . $a_tnode_id);
160  $ref_ids = ilObject::_getAllReferences($import_obj_id);
161  $import_ref_id = end($ref_ids);
162  $import_ref_id_parent = $tree->getParentId($import_ref_id);
163 
164  if ($a_parent_ref_id != $import_ref_id_parent) {
165  // move node
166  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Moving node ' . $a_parent_ref_id . ' to ' . $import_ref_id);
167  $tree->moveTree($import_ref_id, $a_parent_ref_id);
168  }
169 
170  // iterate through childs
171  $childs = $this->getTree()->getChilds($a_tnode_id);
172  foreach ((array) $childs as $node) {
173  $this->handleTreeUpdate($import_ref_id, $node['child']);
174  }
175  return true;
176  }
177 
183  protected function syncNode($tree_obj_id, $parent_id, $a_mapped = false)
184  {
185  $childs = $this->getTree()->getChilds($tree_obj_id);
186 
187  $assignment = new ilECSNodeMappingAssignment(
188  $this->getServer()->getServerId(),
189  $this->mid,
190  $this->tree_id,
191  $tree_obj_id
192  );
193 
194  if ($assignment->getRefId()) {
195  $parent_id = $assignment->getRefId();
196  }
197 
198  // information for deeper levels
199  if ($assignment->isMapped()) {
200  $a_mapped = true;
201  }
202 
203  if ($a_mapped) {
204  $parent_id = $this->syncCategory($assignment, $parent_id);
205  }
206 
207  // this is not necessary
208  #if($parent_id)
209  {
210  // iterate through childs
211  foreach ($childs as $node) {
212  $this->syncNode($node['child'], $parent_id, $a_mapped);
213  }
214  }
215  return true;
216  }
217 
222  protected function syncCategory(ilECSNodeMappingAssignment $ass, $parent_id)
223  {
224  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
225  $data = new ilECSCmsData($ass->getCSId());
226 
227  // Check if node is imported => create
228  // perform title update
229  // perform position update
230  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
232  $this->getServer()->getServerId(),
233  $this->mid,
234  $data->getCmsId()
235  );
236  if ($obj_id) {
237  $refs = ilObject::_getAllReferences($obj_id);
238  $ref_id = end($refs);
239 
240 
241  $cat = ilObjectFactory::getInstanceByRefId($ref_id, false);
242  if (($cat instanceof ilObject) and $this->default_settings['title_update']) {
243  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Updating cms category ');
244  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Title is ' . $data->getTitle());
245  $cat->deleteTranslation($GLOBALS['DIC']['lng']->getDefaultLanguage());
246  $cat->addTranslation(
247  $data->getTitle(),
248  $cat->getLongDescription(),
249  $GLOBALS['DIC']['lng']->getDefaultLanguage(),
250  1
251  );
252  $cat->setTitle($data->getTitle());
253  $cat->update();
254  } else {
255  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Updating cms category -> nothing to do');
256  }
257  return $ref_id;
258  } elseif ($this->getGlobalSettings()->isEmptyContainerCreationEnabled()) {
259  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Creating new cms category');
260 
261  // Create category
262  include_once './Modules/Category/classes/class.ilObjCategory.php';
263  $cat = new ilObjCategory();
264  $cat->setOwner(SYSTEM_USER_ID);
265  $cat->setTitle($data->getTitle());
266  $cat->create(); // true for upload
267  $cat->createReference();
268  $cat->putInTree($parent_id);
269  $cat->setPermissions($parent_id);
270  $cat->deleteTranslation($GLOBALS['DIC']['lng']->getDEfaultLanguage());
271  $cat->addTranslation(
272  $data->getTitle(),
273  $cat->getLongDescription(),
274  $GLOBALS['DIC']['lng']->getDefaultLanguage(),
275  1
276  );
277 
278  // set imported
279  $import = new ilECSImport(
280  $this->getServer()->getServerId(),
281  $cat->getId()
282  );
283  $import->setMID($this->mid);
284  $import->setContentId($data->getCmsId());
285  $import->setImported(true);
286  $import->save();
287 
288  return $cat->getRefId();
289  } else {
290  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Creation of empty containers is disabled.');
291  return 0;
292  }
293  }
294 }
global $DIC
Definition: saml.php:7
static lookupRootId($a_tree_id)
lookup root id
__construct(ilECSSetting $server, $mid, $tree_id)
Constructor.
static getInstanceByServerMid($a_server_id, $a_mid)
Get instance.
checkTreeUpdates($a_root_obj_id)
Start tree update check.
syncCategory(ilECSNodeMappingAssignment $ass, $parent_id)
Sync category.
static lookupObjIdByContentId($a_server_id, $a_mid, $a_content_id, $a_sub_id=null)
Lookup obj_id by content id.
static _getAllReferences($a_id)
get all reference ids of object
getDefaultSettings()
Get default settings.
Storage of ECS imported objects.
Class ilObjCategory.
handleTreeUpdate($a_parent_ref_id, $a_tnode_id)
Handle tree update (recursively)
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
syncNode($tree_obj_id, $parent_id, $a_mapped=false)
Sync node.
static lookupSettings($a_server_id, $a_mid, $a_tree_id, $a_node_id)
Lookup Settings.
setMID($a_mid)
set mid
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
$data
Definition: bench.php:6