ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Avatar.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\User\Profile\Fields\NoOverrides;
36use ILIAS\UI\Renderer as UIRenderer;
37use ILIAS\Refinery\Factory as Refinery;
38
39class Avatar implements FieldDefinition
40{
41 use NoOverrides;
42
44
45 public function __construct(
46 private readonly IRSS $irss,
47 private readonly FileUpload $uploads,
48 private readonly ArrayBasedRequestWrapper $post_wrapper,
49 private readonly UIRenderer $ui_renderer,
50 private readonly Refinery $refinery
51 ) {
52 $this->stakeholder = new \ilUserProfilePictureStakeholder();
53 }
54
55 public function getIdentifier(): string
56 {
57 return 'avatar';
58 }
59
60 public function getLabel(Language $lng): string
61 {
62 return $lng->txt('personal_picture');
63 }
64
65 public function getSection(): AvailableSections
66 {
67 return AvailableSections::PersonalData;
68 }
69
70 public function hiddenInLists(): bool
71 {
72 return true;
73 }
74
75 public function visibleInCoursesForcedTo(): ?bool
76 {
77 return false;
78 }
79
80 public function visibleInGroupsForcedTo(): ?bool
81 {
82 return false;
83 }
84
85 public function visibleInStudyProgrammesForcedTo(): ?bool
86 {
87 return false;
88 }
89
90 public function requiredForcedTo(): ?bool
91 {
92 return false;
93 }
94
95 public function searchableForcedTo(): ?bool
96 {
97 return false;
98 }
99
100 public function availableInCertificatesForcedTo(): ?bool
101 {
102 return false;
103 }
104
105 public function getLegacyInput(
108 ?\ilObjUser $user = null
110 $input = new \ilImageFileInputGUI($this->getLabel($lng));
111 $input->setAllowCapture(
112 $context === Context::User || $context === Context::Registration
113 );
114
115 if (($_FILES[$this->getIdentifier()]['tmp_name'] ?? '') !== '') {
116 $input->setPending($_FILES[$this->getIdentifier()]['tmp_name']);
117 return $input;
118 }
119
120 if ($user === null) {
121 return $input;
122 }
123
124 $picture_path = $user->getPersonalPicturePath();
125 if ($picture_path !== '') {
126 $input->setImage($picture_path);
127 $input->setAlt($this->getLabel($lng));
128 }
129 return $input;
130 }
131
132 public function addValueToUserObject(
133 \ilObjUser $user,
134 mixed $input,
135 ?\ilPropertyFormGUI $form = null
136 ): \ilObjUser {
137 return $this->uploadUserPicture($user, $input, $form);
138 }
139
140 public function retrieveValueFromUser(\ilObjUser $user): string
141 {
142 $define = new \ilUserAvatarResolver($user->getId());
143 if (!$define->hasProfilePicture()) {
144 return '';
145 }
146 $define->setSize('xsmall');
147 return $this->ui_renderer->render($define->getAvatar());
148 }
149
153 public function tempStorePicture(
154 \ilPropertyFormGUI $form
156 $capture = $this->retrieveCapture();
157 if ($capture === '') {
158 return $form;
159 }
160 $form->getItemByPostVar($this->getIdentifier())->setImage($capture);
161 $hidden_user_picture_carry = new \ilHiddenInputGUI('user_picture_carry');
162 $hidden_user_picture_carry->setValue($capture);
163 $form->addItem($hidden_user_picture_carry);
164 return $form;
165 }
166
167 private function uploadUserPicture(
168 \ilObjUser $user,
169 array $input,
170 \ilPropertyFormGUI $form
171 ): \ilObjUser {
172 $capture = $this->retrieveCapture();
173 if ($input['tmp_name'] === '' && $capture === '') {
174 if ($form->getItemByPostVar($this->getIdentifier())->getDeletionFlag()) {
175 $user->removeUserPicture();
176 }
177 return $user;
178 }
179
180 // User has uploaded a file of a captured image
181 if (!$this->uploads->hasBeenProcessed()) {
182 $this->uploads->process();
183 }
184
185 $existing_rid = $user->getAvatarRid();
186 $revision_title = 'Avatar for user ' . $user->getLogin();
187 $this->stakeholder->setOwner($user->getId());
188 $uploads = $this->uploads->getResults();
189
190 if (isset($uploads[$input['tmp_name']])) {
191 $rid = $this->moveUploadToStorage(
192 $existing_rid,
193 $revision_title,
194 $uploads[$input['tmp_name']]
195 );
196 $user->setAvatarRid($rid);
197 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
198 return $user;
199 }
200
201 if ($capture === '') {
202 return $user;
203 }
204
205 $data = base64_decode(
206 str_replace(
207 ['data:image/png;base64,', ' '],
208 ['', '+'],
209 $capture
210 )
211 );
212 if ($data === false) {
213 return $user;
214 }
215 $rid = $this->moveStreamToStorage(
216 $existing_rid,
217 $revision_title,
218 Streams::ofString($data)
219 );
220 $user->setAvatarRid($rid);
221 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
222 return $user;
223 }
224
225 private function moveUploadToStorage(
226 ?ResourceIdentification $existing_rid,
227 string $revision_title,
228 UploadResult $upload_result
230 if ($existing_rid === null) {
231 return $this->irss->manage()->upload(
232 $upload_result,
233 $this->stakeholder,
234 $revision_title
235 );
236 }
237
238 $this->irss->manage()->replaceWithUpload(
239 $existing_rid,
240 $upload_result,
241 $this->stakeholder,
242 $revision_title
243 );
244
245 return $existing_rid;
246 }
247
248 private function moveStreamToStorage(
249 ?ResourceIdentification $existing_rid,
250 string $revision_title,
251 Stream $stream
253 if ($existing_rid === null) {
254 return $this->irss->manage()->stream(
255 $stream,
256 $this->stakeholder,
257 $revision_title
258 );
259 }
260
261 $this->irss->manage()->replaceWithStream(
262 $existing_rid,
263 $stream,
264 $this->stakeholder,
265 $revision_title
266 );
267 return $existing_rid;
268 }
269
270 private function retrieveCapture(): ?string
271 {
272 $from_upload = $this->post_wrapper->retrieve(
273 'upload_capture',
274 $this->refinery->byTrying([
275 $this->refinery->kindlyTo()->string(),
276 $this->refinery->always('')
277 ])
278 );
279
280 if ($from_upload !== '') {
281 return $from_upload;
282 }
283
284 return $this->post_wrapper->retrieve(
285 'user_picture_carry',
286 $this->refinery->byTrying([
287 $this->refinery->kindlyTo()->string(),
288 $this->refinery->always('')
289 ])
290 );
291 }
292}
Builds data types.
Definition: Factory.php:36
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
uploadUserPicture(\ilObjUser $user, array $input, \ilPropertyFormGUI $form)
Definition: Avatar.php:167
addValueToUserObject(\ilObjUser $user, mixed $input, ?\ilPropertyFormGUI $form=null)
Definition: Avatar.php:132
getLegacyInput(Language $lng, Context $context, ?\ilObjUser $user=null)
You don't need to add a post_var to the input as the User will handle this for you,...
Definition: Avatar.php:105
__construct(private readonly IRSS $irss, private readonly FileUpload $uploads, private readonly ArrayBasedRequestWrapper $post_wrapper, private readonly UIRenderer $ui_renderer, private readonly Refinery $refinery)
Definition: Avatar.php:45
tempStorePicture(\ilPropertyFormGUI $form)
Definition: Avatar.php:153
moveUploadToStorage(?ResourceIdentification $existing_rid, string $revision_title, UploadResult $upload_result)
Definition: Avatar.php:225
moveStreamToStorage(?ResourceIdentification $existing_rid, string $revision_title, Stream $stream)
Definition: Avatar.php:248
This class represents a property in a property form.
This class represents an image file property in a property form.
User class.
setAvatarRid(?ResourceIdentification $avatar_rid)
This class represents a property form user interface.
getItemByPostVar(string $a_post_var)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An entity that renders components to a string output.
Definition: Renderer.php:31
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
$context
Definition: webdav.php:31