ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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('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 ' . $user->getLogin();
189 $this->stakeholder->setOwner($user->getId());
190 $uploads = $this->uploads->getResults();
191
192 if (isset($uploads[$input['tmp_name']])) {
193 $rid = $this->moveUploadToStorage(
194 $existing_rid,
195 $revision_title,
196 $uploads[$input['tmp_name']]
197 );
198 $user->setAvatarRid($rid);
199 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
200 return $user;
201 }
202
203 if ($capture === '') {
204 return $user;
205 }
206
207 $data = base64_decode(
208 str_replace(
209 ['data:image/png;base64,', ' '],
210 ['', '+'],
211 $capture
212 )
213 );
214 if ($data === false) {
215 return $user;
216 }
217 $rid = $this->moveStreamToStorage(
218 $existing_rid,
219 $revision_title,
220 Streams::ofString($data)
221 );
222 $user->setAvatarRid($rid);
223 $this->irss->flavours()->ensure($rid, new \ilUserProfilePictureDefinition());
224 return $user;
225 }
226
227 private function moveUploadToStorage(
228 ?ResourceIdentification $existing_rid,
229 string $revision_title,
230 UploadResult $upload_result
232 if ($existing_rid === null) {
233 return $this->irss->manage()->upload(
234 $upload_result,
235 $this->stakeholder,
236 $revision_title
237 );
238 }
239
240 $this->irss->manage()->replaceWithUpload(
241 $existing_rid,
242 $upload_result,
243 $this->stakeholder,
244 $revision_title
245 );
246
247 return $existing_rid;
248 }
249
250 private function moveStreamToStorage(
251 ?ResourceIdentification $existing_rid,
252 string $revision_title,
253 Stream $stream
255 if ($existing_rid === null) {
256 return $this->irss->manage()->stream(
257 $stream,
258 $this->stakeholder,
259 $revision_title
260 );
261 }
262
263 $this->irss->manage()->replaceWithStream(
264 $existing_rid,
265 $stream,
266 $this->stakeholder,
267 $revision_title
268 );
269 return $existing_rid;
270 }
271
272 private function retrieveCapture(): ?string
273 {
274 $from_upload = $this->post_wrapper->retrieve(
275 'avatar_capture',
276 $this->refinery->byTrying([
277 $this->refinery->kindlyTo()->string(),
278 $this->refinery->always('')
279 ])
280 );
281
282 if ($from_upload !== '') {
283 return $from_upload;
284 }
285
286 return $this->post_wrapper->retrieve(
287 'user_picture_carry',
288 $this->refinery->byTrying([
289 $this->refinery->kindlyTo()->string(),
290 $this->refinery->always('')
291 ])
292 );
293 }
294}
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:169
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:227
moveStreamToStorage(?ResourceIdentification $existing_rid, string $revision_title, Stream $stream)
Definition: Avatar.php:250
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