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