-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserSettingsController.php
118 lines (97 loc) · 4.09 KB
/
UserSettingsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Bundle\User\Controller;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\User\ExceptionHandler\ActionResultHandler;
use Ibexa\User\Form\Data\UserSettingUpdateData;
use Ibexa\User\Form\Factory\FormFactory;
use Ibexa\User\Form\SubmitHandler;
use Ibexa\User\UserSetting\UserSettingService;
use Ibexa\User\UserSetting\ValueDefinitionRegistry;
use Ibexa\User\View\UserSettings\ListView;
use Ibexa\User\View\UserSettings\UpdateView;
use JMS\TranslationBundle\Annotation\Desc;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class UserSettingsController extends Controller
{
/** @var \Ibexa\User\Form\Factory\FormFactory */
private $formFactory;
/** @var \Ibexa\User\Form\SubmitHandler */
private $submitHandler;
/** @var \Ibexa\User\UserSetting\UserSettingService */
private $userSettingService;
/** @var \Ibexa\User\UserSetting\ValueDefinitionRegistry */
private $valueDefinitionRegistry;
/** @var \Ibexa\User\ExceptionHandler\ActionResultHandler */
private $actionResultHandler;
/** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
private $configResolver;
public function __construct(
FormFactory $formFactory,
SubmitHandler $submitHandler,
UserSettingService $userSettingService,
ValueDefinitionRegistry $valueDefinitionRegistry,
ActionResultHandler $actionResultHandler,
ConfigResolverInterface $configResolver
) {
$this->formFactory = $formFactory;
$this->submitHandler = $submitHandler;
$this->userSettingService = $userSettingService;
$this->valueDefinitionRegistry = $valueDefinitionRegistry;
$this->actionResultHandler = $actionResultHandler;
$this->configResolver = $configResolver;
}
/**
* @param int $page
*
* @return \Ibexa\User\View\UserSettings\ListView
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
*/
public function listAction(int $page = 1): ListView
{
return new ListView(null, [
'grouped_settings' => $this->userSettingService->loadGroupedUserSettings(),
'value_definitions' => $this->valueDefinitionRegistry->getValueDefinitions(),
]);
}
public function updateAction(Request $request, UpdateView $view)
{
$userSettingGroup = $view->getUserSettingGroup();
$values = [];
foreach ($userSettingGroup->getSettings() as $setting) {
$values[$setting->identifier] = ['value' => $setting->value];
}
$data = new UserSettingUpdateData($userSettingGroup->getIdentifier(), $values);
$form = $this->formFactory->updateUserSetting($userSettingGroup->getIdentifier(), $data);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) {
foreach ($data->getValues() as $identifier => $value) {
$this->userSettingService->setUserSetting($identifier, (string)$value['value']);
}
$this->actionResultHandler->success(
/** @Desc("User settings '%identifier%' updated.") */
'user_setting.update.success',
['%identifier%' => $data->getIdentifier()],
'ibexa_user_settings'
);
return new RedirectResponse($this->generateUrl('ibexa.user_settings.list'));
});
if ($result instanceof Response) {
return $result;
}
}
$view->addParameters([
'form' => $form->createView(),
]);
return $view;
}
}
class_alias(UserSettingsController::class, 'EzSystems\EzPlatformUserBundle\Controller\UserSettingsController');