ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
TileImage.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 
31 class TileImage
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 Stakeholder $storage_stakeholder,
41  private FlavourDefinition $flavour_definition,
42  private ?ObjectTypeSpecificPropertyProviders $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->object_type_specific_property_providers !== null &&
70  (
71  $specific_tile_image = $this->object_type_specific_property_providers->getObjectTypeSpecificTileImage(
72  $this->object_id,
73  $image_factory,
74  $this->storage_services
75  )
76  ) !== null) {
77  return $specific_tile_image;
78  }
79 
80  $path = \ilUtil::getImagePath('cont_tile/cont_tile_default_' . $this->obj_type . '.svg');
81  if (is_file($path)) {
82  return $image_factory->responsive($path, '');
83  }
84  return $image_factory->responsive(\ilUtil::getImagePath('cont_tile/cont_tile_default.svg'), '');
85  }
86 
87  private function getImageFromIRSS(
88  ImageFactory $image_factory,
89  ResourceIdentification $resource
90  ): ?Image {
91  $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
92  $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray();
93  if ($urls === []) {
94  return null;
95  }
96 
97  $available_widths = $this->flavour_definition->getWidths();
98  array_pop($available_widths);
99 
100  if (!isset($urls[count($available_widths)])) {
101  return null;
102  }
103 
104  $image = $image_factory->responsive($urls[count($available_widths)], '');
105  return array_reduce(
106  $available_widths,
107  function ($carry, $size) use ($urls) {
108  $image = $carry['image']->withAdditionalHighResSource($urls[$carry['counter']], $size / 2);
109  $counter = ++$carry['counter'];
110  return [
111  'image' => $image,
112  'counter' => $counter
113  ];
114  },
115  ['image' => $image, 'counter' => 0]
116  )['image'];
117  }
118 
124  public function getSrcUrlForLegacyForm(): string
125  {
126  if ($this->rid === null || $this->rid === '') {
127  return '';
128  }
129 
130  $resource = $this->storage_services->manage()->find($this->rid);
131  if ($resource === null) {
132  return '';
133  }
134 
135  $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
136  $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray(false);
137 
138  return array_pop($urls) ?? '';
139  }
140 
141  public function cloneFor(int $new_object_id): self
142  {
143  $clone = clone $this;
144  $clone->object_id = $new_object_id;
145 
146  if ($this->rid !== null
147  && ($rid = $this->storage_services->manage()->find($this->rid)) !== null) {
148  $i = $this->storage_services->manage()->clone($rid);
149  $clone->rid = $i->serialize();
150  }
151 
152  return $clone;
153  }
154 
155  public function createFromImportDir(string $source_dir): void
156  {
157  $sourceFS = LegacyPathHelper::deriveFilesystemFrom($source_dir);
158  $sourceDir = LegacyPathHelper::createRelativePath($source_dir);
159  $sourceList = $sourceFS->listContents($sourceDir, true);
160 
161  foreach ($sourceList as $item) {
162  if ($item->isDir()) {
163  continue;
164  }
165 
166  $path = $source_dir . DIRECTORY_SEPARATOR . basename($item->getPath());
167  $stream = new Stream(fopen($path, 'r'));
168  $i = $this->storage_services->manage()->stream($stream, $this->storage_stakeholder, 'Tile Image');
169  $this->rid = $i->serialize();
170  }
171  }
172 }
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getImageFromIRSS(ImageFactory $image_factory, ResourceIdentification $resource)
Definition: TileImage.php:87
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
__construct(private int $object_id, private ?string $obj_type, private ?string $rid, private ResourceStorageServices $storage_services, private Stakeholder $storage_stakeholder, private FlavourDefinition $flavour_definition, private ?ObjectTypeSpecificPropertyProviders $object_type_specific_property_providers)
Definition: TileImage.php:35
This is how the factory for UI elements looks.
Definition: Factory.php:27