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