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