Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7044: Added dynamic route to settings submit form #72

Open
wants to merge 8 commits into
base: 4.6
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions src/bundle/Controller/UserSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
use Ibexa\User\View\UserSettings\ListView;
use Ibexa\User\View\UserSettings\UpdateView;
use JMS\TranslationBundle\Annotation\Desc;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Form\Button;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ExceptionInterface as RouteExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class UserSettingsController extends Controller
class UserSettingsController extends Controller implements LoggerAwareInterface
{
use LoggerAwareTrait;

/** @var \Ibexa\User\Form\Factory\FormFactory */
private $formFactory;

Expand All @@ -43,20 +50,26 @@ class UserSettingsController extends Controller

private PermissionResolver $permissionResolver;

private UrlGeneratorInterface $urlGenerator;

public function __construct(
FormFactory $formFactory,
SubmitHandler $submitHandler,
UserSettingService $userSettingService,
ValueDefinitionRegistry $valueDefinitionRegistry,
ActionResultHandler $actionResultHandler,
PermissionResolver $permissionResolver
PermissionResolver $permissionResolver,
UrlGeneratorInterface $urlGenerator,
LoggerInterface $logger = null
) {
$this->formFactory = $formFactory;
$this->submitHandler = $submitHandler;
$this->userSettingService = $userSettingService;
$this->valueDefinitionRegistry = $valueDefinitionRegistry;
$this->actionResultHandler = $actionResultHandler;
$this->permissionResolver = $permissionResolver;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger ?? new NullLogger();
}

/**
Expand Down Expand Up @@ -92,7 +105,7 @@ public function updateAction(Request $request, UpdateView $view)
$form->handleRequest($request);

if ($form->isSubmitted()) {
$result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) use ($form) {
$result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) use ($form, $request) {
foreach ($data->getValues() as $identifier => $value) {
$this->userSettingService->setUserSetting($identifier, (string)$value['value']);
}
Expand All @@ -104,15 +117,28 @@ public function updateAction(Request $request, UpdateView $view)
'ibexa_user_settings'
);

$route = $request->query->get('route');
$routeParameters = $request->query->all('routeParameters');
if (!$this->routeExists($route, $routeParameters)) {
$route = null;
$routeParameters = [];
}

if ($form->getClickedButton() instanceof Button
&& $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT
) {
return $this->redirectToRoute('ibexa.user_settings.update', [
'identifier' => $data->getIdentifier(),
'route' => $route,
'routeParameters' => $routeParameters,
]);
}

return new RedirectResponse($this->generateUrl('ibexa.user_settings.list'));
if ($route !== null) {
return $this->redirectToRoute($route, $routeParameters);
}

return $this->redirectToRoute('ibexa.user_settings.list');
});

if ($result instanceof Response) {
Expand All @@ -127,6 +153,27 @@ public function updateAction(Request $request, UpdateView $view)

return $view;
}

/**
* @param array<mixed> $routeParameters
*/
private function routeExists(string $route, array $routeParameters): bool
{
try {
$this->urlGenerator->generate($route, $routeParameters);

return true;
} catch (RouteExceptionInterface $e) {
$this->logger->warning(
sprintf('Invalid route in query. %s.', $e->getMessage()),
[
'exception' => $e,
],
);
}

return false;
}
}

class_alias(UserSettingsController::class, 'EzSystems\EzPlatformUserBundle\Controller\UserSettingsController');
Loading