ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
CopyrightHelper.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
36 
38 {
45 
46  public function __construct(
47  SettingsInterface $settings,
48  PathFactory $path_factory,
49  CopyrightRepository $copyright_repo,
50  IdentifierHandler $identifier_handler,
51  RendererInterface $renderer,
52  SearchClauseFactory $search_clause_factory
53  ) {
54  $this->settings = $settings;
55  $this->path_factory = $path_factory;
56  $this->copyright_repo = $copyright_repo;
57  $this->identifier_handler = $identifier_handler;
58  $this->renderer = $renderer;
59  $this->search_clause_factory = $search_clause_factory;
60  }
61 
62  public function isCopyrightSelectionActive(): bool
63  {
64  return $this->settings->isCopyrightSelectionActive();
65  }
66 
67  public function hasPresetCopyright(ReaderInterface $reader): bool
68  {
69  if (!$this->isCopyrightSelectionActive()) {
70  return false;
71  }
72  $raw = $this->getRawCopyright($reader);
73 
74  if ($raw === '') {
75  return true;
76  }
77 
78  return $this->identifier_handler->isIdentifierValid($this->getRawCopyright($reader));
79  }
80 
82  {
83  if (!$this->isCopyrightSelectionActive()) {
84  return $this->getNullCopyrightEntryWrapper();
85  }
86  $raw = $this->getRawCopyright($reader);
87 
88  $entry = null;
89  if ($this->identifier_handler->isIdentifierValid($raw)) {
90  $entry = $this->copyright_repo->getEntry(
91  $this->identifier_handler->parseEntryIDFromIdentifier($raw)
92  );
93  } elseif ($raw === '') {
94  $entry = $this->copyright_repo->getDefaultEntry();
95  }
96 
97  if (is_null($entry)) {
98  return $this->getNullCopyrightEntryWrapper();
99  }
100  return $this->getCopyrightEntryWrapper($entry);
101  }
102 
103  public function readCustomCopyright(ReaderInterface $reader): string
104  {
105  $copyright = $this->getRawCopyright($reader);
106 
107  if (
108  !$this->isCopyrightSelectionActive() ||
109  $this->identifier_handler->isIdentifierValid($copyright)
110  ) {
111  return '';
112  }
113  return $copyright;
114  }
115 
117  ManipulatorInterface $manipulator,
118  string $copyright_id
120  return $this->prepareCreateOrUpdateOfCopyright($manipulator, $copyright_id);
121  }
122 
124  ManipulatorInterface $manipulator,
125  string $custom_copyright
127  return $this->prepareCreateOrUpdateOfCopyright($manipulator, $custom_copyright);
128  }
129 
133  public function getAllCopyrightPresets(): \Generator
134  {
135  if (!$this->isCopyrightSelectionActive()) {
136  return;
137  }
138 
139  foreach ($this->copyright_repo->getAllEntries() as $entry) {
140  yield $this->getCopyrightEntryWrapper($entry);
141  }
142  }
143 
148  {
149  if (!$this->isCopyrightSelectionActive()) {
150  return;
151  }
152 
153  foreach ($this->copyright_repo->getActiveEntries() as $entry) {
154  yield $this->getCopyrightEntryWrapper($entry);
155  }
156  }
157 
158  public function getCopyrightSearchClause(
159  string $first_copyright_id,
160  string ...$further_copyright_ids
161  ): SearchClause {
162  $selection_active = $this->isCopyrightSelectionActive();
163  $default_entry_id = 0;
164  if ($selection_active) {
165  $default_entry_id = $this->copyright_repo->getDefaultEntry()->id();
166  }
167 
168  $copyright_search_clauses = [];
169  foreach ([$first_copyright_id, ...$further_copyright_ids] as $copyright_id) {
170  $copyright_search_clauses[] = $this->search_clause_factory->getBasicClause(
172  Mode::EQUALS,
173  $copyright_id
174  );
175 
176  if (
177  !$selection_active ||
178  !$this->identifier_handler->isIdentifierValid($copyright_id) ||
179  $this->identifier_handler->parseEntryIDFromIdentifier($copyright_id) !== $default_entry_id
180  ) {
181  continue;
182  }
183  $copyright_search_clauses[] = $this->search_clause_factory->getBasicClause(
185  Mode::EQUALS,
186  ''
187  );
188  }
189 
190  return $this->search_clause_factory->getJoinedClauses(
191  Operator::OR,
192  ...$copyright_search_clauses
193  );
194  }
195 
197  ManipulatorInterface $manipulator,
198  string $value
200  return $manipulator->prepareCreateOrUpdate(
202  $value
203  );
204  }
205 
206  protected function getRawCopyright(ReaderInterface $reader): string
207  {
208  return $reader->firstData($this->getCopyrightDescriptionPath())->value();
209  }
210 
212  {
213  return $this->path_factory->custom()
214  ->withNextStep('rights')
215  ->withNextStep('description')
216  ->withNextStep('string')
217  ->get();
218  }
219 
221  {
222  return new Copyright(
223  $this->renderer,
224  $this->identifier_handler,
225  $entry
226  );
227  }
228 
230  {
231  return new NullCopyright();
232  }
233 }
hasPresetCopyright(ReaderInterface $reader)
Is the copyright in the LOM of the reader&#39;s object selected from the presets? If not, custom copyright information was entered manually.
firstData(PathInterface $path)
Get the data of the first of the elements specified by the path.
__construct(SettingsInterface $settings, PathFactory $path_factory, CopyrightRepository $copyright_repo, IdentifierHandler $identifier_handler, RendererInterface $renderer, SearchClauseFactory $search_clause_factory)
readPresetCopyright(ReaderInterface $reader)
If possible, returns the preset copyright selected for the reader&#39;s object.
prepareCreateOrUpdateOfCustomCopyright(ManipulatorInterface $manipulator, string $custom_copyright)
The given copyright information is set to be written to the LOM of the manipulator&#39;s object...
isCopyrightSelectionActive()
If copyright selection is not active, there are no copyright presets to choose from, but copyright information can still be added manually to the LOM of objects.
prepareCreateOrUpdateOfCopyrightFromPreset(ManipulatorInterface $manipulator, string $copyright_id)
The preset copyright with the given identifier is set to be selected for the manipulator&#39;s object...
prepareCreateOrUpdateOfCopyright(ManipulatorInterface $manipulator, string $value)
readCustomCopyright(ReaderInterface $reader)
Returns the custom copyright information from the LOM of the reader&#39;s object.
getCopyrightSearchClause(string $first_copyright_id, string ... $further_copyright_ids)
Get a search clause that finds object with one of the given copyright entries in their LOM...
prepareCreateOrUpdate(PathInterface $path, string ... $values)
The values are set to be inserted into the elements specified by the path in order.