ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
CopyrightHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ILIAS\MetaData\Copyright\RepositoryInterface as CopyrightRepository;
33use ILIAS\MetaData\Search\Clauses\FactoryInterface as SearchClauseFactory;
36
38{
40 protected PathFactory $path_factory;
41 protected IdentifierHandler $identifier_handler;
42 protected CopyrightRepository $copyright_repo;
44 protected SearchClauseFactory $search_clause_factory;
45
46 public function __construct(
48 PathFactory $path_factory,
49 CopyrightRepository $copyright_repo,
50 IdentifierHandler $identifier_handler,
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
130 public function getCopyrightPreset(string $copyright_id): CopyrightInterface
131 {
132 $entry = $this->copyright_repo->getEntry(
133 $this->identifier_handler->parseEntryIDFromIdentifier($copyright_id)
134 );
135 return $this->getCopyrightEntryWrapper($entry);
136 }
137
141 public function getAllCopyrightPresets(): \Generator
142 {
143 if (!$this->isCopyrightSelectionActive()) {
144 return;
145 }
146
147 foreach ($this->copyright_repo->getAllEntries() as $entry) {
148 yield $this->getCopyrightEntryWrapper($entry);
149 }
150 }
151
155 public function getNonOutdatedCopyrightPresets(): \Generator
156 {
157 if (!$this->isCopyrightSelectionActive()) {
158 return;
159 }
160
161 foreach ($this->copyright_repo->getActiveEntries() as $entry) {
162 yield $this->getCopyrightEntryWrapper($entry);
163 }
164 }
165
167 string $first_copyright_id,
168 string ...$further_copyright_ids
169 ): SearchClause {
170 $selection_active = $this->isCopyrightSelectionActive();
171 $default_entry_id = 0;
172 if ($selection_active) {
173 $default_entry_id = $this->copyright_repo->getDefaultEntry()->id();
174 }
175
176 $copyright_search_clauses = [];
177 foreach ([$first_copyright_id, ...$further_copyright_ids] as $copyright_id) {
178 $copyright_search_clauses[] = $this->search_clause_factory->getBasicClause(
179 $this->getCopyrightDescriptionPath(),
180 Mode::EQUALS,
181 $copyright_id
182 );
183
184 if (
185 !$selection_active ||
186 !$this->identifier_handler->isIdentifierValid($copyright_id) ||
187 $this->identifier_handler->parseEntryIDFromIdentifier($copyright_id) !== $default_entry_id
188 ) {
189 continue;
190 }
191 $copyright_search_clauses[] = $this->search_clause_factory->getBasicClause(
192 $this->getCopyrightDescriptionPath(),
193 Mode::EQUALS,
194 ''
195 );
196 }
197
198 return $this->search_clause_factory->getJoinedClauses(
200 ...$copyright_search_clauses
201 );
202 }
203
205 ManipulatorInterface $manipulator,
206 string $value
208 return $manipulator->prepareCreateOrUpdate(
209 $this->getCopyrightDescriptionPath(),
210 $value
211 );
212 }
213
214 protected function getRawCopyright(ReaderInterface $reader): string
215 {
216 return $reader->firstData($this->getCopyrightDescriptionPath())->value();
217 }
218
220 {
221 return $this->path_factory->custom()
222 ->withNextStep('rights')
223 ->withNextStep('description')
224 ->withNextStep('string')
225 ->get();
226 }
227
229 {
230 return new Copyright(
231 $this->renderer,
232 $this->identifier_handler,
233 $entry
234 );
235 }
236
238 {
239 return new NullCopyright();
240 }
241}
renderer()
__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'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,...
isCopyrightSelectionActive()
If copyright selection is not active, there are no copyright presets to choose from,...
prepareCreateOrUpdateOfCustomCopyright(ManipulatorInterface $manipulator, string $custom_copyright)
The given copyright information is set to be written to the LOM of the manipulator's object.
prepareCreateOrUpdateOfCopyrightFromPreset(ManipulatorInterface $manipulator, string $copyright_id)
The preset copyright with the given identifier is set to be selected for the manipulator's object.
prepareCreateOrUpdateOfCopyright(ManipulatorInterface $manipulator, string $value)
hasPresetCopyright(ReaderInterface $reader)
Is the copyright in the LOM of the reader's object selected from the presets? If not,...
readCustomCopyright(ReaderInterface $reader)
Returns the custom copyright information from the LOM of the reader's object.
getCopyrightPreset(string $copyright_id)
Returns the preset copyright entry with the given identifier, or a null object if copyright selection...
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
firstData(PathInterface $path)
Get the data of the first of the elements specified by the path.