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\SettingsBundle\Settings\General\SettingsManager; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21
|
|
|
use Symfony\Component\Yaml\Dumper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SettingsManager controller responsible for CRUD actions from frontend for settings. |
25
|
|
|
* |
26
|
|
|
* @package ONGR\SettingsBundle\Controller |
27
|
|
|
*/ |
28
|
|
|
class SettingsManagerController extends Controller |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Action for saving/seting setting values. |
32
|
|
|
* |
33
|
|
|
* @param Request $request |
34
|
|
|
* |
35
|
|
|
* @return Response |
36
|
|
|
*/ |
37
|
|
|
public function setSettingAction(Request $request) |
38
|
|
|
{ |
39
|
|
|
$data = $this->get('ongr_settings.form_validator')->validateSettingForm($request); |
40
|
|
|
$cache = $this->get('es.cache_engine'); |
41
|
|
|
|
42
|
|
|
if ($data['error'] != '') { |
43
|
|
|
$cache->save('settings_errors', $data['error']); |
44
|
|
|
return new RedirectResponse($this->generateUrl('ongr_settings_settings_add')); |
45
|
|
|
} |
46
|
|
|
$manager = $this->getSettingsManager(); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$manager->set( |
50
|
|
|
$data['name'], |
51
|
|
|
$data['type'], |
52
|
|
|
$data['description'], |
53
|
|
|
$data['value'], |
54
|
|
|
$data['profiles'] |
55
|
|
|
); |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
$cache->save('settings_errors', $e->getMessage()); |
58
|
|
|
return new RedirectResponse($this->generateUrl('ongr_settings_settings_add')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$cache->save('settings_success', true); |
62
|
|
|
return new RedirectResponse($this->generateUrl('ongr_settings_settings_add')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Action for rendering single setting edit page. |
67
|
|
|
* |
68
|
|
|
* @param Request $request |
69
|
|
|
* @param string $name |
70
|
|
|
* @param string $profile |
71
|
|
|
* |
72
|
|
|
* @return Response |
73
|
|
|
* @throws NotFoundHttpException |
74
|
|
|
*/ |
75
|
|
|
public function editAction(Request $request, $name, $profile) |
76
|
|
|
{ |
77
|
|
|
$setting = $this->getSettingsManager()->get($name, $profile, false, $request->query->get('type', 'string')); |
78
|
|
|
$params = []; |
79
|
|
|
$params['setting'] = $setting; |
80
|
|
|
$cache = $this->get('es.cache_engine'); |
81
|
|
|
if ($setting->getType() == 'object') { |
82
|
|
|
$dumper = new Dumper(); |
83
|
|
|
$setting->setData( |
84
|
|
|
[ |
|
|
|
|
85
|
|
|
'value' => $dumper->dump(json_decode($setting->getData()['value'], true), 2) |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
View Code Duplication |
if ($cache->contains('settings_errors')) { |
|
|
|
|
90
|
|
|
$params['errors'] = $cache->fetch('settings_errors'); |
91
|
|
|
$cache->delete('settings_errors'); |
92
|
|
|
} elseif ($cache->contains('settings_success')) { |
93
|
|
|
$params['success'] = $cache->fetch('settings_success'); |
94
|
|
|
$cache->delete('settings_success'); |
95
|
|
|
} |
96
|
|
|
return $this->render( |
97
|
|
|
'ONGRSettingsBundle:Settings:edit.html.twig', |
98
|
|
|
$params |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Action updating a setting. |
104
|
|
|
* |
105
|
|
|
* @param Request $request |
106
|
|
|
* |
107
|
|
|
* @return Response |
108
|
|
|
*/ |
109
|
|
|
public function updateAction(Request $request) |
110
|
|
|
{ |
111
|
|
|
$data = $this->get('ongr_settings.form_validator')->validateSettingForm($request); |
112
|
|
|
$manager = $this->getSettingsManager(); |
113
|
|
|
$cache = $this->get('es.cache_engine'); |
114
|
|
|
|
115
|
|
|
if ($data['error'] != '') { |
116
|
|
|
$cache->save('settings_errors', $data['error']); |
117
|
|
|
return new RedirectResponse( |
118
|
|
|
$this->generateUrl( |
119
|
|
|
'ongr_settings_setting_edit', |
120
|
|
|
['name' => $data['name'], 'profile' => $data['profiles'][0]] |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$model = $manager->get( |
126
|
|
|
$data['name'], |
127
|
|
|
$data['profiles'][0], |
128
|
|
|
false, |
129
|
|
|
$data['type'] |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$model->setData((object)['value' => $data['value']]); |
133
|
|
|
$model->setType($data['type']); |
134
|
|
|
$model->setDescription($data['description']); |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$manager->save([$model]); |
138
|
|
|
} catch (\Exception $e) { |
139
|
|
|
$cache->save('settings_errors', $e->getMessage()); |
140
|
|
|
return new RedirectResponse( |
141
|
|
|
$this->generateUrl( |
142
|
|
|
'ongr_settings_setting_edit', |
143
|
|
|
['name' => $data['name'], 'profile' => $data['profiles'][0]] |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$cache->save('settings_success', true); |
149
|
|
|
return new RedirectResponse( |
150
|
|
|
$this->generateUrl( |
151
|
|
|
'ongr_settings_setting_edit', |
152
|
|
|
['name' => $data['name'], 'profile' => $data['profiles'][0]] |
153
|
|
|
) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Action for deleting a setting. |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @param string $profile |
162
|
|
|
* |
163
|
|
|
* @return Response |
164
|
|
|
* @throws NotFoundHttpException |
165
|
|
|
*/ |
166
|
|
|
public function removeAction($name, $profile) |
167
|
|
|
{ |
168
|
|
|
$setting = $this->getSettingsManager()->get($name, $profile); |
169
|
|
|
|
170
|
|
|
$this->getSettingsManager()->remove($setting); |
171
|
|
|
|
172
|
|
|
return new RedirectResponse( |
173
|
|
|
$this->generateUrl( |
174
|
|
|
'ongr_settings_settings_list', |
175
|
|
|
['profile' => $profile] |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Copies a setting to a new profile. |
182
|
|
|
* |
183
|
|
|
* @param Request $request |
184
|
|
|
* @param string $name |
185
|
|
|
* @param string $from |
186
|
|
|
* |
187
|
|
|
* @return Response |
188
|
|
|
* @throws NotFoundHttpException |
189
|
|
|
*/ |
190
|
|
|
public function copyAction(Request $request, $name, $from) |
191
|
|
|
{ |
192
|
|
|
$cache = $this->get('es.cache_engine'); |
193
|
|
|
$profiles = $request->request->get('settingProfiles'); |
194
|
|
|
|
195
|
|
|
if (!is_array($profiles)) { |
196
|
|
|
$cache->save('settings_errors', 'You must select at least one profile'); |
197
|
|
|
return new RedirectResponse( |
198
|
|
|
$this->generateUrl( |
199
|
|
|
'ongr_settings_settings_duplicate', |
200
|
|
|
['profile' => $from, 'name' => $name] |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
$settingsManager = $this->getSettingsManager(); |
205
|
|
|
|
206
|
|
|
$setting = $settingsManager->get($name, $from); |
207
|
|
|
|
208
|
|
|
$this->getSettingsManager()->duplicate($setting, $profiles); |
209
|
|
|
|
210
|
|
|
$cache->save('settings_success', true); |
211
|
|
|
return new RedirectResponse( |
212
|
|
|
$this->generateUrl( |
213
|
|
|
'ongr_settings_settings_duplicate', |
214
|
|
|
['profile' => $from, 'name' => $name] |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return SettingsManager |
221
|
|
|
*/ |
222
|
|
|
protected function getSettingsManager() |
223
|
|
|
{ |
224
|
|
|
return $this->get('ongr_settings.settings_manager'); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: