ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DatabaseManipulator.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
35 
37 {
41  protected \ilLogger $logger;
42 
43  public function __construct(
44  DictionaryInterface $dictionary,
45  DatabaseQuerierInterface $querier,
46  AssignmentFactoryInterface $assignment_factory,
47  \ilLogger $logger
48  ) {
49  $this->dictionary = $dictionary;
50  $this->querier = $querier;
51  $this->assignment_factory = $assignment_factory;
52  $this->logger = $logger;
53  }
54 
55  public function deleteAllMD(RessourceIDInterface $ressource_id): void
56  {
57  $this->querier->deleteAll($ressource_id);
58  }
59 
60  public function manipulateMD(
61  SetInterface $set
62  ): void {
63  foreach ($set->getRoot()->getSubElements() as $sub) {
69  $rows = [];
71  0,
72  $sub
73  ) as $row) {
74  $rows[] = $row;
75  }
76  foreach ($rows as $row) {
77  $this->querier->manipulate(
78  $set->getRessourceID(),
79  $row
80  );
81  }
82  }
83  }
84 
89  int $depth,
90  ElementInterface $element,
91  AssignmentRowInterface $current_row = null,
92  bool $delete_all = false
93  ): \Generator {
94  if ($depth > 20) {
95  throw new \ilMDStructureException('LOM Structure is nested to deep.');
96  }
97  $marker = $this->marker($element);
98  if (!isset($marker) && !$delete_all) {
99  return;
100  }
101  $id = $element->getMDID();
102  $tag = $this->tag($element);
103  $table = $tag?->table() ?? '';
104  if ($table && $current_row?->table() !== $table) {
105  yield $current_row = $this->assignment_factory->row(
106  $table,
107  is_int($id) ? $id : 0,
108  $current_row?->id() ?? 0
109  );
110  }
111 
112  $action = $marker?->action();
113  if ($delete_all) {
114  $action = MarkerAction::DELETE;
115  }
116  switch ($action) {
117  case MarkerAction::NEUTRAL:
118  if ($element->isScaffold()) {
119  return;
120  }
121  break;
122 
123  case MarkerAction::CREATE_OR_UPDATE:
124  if (!is_null($tag)) {
125  $this->createOrUpdateElement(
126  $element,
127  $tag,
128  $marker->dataValue(),
129  $current_row
130  );
131  }
132  break;
133 
135  if (!is_null($tag)) {
136  $current_row->addAction($this->assignment_factory->action(
138  $tag
139  ));
140  }
141  $delete_all = true;
142  }
143 
144  foreach ($element->getSubElements() as $sub) {
146  $depth + 1,
147  $sub,
148  $current_row,
149  $delete_all
150  );
151  }
152  }
153 
154  protected function createOrUpdateElement(
155  ElementInterface $element,
156  TagInterface $tag,
157  string $data_value,
159  ): void {
160  if (!$element->isScaffold()) {
161  $row->addAction($this->assignment_factory->action(
162  Action::UPDATE,
163  $tag,
164  $data_value
165  ));
166  } else {
167  $row->addAction($this->assignment_factory->action(
168  Action::CREATE,
169  $tag,
170  $data_value
171  ));
172  if (!$row->id()) {
173  $row->setId($this->querier->nextID($row->table()));
174  }
175  }
176  }
177 
178  protected function tag(
179  ElementInterface $element,
180  ): ?TagInterface {
181  return $this->dictionary->tagForElement($element);
182  }
183 
184  protected function marker(
185  ElementInterface $element
186  ): ?MarkerInterface {
187  if (!($element instanceof MarkableInterface) || !$element->isMarked()) {
188  return null;
189  }
190  return $element->getMarker();
191  }
192 }
addAction(ActionAssignmentInterface $assignment)
Note that this does not clone!
createOrUpdateElement(ElementInterface $element, TagInterface $tag, string $data_value, AssignmentRowInterface $row)
getRoot()
Returns the root element of the metadata set.
getRessourceID()
Contains the information needed to identify the ILIAS object this metadata set belongs to...
collectAssignmentsFromElementAndSubElements(int $depth, ElementInterface $element, AssignmentRowInterface $current_row=null, bool $delete_all=false)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
isMarked()
Elements can be marked to be created, updated or deleted.
__construct(DictionaryInterface $dictionary, DatabaseQuerierInterface $querier, AssignmentFactoryInterface $assignment_factory, \ilLogger $logger)