ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ManipulatorAdapter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 
33 {
40 
41  public function __construct(
42  ContentAssembler $content_assembler,
43  CopyrightHandler $copyright_handler,
44  PathCollection $path_collection,
45  ManipulatorInterface $manipulator,
46  PathFactory $path_factory,
47  NavigatorFactoryInterface $navigator_factory
48  ) {
49  $this->content_assembler = $content_assembler;
50  $this->copyright_handler = $copyright_handler;
51  $this->path_collection = $path_collection;
52  $this->manipulator = $manipulator;
53  $this->path_factory = $path_factory;
54  $this->navigator_factory = $navigator_factory;
55  }
56 
57  public function update(
58  SetInterface $set,
60  ): bool {
61  $form = null;
62  foreach ($this->content_assembler->get($set, $request) as $type => $entity) {
63  if ($type === ContentType::FORM) {
64  $form = $entity;
65  break;
66  }
67  }
68 
69  if (!$form?->getData()) {
70  return false;
71  }
72  $data = $form->getData();
73 
74  $set = $this->prepareGeneral($set, $data[ContentAssembler::GENERAL]);
75  $set = $this->prepareAuthors($set, $data[ContentAssembler::AUTHORS]);
76  if (
77  $this->copyright_handler->isCPSelectionActive() &&
79  ) {
80  $set = $this->prepareRights($set, $data[ContentAssembler::RIGHTS][0]);
81  }
83 
84  $this->manipulator->execute($set);
85  return true;
86  }
87 
88  protected function prepareGeneral(
89  SetInterface $set,
90  array $data
91  ): SetInterface {
92  foreach ($data as $post_key => $value) {
93  if ($post_key === ContentAssembler::KEYWORDS) {
94  $set = $this->prepareKeywords($set, $value);
95  continue;
96  }
97  $path = $this->path_factory->fromString($post_key);
98  if ($value === null || $value === '') {
99  $set = $this->manipulator->prepareDelete($set, $path);
100  continue;
101  }
102  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, $value);
103  }
104  return $set;
105  }
106 
110  protected function prepareKeywords(
111  SetInterface $set,
112  array $values
113  ): SetInterface {
114  $keyword_els = $this->navigator_factory->navigator(
115  $path = $this->path_collection->keywords(),
116  $set->getRoot()
117  )->elementsAtFinalStep();
118  $number_of_keywords = count(iterator_to_array($keyword_els));
119 
120  if (!empty($values)) {
121  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, ...$values);
122  }
123  if (count($values) < $number_of_keywords) {
124  $delete_path = $this->path_collection->keywordsBetweenIndices(
125  count($values),
126  $number_of_keywords - 1
127  );
128  $set = $this->manipulator->prepareDelete($set, $delete_path);
129  }
130  return $set;
131  }
132 
133  protected function prepareTypicalLearningTime(
134  SetInterface $set,
135  array $data
136  ): SetInterface {
137  foreach ($data as $post_key => $value) {
138  $path = $this->path_factory->fromString($post_key);
139  if ($value === null || $value === '') {
140  $set = $this->manipulator->prepareDelete($set, $path);
141  continue;
142  }
143  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, $value);
144  }
145  return $set;
146  }
147 
148  protected function prepareAuthors(
149  SetInterface $set,
150  array $data
151  ): SetInterface {
152  $paths = [
153  ContentAssembler::FIRST_AUTHOR => $this->path_collection->firstAuthor(),
154  ContentAssembler::SECOND_AUTHOR => $this->path_collection->secondAuthor(),
155  ContentAssembler::THIRD_AUTHOR => $this->path_collection->thirdAuthor()
156  ];
157  foreach ($data as $post_key => $value) {
158  $path = $paths[$post_key];
159  if ($value === null || $value === '') {
160  $set = $this->manipulator->prepareDelete($set, $path);
161  continue;
162  }
163  $set = $this->manipulator->prepareCreateOrUpdate($set, $path, $value);
164  }
165  return $set;
166  }
167 
168  protected function prepareRights(
169  SetInterface $set,
170  array $data
171  ): SetInterface {
172  $description = $data[0];
173  if ($description === ContentAssembler::CUSTOM_CP) {
174  $description = $data[1][ContentAssembler::CUSTOM_CP_DESCRIPTION];
175  }
176 
177  if ($description === '' || $description === null) {
178  $set = $this->manipulator->prepareCreateOrUpdate(
179  $set,
180  $this->path_collection->hasCopyright(),
181  'no'
182  );
183  $set = $this->manipulator->prepareCreateOrUpdate(
184  $set,
185  $this->path_collection->sourceForHasCopyright(),
187  );
188  return $this->manipulator->prepareDelete($set, $this->path_collection->copyright());
189  }
190 
191  $set = $this->manipulator->prepareCreateOrUpdate(
192  $set,
193  $this->path_collection->hasCopyright(),
194  'yes'
195  );
196  $set = $this->manipulator->prepareCreateOrUpdate(
197  $set,
198  $this->path_collection->sourceForHasCopyright(),
200  );
201  $set = $this->manipulator->prepareCreateOrUpdate(
202  $set,
203  $this->path_collection->copyright(),
204  $description
205  );
206 
207  if (
208  $this->copyright_handler->isObjectTypeHarvested($set->getRessourceID()->type()) &&
209  isset($data[1][ContentAssembler::OER_BLOCKED])
210  ) {
211  $this->copyright_handler->setOerHarvesterBlocked(
212  $set->getRessourceID()->objID(),
213  (bool) $data[1][ContentAssembler::OER_BLOCKED]
214  );
215  }
216 
217  return $set;
218  }
219 }
update(SetInterface $set, RequestForFormInterface $request)
prepareTypicalLearningTime(SetInterface $set, array $data)
prepareKeywords(SetInterface $set, array $values)
$path
Definition: ltiservices.php:30
getRoot()
Returns the root element of the metadata set.
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)