ILIAS  release_8 Revision v8.24
class.ilECSCmsTreeSynchronizer.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
26{
29 private ilTree $tree;
30
31 private ?\ilECSSetting $server = null;
32 private int $mid;
33 private int $tree_id;
34 private ?\ilECSCmsTree $ecs_tree = null;
35
36 private array $default_settings = array();
38
39 public function __construct(ilECSSetting $server, int $mid, int $tree_id)
40 {
41 global $DIC;
42
43 $this->logger = $DIC->logger()->wsrv();
44 $this->lng = $DIC->language();
45 $this->tree = $DIC->repositoryTree();
46
47
48 $this->server = $server;
49 $this->mid = $mid;
50 $this->ecs_tree = new ilECSCmsTree($tree_id);
51 $this->tree_id = $tree_id;
52
53 $this->global_settings = ilECSNodeMappingSettings::getInstanceByServerMid($this->server->getServerId(), $this->mid);
54 }
55
59 public function getServer(): ?\ilECSSetting
60 {
61 return $this->server;
62 }
63
64 public function getECSTree(): ?\ilECSCmsTree
65 {
66 return $this->ecs_tree;
67 }
68
72 public function getDefaultSettings(): array
73 {
75 }
76
81 {
83 }
84
88 public function sync(): bool
89 {
90 $this->default_settings = ilECSNodeMappingAssignments::lookupSettings(
91 $this->server->getServerId(),
92 $this->mid,
93 $this->tree_id,
94 0
95 );
96
97 // return if setting is false => no configuration done
98 if (!$this->getDefaultSettings()) {
99 $this->logger->info('No directory allocation settings. Aborting');
100 return true;
101 }
102
103 // lookup obj id of root node
104
105 $root_obj_id = ilECSCmsTree::lookupRootId($this->tree_id);
106 $this->syncNode($root_obj_id, 0);
107
108 // Tree structure is up to date, now check node movements
109 $this->checkTreeUpdates($root_obj_id);
110 return true;
111 }
112
116 protected function checkTreeUpdates(int $a_root_obj_id): void
117 {
118 if ($this->default_settings['tree_update'] === false) {
119 $this->logger->info('Tree update disabled for tree with id ' . $this->ecs_tree->getTreeId());
120 return;
121 }
122
123 // Start recursion
124 $mapping = new ilECSNodeMappingAssignment(
125 $this->server->getServerId(),
126 $this->mid,
127 $this->tree_id,
128 $a_root_obj_id
129 );
130 $a_root_ref_id = $mapping->getRefId();
131 if ($a_root_ref_id) {
132 $this->handleTreeUpdate($a_root_ref_id, $a_root_obj_id);
133 }
134 }
135
139 protected function handleTreeUpdate(int $a_parent_ref_id, int $a_tnode_id): bool
140 {
141 // Check if node is already imported at location "parent_ref_id"
142 // If not => move it
143 $cms_data = new ilECSCmsData($a_tnode_id);
144
145 $import_obj_id = ilECSImportManager::getInstance()->lookupObjIdByContentId(
146 $this->server->getServerId(),
147 $this->mid,
148 //TODO fix this cast
149 (int) $cms_data->getCmsId()
150 );
151 if (!$import_obj_id) {
152 $this->logger->error('cms tree node not imported. tnode_id: ' . $a_tnode_id);
153 return false;
154 }
155
156 $this->logger->info(' parent ref:' . $a_parent_ref_id . ' tnode:' . $a_tnode_id);
157 $ref_ids = ilObject::_getAllReferences($import_obj_id);
158 $import_ref_id = end($ref_ids);
159 $import_ref_id_parent = $this->tree->getParentId($import_ref_id);
160
161 if ($a_parent_ref_id !== $import_ref_id_parent) {
162 // move node
163 $this->logger->info('Moving node ' . $a_parent_ref_id . ' to ' . $import_ref_id);
164 $this->tree->moveTree($import_ref_id, $a_parent_ref_id);
165 }
166
167 // iterate through childs
168 $childs = $this->ecs_tree->getChilds($a_tnode_id);
169 foreach ($childs as $node) {
170 $this->handleTreeUpdate($import_ref_id, $node['child']);
171 }
172 return true;
173 }
174
178 protected function syncNode($tree_obj_id, $parent_id, $a_mapped = false): bool
179 {
180 $childs = $this->ecs_tree->getChilds($tree_obj_id);
181
182 $assignment = new ilECSNodeMappingAssignment(
183 $this->server->getServerId(),
184 $this->mid,
185 $this->tree_id,
186 $tree_obj_id
187 );
188
189 if ($assignment->getRefId()) {
190 $parent_id = $assignment->getRefId();
191 }
192
193 // information for deeper levels
194 if ($assignment->isMapped()) {
195 $a_mapped = true;
196 }
197
198 if ($a_mapped) {
199 $parent_id = $this->syncCategory($assignment, $parent_id);
200 }
201
202 // this is not necessary
203 #if($parent_id)
204 {
205 // iterate through childs
206 foreach ($childs as $node) {
207 $this->syncNode($node['child'], $parent_id, $a_mapped);
208 }
209 }
210 return true;
211 }
212
216 protected function syncCategory(ilECSNodeMappingAssignment $ass, $parent_id)
217 {
218 $data = new ilECSCmsData($ass->getCSId());
219
220 // Check if node is imported => create
221 // perform title update
222 // perform position update
223 $obj_id = ilECSImportManager::getInstance()->lookupObjIdByContentId(
224 $this->server->getServerId(),
225 $this->mid,
226 //TODO fix this cast
227 (int) $data->getCmsId()
228 );
229 if ($obj_id) {
230 $refs = ilObject::_getAllReferences($obj_id);
231 $ref_id = end($refs);
232
233
235 if (($cat instanceof ilObject) && $this->default_settings['title_update']) {
236 $this->logger->info('Updating cms category ');
237 $this->logger->info('Title is ' . $data->getTitle());
238 $cat->deleteTranslation($this->lng->getDefaultLanguage());
239 $cat->addTranslation(
240 $data->getTitle(),
241 $cat->getLongDescription(),
242 $this->lng->getDefaultLanguage(),
243 $this->lng->getDefaultLanguage()
244 );
245 $cat->setTitle($data->getTitle());
246 $cat->update();
247 } else {
248 $this->logger->info('Updating cms category -> nothing to do');
249 }
250 return $ref_id;
251 }
252
253 if ($this->global_settings->isEmptyContainerCreationEnabled()) {
254 $this->logger->info('Creating new cms category');
255
256 // Create category
257 $cat = new ilObjCategory();
258 $cat->setOwner(SYSTEM_USER_ID);
259 $cat->setTitle($data->getTitle());
260 $cat->create(); // true for upload
261 $cat->createReference();
262 $cat->putInTree($parent_id);
263 $cat->setPermissions($parent_id);
264 $cat->deleteTranslation($this->lng->getDEfaultLanguage());
265 $cat->addTranslation(
266 $data->getTitle(),
267 $cat->getLongDescription(),
268 $this->lng->getDefaultLanguage(),
269 $this->lng->getDefaultLanguage()
270 );
271
272 // set imported
273 $import = new ilECSImport(
274 $this->server->getServerId(),
275 $cat->getId()
276 );
277 $import->setMID($this->mid);
278 $import->setContentId($data->getCmsId());
279 $import->setImported(true);
280 $import->save();
281
282 return $cat->getRefId();
283 }
284
285 $this->logger->info('Creation of empty containers is disabled.');
286 return 0;
287 }
288}
checkTreeUpdates(int $a_root_obj_id)
Start tree update check.
handleTreeUpdate(int $a_parent_ref_id, int $a_tnode_id)
Handle tree update (recursively)
syncCategory(ilECSNodeMappingAssignment $ass, $parent_id)
Sync category.
__construct(ilECSSetting $server, int $mid, int $tree_id)
ilECSNodeMappingSettings $global_settings
syncNode($tree_obj_id, $parent_id, $a_mapped=false)
Sync node.
static lookupRootId($a_tree_id)
lookup root id
static getInstance()
Get the singleton instance of this ilECSImportManager.
Storage of ECS imported objects.
static lookupSettings(int $a_server_id, int $a_mid, int $a_tree_id, int $a_node_id)
Lookup Settings.
static getInstanceByServerMid(int $a_server_id, int $a_mid)
Get instance.
language handling
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67