ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCopyWizardOptions.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
28  private static array $instances = [];
29 
30  public const COPY_WIZARD_UNDEFINED = 0;
31  public const COPY_WIZARD_OMIT = 1;
32  public const COPY_WIZARD_COPY = 2;
33  public const COPY_WIZARD_LINK = 3;
34  public const COPY_WIZARD_LINK_TO_TARGET = 4;
35 
36  protected const OWNER_KEY = -3;
37  protected const DISABLE_SOAP = -4;
38  protected const ROOT_NODE = -5;
39  protected const DISABLE_TREE_COPY = -6;
40 
41  protected ilDBInterface $db;
42  protected ilTree $tree;
43 
44  private int $copy_id;
45  private array $options = [];
46  private array $tmp_tree = [];
47 
51  private function __construct(int $a_copy_id = 0)
52  {
53  global $DIC;
54 
55  $this->db = $DIC->database();
56  $this->tree = $DIC->repositoryTree();
57  $this->copy_id = $a_copy_id;
58 
59  if ($this->copy_id) {
60  $this->read();
61  }
62  }
63 
64  public static function _getInstance(int $a_copy_id): ilCopyWizardOptions
65  {
66  if (isset(self::$instances[$a_copy_id])) {
67  return self::$instances[$a_copy_id];
68  }
69  return self::$instances[$a_copy_id] = new ilCopyWizardOptions($a_copy_id);
70  }
71 
72  public function getRequiredSteps(): int
73  {
74  $steps = 0;
75  if (array_key_exists(0, $this->options) && is_array($this->options[0])) {
76  $steps += count($this->options[0]);
77  }
78  if (array_key_exists(-1, $this->options) && is_array($this->options[-1])) {
79  $steps += count($this->options[-1]);
80  }
81  return $steps;
82  }
83 
84  public static function _isFinished(int $a_copy_id): bool
85  {
86  global $DIC;
87 
88  $ilDB = $DIC->database();
89 
90  $query = "SELECT * FROM copy_wizard_options " .
91  "WHERE copy_id = " . $ilDB->quote($a_copy_id, 'integer') . " ";
92  $res = $ilDB->query($query);
93  return !$res->numRows();
94  }
95 
100  public static function _allocateCopyId(): int
101  {
102  global $DIC;
103 
104  $ilDB = $DIC->database();
105  $query = "SELECT MAX(copy_id) latest FROM copy_wizard_options ";
106  $res = $ilDB->query($query);
107  $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
108 
109  $ilDB->insert("copy_wizard_options", array(
110  "copy_id" => array("integer", ((int) $row->latest) + 1),
111  "source_id" => array("integer", 0)
112  ));
113  return ((int) $row->latest) + 1;
114  }
115 
119  public function saveOwner(int $a_user_id): void
120  {
121  global $DIC;
122 
123  $ilDB = $DIC->database();
124  $ilDB->insert("copy_wizard_options", array(
125  "copy_id" => array("integer", $this->getCopyId()),
126  "source_id" => array("integer", self::OWNER_KEY),
127  "options" => array('clob', serialize(array($a_user_id)))
128  ));
129  }
130 
134  public function saveRoot(int $a_root): void
135  {
136  global $DIC;
137 
138  $ilDB = $DIC['ilDB'];
139 
140  $ilDB->insert("copy_wizard_options", array(
141  "copy_id" => array("integer", $this->getCopyId()),
142  "source_id" => array("integer", self::ROOT_NODE),
143  "options" => array('clob', serialize(array($a_root)))
144  ));
145  }
146 
152  public function isRootNode(int $a_root): bool
153  {
154  return in_array($a_root, $this->getOptions(self::ROOT_NODE));
155  }
156 
157  public function getRootNode(): ?int
158  {
159  $options = $this->getOptions(self::ROOT_NODE);
160  if (!is_array($options) || !array_key_exists(0, $options)) {
161  return null;
162  }
163  return (int) $options[0];
164  }
165 
169  public function disableSOAP(): void
170  {
171  $this->options[self::DISABLE_SOAP] = 1;
172 
173  $this->db->insert("copy_wizard_options", array(
174  "copy_id" => array("integer", $this->getCopyId()),
175  "source_id" => array("integer", self::DISABLE_SOAP),
176  "options" => array('clob', serialize(array(1)))
177  ));
178  }
179 
184  public function disableTreeCopy(): void
185  {
186  $this->options[self::DISABLE_TREE_COPY] = 1;
187 
188  $this->db->insert("copy_wizard_options", array(
189  "copy_id" => array("integer", $this->getCopyId()),
190  "source_id" => array("integer", self::DISABLE_TREE_COPY),
191  "options" => array('clob', serialize(array(1)))
192  ));
193  }
194 
198  public function isTreeCopyDisabled(): bool
199  {
200  if (isset($this->options[self::DISABLE_TREE_COPY]) && $this->options[self::DISABLE_TREE_COPY]) {
201  return true;
202  }
203  return false;
204  }
205 
209  public function isSOAPEnabled(): bool
210  {
211  if (isset($this->options[self::DISABLE_SOAP]) and $this->options[self::DISABLE_SOAP]) {
212  return false;
213  }
214  return true;
215  }
216 
220  public function checkOwner(int $a_user_id): bool
221  {
222  return in_array($a_user_id, $this->getOptions(self::OWNER_KEY));
223  }
224 
225  public function getCopyId(): int
226  {
227  return $this->copy_id;
228  }
229 
230  public function initContainer(int $a_source_id, int $a_target_id): void
231  {
232  $mapping_source = $this->tree->getParentId($a_source_id);
233  $this->addEntry($a_source_id, array('type' => ilCopyWizardOptions::COPY_WIZARD_COPY));
234  $this->appendMapping($mapping_source, $a_target_id);
235  }
236 
243  public function storeTree(int $a_source_id): void
244  {
245  $this->tmp_tree = [];
246  $this->readTree($a_source_id);
247  $a_tree_structure = $this->tmp_tree;
248 
249  $this->db->update("copy_wizard_options", array(
250  "options" => array('clob', serialize($a_tree_structure))
251  ), array(
252  "copy_id" => array('integer', $this->getCopyId()),
253  "source_id" => array('integer',
254  0
255  )
256  ));
257 
258  $this->db->insert('copy_wizard_options', array(
259  'copy_id' => array('integer', $this->getCopyId()),
260  'source_id' => array('integer', -1),
261  'options' => array('clob', serialize($a_tree_structure))
262  ));
263  }
264 
268  private function fetchFirstNodeById($a_id): ?array
269  {
270  $tree = $this->getOptions($a_id);
271  if (isset($tree[0]) and is_array($tree[0])) {
272  return $tree[0];
273  }
274  return null;
275  }
276 
280  public function fetchFirstNode(): ?array
281  {
282  return $this->fetchFirstNodeById(0);
283  }
284 
288  public function fetchFirstDependenciesNode(): ?array
289  {
290  return $this->fetchFirstNodeById(-1);
291  }
292 
296  public function dropFirstNodeById(int $a_id): bool
297  {
298  if (!isset($this->options[$a_id]) || !is_array($this->options[$a_id])) {
299  return false;
300  }
301  $this->options[$a_id] = array_slice($this->options[$a_id], 1);
302 
303  $this->db->update('copy_wizard_options', array(
304  'options' => array('clob', serialize($this->options[$a_id]))
305  ), array(
306  'copy_id' => array('integer', $this->getCopyId()),
307  'source_id' => array('integer', $a_id)
308  ));
309 
310  $this->read();
311  // check for role_folder
312  if (($node = $this->fetchFirstNodeById($a_id)) === null) {
313  return true;
314  }
315  if ($node['type'] == 'rolf') {
316  $this->dropFirstNodeById($a_id);
317  }
318  return true;
319  }
320 
324  public function dropFirstNode(): bool
325  {
326  return $this->dropFirstNodeById(0);
327  }
328 
332  public function dropFirstDependenciesNode(): bool
333  {
334  return $this->dropFirstNodeById(-1);
335  }
336 
342  public function getOptions(int $a_source_id): array
343  {
344  if (isset($this->options[$a_source_id]) and is_array($this->options[$a_source_id])) {
345  return $this->options[$a_source_id];
346  }
347  return [];
348  }
349 
353  public function addEntry(int $a_source_id, array $a_options): void
354  {
355  $query = "DELETE FROM copy_wizard_options " .
356  "WHERE copy_id = " . $this->db->quote($this->copy_id, 'integer') . " " .
357  "AND source_id = " . $this->db->quote($a_source_id, 'integer');
358  $res = $this->db->manipulate($query);
359  $this->db->insert('copy_wizard_options', array(
360  'copy_id' => array('integer', $this->copy_id),
361  'source_id' => array('integer', $a_source_id),
362  'options' => array('clob', serialize($a_options))
363  ));
364  }
365 
372  public function appendMapping($a_source_id, $a_target_id): void
373  {
374  $query = "SELECT * FROM copy_wizard_options " .
375  "WHERE copy_id = " . $this->db->quote($this->copy_id, 'integer') . " " .
376  "AND source_id = -2 ";
377  $res = $this->db->query($query);
378  $mappings = array();
379  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
380  $mappings = unserialize((string) $row->options);
381  }
382  $mappings[$a_source_id] = $a_target_id;
383 
384  $query = "DELETE FROM copy_wizard_options " .
385  "WHERE copy_id = " . $this->db->quote($this->getCopyId(), 'integer') . " " .
386  "AND source_id = -2 ";
387  $res = $this->db->manipulate($query);
388 
389  $this->db->insert('copy_wizard_options', array(
390  'copy_id' => array('integer', $this->getCopyId()),
391  'source_id' => array('integer', -2),
392  'options' => array('clob', serialize($mappings))
393  ));
394  }
395 
396  public function getMappings(): array
397  {
398  if (isset($this->options[-2]) and is_array($this->options[-2])) {
399  return $this->options[-2];
400  }
401  return [];
402  }
403 
404  public function deleteAll(): void
405  {
406  if (isset(self::$instances[$this->copy_id])) {
407  unset(self::$instances[$this->copy_id]);
408  }
409  $query = "DELETE FROM copy_wizard_options " .
410  "WHERE copy_id = " . $this->db->quote($this->copy_id, 'integer');
411  $res = $this->db->manipulate($query);
412  }
413 
414  public function read(): void
415  {
416  $query = "SELECT * FROM copy_wizard_options " .
417  "WHERE copy_id = " . $this->db->quote($this->copy_id, 'integer');
418  $res = $this->db->query($query);
419 
420  $this->options = [];
421  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
422  $this->options[(int) $row->source_id] = unserialize((string) $row->options);
423  }
424  }
425 
431  private function readTree(int $a_source_id): void
432  {
433  $this->tmp_tree[] = $this->tree->getNodeData($a_source_id);
434  foreach ($this->tree->getChilds($a_source_id) as $sub_nodes) {
435  $sub_node_ref_id = (int) $sub_nodes['child'];
436  // check ommited, linked ...
437  $options = $this->options[$sub_node_ref_id] ?? [];
438  $type = (int) ($options['type'] ?? 0);
439  if ($type === self::COPY_WIZARD_COPY or
440  $type === self::COPY_WIZARD_LINK) {
441  $this->readTree($sub_node_ref_id);
442  }
443  }
444  }
445 }
fetchFirstNodeById($a_id)
Get first node of stored tree.
saveRoot(int $a_root)
Save root node id.
$res
Definition: ltiservices.php:69
$type
fetchFirstNode()
Fetch first node for cloneObject.
checkOwner(int $a_user_id)
check owner
disableSOAP()
Disable soap calls.
dropFirstNodeById(int $a_id)
Drop first node by id.
fetchFirstDependenciesNode()
Fetch first dependencies node.
storeTree(int $a_source_id)
Save tree Stores two copies of the tree structure: id 0 is used for recursive call of cloneObject() i...
saveOwner(int $a_user_id)
Save owner for copy.
readTree(int $a_source_id)
Purge ommitted node recursively private.
getOptions(int $a_source_id)
Get entry by source public.
global $DIC
Definition: feed.php:28
isSOAPEnabled()
Check if SOAP calls are disabled.
__construct(int $a_copy_id=0)
Private Constructor (Singleton class)
initContainer(int $a_source_id, int $a_target_id)
appendMapping($a_source_id, $a_target_id)
Add mapping of source -> target.
isTreeCopyDisabled()
Check if tree copy is enabled.
static _isFinished(int $a_copy_id)
disableTreeCopy()
Disable copying of tree.
static _allocateCopyId()
Allocate a copy for further entries.
$query
$steps
Definition: latex.php:3
addEntry(int $a_source_id, array $a_options)
Add new entry.
dropFirstDependenciesNode()
Drop first node (for cloneDependencies())
isRootNode(int $a_root)
Is root node public.
static _getInstance(int $a_copy_id)
dropFirstNode()
Drop first node (for cloneObject())