ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilObjFileDefaultIconsObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\File\Icon;
22 
25 use ilDBInterface;
26 
31 {
32  private const PATH_DEFAULT_ICON_DIR = __DIR__ . "/../../../../../public/assets/images/default_file_icons/";
33 
34  public function __construct(
35  private bool $reset_default = false,
36  private bool $reset_all = false
37  ) {
38  }
39 
43  public function getHash(): string
44  {
45  return hash(
46  "sha256",
47  get_class($this)
48  );
49  }
50 
54  public function getLabel(): string
55  {
56  return "Creation of the default icons for file objects.";
57  }
58 
62  public function isNotable(): bool
63  {
64  return true;
65  }
66 
70  public function getPreconditions(Environment $environment): array
71  {
72  return array_merge(
74  [
76  ]
77  );
78  }
79 
83  public function achieve(Environment $environment): Environment
84  {
87  $environment
88  );
92  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
93 
94  $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
95  $default_icon_filenames = preg_grep("/^icon_file_/", $scan_result);
96  foreach ($default_icon_filenames as $default_icon_filename) {
97  $icon_file_prefix = "icon_file_";
98  $icon_file_suffix = ".svg";
99  $suffix = str_replace($icon_file_prefix, "", $default_icon_filename);
100  $suffix = str_replace($icon_file_suffix, "", $suffix);
101 
102  //check if there is an existing db entry for a default icon with this suffix
103  $query = "SELECT * FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " AS s"
104  . " INNER JOIN " . IconDatabaseRepository::ICON_TABLE_NAME . " AS i"
106  . " WHERE s." . IconDatabaseRepository::SUFFIX . " = %s AND i." . IconDatabaseRepository::IS_DEFAULT_ICON . " = %s";
107  $statement = $db->queryF(
108  $query,
109  ['text', 'integer'],
110  [$suffix, 1]
111  );
112  $num_matches = $db->numRows($statement);
113 
114  //skip copying the file to the resource storage and creating the new db entries if this default icon has been added already
115  if ($num_matches > 0) {
116  continue;
117  }
118 
119  $path_default_file_icon = self::PATH_DEFAULT_ICON_DIR . $default_icon_filename;
120 
121  //move copy of default icon in temp dir to resource storage
122  $resource_identification = $helper->movePathToStorage(
123  $path_default_file_icon,
124  6,
125  null,
126  null,
127  true
128  );
129 
130  if ($resource_identification === null) {
131  continue;
132  }
133  $rid = $resource_identification->serialize();
134  //create icon & icon suffix db entry for newly added default icon
135  $db->insert(
137  [
139  IconDatabaseRepository::SUFFIX => ['text', $suffix],
140  ]
141  );
142  $db->insert(
144  [
146  IconDatabaseRepository::ICON_ACTIVE => ['integer', true],
147  IconDatabaseRepository::IS_DEFAULT_ICON => ['integer', true]
148  ]
149  );
150  }
151 
152  return $environment;
153  }
154 
158  public function isApplicable(Environment $environment): bool
159  {
163  $db = $environment->getResource(Environment::RESOURCE_DATABASE);
164 
165  // this removes all icons from the database and therefore forces a re-creation of the default icons
166  if ($this->reset_all) {
167  if ($db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
168  $db->manipulate("DELETE FROM " . IconDatabaseRepository::ICON_TABLE_NAME);
169  }
171  $db->manipulate("DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME);
172  }
173  return true;
174  }
175 
176  // this removes all default icons from the database and therefore forces a re-creation of the default icons but keeps custom icons
177  if ($this->reset_default && $db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
178  // we can proceed if icon tables are not available
179  if (
182  ) {
183  return true;
184  }
185 
186  // selects the rids of all default icons
187  $query = "SELECT rid FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
188  $statement = $db->query($query);
189  $rids = [];
190  while ($row = $db->fetchAssoc($statement)) {
192  }
193  // delete all default icons
194  $db->manipulate(
196  );
197 
198  // now we deactivate all custom icons which override the default icons. therefore we must search for the suffixes related to the default icons.
199  // read all suffixes of the default icons
200  $query = "SELECT " . IconDatabaseRepository::SUFFIX . " FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
202  $rids,
203  false,
204  'text'
205  );
206  $statement = $db->query($query);
207  $suffixes = [];
208  while ($row = $db->fetchAssoc($statement)) {
209  $suffixes[] = $row[IconDatabaseRepository::SUFFIX];
210  }
211  // remove all entries of the default icons
212  $db->manipulate(
213  "DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
215  $rids,
216  false,
217  'text'
218  )
219  );
220 
221  $rids = [];
222  if (!empty($suffixes)) {
223  // collect rids of custom icons which override the default icons
226  $suffixes,
227  false,
228  'text'
229  );
230  $statement = $db->query($query);
231 
232  while ($row = $db->fetchAssoc($statement)) {
234  }
235  }
236 
237  // deactivate all custom icons which override the default icons
238  if (!empty($rids)) {
239  $db->manipulate(
240  "UPDATE " . IconDatabaseRepository::ICON_TABLE_NAME . " SET " . IconDatabaseRepository::ICON_ACTIVE . " = 0 WHERE " . $db->in(
242  $rids,
243  false,
244  'text'
245  )
246  );
247  }
248 
249  // clean up orphaned suffixes
250  $db->manipulate(
253  . " WHERE i." . IconDatabaseRepository::ICON_RESOURCE_IDENTIFICATION . " IS NULL"
254  );
255 
256  return true;
257  }
258 
259  if ($db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
260  $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
261  $num_default_icons_in_dir = is_countable(preg_grep("/^icon_file_/", $scan_result)) ? count(
262  preg_grep("/^icon_file_/", $scan_result)
263  ) : 0;
264 
265  $query = "SELECT * FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
266  $statement = $db->query($query);
267  $num_default_icons_in_db = $db->numRows($statement);
268 
269  if ($num_default_icons_in_db < $num_default_icons_in_dir) {
270  return true;
271  }
272  }
273  return false;
274  }
275 }
An objective is a desired state of the system that is supposed to be created by the setup...
Definition: Objective.php:30
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
achieve(Environment $environment)
Objectives can be achieved.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
__construct(private bool $reset_default=false, private bool $reset_all=false)