ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
TileImage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\ResourceStorage\Services as ResourceStorageServices;
28use ILIAS\UI\Component\Image\Factory as ImageFactory;
31
33{
35
36 protected string $ext = '';
37
38 public function __construct(
39 private int $object_id,
40 private ?string $obj_type,
41 private ?string $rid,
42 private ResourceStorageServices $storage_services,
43 private Stakeholder $storage_stakeholder,
44 private FlavourDefinition $flavour_definition,
45 private ?ObjectTypeSpecificPropertyProviders $object_type_specific_property_providers
46 ) {
47 }
48
49 public function getRid(): ?string
50 {
51 return $this->rid;
52 }
53
54 public function withRid(?string $rid): self
55 {
56 $clone = clone $this;
57 $clone->rid = $rid;
58 return $clone;
59 }
60
61 public function getImage(
62 ImageFactory $image_factory
63 ): Image {
64 if ($this->rid !== null
65 && $this->rid !== ''
66 && ($resource = $this->storage_services->manage()->find($this->rid)) !== null
67 && ($from_IRSS = $this->getImageFromIRSS($image_factory, $resource)) !== null
68 ) {
69 return $from_IRSS;
70 }
71
72 if ($this->object_type_specific_property_providers !== null &&
73 (
74 $specific_tile_image = $this->object_type_specific_property_providers->getObjectTypeSpecificTileImage(
75 $this->object_id,
76 $image_factory,
77 $this->storage_services
78 )
79 ) !== null) {
80 return $specific_tile_image;
81 }
82
83 $path = \ilUtil::getImagePath('cont_tile/cont_tile_default_' . $this->obj_type . '.svg');
84 if (is_file($path)) {
85 return $image_factory->responsive($path, '');
86 }
87 return $image_factory->responsive(\ilUtil::getImagePath('cont_tile/cont_tile_default.svg'), '');
88 }
89
90 private function getImageFromIRSS(
91 ImageFactory $image_factory,
93 ): ?Image {
94 $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
95 $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray();
96 if ($urls === []) {
97 return null;
98 }
99
100 $available_widths = $this->flavour_definition->getWidths();
101 array_pop($available_widths);
102
103 if (!isset($urls[count($available_widths)])) {
104 return null;
105 }
106
107 $image = $image_factory->responsive($urls[count($available_widths)], '');
108 return array_reduce(
109 $available_widths,
110 function ($carry, $size) use ($urls) {
111 $image = $carry['image']->withAdditionalHighResSource($urls[$carry['counter']], $size / 2);
112 $counter = ++$carry['counter'];
113 return [
114 'image' => $image,
115 'counter' => $counter
116 ];
117 },
118 ['image' => $image, 'counter' => 0]
119 )['image'];
120 }
121
127 public function getSrcUrlForLegacyForm(): string
128 {
129 if ($this->rid === null || $this->rid === '') {
130 return '';
131 }
132
133 $resource = $this->storage_services->manage()->find($this->rid);
134 if ($resource === null) {
135 return '';
136 }
137
138 $flavour = $this->storage_services->flavours()->get($resource, $this->flavour_definition);
139 $urls = $this->storage_services->consume()->flavourUrls($flavour)->getURLsAsArray(false);
140
141 return array_pop($urls) ?? '';
142 }
143
144 public function cloneFor(int $new_object_id): self
145 {
146 $clone = clone $this;
147 $clone->object_id = $new_object_id;
148
149 if ($this->rid !== null
150 && ($rid = $this->storage_services->manage()->find($this->rid)) !== null) {
151 $i = $this->storage_services->manage()->clone($rid);
152 $clone->rid = $i->serialize();
153 }
154
155 return $clone;
156 }
157
158 public function createFromImportDir(string $source_dir): void
159 {
160 $sourceFS = LegacyPathHelper::deriveFilesystemFrom($source_dir);
161 $sourceDir = LegacyPathHelper::createRelativePath($source_dir);
162 $sourceList = $sourceFS->listContents($sourceDir, true);
163
164 foreach ($sourceList as $item) {
165 if ($item->isDir()) {
166 continue;
167 }
168
169 $path = $source_dir . DIRECTORY_SEPARATOR . basename($item->getPath());
170 $stream = new Stream(fopen($path, 'r'));
171 $i = $this->storage_services->manage()->stream($stream, $this->storage_stakeholder, 'Tile Image');
172 $this->rid = $i->serialize();
173 }
174 }
175}
Mime type determination.
Definition: MimeType.php:30
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
getImageFromIRSS(ImageFactory $image_factory, ResourceIdentification $resource)
Definition: TileImage.php:90
__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:38
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
This is how the factory for UI elements looks.
Definition: Factory.php:28
$path
Definition: ltiservices.php:30
get(string $class_name)
if(!file_exists('../ilias.ini.php'))
$counter