ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilECSCmsTreeCommandQueueHandler.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 {
25  private ilLogger $log;
26 
27  private ?\ilECSSetting $server = null;
28  private int $mid = 0;
29 
30  public function __construct(ilECSSetting $server)
31  {
32  global $DIC;
33  $this->log = $DIC->logger()->wsrv();
34 
35  $this->server = $server;
36  $this->init();
37  }
38 
42  public function getServer(): ilECSSetting
43  {
44  return $this->server;
45  }
46 
47 
51  public function handleCreate(ilECSSetting $server, $a_content_id): bool
52  {
53  $this->log->debug('ECS cms tree create');
54 
55 
56  try {
57  $dir_reader = new ilECSDirectoryTreeConnector($this->getServer());
58  $res = $dir_reader->getDirectoryTree($a_content_id);
59  $nodes = $res->getResult();
60 
61  if ($this->log->isHandling(ilLogLevel::DEBUG)) {
62  $this->log->dump($nodes, ilLogLevel::DEBUG);
63  }
64  } catch (ilECSConnectorException $e) {
65  $this->log->error('Tree creation failed with mesage ' . $e->getMessage());
66  return false;
67  }
68 
69  $cms_tree = $nodes;
70  $data = new ilECSCmsData();
71  $data->setServerId($server->getServerId());
72  $data->setMid($this->mid);
73  $data->setCmsId($cms_tree->rootID);
74  $data->setTreeId($a_content_id);
75  $data->setTitle($cms_tree->directoryTreeTitle);
76  $data->setTerm($cms_tree->term);
77  $data->save();
78 
79  $tree = new ilECSCmsTree($a_content_id);
80  $tree->insertRootNode($a_content_id, $data->getObjId());
81  $tree->setRootId($data->getObjId());
82 
83 
84  foreach ((array) $cms_tree->nodes as $node) {
85  // Add data entry
86  $data = new ilECSCmsData();
87  $data->setServerId($this->getServer()->getServerId());
88  $data->setMid($this->mid);
89  $data->setCmsId($node->id);
90  $data->setTreeId($a_content_id);
91  $data->setTitle($node->title);
92  $data->setTerm($node->term);
93  $data->save();
94 
95  // add to tree
96  if ($node->parent->id) {
97  $parent_id = ilECSCmsData::lookupObjId(
98  $this->getServer()->getServerId(),
99  $this->mid,
100  $a_content_id,
101  $node->parent->id
102  );
103  $tree->insertNode($data->getObjId(), $parent_id);
104  }
105  }
106  return true;
107  }
108 
112  public function handleDelete(ilECSSetting $server, int $a_content_id): bool
113  {
114  $this->log->debug('ECS cms tree delete');
115 
116  $data = new ilECSCmsData();
117  $data->setServerId($this->getServer()->getServerId());
118  $data->setMid($this->mid);
119  $data->setTreeId($a_content_id);
120  $data->deleteTree();
121 
122  $tree = new ilECSCmsTree($a_content_id);
123  $tree->deleteTree($tree->getNodeData(ilECSCmsTree::lookupRootId($a_content_id)));
124 
126  $this->getServer()->getServerId(),
127  $this->mid,
128  $a_content_id
129  );
130  return true;
131  }
132 
136  public function handleUpdate(ilECSSetting $server, $a_content_id): bool
137  {
138  $this->log->debug('ECS cms tree update');
139 
140 
141  // 1) Mark all nodes as deleted
142  // 2a) Delete cms tree
143  // 2) Add cms tree table entries
144  // 2) Replace the cms data table entries
145  // 3) Insert deleted tree nodes in tree
146  // 4) Sync tree
147 
148  try {
149  $dir_reader = new ilECSDirectoryTreeConnector($this->getServer());
150  $res = $dir_reader->getDirectoryTree($a_content_id);
151  $nodes = $res->getResult();
152  if ($this->log->isHandling(ilLogLevel::DEBUG)) {
153  $this->log->dump($nodes, ilLogLevel::DEBUG);
154  }
155  } catch (ilECSConnectorException $e) {
156  $this->log->error('Tree creation failed with message ' . $e->getMessage());
157  return false;
158  }
159 
160  // read old tree structure
161  $tree = new ilECSCmsTree($a_content_id);
162 
163  $root_node = $tree->getNodeData(ilECSCmsTree::lookupRootId($a_content_id));
164 
165  $old_nodes = array();
166  if ($root_node['child']) {
167  $old_nodes = $tree->getSubTree($root_node, true);
168  }
169 
170  if ($this->log->isHandling(ilLogLevel::DEBUG)) {
171  $this->log->debug('Old tree data... ');
172  $this->log->dump($old_nodes, ilLogLevel::DEBUG);
173  }
174 
175  // Delete old cms tree
176  ilECSCmsTree::deleteByTreeId($a_content_id);
177 
178  // Mark all nodes in cms data as deleted
180  $this->getServer()->getServerId(),
181  $this->mid,
182  $a_content_id,
183  true
184  );
185 
186  // Check for update or new entry
187  $cms_tree = $nodes;
188 
189  $data_obj_id = ilECSCmsData::lookupObjId(
190  $this->getServer()->getServerId(),
191  $this->mid,
192  $a_content_id,
193  $cms_tree->rootID
194  );
195 
196  $data = new ilECSCmsData($data_obj_id);
197  $data->setServerId($server->getServerId());
198  $data->setMid($this->mid);
199  $data->setCmsId($cms_tree->rootID);
200  $data->setTreeId($a_content_id);
201  $data->setTitle($cms_tree->directoryTreeTitle);
202  $data->setTerm($cms_tree->term);
203 
204  if ($data_obj_id) {
205  $data->setDeleted(false);
206  $data->update();
207  } else {
208  $data->save();
209  }
210 
211  $tree->insertRootNode($a_content_id, $data->getObjId());
212  $tree->setRootId($data->getObjId());
213 
214 
215  foreach ((array) $cms_tree->nodes as $node) {
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  $data->update();
231  } else {
232  $data->setCmsId($node->id);
233  $data->setMid($this->mid);
234  $data->setServerId($this->getServer()->getServerId());
235  $data->setTreeId($a_content_id);
236  $data->setDeleted(false);
237  $data->save();
238 
239  $data_obj_id = $data->getObjId();
240  }
241 
242  // add to tree
243  $parent_id = ilECSCmsData::lookupObjId(
244  $this->getServer()->getServerId(),
245  $this->mid,
246  $a_content_id,
247  $node->parent->id
248  );
249  $tree->insertNode($data->getObjId(), $parent_id);
250  }
251 
252  // Insert deleted nodes in tree
254  $this->getServer()->getServerId(),
255  $this->mid,
256  $a_content_id
257  );
258 
259  foreach ($deleted as $obj_id) {
260  $parent = 0;
261  foreach ($old_nodes as $node) {
262  if ($node['child'] === $obj_id) {
263  $parent = $node['parent'];
264  break;
265  }
266  }
267 
268  if ($parent && $tree->isInTree($parent)) {
269  $tree->insertNode($obj_id, $parent);
270  }
271  }
272 
273  // Sync tree
274  $sync = new ilECSCmsTreeSynchronizer(
275  $this->getServer(),
276  $this->mid,
277  $a_content_id
278  );
279  $sync->sync();
280 
281  return true;
282  }
283 
287  private function init(): void
288  {
289  $this->mid = ilECSParticipantSettings::getInstanceByServerId($this->getServer()->getServerId())->lookupCmsMid();
290  }
291 }
$res
Definition: ltiservices.php:69
static writeAllDeleted($a_server_id, $a_mid, $a_tree_id, $a_deleted_flag)
Write deleted status.
handleDelete(ilECSSetting $server, int $a_content_id)
Handle delete.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
getServerId()
Get current server id.
static lookupRootId($a_tree_id)
lookup root id
handleUpdate(ilECSSetting $server, $a_content_id)
Handle update.
static lookupObjId(int $a_server_id, int $a_mid, int $a_tree_id, string $cms_id)
static deleteMappings(int $a_server_id, int $a_mid, int $a_tree_id)
Delete mappings.
Interface for all command queue handler classes.
static findDeletedNodes(int $a_server_id, int $a_mid, int $a_tree_id)
Find deleted nodes Uses a left join since this is more robust.
global $DIC
Definition: feed.php:28
handleCreate(ilECSSetting $server, $a_content_id)
static deleteByTreeId(int $a_tree_id)
Delete tree by tree_id.