ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  {
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['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['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 $tree;
141 
142  // Check if node is already imported at location "parent_ref_id"
143  // If not => move it
144  $cms_data = new ilECSCmsData($a_tnode_id);
145 
146  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
147  $import_obj_id = ilECSImport::lookupObjIdByContentId(
148  $this->getServer()->getServerId(),
149  $this->mid,
150  $cms_data->getCmsId()
151  );
152  if (!$import_obj_id) {
153  $GLOBALS['ilLog']->write(__METHOD__ . ': cms tree node not imported. tnode_id: ' . $a_tnode_id);
154  return false;
155  }
156 
157  $GLOBALS['ilLog']->write(__METHOD__ . ' parent ref:' . $a_parent_ref_id . ' tnode:' . $a_tnode_id);
158  $ref_ids = ilObject::_getAllReferences($import_obj_id);
159  $import_ref_id = end($ref_ids);
160  $import_ref_id_parent = $tree->getParentId($import_ref_id);
161 
162  if ($a_parent_ref_id != $import_ref_id_parent) {
163  // move node
164  $GLOBALS['ilLog']->write(__METHOD__ . ': Moving node ' . $a_parent_ref_id . ' to ' . $import_ref_id);
165  $tree->moveTree($import_ref_id, $a_parent_ref_id);
166  }
167 
168  // iterate through childs
169  $childs = $this->getTree()->getChilds($a_tnode_id);
170  foreach ((array) $childs as $node) {
171  $this->handleTreeUpdate($import_ref_id, $node['child']);
172  }
173  return true;
174  }
175 
181  protected function syncNode($tree_obj_id, $parent_id, $a_mapped = false)
182  {
183  $childs = $this->getTree()->getChilds($tree_obj_id);
184 
185  $assignment = new ilECSNodeMappingAssignment(
186  $this->getServer()->getServerId(),
187  $this->mid,
188  $this->tree_id,
189  $tree_obj_id
190  );
191 
192  if ($assignment->getRefId()) {
193  $parent_id = $assignment->getRefId();
194  }
195 
196  // information for deeper levels
197  if ($assignment->isMapped()) {
198  $a_mapped = true;
199  }
200 
201  if ($a_mapped) {
202  $parent_id = $this->syncCategory($assignment, $parent_id);
203  }
204 
205  // this is not necessary
206  #if($parent_id)
207  {
208  // iterate through childs
209  foreach ($childs as $node) {
210  $this->syncNode($node['child'], $parent_id, $a_mapped);
211  }
212  }
213  return true;
214  }
215 
220  protected function syncCategory(ilECSNodeMappingAssignment $ass, $parent_id)
221  {
222  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
223  $data = new ilECSCmsData($ass->getCSId());
224 
225  // Check if node is imported => create
226  // perform title update
227  // perform position update
228  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
230  $this->getServer()->getServerId(),
231  $this->mid,
232  $data->getCmsId()
233  );
234  if ($obj_id) {
235  $refs = ilObject::_getAllReferences($obj_id);
236  $ref_id = end($refs);
237 
238 
239  $cat = ilObjectFactory::getInstanceByRefId($ref_id, false);
240  if (($cat instanceof ilObject) and $this->default_settings['title_update']) {
241  $GLOBALS['ilLog']->write(__METHOD__ . ': Updating cms category ');
242  $GLOBALS['ilLog']->write(__METHOD__ . ': Title is ' . $data->getTitle());
243  $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
244  $cat->addTranslation(
245  $data->getTitle(),
246  $cat->getLongDescription(),
247  $GLOBALS['lng']->getDefaultLanguage(),
248  1
249  );
250  $cat->setTitle($data->getTitle());
251  $cat->update();
252  } else {
253  $GLOBALS['ilLog']->write(__METHOD__ . ': Updating cms category -> nothing to do');
254  }
255  return $ref_id;
256  } elseif ($this->getGlobalSettings()->isEmptyContainerCreationEnabled()) {
257  $GLOBALS['ilLog']->write(__METHOD__ . ': Creating new cms category');
258 
259  // Create category
260  include_once './Modules/Category/classes/class.ilObjCategory.php';
261  $cat = new ilObjCategory();
262  $cat->setOwner(SYSTEM_USER_ID);
263  $cat->setTitle($data->getTitle());
264  $cat->create(); // true for upload
265  $cat->createReference();
266  $cat->putInTree($parent_id);
267  $cat->setPermissions($parent_id);
268  $cat->deleteTranslation($GLOBALS['lng']->getDEfaultLanguage());
269  $cat->addTranslation(
270  $data->getTitle(),
271  $cat->getLongDescription(),
272  $GLOBALS['lng']->getDefaultLanguage(),
273  1
274  );
275 
276  // set imported
277  $import = new ilECSImport(
278  $this->getServer()->getServerId(),
279  $cat->getId()
280  );
281  $import->setMID($this->mid);
282  $import->setContentId($data->getCmsId());
283  $import->setImported(true);
284  $import->save();
285 
286  return $cat->getRefId();
287  } else {
288  $GLOBALS['ilLog']->write(__METHOD__ . ': Creation of empty containers is disabled.');
289  return 0;
290  }
291  }
292 }
static lookupRootId($a_tree_id)
lookup root id
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
__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.
Create styles array
The data for the language used.
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