|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\CookiesBundle\Cookie\Model\CookieInterface; |
|
15
|
|
|
use ONGR\SettingsBundle\Form\Type\SettingsType; |
|
16
|
|
|
use ONGR\SettingsBundle\Settings\Personal\PersonalSettingsManager; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Controller for managing General (private) settings. |
|
24
|
|
|
*/ |
|
25
|
|
|
class PersonalSettingsController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Main action for changing settings. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Request $request |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
33
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function settingsAction(Request $request) |
|
36
|
|
|
{ |
|
37
|
|
|
$manager = $this->getPersonalSettingsManager(); |
|
38
|
|
|
|
|
39
|
|
|
// Handle form. |
|
40
|
|
|
$settingsData = $manager->getSettings(); |
|
41
|
|
|
$settingsMap = $manager->getSettingsMap(); |
|
42
|
|
|
$options = [ |
|
43
|
|
|
'settingsStructure' => $settingsMap, |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
$form = $this->createForm(SettingsType::class, $settingsData, $options); |
|
47
|
|
|
$form->handleRequest($request); |
|
48
|
|
|
if ($form->isValid()) { |
|
49
|
|
|
$manager->setSettingsFromForm($form->getData()); |
|
50
|
|
|
$redirectResponse = $this->redirect($request->getUri()); |
|
51
|
|
|
$settings = $manager->getSettings(); |
|
52
|
|
|
$this->attachCookies($settings, $settingsMap); |
|
53
|
|
|
|
|
54
|
|
|
return $redirectResponse; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Build settings layout within categories. |
|
58
|
|
|
$categoryMap = $manager->getCategoryMap(); |
|
59
|
|
|
foreach ($settingsMap as $settingId => $setting) { |
|
60
|
|
|
$categoryMap[$setting['category']]['settings'][$settingId] = array_merge( |
|
61
|
|
|
$setting, |
|
62
|
|
|
[ |
|
63
|
|
|
'link' => $request->getUriForPath( |
|
64
|
|
|
$this->generateUrl( |
|
65
|
|
|
'ongr_settings_personal_settings_change', |
|
66
|
|
|
[ |
|
67
|
|
|
'encodedName' => base64_encode($settingId), |
|
68
|
|
|
] |
|
69
|
|
|
) |
|
70
|
|
|
), |
|
71
|
|
|
] |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Render. |
|
76
|
|
|
return $this->render( |
|
77
|
|
|
'ONGRSettingsBundle:Settings:settings.html.twig', |
|
78
|
|
|
[ |
|
79
|
|
|
'form' => $form->createView(), |
|
80
|
|
|
'categories' => $categoryMap, |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Creates new Setting. |
|
87
|
|
|
* |
|
88
|
|
|
* @param Request $request Request to process, not used here. |
|
89
|
|
|
* @param string $encodedName Base64 encoded setting name. |
|
90
|
|
|
* |
|
91
|
|
|
* @return JsonResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function changeSettingAction(Request $request, $encodedName) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$manager = $this->getPersonalSettingsManager(); |
|
96
|
|
|
|
|
97
|
|
|
$name = base64_decode($encodedName); |
|
98
|
|
|
|
|
99
|
|
|
$settingsStructure = $manager->getSettingsMap(); |
|
100
|
|
|
if (array_key_exists($name, $settingsStructure)) { |
|
101
|
|
|
$settings = $manager->getSettings(); |
|
102
|
|
|
if (array_key_exists($name, $settings)) { |
|
103
|
|
|
$settings[$name] = !$settings[$name]; |
|
104
|
|
|
} else { |
|
105
|
|
|
$settings[$name] = true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$manager->setSettingsFromCookie($settings); |
|
109
|
|
|
$this->attachCookies($manager->getSettings(), $manager->getSettingsMap()); |
|
110
|
|
|
|
|
111
|
|
|
return new JsonResponse(); |
|
112
|
|
|
} else { |
|
113
|
|
|
return new JsonResponse(Response::$statusTexts[403], 403); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return PersonalSettingsManager |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getPersonalSettingsManager() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->get('ongr_settings.settings.personal_settings_manager'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets cookie values from settings based on settings map. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $settings |
|
129
|
|
|
* @param array $settingsMap |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function attachCookies(array $settings, array $settingsMap) |
|
132
|
|
|
{ |
|
133
|
|
|
$cookies = []; |
|
134
|
|
|
foreach ($settings as $settingId => $setting) { |
|
135
|
|
|
// If array key is not existing because change in settings avoid exception. |
|
136
|
|
|
if (array_key_exists($settingId, $settingsMap)) { |
|
137
|
|
|
$cookieServiceName = $settingsMap[$settingId]['cookie']; |
|
138
|
|
|
$cookies[$cookieServiceName][$settingId] = $setting; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
foreach ($cookies as $cookieServiceName => $cookieSettings) { |
|
143
|
|
|
/** @var CookieInterface $cookie */ |
|
144
|
|
|
$cookie = $this->get($cookieServiceName); |
|
145
|
|
|
$cookie->setValue($cookieSettings); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.