ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSCmsTreeCommandQueueHandler.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/interfaces/interface.ilECSCommandQueueHandler.php';
6 
13 {
14  private $server = null;
15  private $mid = 0;
16 
17 
21  public function __construct(ilECSSetting $server)
22  {
23  $this->server = $server;
24  $this->init();
25  }
26 
31  public function getServer()
32  {
33  return $this->server;
34  }
35 
36 
42  public function handleCreate(ilECSSetting $server, $a_content_id)
43  {
44  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
45  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
46  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSDirectoryTreeConnector.php';
47 
48  try
49  {
50  $dir_reader = new ilECSDirectoryTreeConnector($this->getServer());
51  $res = $dir_reader->getDirectoryTree($a_content_id);
52  $nodes = $res->getResult();
53  }
54  catch(ilECSConnectorException $e)
55  {
56  $GLOBALS['ilLog']->write(__METHOD__.': Tree creation failed with mesage ' . $e->getMessage());
57  return false;
58  }
59 
60  $cms_tree = $nodes;
61  $data = new ilECSCmsData();
62  $data->setServerId($server->getServerId());
63  $data->setMid($this->mid);
64  $data->setCmsId($cms_tree->rootID);
65  $data->setTreeId($a_content_id);
66  $data->setTitle($cms_tree->directoryTreeTitle);
67  $data->setTerm($cms_tree->term);
68  $data->save();
69 
70  $tree = new ilECSCmsTree($a_content_id);
71  $tree->insertRootNode($a_content_id, $data->getObjId());
72  $tree->setRootId($data->getObjId());
73 
74 
75  foreach((array) $cms_tree->nodes as $node)
76  {
77  // Add data entry
78  $data = new ilECSCmsData();
79  $data->setServerId($this->getServer()->getServerId());
80  $data->setMid($this->mid);
81  $data->setCmsId($node->id);
82  $data->setTreeId($a_content_id);
83  $data->setTitle($node->title);
84  $data->setTerm($node->term);
85  $data->save();
86 
87  // add to tree
88  if($node->parent->id)
89  {
90  $parent_id = ilECSCmsData::lookupObjId(
91  $this->getServer()->getServerId(),
92  $this->mid,
93  $a_content_id,
94  (int) $node->parent->id
95  );
96  $tree->insertNode($data->getObjId(), $parent_id);
97  }
98  }
99  return true;
100  }
101 
107  public function handleDelete(ilECSSetting $server, $a_content_id)
108  {
109  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
110  $data = new ilECSCmsData();
111  $data->setServerId($this->getServer()->getServerId());
112  $data->setMid($this->mid);
113  $data->setTreeId($a_content_id);
114  $data->deleteTree();
115 
116  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
117  $tree = new ilECSCmsTree($a_content_id);
118  $tree->deleteTree($tree->getNodeData(ilECSCmsTree::lookupRootId($a_content_id)));
119 
120  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignments.php';
122  $this->getServer()->getServerId(),
123  $this->mid,
124  $a_content_id
125  );
126  return true;
127  }
128 
134  public function handleUpdate(ilECSSetting $server, $a_content_id)
135  {
136  // 1) Mark all nodes as deleted
137  // 2a) Delete cms tree
138  // 2) Add cms tree table entries
139  // 2) Replace the cms data table entries
140  // 3) Insert deleted tree nodes in tree
141  // 4) Sync tree
142 
143  try
144  {
145  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSDirectoryTreeConnector.php';
146  $dir_reader = new ilECSDirectoryTreeConnector($this->getServer());
147  $res = $dir_reader->getDirectoryTree($a_content_id);
148  $nodes = $res->getResult();
149  }
150  catch(ilECSConnectorException $e)
151  {
152  $GLOBALS['ilLog']->write(__METHOD__.': Tree creation failed with mesage ' . $e->getMessage());
153  return false;
154  }
155 
156  // read old tree structure
157  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
158  $tree = new ilECSCmsTree($a_content_id);
159 
160  $root_node = $tree->getNodeData(ilECSCmsTree::lookupRootId($a_content_id));
161 
162  $old_nodes = array();
163  if($root_node['child'])
164  {
165  $old_nodes = $tree->getSubTree($root_node,true);
166  }
167 
168  //$GLOBALS['ilLog']->write(__METHOD__.': OLD TREE DATA ........'.print_r($old_nodes,true));
169 
170  // Delete old cms tree
171  ilECSCmsTree::deleteByTreeId($a_content_id);
172 
173  // Mark all nodes in cms data as deleted
174  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
176  $this->getServer()->getServerId(),
177  $this->mid,
178  $a_content_id,
179  true
180  );
181 
182  // Check for update or new entry
183  $cms_tree = $nodes;
184 
185  $data_obj_id = ilECSCmsData::lookupObjId(
186  $this->getServer()->getServerId(),
187  $this->mid,
188  $a_content_id,
189  $cms_tree->rootID
190  );
191 
192  $data = new ilECSCmsData($data_obj_id);
193  $data->setServerId($server->getServerId());
194  $data->setMid($this->mid);
195  $data->setCmsId($cms_tree->rootID);
196  $data->setTreeId($a_content_id);
197  $data->setTitle($cms_tree->directoryTreeTitle);
198  $data->setTerm($cms_tree->term);
199 
200  if($data_obj_id)
201  {
202  $data->setDeleted(false);
203  $data->update();
204  }
205  else
206  {
207  $data->save();
208  }
209 
210  $tree->insertRootNode($a_content_id, $data->getObjId());
211  $tree->setRootId($data->getObjId());
212 
213 
214  foreach((array) $cms_tree->nodes as $node)
215  {
216  $data_obj_id = ilECSCmsData::lookupObjId(
217  $this->getServer()->getServerId(),
218  $this->mid,
219  $a_content_id,
220  $node->id
221  );
222 
223  // update data entry
224  $data = new ilECSCmsData($data_obj_id);
225  $data->setTitle($node->title);
226  $data->setTerm($node->term);
227  $data->setDeleted(false);
228 
229  if($data_obj_id)
230  {
231  $data->update();
232  }
233  else
234  {
235  $data->setCmsId($node->id);
236  $data->setMid($this->mid);
237  $data->setServerId($this->getServer()->getServerId());
238  $data->setTreeId($a_content_id);
239  $data->setDeleted(false);
240  $data->save();
241 
242  $data_obj_id = $data->getObjId();
243  }
244 
245  // add to tree
246  $parent_id = ilECSCmsData::lookupObjId(
247  $this->getServer()->getServerId(),
248  $this->mid,
249  $a_content_id,
250  (int) $node->parent->id
251  );
252  $tree->insertNode($data->getObjId(), $parent_id);
253  }
254 
255  // Insert deleted nodes in tree
257  $this->getServer()->getServerId(),
258  $this->mid,
259  $a_content_id
260  );
261 
262  foreach((array) $deleted as $obj_id)
263  {
264  $parent = 0;
265  foreach((array) $old_nodes as $tmp_id => $node)
266  {
267 
268  if($node['child'] == $obj_id)
269  {
270  $parent = $node['parent'];
271  break;
272  }
273  }
274 
275  if($tree->isInTree($parent) and $parent)
276  {
277  $tree->insertNode($obj_id, $parent);
278  }
279  }
280 
281  // Sync tree
282  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTreeSynchronizer.php';
283  $sync = new ilECSCmsTreeSynchronizer(
284  $this->getServer(),
285  $this->mid,
286  $a_content_id
287  );
288  $sync->sync();
289 
290  return true;
291  }
292 
296  private function init()
297  {
298  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
299  $this->mid = ilECSParticipantSettings::loookupCmsMid($this->getServer()->getServerId());
300  }
301 }
302 ?>