ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjectTileImage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 use ILIAS\UI\Component\Image\Factory as ImageFactory;
30 
32 {
33  protected string $ext = '';
34 
35  public function __construct(
36  private int $object_id,
37  private ?string $obj_type,
38  private ?string $rid,
39  private ResourceStorageServices $storage_services,
40  private ilObjectTileImageStakeholder $storage_stakeholder,
41  private ilObjectTileImageFlavourDefinition $flavour_definition,
42  private ?ilObjectTypeSpecificPropertyProviders $object_type_specific_property_providers
43  ) {
44  }
45 
46  public function getRid(): ?string
47  {
48  return $this->rid;
49  }
50 
51  public function withRid(?string $rid): self
52  {
53  $clone = clone $this;
54  $clone->rid = $rid;
55  return $clone;
56  }
57 
58  public function getImage(
59  ImageFactory $image_factory
60  ): Image {
61  if ($this->rid !== null
62  && $this->rid !== ''
63  && ($resource = $this->storage_services->manage()->find($this->rid)) !== null
64  && ($from_IRSS = $this->getImageFromIRSS($image_factory, $resource)) !== null
65  ) {
66  return $from_IRSS;
67  }
68 
69  if ($this->exists()) {
70  return $image_factory->responsive($this->getFullPath(), '');
71  }
72 
73  if ($this->object_type_specific_property_providers !== null &&
74  (
75  $specific_tile_image = $this->object_type_specific_property_providers->getObjectTypeSpecificTileImage(
76  $this->object_id,
77  $image_factory,
78  $this->storage_services
79  )
80  ) !== null) {
81  return $specific_tile_image;
82  }
83 
84  $path = \ilUtil::getImagePath('cont_tile/cont_tile_default_' . $this->obj_type . '.svg');
85  if (is_file($path)) {
86  return $image_factory->responsive($path, '');
87  }
88  return $image_factory->responsive(\ilUtil::getImagePath('cont_tile/cont_tile_default.svg'), '');
89  }
90 
91  private function getImageFromIRSS(
92  ImageFactory $image_factory,
93  ResourceIdentification $resource
94  ): ?Image {
95  $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
96  $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray();
97  if ($urls === []) {
98  return null;
99  }
100 
101  $available_widths = $this->flavour_definition->getWidths();
102  array_pop($available_widths);
103 
104  if (!isset($urls[count($available_widths)])) {
105  return null;
106  }
107 
108  $image = $image_factory->responsive($urls[count($available_widths)], '');
109  return array_reduce(
110  $available_widths,
111  function ($carry, $size) use ($urls) {
112  $image = $carry['image']->withAdditionalHighResSource($urls[$carry['counter']], $size / 2);
113  $counter = ++$carry['counter'];
114  return [
115  'image' => $image,
116  'counter' => $counter
117  ];
118  },
119  ['image' => $image, 'counter' => 0]
120  )['image'];
121  }
122 
128  public function getSrcUrlForLegacyForm(): string
129  {
130  if ($this->rid !== null && $this->rid !== '') {
131  $resource = $this->storage_services->manage()->find($this->rid);
132  if ($resource === null) {
133  return '';
134  }
135 
136  $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
137  $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray(false);
138 
139  return array_pop($urls) ?? '';
140  }
141 
142  if (!$this->exists()) {
143  return '';
144  }
145  return $this->getFullPath();
146  }
147 
148  public function deleteLegacyTileImage(): void
149  {
150  if ($this->exists()) {
151  unlink(ILIAS_ABSOLUTE_PATH . DIRECTORY_SEPARATOR . $this->getFullPath());
152  rmdir(dirname(ILIAS_ABSOLUTE_PATH . DIRECTORY_SEPARATOR . $this->getFullPath()));
153  \ilContainer::_deleteContainerSettings($this->object_id, 'tile_image');
154  }
155  }
156 
157  public function cloneFor(int $new_object_id): self
158  {
159  $clone = clone $this;
160  $clone->object_id = $new_object_id;
161 
162  if ($this->rid !== null
163  && $this->rid !== ''
164  && ($resource = $this->storage_services->manage()->find($this->rid)) !== null) {
165  $i = $this->storage_services->manage()->clone($resource);
166  $clone->rid = $i->serialize();
167  }
168 
169  return $clone;
170  }
171 
172  private function exists(): bool
173  {
174  if (!\ilContainer::_lookupContainerSetting($this->object_id, 'tile_image', '0')) {
175  return false;
176  }
177 
178  return is_file($this->getFullPath());
179  }
180 
181  private function getFullPath(): string
182  {
183  if ($this->ext === '') {
184  $this->ext = \ilContainer::_lookupContainerSetting($this->object_id, 'tile_image');
185  }
186 
187  return implode(
188  DIRECTORY_SEPARATOR,
189  [
191  'obj_data',
192  'tile_image',
193  'tile_image_' . $this->object_id,
194  'tile_image.' . $this->ext
195  ]
196  );
197  }
198 
199  public function createFromImportDir(string $source_dir): void
200  {
201  $sourceFS = LegacyPathHelper::deriveFilesystemFrom($source_dir);
202  $sourceDir = LegacyPathHelper::createRelativePath($source_dir);
203  $sourceList = $sourceFS->listContents($sourceDir, true);
204 
205  foreach ($sourceList as $item) {
206  if ($item->isDir()) {
207  continue;
208  }
209 
210  $path = $source_dir . DIRECTORY_SEPARATOR . basename($item->getPath());
211  $stream = new Stream(fopen($path, 'r'));
212  $i = $this->storage_services->manage()->stream($stream, $this->storage_stakeholder, 'Tile Image');
213  $this->rid = $i->serialize();
214  }
215  }
216 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
$path
Definition: ltiservices.php:32
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(private int $object_id, private ?string $obj_type, private ?string $rid, private ResourceStorageServices $storage_services, private ilObjectTileImageStakeholder $storage_stakeholder, private ilObjectTileImageFlavourDefinition $flavour_definition, private ?ilObjectTypeSpecificPropertyProviders $object_type_specific_property_providers)
getImageFromIRSS(ImageFactory $image_factory, ResourceIdentification $resource)
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
This is how the factory for UI elements looks.
Definition: Factory.php:27
static _deleteContainerSettings(int $a_id, string $a_keyword="", bool $a_keyword_like=false)