ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  private \ilDBInterface $db;
41  private Services $irss;
42 
43  public function __construct()
44  {
45  global $DIC;
47  $this->db = $DIC->database();
48  $this->irss = $DIC->resourceStorage();
49  }
50 
51  public function createIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes): Icon
52  {
53  $icon = new CustomIcon($a_rid, $a_active, $a_is_default_icon, $a_suffixes);
54  foreach ($icon->getSuffixes() as $suffix) {
55  $this->db->insert(
56  self::SUFFIX_TABLE_NAME,
57  [
58  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
59  self::SUFFIX => ['text', $suffix],
60  ]
61  );
62  }
63  $this->db->insert(
64  self::ICON_TABLE_NAME,
65  [
66  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
67  self::ICON_ACTIVE => ['integer', $icon->isActive()],
68  self::IS_DEFAULT_ICON => ['integer', $icon->isDefaultIcon()]
69  ]
70  );
71  return $icon;
72  }
73 
74  public function getIconsForFilter(array $filter): array
75  {
76  $icons = [];
77 
78  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
79  . ", i." . self::ICON_ACTIVE
80  . ", i." . self::IS_DEFAULT_ICON
81  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
82  . " FROM " . self::ICON_TABLE_NAME . " AS i"
83  . " JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
84  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION;
85 
86  if ($filter !== []) {
87  $query .= " WHERE true ";
88  if (($filter['active'] ?? null) !== null && $filter['active'] !== '') {
89  $query .= " AND i.active = " . $this->db->quote($filter['active'], 'integer');
90  }
91 
92  if (($filter['suffixes'] ?? null) !== null && $filter['suffixes'] !== '') {
93  $query .= " AND s.suffix LIKE " . $this->db->quote('%' . $filter['suffixes'] . '%', 'text');
94  }
95 
96  if (($filter['is_default_icon'] ?? null) !== null && $filter['is_default_icon'] !== '') {
97  $query .= " AND i.is_default_icon = " . $this->db->quote($filter['is_default_icon'], 'integer');
98  }
99  }
100 
101  $query .= " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
102 
103  $result = $this->db->query($query);
104 
105  while ($data = $this->db->fetchAssoc($result)) {
106  $icon = new CustomIcon(
107  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
108  (bool) $data[self::ICON_ACTIVE],
109  (bool) $data[self::IS_DEFAULT_ICON],
110  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
111  );
112  $icons[$rid] = $icon;
113  }
114 
115  return $icons;
116  }
117 
121  public function getIcons(): array
122  {
123  $icons = [];
124 
125  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
126  . ", i." . self::ICON_ACTIVE
127  . ", i." . self::IS_DEFAULT_ICON
128  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
129  . " FROM " . self::ICON_TABLE_NAME . " AS i"
130  . " INNER JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
131  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION
132  . " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
133 
134  $result = $this->db->query($query);
135 
136  while ($data = $this->db->fetchAssoc($result)) {
137  $icon = new CustomIcon(
138  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
139  (bool) $data[self::ICON_ACTIVE],
140  (bool) $data[self::IS_DEFAULT_ICON],
141  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
142  );
143  $icons[$rid] = $icon;
144  }
145 
146  return $icons;
147  }
148 
149  public function getIconByRid(string $a_rid): Icon
150  {
151  $icon = new NullIcon();
152 
153  $query = "SELECT i." . self::ICON_RESOURCE_IDENTIFICATION
154  . ", i." . self::ICON_ACTIVE
155  . ", i." . self::IS_DEFAULT_ICON
156  . ", GROUP_CONCAT(s." . self::SUFFIX . ") AS " . self::SUFFIXES
157  . " FROM " . self::ICON_TABLE_NAME . " AS i"
158  . " INNER JOIN " . self::SUFFIX_TABLE_NAME . " AS s"
159  . " ON " . "i." . self::ICON_RESOURCE_IDENTIFICATION . " = " . "s." . self::ICON_RESOURCE_IDENTIFICATION
160  . " WHERE i." . self::ICON_RESOURCE_IDENTIFICATION . " = %s"
161  . " GROUP BY i." . self::ICON_RESOURCE_IDENTIFICATION;
162 
163  $result = $this->db->queryF(
164  $query,
165  ["text"],
166  [$a_rid]
167  );
168 
169  while ($data = $this->db->fetchAssoc($result)) {
170  $icon = new CustomIcon(
171  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION],
172  (bool) $data[self::ICON_ACTIVE],
173  (bool) $data[self::IS_DEFAULT_ICON],
174  $this->turnSuffixesStringIntoArray($data[self::SUFFIXES])
175  );
176  }
177 
178  return $icon;
179  }
180 
181  public function getActiveIconForSuffix(string $a_suffix): Icon
182  {
183  $rid = null;
184  $icon = new NullIcon();
185 
186  // Determine the icon's rid first and then determine the icon by its rid.
187  // This is done because a query like the one in getIconByRid with a where-clause
188  // for the suffix would not return all suffixes of the matching icon.
189  $query = "SELECT s." . self::ICON_RESOURCE_IDENTIFICATION . " FROM " . self::SUFFIX_TABLE_NAME . " AS s"
190  . " INNER JOIN " . self::ICON_TABLE_NAME . " AS i"
191  . " ON s." . self::ICON_RESOURCE_IDENTIFICATION . " = i." . self::ICON_RESOURCE_IDENTIFICATION
192  . " WHERE s." . self::SUFFIX . " = %s AND i." . self::ICON_ACTIVE . " = %s";
193  $result = $this->db->queryF(
194  $query,
195  ["text", "integer"],
196  [$a_suffix, 1]
197  );
198  while ($data = $this->db->fetchAssoc($result)) {
199  $rid = $data[self::ICON_RESOURCE_IDENTIFICATION];
200  }
201 
202  if ($rid !== null) {
203  return $this->getIconByRid($rid);
204  }
205 
206  return $icon;
207  }
208 
209  public function getIconFilePathBySuffix(string $suffix): string
210  {
211  if ($suffix !== "") {
212  $icon = $this->getActiveIconForSuffix($suffix);
213  if (!$icon instanceof NullIcon) {
214  $resource_identification = $this->irss->manage()->find($icon->getRid());
215  if ($resource_identification instanceof ResourceIdentification) {
216  try {
217  return $this->irss->consume()->src($resource_identification)->getSrc(
218  false
219  );
220  } catch (\Throwable) {
221  // error reading the resource, continue
222  }
223  }
224  }
225  }
226  return ilUtil::getImagePath("standard/icon_file.svg");
227  }
228 
229  public function updateIcon(string $a_rid, bool $a_active, bool $a_is_default_icon, array $a_suffixes): Icon
230  {
231  $icon = new CustomIcon($a_rid, $a_active, $a_is_default_icon, $a_suffixes);
232  // 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
233  $this->db->manipulateF(
234  "DELETE FROM " . self::SUFFIX_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
235  ['text'],
236  [$icon->getRid()]
237  );
238  foreach ($icon->getSuffixes() as $suffix) {
239  $this->db->insert(
240  self::SUFFIX_TABLE_NAME,
241  [
242  self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()],
243  self::SUFFIX => ['text', $suffix],
244  ]
245  );
246  }
247  $this->db->update(
248  self::ICON_TABLE_NAME,
249  [
250  self::ICON_ACTIVE => ['integer', $icon->isActive()],
251  self::IS_DEFAULT_ICON => ['integer', $icon->isDefaultIcon()]
252  ],
253  [self::ICON_RESOURCE_IDENTIFICATION => ['text', $icon->getRid()]]
254  );
255  return $icon;
256  }
257 
258  public function deleteIconByRid(string $a_rid): bool
259  {
260  $icon = $this->getIconByRid($a_rid);
261  if (!$icon instanceof NullIcon) {
262  $this->db->manipulateF(
263  "DELETE FROM " . self::SUFFIX_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
264  ['text'],
265  [$a_rid]
266  );
267  $this->db->manipulateF(
268  "DELETE FROM " . self::ICON_TABLE_NAME . " WHERE " . self::ICON_RESOURCE_IDENTIFICATION . " = %s",
269  ['text'],
270  [$a_rid]
271  );
272  return true;
273  }
274  return false;
275  }
276 }
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)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...