ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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__ . "/../../../../templates/default/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  {
73  }
74 
78  public function achieve(Environment $environment): Environment
79  {
82  $environment
83  );
88 
89  $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
90  $default_icon_filenames = preg_grep("/^icon_file_/", $scan_result);
91  foreach ($default_icon_filenames as $default_icon_filename) {
92  $icon_file_prefix = "icon_file_";
93  $icon_file_suffix = ".svg";
94  $suffix = str_replace($icon_file_prefix, "", $default_icon_filename);
95  $suffix = str_replace($icon_file_suffix, "", $suffix);
96 
97  //check if there is an existing db entry for a default icon with this suffix
98  $query = "SELECT * FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " AS s"
99  . " INNER JOIN " . IconDatabaseRepository::ICON_TABLE_NAME . " AS i"
101  . " WHERE s." . IconDatabaseRepository::SUFFIX . " = %s AND i." . IconDatabaseRepository::IS_DEFAULT_ICON . " = %s";
102  $statement = $db->queryF(
103  $query,
104  ['text', 'integer'],
105  [$suffix, 1]
106  );
107  $num_matches = $db->numRows($statement);
108 
109  //skip copying the file to the resource storage and creating the new db entries if this default icon has been added already
110  if ($num_matches > 0) {
111  continue;
112  }
113 
114  $path_default_file_icon = self::PATH_DEFAULT_ICON_DIR . $default_icon_filename;
115 
116  //move copy of default icon in temp dir to resource storage
117  $resource_identification = $helper->movePathToStorage(
118  $path_default_file_icon,
119  6,
120  null,
121  null,
122  true
123  );
124 
125  if ($resource_identification === null) {
126  continue;
127  }
128  $rid = $resource_identification->serialize();
129  //create icon & icon suffix db entry for newly added default icon
130  $db->insert(
132  [
134  IconDatabaseRepository::SUFFIX => ['text', $suffix],
135  ]
136  );
137  $db->insert(
139  [
141  IconDatabaseRepository::ICON_ACTIVE => ['integer', true],
142  IconDatabaseRepository::IS_DEFAULT_ICON => ['integer', true]
143  ]
144  );
145  }
146 
147  return $environment;
148  }
149 
153  public function isApplicable(Environment $environment): bool
154  {
159 
160  // this removes all icons from the database and therefore forces a re-creation of the default icons
161  if ($this->reset_all) {
162  if ($db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
163  $db->manipulate("DELETE FROM " . IconDatabaseRepository::ICON_TABLE_NAME);
164  }
166  $db->manipulate("DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME);
167  }
168  return true;
169  }
170 
171  // this removes all default icons from the database and therefore forces a re-creation of the default icons but keeps custom icons
172  if ($this->reset_default && $db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
173  // we can proceed if icon tables are not available
174  if (
177  ) {
178  return true;
179  }
180 
181  // selects the rids of all default icons
182  $query = "SELECT rid FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
183  $statement = $db->query($query);
184  $rids = [];
185  while ($row = $db->fetchAssoc($statement)) {
187  }
188  // delete all default icons
189  $db->manipulate(
191  );
192 
193  // now we deactivate all custom icons which override the default icons. therefore we must search for the suffixes related to the default icons.
194  // read all suffixes of the default icons
195  $query = "SELECT " . IconDatabaseRepository::SUFFIX . " FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
197  $rids,
198  false,
199  'text'
200  );
201  $statement = $db->query($query);
202  $suffixes = [];
203  while ($row = $db->fetchAssoc($statement)) {
204  $suffixes[] = $row[IconDatabaseRepository::SUFFIX];
205  }
206  // remove all entries of the default icons
207  $db->manipulate(
208  "DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
210  $rids,
211  false,
212  'text'
213  )
214  );
215 
216  $rids = [];
217  if (!empty($suffixes)) {
218  // collect rids of custom icons which override the default icons
221  $suffixes,
222  false,
223  'text'
224  );
225  $statement = $db->query($query);
226 
227  while ($row = $db->fetchAssoc($statement)) {
229  }
230  }
231 
232  // deactivate all custom icons which override the default icons
233  if (!empty($rids)) {
234  $db->manipulate(
235  "UPDATE " . IconDatabaseRepository::ICON_TABLE_NAME . " SET " . IconDatabaseRepository::ICON_ACTIVE . " = 0 WHERE " . $db->in(
237  $rids,
238  false,
239  'text'
240  )
241  );
242  }
243 
244  // clean up orphaned suffixes
245  $db->manipulate(
248  . " WHERE i." . IconDatabaseRepository::ICON_RESOURCE_IDENTIFICATION . " IS NULL"
249  );
250 
251  return true;
252  }
253 
254  if ($db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
255  $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
256  $num_default_icons_in_dir = is_countable(preg_grep("/^icon_file_/", $scan_result)) ? count(
257  preg_grep("/^icon_file_/", $scan_result)
258  ) : 0;
259 
260  $query = "SELECT * FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
261  $statement = $db->query($query);
262  $num_default_icons_in_db = $db->numRows($statement);
263 
264  if ($num_default_icons_in_db < $num_default_icons_in_dir) {
265  return true;
266  }
267  }
268  return false;
269  }
270 }
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)