fix order and update of user settings form

This commit is contained in:
Kevin Papst
2020-08-18 23:22:16 +02:00
parent fbb3dd89cf
commit d8ae7635d1
7 changed files with 74 additions and 89 deletions

View File

@@ -21,7 +21,6 @@ use App\Form\UserTeamsType;
use App\Repository\TeamRepository;
use App\Repository\TimesheetRepository;
use App\Utils\LocaleSettings;
use Doctrine\Common\Collections\ArrayCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
@@ -212,13 +211,6 @@ class ProfileController extends AbstractController
$event = new PrepareUserEvent($profile);
$this->dispatcher->dispatch($event);
/** @var \ArrayIterator $iterator */
$iterator = $profile->getPreferences()->getIterator();
$iterator->uasort(function (UserPreference $a, UserPreference $b) {
return ($a->getOrder() < $b->getOrder()) ? -1 : 1;
});
$profile->setPreferences(new ArrayCollection(iterator_to_array($iterator)));
$original = [];
foreach ($profile->getPreferences() as $preference) {
$original[$preference->getName()] = $preference;
@@ -227,48 +219,59 @@ class ProfileController extends AbstractController
$form = $this->createPreferencesForm($profile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$preferences = $profile->getPreferences();
if ($form->isSubmitted()) {
if ($form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$preferences = $profile->getPreferences();
// do not allow to add unknown preferences
foreach ($preferences as $preference) {
if (!isset($original[$preference->getName()])) {
$preferences->removeElement($preference);
// do not allow to add unknown preferences
foreach ($preferences as $preference) {
if (!isset($original[$preference->getName()])) {
$preferences->removeElement($preference);
}
}
}
// but allow to delete already saved settings
foreach ($original as $name => $preference) {
if (false === $profile->getPreferences()->contains($preference)) {
$entityManager->remove($preference);
// but allow to delete already saved settings
foreach ($original as $name => $preference) {
if (false === $profile->getPreferences()->contains($preference)) {
$entityManager->remove($preference);
}
}
$profile->setPreferences($preferences);
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.update.success');
// switch locale ONLY if updated profile is the current user
$locale = $request->getLocale();
if ($this->getUser()->getId() === $profile->getId()) {
$locale = $profile->getPreferenceValue('language', $locale);
}
return $this->redirectToRoute('user_profile_preferences', [
'_locale' => $locale,
'username' => $profile->getUsername()
]);
} else {
$this->flashError('action.update.error', ['%reason%' => 'Validation failed']);
}
$profile->setPreferences($preferences);
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.update.success');
// switch locale ONLY if updated profile is the current user
$locale = $request->getLocale();
if ($this->getUser()->getId() === $profile->getId()) {
$locale = $profile->getPreferenceValue('language', $locale);
}
return $this->redirectToRoute('user_profile_preferences', [
'_locale' => $locale,
'username' => $profile->getUsername()
]);
}
// prepare ordered preferences
$sections = [];
/** @var \ArrayIterator $iterator */
$iterator = $profile->getPreferences()->getIterator();
$iterator->uasort(function (UserPreference $a, UserPreference $b) {
return ($a->getOrder() < $b->getOrder()) ? -1 : 1;
});
/** @var UserPreference $pref */
foreach ($profile->getPreferences() as $pref) {
foreach ($iterator as $pref) {
if ($pref->isEnabled()) {
$sections[$pref->getSection()] = $pref->getSection();
$sections[$pref->getSection()][] = $pref->getName();
}
}

View File

@@ -336,7 +336,7 @@ class User extends BaseUser implements UserInterface
}
foreach ($this->preferences as $preference) {
if ($preference->getName() == $name) {
if ($preference->getName() === $name) {
return $preference;
}
}

View File

@@ -14,7 +14,7 @@ use App\Entity\UserPreference;
use Symfony\Contracts\EventDispatcher\Event;
/**
* This event should be used, if further user preferences should added dynamically
* This event should be used, if further user preferences should be added dynamically.
*/
final class UserPreferenceEvent extends Event
{
@@ -26,11 +26,11 @@ final class UserPreferenceEvent extends Event
/**
* @var User
*/
protected $user;
private $user;
/**
* @var UserPreference[]
*/
protected $preferences;
private $preferences = [];
/**
* @param User $user
@@ -44,9 +44,10 @@ final class UserPreferenceEvent extends Event
/**
* Do not set the preferences directly to the user object, but ONLY via addPreference()
*
* @return User
*/
public function getUser()
public function getUser(): User
{
return $this->user;
}
@@ -54,7 +55,7 @@ final class UserPreferenceEvent extends Event
/**
* @return UserPreference[]
*/
public function getPreferences()
public function getPreferences(): array
{
return $this->preferences;
}

View File

@@ -17,31 +17,23 @@ use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserProfileSubscriber implements EventSubscriberInterface
final class UserProfileSubscriber implements EventSubscriberInterface
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
private $eventDispatcher;
/**
* @var TokenStorageInterface
*/
protected $storage;
private $storage;
/**
* @param EventDispatcherInterface $dispatcher
* @param TokenStorageInterface $storage
*/
public function __construct(EventDispatcherInterface $dispatcher, TokenStorageInterface $storage)
{
$this->eventDispatcher = $dispatcher;
$this->storage = $storage;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
@@ -49,10 +41,7 @@ class UserProfileSubscriber implements EventSubscriberInterface
];
}
/**
* @param KernelEvent $event
*/
public function prepareUserProfile(KernelEvent $event)
public function prepareUserProfile(KernelEvent $event): void
{
if (!$this->canHandleEvent($event)) {
return;
@@ -65,11 +54,7 @@ class UserProfileSubscriber implements EventSubscriberInterface
$this->eventDispatcher->dispatch($event);
}
/**
* @param KernelEvent $event
* @return bool
*/
protected function canHandleEvent(KernelEvent $event): bool
private function canHandleEvent(KernelEvent $event): bool
{
// Ignore sub-requests
if (!$event->isMasterRequest()) {

View File

@@ -34,10 +34,6 @@ class UserPreferenceType extends AbstractType
$this->translate = $translator;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
@@ -56,7 +52,7 @@ class UserPreferenceType extends AbstractType
}
$required = true;
if (CheckboxType::class == $preference->getType()) {
if (CheckboxType::class === $preference->getType()) {
$required = false;
}