ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  {
93  $GLOBALS['ilLog']->write(__METHOD__.': No directory allocation settings. Aborting');
94  return true;
95  }
96 
97  // lookup obj id of root node
98  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
99 
100  $root_obj_id = ilECSCmsTree::lookupRootId($this->tree_id);
101  $this->syncNode($root_obj_id,0);
102 
103  // Tree structure is up to date, now check node movements
104  $this->checkTreeUpdates($root_obj_id);
105  return true;
106  }
107 
113  protected function checkTreeUpdates($a_root_obj_id)
114  {
115  if($this->default_settings['tree_update'] == false)
116  {
117  $GLOBALS['ilLog']->write(__METHOD__.': Tree update disabled for tree with id '. $this->getTree()->getTreeId());
118  return false;
119  }
120 
121  // Start recursion
122  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
123  $mapping = new ilECSNodeMappingAssignment(
124  $this->getServer()->getServerId(),
125  $this->mid,
126  $this->tree_id,
127  $a_root_obj_id
128  );
129  $a_root_ref_id = $mapping->getRefId();
130  if($a_root_ref_id)
131  {
132  $this->handleTreeUpdate($a_root_ref_id, $a_root_obj_id);
133  }
134  }
135 
141  protected function handleTreeUpdate($a_parent_ref_id, $a_tnode_id)
142  {
143  global $tree;
144 
145  // Check if node is already imported at location "parent_ref_id"
146  // If not => move it
147  $cms_data = new ilECSCmsData($a_tnode_id);
148 
149  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
150  $import_obj_id = ilECSImport::_lookupObjId(
151  $this->getServer()->getServerId(),
152  $cms_data->getCmsId(),
153  $this->mid);
154  if(!$import_obj_id)
155  {
156  $GLOBALS['ilLog']->write(__METHOD__.': cms tree node not imported. tnode_id: '. $a_tnode_id);
157  return false;
158  }
159 
160  $GLOBALS['ilLog']->write(__METHOD__.' parent ref:'.$a_parent_ref_id.' tnode:'. $a_tnode_id);
161  $ref_ids = ilObject::_getAllReferences($import_obj_id);
162  $import_ref_id = end($ref_ids);
163  $import_ref_id_parent = $tree->getParentId($import_ref_id);
164 
165  if($a_parent_ref_id != $import_ref_id_parent)
166  {
167  // move node
168  $GLOBALS['ilLog']->write(__METHOD__.': Moving node '.$a_parent_ref_id.' to '.$import_ref_id);
169  $tree->moveTree($import_ref_id,$a_parent_ref_id);
170  }
171 
172  // iterate through childs
173  $childs = $this->getTree()->getChilds($a_tnode_id);
174  foreach((array) $childs as $node)
175  {
176  $this->handleTreeUpdate($import_ref_id, $node['child']);
177  }
178  return true;
179  }
180 
186  protected function syncNode($tree_obj_id,$parent_id,$a_mapped = false)
187  {
188  $childs = $this->getTree()->getChilds($tree_obj_id);
189 
190  $assignment = new ilECSNodeMappingAssignment(
191  $this->getServer()->getServerId(),
192  $this->mid,
193  $this->tree_id,
194  $tree_obj_id);
195 
196  if($assignment->getRefId())
197  {
198  $parent_id = $assignment->getRefId();
199  }
200 
201  // information for deeper levels
202  if($assignment->isMapped())
203  {
204  $a_mapped = true;
205  }
206 
207  if($a_mapped)
208  {
209  $parent_id = $this->syncCategory($assignment,$parent_id);
210  }
211 
212  // this is not necessary
213  #if($parent_id)
214  {
215  // iterate through childs
216  foreach($childs as $node)
217  {
218  $this->syncNode($node['child'],$parent_id,$a_mapped);
219  }
220  }
221  return true;
222  }
223 
228  protected function syncCategory(ilECSNodeMappingAssignment $ass,$parent_id)
229  {
230  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
231  $data = new ilECSCmsData($ass->getCSId());
232 
233  // Check if node is imported => create
234  // perform title update
235  // perform position update
236  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
237  $obj_id = ilECSImport::_lookupObjId(
238  $this->getServer()->getServerId(),
239  $data->getCmsId(),
240  $this->mid);
241  if($obj_id)
242  {
243  $refs = ilObject::_getAllReferences($obj_id);
244  $ref_id = end($refs);
245 
246 
248  if(($cat instanceof ilObject) and $this->default_settings['title_update'])
249  {
250  $GLOBALS['ilLog']->write(__METHOD__.': Updating cms category ');
251  $GLOBALS['ilLog']->write(__METHOD__.': Title is '. $data->getTitle());
252  $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
253  $cat->addTranslation(
254  $data->getTitle(),
255  $cat->getLongDescription(),
256  $GLOBALS['lng']->getDefaultLanguage(),
257  1
258  );
259  $cat->setTitle($data->getTitle());
260  $cat->update();
261  }
262  else
263  {
264  $GLOBALS['ilLog']->write(__METHOD__.': Updating cms category -> nothing to do');
265  }
266  return $ref_id;
267  }
268  elseif($this->getGlobalSettings()->isEmptyContainerCreationEnabled())
269  {
270  $GLOBALS['ilLog']->write(__METHOD__.': Creating new cms category');
271 
272  // Create category
273  include_once './Modules/Category/classes/class.ilObjCategory.php';
274  $cat = new ilObjCategory();
275  $cat->setTitle($data->getTitle());
276  $cat->create(); // true for upload
277  $cat->createReference();
278  $cat->putInTree($parent_id);
279  $cat->setPermissions($parent_id);
280  $cat->deleteTranslation($GLOBALS['lng']->getDEfaultLanguage());
281  $cat->addTranslation(
282  $data->getTitle(),
283  $cat->getLongDescription(),
284  $GLOBALS['lng']->getDefaultLanguage(),
285  1
286  );
287 
288  // set imported
289  $import = new ilECSImport(
290  $this->getServer()->getServerId(),
291  $cat->getId()
292  );
293  $import->setMID($this->mid);
294  $import->setEContentId($data->getCmsId());
295  $import->setImported(true);
296  $import->save();
297 
298  return $cat->getRefId();
299  }
300  else
301  {
302  $GLOBALS['ilLog']->write(__METHOD__.': Creation of empty containers is disabled.');
303  return 0;
304  }
305  }
306 }
307 ?>