ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
IconDatabaseRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\File\Icon;
22 
24 use ilUtil;
26 
31 {
32  public const ICON_TABLE_NAME = 'il_file_icon';
33  public const ICON_RESOURCE_IDENTIFICATION = 'rid';
34  public const ICON_ACTIVE = 'active';
35  public const IS_DEFAULT_ICON = 'is_default_icon';
36  public const SUFFIX_TABLE_NAME = 'il_file_icon_suffixes';
37  public const SUFFIX = 'suffix';
38  public const SUFFIXES = 'suffixes';
39 
40  public function __construct(
41  private ?\ilDBInterface $db = null,
42  private ?Services $irss = null
43  ) {
44  global $DIC;
46  $this->db ??= $DIC->database();
47  $this->irss ??= $DIC->resourceStorage();
48  }
49 
50  public function createIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes): Icon
51  {
52  $icon = new CustomIcon($a_rid, $a_active, $a_is_default_icon, $a_suffixes);
53  foreach ($icon->getSuffixes() as $suffix) {
54  $this->db->insert(
55  self::SUFFIX_TABLE_NAME,
56  [
57  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
58  self::SUFFIX => ['text', $suffix],
59  ]
60  );
61  }
62  $this->db->insert(
63  self::ICON_TABLE_NAME,
64  [
65  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
66  self::ICON_ACTIVE => ['integer', $icon->isActive()],
67  self::IS_DEFAULT_ICON => ['integer', $icon->isDefaultIcon()]
68  ]
69  );
70  return $icon;
71  }
72 
73 
74  public function getIconsForFilter(array $filter): array
75  {
76  $icons = [];
77 
78  $rid_filter = null;
79  // due to the database schema, the suffixes filter has to be handled separately
80  if ($filter !== [] && ($filter['suffixes'] ?? null) !== null && $filter['suffixes'] !== '') {
81  $suffixes = explode(',', (string) $filter['suffixes']);
82  $suffixes = array_map(fn($suffix): string => $this->db->quote(trim((string) $suffix), 'text'), $suffixes);
83  $q = "SELECT rid FROM " . self::SUFFIX_TABLE_NAME . " WHERE suffix IN (" . implode(',', $suffixes) . ")";
84  $rid_filter = $this->db->fetchAll($this->db->query($q));
85  $rid_filter = array_map(fn($row) => $row['rid'], $rid_filter);
86  }
87 
88  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
89  . ", i." . self::ICON_ACTIVE
90  . ", i." . self::IS_DEFAULT_ICON
91  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
92  . " FROM " . self::ICON_TABLE_NAME . " AS i"
93  . " JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
94  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION;
95 
96  if ($filter !== []) {
97  $query .= " WHERE true ";
98  if (($filter['active'] ?? null) !== null && $filter['active'] !== '') {
99  $query .= " AND i.active = " . $this->db->quote($filter['active'], 'integer');
100  }
101 
102  if ($rid_filter) {
103  $rid_filter = array_map(fn($rid): string => $this->db->quote(trim((string) $rid), 'text'), $rid_filter);
104  $query .= " AND i.rid IN (" . implode(',', $rid_filter) . ")";
105  }
106 
107  if (($filter['is_default_icon'] ?? null) !== null && $filter['is_default_icon'] !== '') {
108  $query .= " AND i.is_default_icon = " . $this->db->quote($filter['is_default_icon'], 'integer');
109  }
110  }
111 
112  $query .= " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
113 
114  $result = $this->db->query($query);
115 
116  while ($data = $this->db->fetchAssoc($result)) {
117  $icon = new CustomIcon(
118  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
119  (bool) $data[self::ICON_ACTIVE],
120  (bool) $data[self::IS_DEFAULT_ICON],
121  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
122  );
123  $icons[$rid] = $icon;
124  }
125 
126  return $icons;
127  }
128 
132  public function getIcons(): array
133  {
134  $icons = [];
135 
136  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
137  . ", i." . self::ICON_ACTIVE
138  . ", i." . self::IS_DEFAULT_ICON
139  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
140  . " FROM " . self::ICON_TABLE_NAME . " AS i"
141  . " INNER JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
142  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION
143  . " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
144 
145  $result = $this->db->query($query);
146 
147  while ($data = $this->db->fetchAssoc($result)) {
148  $icon = new CustomIcon(
149  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
150  (bool) $data[self::ICON_ACTIVE],
151  (bool) $data[self::IS_DEFAULT_ICON],
152  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
153  );
154  $icons[$rid] = $icon;
155  }
156 
157  return $icons;
158  }
159 
160  public function getIconByRid(string $a_rid): Icon
161  {
162  $icon = new NullIcon();
163 
164  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
165  . ", i." . self::ICON_ACTIVE
166  . ", i." . self::IS_DEFAULT_ICON
167  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
168  . " FROM " . self::ICON_TABLE_NAME . " AS i"
169  . " INNER JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
170  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION
171  . " WHERE i." . self::ICON_RESOURCE_IDENTIFICATION . " = %s"
172  . " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
173 
174  $result = $this->db->queryF(
175  $query,
176  ["text"],
177  [$a_rid]
178  );
179 
180  while ($data = $this->db->fetchAssoc($result)) {
181  $icon = new CustomIcon(
182  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
183  (bool) $data[self::ICON_ACTIVE],
184  (bool) $data[self::IS_DEFAULT_ICON],
185  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
186  );
187  }
188 
189  return $icon;
190  }
191 
192  public function getActiveIconForSuffix(string $a_suffix): Icon
193  {
194  $rid = null;
195  $icon = new NullIcon();
196 
197  // Determine the icon's rid first and then determine the icon by its rid.
198  // This is done because a query like the one in getIconByRid with a where-clause
199  // for the suffix would not return all suffixes of the matching icon.
200  $query = "SELECT s." . self::ICON_RESOURCE_IDENTIFICATION . " FROM " . self::SUFFIX_TABLE_NAME . " AS s"
201  . " INNER JOIN " . self::ICON_TABLE_NAME . " AS i"
202  . " ON s." . self::ICON_RESOURCE_IDENTIFICATION . " = i." . self::ICON_RESOURCE_IDENTIFICATION
203  . " WHERE s." . self::SUFFIX . " = %s AND i." . self::ICON_ACTIVE . " = %s";
204  $result = $this->db->queryF(
205  $query,
206  ["text", "integer"],
207  [$a_suffix, 1]
208  );
209  while ($data = $this->db->fetchAssoc($result)) {
210  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION];
211  }
212 
213  if ($rid !== null) {
214  return $this->getIconByRid($rid);
215  }
216 
217  return $icon;
218  }
219 
220  public function getIconFilePathBySuffix(string $suffix): string
221  {
222  if ($suffix !== "") {
223  $icon = $this->getActiveIconForSuffix($suffix);
224  if (!$icon instanceof NullIcon) {
225  $resource_identification = $this->irss->manage()->find($icon->getRid());
226  if ($resource_identification instanceof ResourceIdentification) {
227  try {
228  return $this->irss->consume()->src($resource_identification)->getSrc(
229  false
230  );
231  } catch (\Throwable) {
232  // error reading the resource, continue
233  }
234  }
235  }
236  }
237  return ilUtil::getImagePath("standard/icon_file.svg");
238  }
239 
240  public function updateIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes): Icon
241  {
242  $icon = new CustomIcon($a_rid, $a_active, $a_is_default_icon, $a_suffixes);
243  // Delete the old suffix entries of the given icon first as they can not be identified by form input and therefore cannot be overwritten - only deleted and created anew
244  $this->db->manipulateF(
245  "DELETE FROM " . self::SUFFIX_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
246  ['text'],
247  [$icon->getRid()]
248  );
249  foreach ($icon->getSuffixes() as $suffix) {
250  $this->db->insert(
251  self::SUFFIX_TABLE_NAME,
252  [
253  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
254  self::SUFFIX => ['text', $suffix],
255  ]
256  );
257  }
258  $this->db->update(
259  self::ICON_TABLE_NAME,
260  [
261  self::ICON_ACTIVE => ['integer', $icon->isActive()],
262  self::IS_DEFAULT_ICON => ['integer', $icon->isDefaultIcon()]
263  ],
264  [self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()]]
265  );
266  return $icon;
267  }
268 
269  public function deleteIconByRid(string $a_rid): bool
270  {
271  $icon = $this->getIconByRid($a_rid);
272  if (!$icon instanceof NullIcon) {
273  $this->db->manipulateF(
274  "DELETE FROM " . self::SUFFIX_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
275  ['text'],
276  [$a_rid]
277  );
278  $this->db->manipulateF(
279  "DELETE FROM " . self::ICON_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
280  ['text'],
281  [$a_rid]
282  );
283  return true;
284  }
285  return false;
286  }
287 }
createIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes)
updateIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
__construct(Container $dic, ilPlugin $plugin)
__construct(private ?\ilDBInterface $db=null, private ?Services $irss=null)
$q
Definition: shib_logout.php:21