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