Allow a user to change and remove his profile picture from user settings.

This commit is contained in:
Jan Böhmer 2023-01-25 00:10:17 +01:00
parent 04b99cd247
commit 0063d360ce
4 changed files with 60 additions and 1 deletions

View file

@ -245,6 +245,16 @@ class UserSettingsController extends AbstractController
//$em->flush();
//For some reason the avatar is not set as master picture attachment, so we do it again here
$user->setMasterPictureAttachment($attachment);
$page_need_reload = true;
}
if ($form->getClickedButton() && 'remove_avatar' === $form->getClickedButton()->getName()) {
//Remove the avatar attachment from the user if requested
if ($user->getMasterPictureAttachment() !== null) {
$em->remove($user->getMasterPictureAttachment());
$user->setMasterPictureAttachment(null);
$page_need_reload = true;
}
}
$em->flush();

View file

@ -25,6 +25,7 @@ namespace App\Form;
use App\Entity\UserSystem\User;
use App\Form\Type\CurrencyEntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
@ -34,6 +35,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\File;
@ -77,9 +79,10 @@ class UserSettingsType extends AbstractType
'disabled' => !$this->security->isGranted('edit_infos', $options['data']) || $this->demo_mode,
])
->add('avatar_file', FileType::class, [
'label' => 'user.change_avatar.label',
'label' => 'user_settings.change_avatar.label',
'mapped' => false,
'required' => false,
'disabled' => !$this->security->isGranted('edit_infos', $options['data']) || $this->demo_mode,
'attr' => [
'accept' => 'image/*',
],
@ -134,6 +137,25 @@ class UserSettingsType extends AbstractType
//Buttons
->add('save', SubmitType::class, ['label' => 'save'])
->add('reset', ResetType::class, ['label' => 'reset']);
//Add the remove_avatar button if the user has an avatar (we have to add this dynamically)
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) {
$data = $event->getData();
if (!$data instanceof User) {
return;
}
$form = $event->getForm();
//if ($data->getMasterPictureAttachment()) {
$form->add('remove_avatar', SubmitType::class, [
'label' => 'user_settings.remove_avatar.label',
'disabled' => !$this->security->isGranted('edit_infos', $data) || $this->demo_mode,
'attr' => [
'class' => 'btn btn-link',
],
]);
//}
});
}
public function configureOptions(OptionsResolver $resolver): void