ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
ManipulatorAdapter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
34 {
41  protected VocabularyAdapter $vocabulary_adapter;
42 
43  public function __construct(
44  ContentAssembler $content_assembler,
45  CopyrightHandler $copyright_handler,
46  PathCollection $path_collection,
47  ManipulatorInterface $manipulator,
48  PathFactory $path_factory,
49  NavigatorFactoryInterface $navigator_factory,
50  VocabularyAdapter $vocabulary_adapter
51  ) {
52  $this->content_assembler = $content_assembler;
53  $this->copyright_handler = $copyright_handler;
54  $this->path_collection = $path_collection;
55  $this->manipulator = $manipulator;
56  $this->path_factory = $path_factory;
57  $this->navigator_factory = $navigator_factory;
58  $this->vocabulary_adapter = $vocabulary_adapter;
59  }
60 
61  public function update(
62  SetInterface $set,
64  ): bool {
65  $form = null;
66  foreach ($this->content_assembler->get($set, $request) as $type => $entity) {
67  if ($type === ContentType::FORM) {
68  $form = $entity;
69  break;
70  }
71  }
72 
73  if (!$form?->getData()) {
74  return false;
75  }
76  $data = $form->getData();
77 
78  $set = $this->prepareGeneral($set, $data[ContentAssembler::GENERAL]);
81  if (
82  $this->copyright_handler->isCPSelectionActive() &&
84  ) {
85  $set = $this->prepareRights($set, $data[ContentAssembler::RIGHTS][0]);
86  }
87 
88  $this->manipulator->execute($set);
89  return true;
90  }
91 
92  protected function prepareGeneral(
93  SetInterface $set,
94  array $data
95  ): SetInterface {
96  foreach ($data as $post_key => $value) {
97  if ($post_key === ContentAssembler::KEYWORDS) {
98  $set = $this->prepareKeywords($set, $value);
99  continue;
100  }
101  $path = $this->path_factory->fromString($post_key);
102  if ($value === null || $value === '') {
103  $set = $this->manipulator->prepareDelete($set, $path);
104  continue;
105  }
106  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, $value);
107  }
108  return $set;
109  }
110 
114  protected function prepareKeywords(
115  SetInterface $set,
116  array $values
117  ): SetInterface {
118  $keyword_els = $this->navigator_factory->navigator(
119  $path = $this->path_collection->keywords(),
120  $set->getRoot()
121  )->elementsAtFinalStep();
122  $number_of_keywords = count(iterator_to_array($keyword_els));
123 
124  if (!empty($values)) {
125  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, ...$values);
126  }
127  if (count($values) < $number_of_keywords) {
128  $delete_path = $this->path_collection->keywordsBetweenIndices(
129  count($values),
130  $number_of_keywords - 1
131  );
132  $set = $this->manipulator->prepareDelete($set, $delete_path);
133  }
134  return $set;
135  }
136 
137  protected function prepareClassification(
138  SetInterface $set,
139  array $data
140  ): SetInterface {
141  $type_value = $data[ContentAssembler::LEARNING_RESOURCE_TYPE] ?? '';
142  $type_source = $this->vocabulary_adapter->sourceMapForSlot(SlotIdentifier::EDUCATIONAL_LEARNING_RESOURCE_TYPE)($type_value);
143  if ($type_value === null || $type_value === '') {
144  $set = $this->manipulator->prepareDelete($set, $this->path_collection->firstLearningResourceType());
145  } else {
146  $set = $this->manipulator->prepareCreateOrUpdate(
147  $set,
148  $this->path_collection->firstLearningResourceType(),
149  $type_value
150  );
151  }
152  if ($type_source === null || $type_source === '') {
153  $set = $this->manipulator->prepareDelete($set, $this->path_collection->firstLearningResourceTypeSource());
154  } else {
155  $set = $this->manipulator->prepareCreateOrUpdate(
156  $set,
157  $this->path_collection->firstLearningResourceTypeSource(),
158  $type_source
159  );
160  }
161 
162  $discipline = $data[ContentAssembler::DISCIPLINE] ?? '';
163  if ($discipline === null || $discipline === '') {
164  $set = $this->manipulator->prepareDelete($set, $this->path_collection->firstDiscipline());
165  } else {
166  $set = $this->manipulator->prepareCreateOrUpdate(
167  $set,
168  $this->path_collection->firstDiscipline(),
169  $discipline
170  );
171  }
172 
173  return $set;
174  }
175 
176  protected function prepareContributors(
177  SetInterface $set,
178  array $data
179  ): SetInterface {
180  $paths = [
181  ContentAssembler::FIRST_AUTHOR => $this->path_collection->firstAuthor(),
182  ContentAssembler::SECOND_AUTHOR => $this->path_collection->secondAuthor(),
183  ContentAssembler::THIRD_AUTHOR => $this->path_collection->thirdAuthor(),
184  ContentAssembler::PUBLISHER => $this->path_collection->firstPublisher()
185  ];
186  foreach ($data as $post_key => $value) {
187  $path = $paths[$post_key];
188  if ($value === null || $value === '') {
189  $set = $this->manipulator->prepareDelete($set, $path);
190  continue;
191  }
192  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, $value);
193  }
194  return $set;
195  }
196 
197  protected function prepareRights(
198  SetInterface $set,
199  array $data
200  ): SetInterface {
201  $description = $data[0];
202  if ($description === ContentAssembler::CUSTOM_CP) {
203  $description = $data[1][ContentAssembler::CUSTOM_CP_DESCRIPTION];
204  }
205 
206  if ($description === '' || $description === null) {
207  $set = $this->manipulator->prepareCreateOrUpdate(
208  $set,
209  $this->path_collection->hasCopyright(),
210  'no'
211  );
212  $set = $this->manipulator->prepareCreateOrUpdate(
213  $set,
214  $this->path_collection->sourceForHasCopyright(),
216  );
217  return $this->manipulator->prepareDelete($set, $this->path_collection->copyright());
218  }
219 
220  $set = $this->manipulator->prepareCreateOrUpdate(
221  $set,
222  $this->path_collection->hasCopyright(),
223  'yes'
224  );
225  $set = $this->manipulator->prepareCreateOrUpdate(
226  $set,
227  $this->path_collection->sourceForHasCopyright(),
229  );
230  $set = $this->manipulator->prepareCreateOrUpdate(
231  $set,
232  $this->path_collection->copyright(),
233  $description
234  );
235 
236  if (
237  $this->copyright_handler->isObjectTypeHarvested($set->getRessourceID()->type()) &&
238  isset($data[1][ContentAssembler::OER_BLOCKED])
239  ) {
240  $this->copyright_handler->setOerHarvesterBlocked(
241  $set->getRessourceID()->objID(),
242  (bool) $data[1][ContentAssembler::OER_BLOCKED]
243  );
244  }
245 
246  return $set;
247  }
248 }
update(SetInterface $set, RequestForFormInterface $request)
prepareClassification(SetInterface $set, array $data)
prepareKeywords(SetInterface $set, array $values)
prepareContributors(SetInterface $set, array $data)
$path
Definition: ltiservices.php:29
getRoot()
Returns the root element of the metadata set.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getRessourceID()
Contains the information needed to identify the ILIAS object this metadata set belongs to...
prepareGeneral(SetInterface $set, array $data)
__construct(ContentAssembler $content_assembler, CopyrightHandler $copyright_handler, PathCollection $path_collection, ManipulatorInterface $manipulator, PathFactory $path_factory, NavigatorFactoryInterface $navigator_factory, VocabularyAdapter $vocabulary_adapter)