ILIAS  trunk Revision v12.0_alpha-1338-g8f7e531aa3c
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(
107 Context $context,
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('small', true);
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
143 if ($user->getAvatarRid() === null) {
144 return '';
145 }
146 $define = new \ilUserAvatarResolver($user->getId());
147 $define->setSize('xsmall');
148 $define->setForcePicture(true);
149 return $define->getLegacyPictureURL();
150 }
151
155 public function tempStorePicture(
156 \ilPropertyFormGUI $form
158 $capture = $this->retrieveCapture();
159 if ($capture === '') {
160 return $form;
161 }
162 $form->getItemByPostVar($this->getIdentifier())->setImage($capture);
163 $hidden_user_picture_carry = new \ilHiddenInputGUI('user_picture_carry');
164 $hidden_user_picture_carry->setValue($capture);
165 $form->addItem($hidden_user_picture_carry);
166 return $form;
167 }
168
169 private function uploadUserPicture(
170 \ilObjUser $user,
171 array $input,
172 ?\ilPropertyFormGUI $form
173 ): \ilObjUser {
174 $capture = $this->retrieveCapture();
175 if ($input['tmp_name'] === '' && $capture === '') {
176 if ($form?->getItemByPostVar($this->getIdentifier())->getDeletionFlag()) {
177 $user->removeUserPicture();
178 }
179 return $user;
180 }
181
182 // User has uploaded a file of a captured image
183 if (!$this->uploads->hasBeenProcessed()) {
184 $this->uploads->process();
185 }
186
187 $existing_rid = $user->getAvatarRid();
188 $revision_title = 'Avatar for user ' . ($input['alias'] ?? $user->getLogin());
189 $this->stakeholder->setOwner($user->getId());
190 $uploads = $this->uploads->getResults();
191
192 if (is_file($input['tmp_name'])) {
193 $rid = $this->moveStreamToStorage(
194 $existing_rid,
195 $revision_title,
196 Streams::ofString(
197 file_get_contents($input['tmp_name'])
198 )
199 );
200 $user->setAvatarRid($rid);
201 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
202 return $user;
203 }
204
205 if (isset($uploads[$input['tmp_name']])) {
206 $rid = $this->moveUploadToStorage(
207 $existing_rid,
208 $revision_title,
209 $uploads[$input['tmp_name']]
210 );
211 $user->setAvatarRid($rid);
212 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
213 return $user;
214 }
215
216 if ($capture === '') {
217 return $user;
218 }
219
220 $data = base64_decode(
221 str_replace(
222 ['data:image/png;base64,', ' '],
223 ['', '+'],
224 $capture
225 )
226 );
227 if ($data === false) {
228 return $user;
229 }
230 $rid = $this->moveStreamToStorage(
231 $existing_rid,
232 $revision_title,
233 Streams::ofString($data)
234 );
235 $user->setAvatarRid($rid);
236 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
237 return $user;
238 }
239
240 private function moveUploadToStorage(
241 ?ResourceIdentification $existing_rid,
242 string $revision_title,
243 UploadResult $upload_result
245 if ($existing_rid === null) {
246 return $this->irss->manage()->upload(
247 $upload_result,
248 $this->stakeholder,
249 $revision_title
250 );
251 }
252
253 $this->irss->manage()->replaceWithUpload(
254 $existing_rid,
255 $upload_result,
256 $this->stakeholder,
257 $revision_title
258 );
259
260 return $existing_rid;
261 }
262
263 private function moveStreamToStorage(
264 ?ResourceIdentification $existing_rid,
265 string $revision_title,
266 Stream $stream
268 if ($existing_rid === null) {
269 return $this->irss->manage()->stream(
270 $stream,
271 $this->stakeholder,
272 $revision_title
273 );
274 }
275
276 $this->irss->manage()->replaceWithStream(
277 $existing_rid,
278 $stream,
279 $this->stakeholder,
280 $revision_title
281 );
282 return $existing_rid;
283 }
284
285 private function retrieveCapture(): ?string
286 {
287 $from_upload = $this->post_wrapper->retrieve(
288 'avatar_capture',
289 $this->refinery->byTrying([
290 $this->refinery->kindlyTo()->string(),
291 $this->refinery->always('')
292 ])
293 );
294
295 if ($from_upload !== '') {
296 return $from_upload;
297 }
298
299 return $this->post_wrapper->retrieve(
300 'user_picture_carry',
301 $this->refinery->byTrying([
302 $this->refinery->kindlyTo()->string(),
303 $this->refinery->always('')
304 ])
305 );
306 }
307}
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
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:155
moveUploadToStorage(?ResourceIdentification $existing_rid, string $revision_title, UploadResult $upload_result)
Definition: Avatar.php:240
moveStreamToStorage(?ResourceIdentification $existing_rid, string $revision_title, Stream $stream)
Definition: Avatar.php:263
uploadUserPicture(\ilObjUser $user, array $input, ?\ilPropertyFormGUI $form)
Definition: Avatar.php:169
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:26
if(!file_exists('../ilias.ini.php'))