ILIAS  release_8 Revision v8.24
class.ilMDCopyrightSelectionEntry.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
33{
34 protected ilLogger $logger;
35 protected ilDBInterface $db;
36
37 private int $entry_id;
38 private string $title = '';
39 private string $description = '';
40 private string $copyright = '';
41 private bool $costs = false;
42 private string $language = '';
44 private int $usage = 0;
45
46 protected bool $outdated = false;
47
48 protected int $order_position = 0;
49
51
52 public function __construct(int $a_entry_id)
53 {
54 global $DIC;
55
56 $this->logger = $DIC->logger()->meta();
57 $this->db = $DIC->database();
58 $this->entry_id = $a_entry_id;
59 $this->read();
60 }
61
65 public static function _getEntries(): array
66 {
67 global $DIC;
68
69 $ilDB = $DIC->database();
70
71 $query = "SELECT entry_id FROM il_md_cpr_selections ORDER BY is_default DESC, position ASC";
72 $res = $ilDB->query($query);
73
74 $entries = [];
75 while ($row = $ilDB->fetchObject($res)) {
76 $entries[] = new ilMDCopyrightSelectionEntry((int) $row->entry_id);
77 }
78 return $entries;
79 }
80
81 public static function lookupCopyyrightTitle(string $a_cp_string): string
82 {
83 global $DIC;
84
85 $ilDB = $DIC->database();
86
87 if (!$entry_id = self::_extractEntryId($a_cp_string)) {
88 return $a_cp_string;
89 }
90
91 $query = "SELECT title FROM il_md_cpr_selections " .
92 "WHERE entry_id = " . $ilDB->quote($entry_id, ilDBConstants::T_INTEGER) . " ";
93 $res = $ilDB->query($query);
94 $row = $ilDB->fetchObject($res);
95 return $row->title ?? '';
96 }
97
98 protected static function getCopyrightDataFromRow(stdClass $row): CopyrightDataInterface
99 {
100 return new CopyrightData(
101 $row->full_name ?? '',
102 !empty($row->link ?? '') ? new URI($row->link) : null,
103 !empty($row->image_link ?? '') ? new URI($row->image_link) : null,
104 $row->alt_text ?? ''
105 );
106 }
107
108 protected static function renderCopyrightFromRow(stdClass $row): string
109 {
110 global $DIC;
111
112 $renderer = new Renderer(
113 $DIC->ui()->factory()
114 );
115 $ui_renderer = $DIC->ui()->renderer();
116
118 return $ui_renderer->render($renderer->toUIComponents($data));
119 }
120
121 public static function _lookupCopyright(string $a_cp_string): string
122 {
123 global $DIC;
124
125 $ilDB = $DIC->database();
126
127 if (!$entry_id = self::_extractEntryId($a_cp_string)) {
128 return $a_cp_string;
129 }
130
131 $query = "SELECT full_name, link, image_link, alt_text FROM il_md_cpr_selections " .
132 "WHERE entry_id = " . $ilDB->quote($entry_id, ilDBConstants::T_INTEGER) . " ";
133 $res = $ilDB->query($query);
134 $row = $ilDB->fetchObject($res);
135
136 return self::renderCopyrightFromRow($row);
137 }
138
139 public static function _lookupCopyrightForExport(string $a_cp_string): string
140 {
141 global $DIC;
142
143 $ilDB = $DIC->database();
144
145 if (!$entry_id = self::_extractEntryId($a_cp_string)) {
146 return $a_cp_string;
147 }
148
149 $query = "SELECT full_name, link, image_link, alt_text FROM il_md_cpr_selections " .
150 "WHERE entry_id = " . $ilDB->quote($entry_id, ilDBConstants::T_INTEGER) . " ";
151 $res = $ilDB->query($query);
152 $row = $ilDB->fetchObject($res);
153
155
156 return (string) ($data->link() ?? $data->fullName());
157 }
158
159 public static function lookupCopyrightFromImport(string $copyright_text): int
160 {
161 global $DIC;
162
163 $db = $DIC->database();
164
165 // url should be made to match regardless of scheme
166 $normalized_copyright = str_replace('https://', 'http://', $copyright_text);
167 $matches_by_name = null;
168
169 $query = 'SELECT entry_id, full_name, link, image_link, alt_text FROM il_md_cpr_selections';
170 $res = $db->query($query);
171 while ($row = $db->fetchObject($res)) {
172 $entry_id = (int) $row->entry_id;
173 $data = self::getCopyrightDataFromRow($row);
174
175 $entry_link = (string) $data->link();
176 $normalized_link = str_replace('https://', 'http://', $entry_link);
177 if ($normalized_link !== '' && str_contains($normalized_copyright, $normalized_link)) {
178 return $entry_id;
179 }
180
181 if (
182 is_null($matches_by_name) &&
183 trim($copyright_text) === trim($data->fullName())
184 ) {
185 $matches_by_name = $entry_id;
186 }
187 }
188
189 if (!is_null($matches_by_name)) {
190 return $matches_by_name;
191 }
192 return 0;
193 }
194
195 public static function _extractEntryId(string $a_cp_string): int
196 {
197 if (!preg_match('/il_copyright_entry__([0-9]+)__([0-9]+)/', $a_cp_string, $matches)) {
198 return 0;
199 }
200 if ($matches[1] != IL_INST_ID) {
201 return 0;
202 }
203 return (int) ($matches[2] ?? 0);
204 }
205
206 public static function isEntry($a_cp_string): bool
207 {
208 if (!preg_match('/il_copyright_entry__([0-9]+)__([0-9]+)/', $a_cp_string)) {
209 return false;
210 }
211 return true;
212 }
213
214 public function getUsage(): int
215 {
216 return $this->usage;
217 }
218
219 public function getEntryId(): int
220 {
221 return $this->entry_id;
222 }
223
228 public function getIsDefault(): bool
229 {
230 $query = "SELECT is_default FROM il_md_cpr_selections " .
231 "WHERE entry_id = " . $this->db->quote($this->entry_id, 'integer');
232
233 $res = $this->db->query($query);
234 $row = $this->db->fetchAssoc($res);
235
236 return (bool) ($row['is_default'] ?? false);
237 }
238
239 public function setOutdated(bool $a_value): void
240 {
241 $this->outdated = $a_value;
242 }
243
244 public function getOutdated(): bool
245 {
246 return $this->outdated;
247 }
248
249 public static function getDefault(): int
250 {
251 global $DIC;
252
253 $db = $DIC->database();
254
255 $query = "SELECT entry_id FROM il_md_cpr_selections " .
256 "WHERE is_default = " . $db->quote(1, 'integer');
257
258 $res = $db->query($query);
259 $row = $db->fetchAssoc($res);
260
261 return (int) $row['entry_id'];
262 }
263
264 public function setTitle(string $a_title): void
265 {
266 $this->title = $a_title;
267 }
268
269 public function getTitle(): string
270 {
271 return $this->title;
272 }
273
274 public function setDescription(string $a_desc): void
275 {
276 $this->description = $a_desc;
277 }
278
279 public function getDescription(): string
280 {
281 return $this->description;
282 }
283
285 {
286 if (isset($this->copyright_data)) {
288 }
289 return new NullCopyrightData();
290 }
291
292 public function setCopyrightData(
293 string $full_name,
294 ?URI $link,
295 ?URI $image_link,
296 string $alt_text
297 ): void {
298 $this->copyright_data = new CopyrightData(
299 $full_name,
300 $link,
301 $image_link,
302 $alt_text
303 );
304 }
305
306 public function setCopyright(string $a_copyright): void
307 {
308 $this->copyright = $a_copyright;
309 }
310
311 public function getCopyright(): string
312 {
313 return $this->copyright;
314 }
315
316 public function setCosts(bool $a_costs): void
317 {
318 $this->costs = $a_costs;
319 }
320
321 public function getCosts(): bool
322 {
323 return $this->costs;
324 }
325
326 public function setLanguage(string $a_lang_key): void
327 {
328 $this->language = $a_lang_key;
329 }
330
331 public function getLanguage(): string
332 {
333 return $this->language;
334 }
335
336 public function setCopyrightAndOtherRestrictions(bool $a_status): void
337 {
338 $this->copyright_and_other_restrictions = $a_status;
339 }
340
341 public function getCopyrightAndOtherRestrictions(): bool
342 {
343 return $this->copyright_and_other_restrictions;
344 }
345
346 public function setOrderPosition(int $a_position): void
347 {
348 $this->order_position = $a_position;
349 }
350
351 public function getOrderPosition(): int
352 {
353 return $this->order_position;
354 }
355
356 protected function getNextOrderPosition(): int
357 {
358 $query = "SELECT count(entry_id) total FROM il_md_cpr_selections";
359 $res = $this->db->query($query);
360 $row = $this->db->fetchAssoc($res);
361
362 return $row['total'] + 1;
363 }
364
365 public function add(): bool
366 {
367 $next_id = $this->db->nextId('il_md_cpr_selections');
368
369 $this->db->insert('il_md_cpr_selections', array(
370 'entry_id' => array('integer', $next_id),
371 'title' => array('text', $this->getTitle()),
372 'description' => array('clob', $this->getDescription()),
373 'full_name' => array('clob', $this->getCopyrightData()->fullName()),
374 'link' => array('clob', (string) $this->getCopyrightData()->link()),
375 'image_link' => array('clob', (string) $this->getCopyrightData()->imageLink()),
376 'alt_text' => array('clob', $this->getCopyrightData()->altText()),
377 'language' => array('text', $this->getLanguage()),
378 'costs' => array('integer', $this->getCosts()),
379 'cpr_restrictions' => array('integer', $this->getCopyrightAndOtherRestrictions()),
380 'position' => array('integer', $this->getNextOrderPosition())
381 ));
382 $this->entry_id = $next_id;
383 return true;
384 }
385
386 public function update(): bool
387 {
388 $this->db->update('il_md_cpr_selections', array(
389 'title' => array('text', $this->getTitle()),
390 'description' => array('clob', $this->getDescription()),
391 'full_name' => array('clob', $this->getCopyrightData()->fullName()),
392 'link' => array('clob', (string) $this->getCopyrightData()->link()),
393 'image_link' => array('clob', (string) $this->getCopyrightData()->imageLink()),
394 'alt_text' => array('clob', $this->getCopyrightData()->altText()),
395 'language' => array('text', $this->getLanguage()),
396 'costs' => array('integer', $this->getCosts()),
397 'cpr_restrictions' => array('integer', $this->getCopyrightAndOtherRestrictions()),
398 'outdated' => array('integer', $this->getOutdated()),
399 'position' => array('integer', $this->getOrderPosition())
400 ), array(
401 'entry_id' => array('integer', $this->getEntryId())
402 ));
403 return true;
404 }
405
406 public function delete(): void
407 {
408 $query = "DELETE FROM il_md_cpr_selections " .
409 "WHERE entry_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
410 $res = $this->db->manipulate($query);
411 }
412
413 public function validate(): bool
414 {
415 return $this->getTitle() !== '';
416 }
417
418 private function read(): void
419 {
420 $query = "SELECT * FROM il_md_cpr_selections " .
421 "WHERE entry_id = " . $this->db->quote($this->entry_id, 'integer') . " " .
422 "ORDER BY is_default DESC, position ASC ";
423
424 $res = $this->db->query($query);
425 while ($row = $this->db->fetchObject($res)) {
426 $this->setTitle($row->title ?? '');
427 $this->setDescription($row->description ?? '');
428
429 $data = self::getCopyrightDataFromRow($row);
430 $this->setCopyrightData(
431 $data->fullName(),
432 $data->link(),
433 $data->imageLink(),
434 $data->altText()
435 );
436 $this->setCopyright(self::renderCopyrightFromRow($row));
437
438 $this->setLanguage($row->language);
439 $this->setCosts((bool) $row->costs);
440 $this->setOutdated((bool) $row->outdated);
441 $this->setOrderPosition((int) $row->position);
442 // Fixed
443 $this->setCopyrightAndOtherRestrictions(true);
444 }
445
446 $query = "SELECT count(meta_rights_id) used FROM il_meta_rights " .
447 "WHERE description = " . $this->db->quote(
448 'il_copyright_entry__' . IL_INST_ID . '__' . $this->getEntryId(),
449 'text'
450 );
451
452 $res = $this->db->query($query);
453 $row = $this->db->fetchObject($res);
454 $this->usage = (int) ($row->used ?? 0);
455 }
456
457 public static function createIdentifier(int $a_entry_id): string
458 {
459 return 'il_copyright_entry__' . IL_INST_ID . '__' . $a_entry_id;
460 }
461}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Component logger with individual log levels by component id.
static lookupCopyrightFromImport(string $copyright_text)
setCopyrightData(string $full_name, ?URI $link, ?URI $image_link, string $alt_text)
static _lookupCopyrightForExport(string $a_cp_string)
static lookupCopyyrightTitle(string $a_cp_string)
getIsDefault()
Get if the entry is default No setter for this.
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
fetchObject(ilDBStatement $query_result)
quote($value, string $type)
query(string $query)
Run a (read-only) Query on the database.
fetchAssoc(ilDBStatement $statement)
$res
Definition: ltiservices.php:69
$query