ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DatabaseRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Copyright;
22 
23 use ILIAS\Data\URI;
24 
26 {
27  protected \ilDBInterface $db;
28 
29  public function __construct(\ilDBInterface $db)
30  {
31  $this->db = $db;
32  }
33 
34  public function getEntry(int $id): EntryInterface
35  {
36  $res = $this->db->query(
37  'SELECT * FROM il_md_cpr_selections WHERE entry_id = ' .
38  $this->db->quote($id, \ilDBConstants::T_INTEGER)
39  );
40 
41  if ($row = $this->db->fetchAssoc($res)) {
42  return $this->entryFromRow($row);
43  }
44  return new NullEntry();
45  }
46 
47  public function getAllEntries(): \Generator
48  {
49  $res = $this->db->query(
50  'SELECT * FROM il_md_cpr_selections
51  ORDER BY is_default DESC, position ASC'
52  );
53 
54  while ($row = $this->db->fetchAssoc($res)) {
55  yield $this->entryFromRow($row);
56  }
57  }
58 
59  public function getActiveEntries(): \Generator
60  {
61  $res = $this->db->query(
62  'SELECT * FROM il_md_cpr_selections WHERE outdated = 0
63  ORDER BY is_default DESC, position ASC'
64  );
65 
66  while ($row = $this->db->fetchAssoc($res)) {
67  yield $this->entryFromRow($row);
68  }
69  }
70 
71  public function getDefaultEntry(): EntryInterface
72  {
73  $res = $this->db->query(
74  'SELECT * FROM il_md_cpr_selections WHERE is_default = 1'
75  );
76 
77  if ($row = $this->db->fetchAssoc($res)) {
78  return $this->entryFromRow($row);
79  }
80  return new NullEntry();
81  }
82 
83  protected function entryFromRow(array $row): EntryInterface
84  {
85  $data = new CopyrightData(
86  $row['full_name'] ?? '',
87  !empty($row['link'] ?? '') ? $this->getURI($row['link']) : null,
88  !empty($row['image_link']) ? $this->getURI($row['image_link']) : null,
89  $row['image_file'] ?? '',
90  $row['alt_text'] ?? '',
91  $row['is_default'] ? true : false
92  );
93 
94  return new Entry(
95  $row['entry_id'],
96  $row['title'] ?? '',
97  $row['description'] ?? '',
98  $row['is_default'] ? true : false,
99  $row['outdated'] ? true : false,
100  $row['position'] ?? 0,
101  $data
102  );
103  }
104 
105  protected function getURI(string $uri): URI
106  {
107  return new URI($uri);
108  }
109 
110  public function deleteEntry(int $id): void
111  {
112  $this->db->manipulate(
113  'DELETE FROM il_md_cpr_selections WHERE entry_id = ' .
114  $this->db->quote($id, \ilDBConstants::T_INTEGER)
115  );
116  }
117 
118  public function createEntry(
119  string $title,
120  string $description = '',
121  bool $is_outdated = false,
122  string $full_name = '',
123  ?URI $link = null,
124  URI|string $image = '',
125  string $alt_text = ''
126  ): int {
127  $this->checkTitle($title);
128 
129  $next_id = $this->db->nextId('il_md_cpr_selections');
130  if (is_string($image)) {
131  $image_link = '';
132  $image_file = $image;
133  } else {
134  $image_link = (string) $image;
135  $image_file = '';
136  }
137 
138  $this->db->insert(
139  'il_md_cpr_selections',
140  [
141  'entry_id' => [\ilDBConstants::T_INTEGER, $next_id],
142  'title' => [\ilDBConstants::T_TEXT, $title],
143  'description' => [\ilDBConstants::T_TEXT, $description],
144  'is_default' => [\ilDBConstants::T_INTEGER, 0],
145  'outdated' => [\ilDBConstants::T_INTEGER, (int) $is_outdated],
146  'position' => [\ilDBConstants::T_INTEGER, $this->getNextPosition()],
147  'full_name' => [\ilDBConstants::T_TEXT, $full_name],
148  'link' => [\ilDBConstants::T_TEXT, (string) $link],
149  'image_link' => [\ilDBConstants::T_TEXT, $image_link],
150  'image_file' => [\ilDBConstants::T_TEXT, $image_file],
151  'alt_text' => [\ilDBConstants::T_TEXT, $alt_text],
152  'migrated' => [\ilDBConstants::T_INTEGER, 1]
153  ]
154  );
155 
156  return $next_id;
157  }
158 
159  protected function getNextPosition(): int
160  {
161  $res = $this->db->query(
162  'SELECT MAX(position) AS max FROM il_md_cpr_selections WHERE is_default = 0'
163  );
164  $row = $this->db->fetchAssoc($res);
165 
166  return isset($row['max']) ? $row['max'] + 1 : 0;
167  }
168 
169  public function updateEntry(
170  int $id,
171  string $title,
172  string $description = '',
173  bool $is_outdated = false,
174  string $full_name = '',
175  ?URI $link = null,
176  URI|string $image = '',
177  string $alt_text = ''
178  ): void {
179  $this->checkTitle($title);
180 
181  if (is_string($image)) {
182  $image_link = '';
183  $image_file = $image;
184  } else {
185  $image_link = (string) $image;
186  $image_file = '';
187  }
188 
189  $this->db->update(
190  'il_md_cpr_selections',
191  [
192  'title' => [\ilDBConstants::T_TEXT, $title],
193  'description' => [\ilDBConstants::T_TEXT, $description],
194  'outdated' => [\ilDBConstants::T_INTEGER, (int) $is_outdated],
195  'full_name' => [\ilDBConstants::T_TEXT, $full_name],
196  'link' => [\ilDBConstants::T_TEXT, (string) $link],
197  'image_link' => [\ilDBConstants::T_TEXT, $image_link],
198  'image_file' => [\ilDBConstants::T_TEXT, $image_file],
199  'alt_text' => [\ilDBConstants::T_TEXT, $alt_text]
200  ],
201  [
202  'entry_id' => [\ilDBConstants::T_INTEGER, $id]
203  ]
204  );
205  }
206 
207  protected function checkTitle(string $title): void
208  {
209  if ($title === '') {
210  throw new \ilMDCopyrightException(
211  'Copyright entries can not have an empty title'
212  );
213  }
214  }
215 
216  public function reorderEntries(int ...$ids): void
217  {
218  $pos = 0;
219  $default_id = $this->getDefaultID();
220  foreach ($ids as $id) {
221  if ($id === $default_id) {
222  continue;
223  }
224  $this->updatePosition($id, $pos);
225  $pos++;
226  }
227  }
228 
229  protected function getDefaultID(): int
230  {
231  $res = $this->db->query(
232  'SELECT entry_id FROM il_md_cpr_selections WHERE is_default = 1'
233  );
234 
235  if ($row = $this->db->fetchAssoc($res)) {
236  return $row['entry_id'] ?? 0;
237  }
238  return 0;
239  }
240 
241  protected function updatePosition(int $id, int $position): void
242  {
243  $this->db->update(
244  'il_md_cpr_selections',
245  ['position' => [\ilDBConstants::T_INTEGER, $position]],
246  ['entry_id' => [\ilDBConstants::T_INTEGER, $id]]
247  );
248  }
249 }
$res
Definition: ltiservices.php:69
createEntry(string $title, string $description='', bool $is_outdated=false, string $full_name='', ?URI $link=null, URI|string $image='', string $alt_text='')
Returns the ID of the newly created entry.
getAllEntries()
The default entry is returned first, and the remaining entries are returned according to their positi...
reorderEntries(int ... $ids)
Updates the position of entries according to the order their IDs are passed.
updateEntry(int $id, string $title, string $description='', bool $is_outdated=false, string $full_name='', ?URI $link=null, URI|string $image='', string $alt_text='')
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:18
getActiveEntries()
The default entry is returned first, and the remaining entries are returned according to their positi...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23