ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMDCopyrightSelectionEntry.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
34 {
35  protected ilLogger $logger;
36  protected ilDBInterface $db;
40 
41  private int $entry_id;
42  private string $title = '';
43  private string $description = '';
44  private string $copyright = '';
45  // set null as initial value to trigger a query on demand
46  private ?int $usage = null;
47 
48  protected bool $outdated = false;
49 
50  protected int $order_position = 0;
51 
52  public function __construct(int $a_entry_id)
53  {
54  global $DIC;
55 
56  $this->renderer = new Renderer(
57  $DIC->ui()->factory(),
58  $DIC->resourceStorage()
59  );
60  $this->repository = new DatabaseRepository(new Wrapper($DIC->database()));
61  $this->ui_renderer = $DIC->ui()->renderer();
62  $this->logger = $DIC->logger()->meta();
63  $this->db = $DIC->database();
64  $this->entry_id = $a_entry_id;
65  $this->read();
66  }
67 
71  public static function _getEntries(): array
72  {
73  global $DIC;
74 
75  $ilDB = $DIC->database();
76 
77  $query = "SELECT entry_id FROM il_md_cpr_selections ORDER BY is_default DESC, position ASC";
78  $res = $ilDB->query($query);
79 
80  $entries = [];
81  while ($row = $ilDB->fetchObject($res)) {
82  $entries[] = new ilMDCopyrightSelectionEntry((int) $row->entry_id);
83  }
84  return $entries;
85  }
86 
87  public static function lookupCopyyrightTitle(string $a_cp_string): string
88  {
89  global $DIC;
90 
91  $ilDB = $DIC->database();
92 
93  if (!$entry_id = self::_extractEntryId($a_cp_string)) {
94  return $a_cp_string;
95  }
96 
97  $query = "SELECT title FROM il_md_cpr_selections " .
98  "WHERE entry_id = " . $ilDB->quote($entry_id, ilDBConstants::T_INTEGER) . " ";
99  $res = $ilDB->query($query);
100  $row = $ilDB->fetchObject($res);
101  return $row->title ?? '';
102  }
103 
104  public static function _lookupCopyright(string $a_cp_string): string
105  {
106  global $DIC;
107 
108  $renderer = new Renderer(
109  $DIC->ui()->factory(),
110  $DIC->resourceStorage()
111  );
112  $repository = new DatabaseRepository(new Wrapper($DIC->database()));
113  $ui_renderer = $DIC->ui()->renderer();
114 
115  if (!$entry_id = self::_extractEntryId($a_cp_string)) {
116  return $a_cp_string;
117  }
118 
119  $entry = $repository->getEntry($entry_id);
120  $components = $renderer->toUIComponents($entry->copyrightData());
121 
122  return $ui_renderer->render($components);
123  }
124 
125  public static function _lookupCopyrightForExport(string $a_cp_string): string
126  {
127  global $DIC;
128 
129  $repository = new DatabaseRepository(new Wrapper($DIC->database()));
130 
131  if (!$entry_id = self::_extractEntryId($a_cp_string)) {
132  return $a_cp_string;
133  }
134 
135  $data = $repository->getEntry($entry_id)->copyrightData();
136 
137  return (string) ($data->link() ?? $data->fullName());
138  }
139 
140  public static function lookupCopyrightFromImport(string $copyright_text): int
141  {
142  global $DIC;
143 
144  $repository = new DatabaseRepository(new Wrapper($DIC->database()));
145 
146  // url should be made to match regardless of scheme
147  $normalized_copyright = str_replace('https://', 'http://', $copyright_text);
148 
149  $matches_by_name = null;
150  foreach ($repository->getAllEntries() as $entry) {
151  $entry_link = (string) $entry->copyrightData()->link();
152  $normalized_link = str_replace('https://', 'http://', $entry_link);
153  if ($normalized_link !== '' && str_contains($normalized_copyright, $normalized_link)) {
154  return $entry->id();
155  }
156 
157  if (
158  is_null($matches_by_name) &&
159  trim($copyright_text) === trim($entry->copyrightData()->fullName())
160  ) {
161  $matches_by_name = $entry->id();
162  }
163  }
164 
165  if (!is_null($matches_by_name)) {
166  return $matches_by_name;
167  }
168  return 0;
169  }
170 
171  public static function _extractEntryId(string $a_cp_string): int
172  {
173  if (!preg_match('/il_copyright_entry__([0-9]+)__([0-9]+)/', $a_cp_string, $matches)) {
174  return 0;
175  }
176  if ($matches[1] != IL_INST_ID) {
177  return 0;
178  }
179  return (int) ($matches[2] ?? 0);
180  }
181 
182  public static function isEntry($a_cp_string): bool
183  {
184  if (!preg_match('/il_copyright_entry__([0-9]+)__([0-9]+)/', $a_cp_string)) {
185  return false;
186  }
187  return true;
188  }
189 
190  public function getUsage(): int
191  {
192  // do a time-consuming usage count on demand
193  if ($this->usage === null) {
194  $query = "SELECT count(meta_rights_id) used FROM il_meta_rights " .
195  "WHERE description = " . $this->db->quote(
196  'il_copyright_entry__' . IL_INST_ID . '__' . $this->getEntryId(),
197  'text'
198  );
199 
200  $res = $this->db->query($query);
201  $row = $this->db->fetchObject($res);
202  $this->usage = (int) $row->used;
203  }
204 
205  return $this->usage;
206  }
207 
208  public function getEntryId(): int
209  {
210  return $this->entry_id;
211  }
212 
217  public function getIsDefault(): bool
218  {
219  $query = "SELECT is_default FROM il_md_cpr_selections " .
220  "WHERE entry_id = " . $this->db->quote($this->entry_id, 'integer');
221 
222  $res = $this->db->query($query);
223  $row = $this->db->fetchAssoc($res);
224 
225  return (bool) ($row['is_default'] ?? false);
226  }
227 
228  public function setOutdated(bool $a_value): void
229  {
230  $this->outdated = $a_value;
231  }
232 
233  public function getOutdated(): bool
234  {
235  return $this->outdated;
236  }
237 
238  public static function getDefault(): int
239  {
240  global $DIC;
241 
242  $db = $DIC->database();
243 
244  $query = "SELECT entry_id FROM il_md_cpr_selections " .
245  "WHERE is_default = " . $db->quote(1, 'integer');
246 
247  $res = $db->query($query);
248  $row = $db->fetchAssoc($res);
249 
250  return (int) $row['entry_id'];
251  }
252 
253  public function setTitle(string $a_title): void
254  {
255  $this->title = $a_title;
256  }
257 
258  public function getTitle(): string
259  {
260  return $this->title;
261  }
262 
263  public function setDescription(string $a_desc): void
264  {
265  $this->description = $a_desc;
266  }
267 
268  public function getDescription(): string
269  {
270  return $this->description;
271  }
272 
273  public function setCopyright(string $a_copyright): void
274  {
275  $this->copyright = $a_copyright;
276  }
277 
278  public function getCopyright(): string
279  {
280  return $this->copyright;
281  }
282 
283  public function setOrderPosition(int $a_position): void
284  {
285  $this->order_position = $a_position;
286  }
287 
288  public function getOrderPosition(): int
289  {
290  return $this->order_position;
291  }
292 
293  protected function getNextOrderPosition(): int
294  {
295  $query = "SELECT count(entry_id) total FROM il_md_cpr_selections";
296  $res = $this->db->query($query);
297  $row = $this->db->fetchAssoc($res);
298 
299  return $row['total'] + 1;
300  }
301 
302  public function add(): bool
303  {
304  $next_id = $this->db->nextId('il_md_cpr_selections');
305 
306  $this->db->insert('il_md_cpr_selections', array(
307  'entry_id' => array('integer', $next_id),
308  'title' => array('text', $this->getTitle()),
309  'description' => array('clob', $this->getDescription()),
310  //'copyright' => array('clob', $this->getCopyright()),
311  'outdated' => array('integer', $this->getOutdated()),
312  'position' => array('integer', $this->getNextOrderPosition())
313  ));
314  $this->entry_id = $next_id;
315  return true;
316  }
317 
318  public function update(): bool
319  {
320  $this->db->update('il_md_cpr_selections', array(
321  'title' => array('text', $this->getTitle()),
322  'description' => array('clob', $this->getDescription()),
323  //'copyright' => array('clob', $this->getCopyright()),
324  'outdated' => array('integer', $this->getOutdated()),
325  'position' => array('integer', $this->getOrderPosition())
326  ), array(
327  'entry_id' => array('integer', $this->getEntryId())
328  ));
329  return true;
330  }
331 
332  public function delete(): void
333  {
334  /*$query = "DELETE FROM il_md_cpr_selections " .
335  "WHERE entry_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
336  $res = $this->db->manipulate($query);*/
337  }
338 
339  public function validate(): bool
340  {
341  return $this->getTitle() !== '';
342  }
343 
344  private function read(): void
345  {
346  $entry = $this->repository->getEntry($this->entry_id);
347 
348  $rendered_cp = $this->ui_renderer->render(
349  $this->renderer->toUIComponents($entry->copyrightData())
350  );
351 
352  $this->setTitle($entry->title());
353  $this->setDescription($entry->description());
354  $this->setCopyright($rendered_cp);
355  $this->setOutdated($entry->isOutdated());
356  $this->setOrderPosition($entry->position());
357  }
358 
359  public static function createIdentifier(int $a_entry_id): string
360  {
361  return 'il_copyright_entry__' . IL_INST_ID . '__' . $a_entry_id;
362  }
363 }
$res
Definition: ltiservices.php:69
const IL_INST_ID
Definition: constants.php:40
fetchAssoc(ilDBStatement $statement)
static _lookupCopyrightForExport(string $a_cp_string)
repository()
description: > Example for rendering a repository card
Definition: repository.php:17
quote($value, string $type)
$components
getAllEntries()
The default entry is returned first, and the remaining entries are returned according to their positi...
global $DIC
Definition: shib_login.php:25
query(string $query)
Run a (read-only) Query on the database.
static lookupCopyrightFromImport(string $copyright_text)
toUIComponents(CopyrightDataInterface $copyright)
Returns a string in a legacy UI component if only a string can be returned.
getIsDefault()
Get if the entry is default No setter for this.
static lookupCopyyrightTitle(string $a_cp_string)