ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
Icon.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
36 class Icon implements Property
37 {
39  private const INPUT_LABEL = 'custom_icon';
40 
41  private bool $deleted_flag = false;
42  private ?string $temp_file_name = null;
43 
44  public function __construct(
45  private bool $custom_icons_enabled,
46  private ?Custom $custom_icon = null,
47  private ?ObjectTypeSpecificPropertyProviders $object_type_specific_property_providers = null
48  ) {
49  }
50 
51  public function getObjectTypeSpecificIcon(
52  int $object_id,
53  IconFactory $factory,
54  StorageService $irss
55  ): ?CustomIcon {
56  if ($this->object_type_specific_property_providers === null) {
57  return null;
58  }
59  return $this->object_type_specific_property_providers->getObjectTypeSpecificIcon($object_id, $factory, $irss);
60  }
61 
62  public function getCustomIcon(): ?Custom
63  {
64  return $this->custom_icons_enabled ? $this->custom_icon : null;
65  }
66 
67  public function getDeletedFlag(): bool
68  {
69  return $this->deleted_flag;
70  }
71 
72  public function withDeletedFlag(): self
73  {
74  $clone = clone $this;
75  $clone->deleted_flag = true;
76  return $clone;
77  }
78 
79  public function getTempFileName(): ?string
80  {
81  return $this->temp_file_name;
82  }
83 
84  public function withTempFileName(string $name): self
85  {
86  $clone = $this;
87  $clone->temp_file_name = $name;
88  return $clone;
89  }
90 
91  public function toForm(
92  \ilLanguage $language,
93  FieldFactory $field_factory,
95  ): ?File {
96  if (!$this->custom_icons_enabled) {
97  return null;
98  }
99  $trafo = $refinery->custom()->transformation(
100  function ($v): ?Property {
101  $property_icon = new self(
102  $this->custom_icons_enabled,
103  $this->custom_icon
104  );
105 
106  if (count($v) > 0 && $v[0] !== 'custom_icon') {
107  return $property_icon
108  ->withTempFileName($v[0]);
109  }
110 
111  if (count($v) === 0 && $this->custom_icon->exists()) {
112  return $property_icon
113  ->withDeletedFlag();
114  }
115 
116  return $property_icon;
117  }
118  );
119  $custom_icon = $field_factory
120  ->file(new \CustomIconUploadHandlerGUI($this->custom_icon), $language->txt(self::INPUT_LABEL))
121  ->withAcceptedMimeTypes(self::SUPPORTED_MIME_TYPES)
122  ->withAdditionalTransformation($trafo);
123 
124  if (!$this->custom_icon->exists()) {
125  return $custom_icon;
126  }
127 
128  return $custom_icon->withValue(['custom_icon']);
129  }
130 
131  public function toLegacyForm(
132  \ilLanguage $language
133  ): ?\ilImageFileInputGUI {
134  if (!$this->custom_icons_enabled) {
135  return null;
136  }
137  $custom_icon_input = new \ilImageFileInputGUI($language->txt(self::INPUT_LABEL), 'icon');
138  $custom_icon_input->setSuffixes($this->custom_icon->getSupportedFileExtensions());
139  $custom_icon_input->setUseCache(false);
140  if ($this->custom_icon->exists()) {
141  $custom_icon_input->setImage($this->custom_icon->getFullPath());
142  } else {
143  $custom_icon_input->setImage('');
144  }
145 
146  return $custom_icon_input;
147  }
148 }
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
custom_icon()
description: > Example for rendering custom icons.
Definition: custom_icon.php:33
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This is how a factory for icons looks like.
Definition: Factory.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(private bool $custom_icons_enabled, private ?Custom $custom_icon=null, private ?ObjectTypeSpecificPropertyProviders $object_type_specific_property_providers=null)
Definition: Icon.php:44
toForm(\ilLanguage $language, FieldFactory $field_factory, Refinery $refinery)
Definition: Icon.php:91
getObjectTypeSpecificIcon(int $object_id, IconFactory $factory, StorageService $irss)
Definition: Icon.php:51
This class represents an image file property in a property form.
This describes file field.
Definition: File.php:28