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\DependencyInjection\Exception\ParameterNotFoundException; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21
|
|
|
use Symfony\Component\Yaml\Parser; |
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 = json_decode($request->request->get('data'), true); |
40
|
|
|
$manager = $this->getSettingsManager(); |
41
|
|
|
$parser = new Parser(); |
42
|
|
|
$value = $data['setting_value']; |
43
|
|
|
$type = $data['setting_type']; |
44
|
|
|
$name = htmlentities($data['setting_name']); |
45
|
|
|
$profiles = $data['setting_profiles']; |
46
|
|
|
$description = htmlentities($data['setting_description']); |
|
|
|
|
47
|
|
|
$response = []; |
48
|
|
|
$response['error'] = ''; |
49
|
|
|
|
50
|
|
|
switch ($type) { |
51
|
|
|
case 'Boolean': |
52
|
|
|
$value == 'true' ? $value = true : $value = false; |
53
|
|
|
break; |
54
|
|
|
case 'Default': |
55
|
|
|
$value = htmlentities($value); |
56
|
|
|
break; |
57
|
|
|
case 'Object': |
58
|
|
|
try { |
59
|
|
|
$value = $parser->parse($value); |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
$response['error'] = 'Passed setting value is not correct yaml'; |
62
|
|
|
$response['code'] = 406; |
63
|
|
|
return new JsonResponse(json_encode($response)); |
64
|
|
|
} |
65
|
|
|
break; |
66
|
|
|
case 'Array': |
67
|
|
|
foreach ($value as $key => $item) { |
68
|
|
|
$value[$key] = htmlentities($item); |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
try { |
73
|
|
|
foreach ($profiles as $profile) { |
74
|
|
|
$manager->set($name, $value, $profile); |
75
|
|
|
} |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
$response['error'] = 'Insertion failed: '.$e->getMessage(); |
78
|
|
|
$response['code'] = 406; |
79
|
|
|
} |
80
|
|
|
if (!isset($response['code'])) { |
81
|
|
|
$response['code'] = 201; |
82
|
|
|
} |
83
|
|
|
return new JsonResponse(json_encode($response)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Action for rendering single setting edit page. |
88
|
|
|
* |
89
|
|
|
* @param Request $request |
90
|
|
|
* @param string $name |
91
|
|
|
* @param string $profile |
92
|
|
|
* |
93
|
|
|
* @return Response |
94
|
|
|
* @throws NotFoundHttpException |
95
|
|
|
*/ |
96
|
|
|
public function editAction(Request $request, $name, $profile) |
97
|
|
|
{ |
98
|
|
|
$setting = $this->getSettingsManager()->get($name, $profile, false, $request->query->get('type', 'string')); |
99
|
|
|
|
100
|
|
|
return $this->render( |
101
|
|
|
'ONGRSettingsBundle:Settings:edit.html.twig', |
102
|
|
|
[ |
103
|
|
|
'setting' => $setting, |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Action for Angularjs to edit settings. |
110
|
|
|
* |
111
|
|
|
* @param Request $request |
112
|
|
|
* @param string $name |
113
|
|
|
* @param string $profile |
114
|
|
|
* |
115
|
|
|
* @return Response |
116
|
|
|
* @throws NotFoundHttpException |
117
|
|
|
*/ |
118
|
|
|
public function ngEditAction(Request $request, $name, $profile) |
119
|
|
|
{ |
120
|
|
|
$content = $request->getContent(); |
121
|
|
|
if (empty($content)) { |
122
|
|
|
return new Response(Response::$statusTexts[400], 400); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$content = json_decode($content, true); |
126
|
|
|
if ($content === null || empty($content['setting'])) { |
127
|
|
|
return new Response(Response::$statusTexts[400], 400); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$type = isset($content['setting']['type']) ? $content['setting']['type'] : 'string'; |
131
|
|
|
|
132
|
|
|
$manager = $this->getSettingsManager(); |
133
|
|
|
$model = $manager->get($name, $profile, false, $type); |
134
|
|
|
|
135
|
|
|
$model->setData($content['setting']['data']); |
136
|
|
|
|
137
|
|
|
if (isset($content['setting']['description'])) { |
138
|
|
|
$model->setDescription($content['setting']['description']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$manager->save($model); |
142
|
|
|
|
143
|
|
|
return new Response(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Action for deleting a setting. |
148
|
|
|
* |
149
|
|
|
* @param string $name |
150
|
|
|
* @param string $profile |
151
|
|
|
* |
152
|
|
|
* @return Response |
153
|
|
|
* @throws NotFoundHttpException |
154
|
|
|
*/ |
155
|
|
|
public function removeAction($name, $profile) |
156
|
|
|
{ |
157
|
|
|
$setting = $this->getSettingsManager()->get($name, $profile); |
158
|
|
|
|
159
|
|
|
$this->getSettingsManager()->remove($setting); |
160
|
|
|
|
161
|
|
|
return new Response(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Copies a setting to a new profile. |
166
|
|
|
* |
167
|
|
|
* @param string $name |
168
|
|
|
* @param string $from |
169
|
|
|
* @param string $to |
170
|
|
|
* |
171
|
|
|
* @return Response |
172
|
|
|
* @throws NotFoundHttpException |
173
|
|
|
*/ |
174
|
|
|
public function copyAction($name, $from, $to) |
175
|
|
|
{ |
176
|
|
|
$settingsManager = $this->getSettingsManager(); |
177
|
|
|
|
178
|
|
|
$setting = $settingsManager->get($name, $from); |
179
|
|
|
|
180
|
|
|
$this->getSettingsManager()->duplicate($setting, $to); |
181
|
|
|
|
182
|
|
|
return new Response(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return SettingsManager |
187
|
|
|
*/ |
188
|
|
|
protected function getSettingsManager() |
189
|
|
|
{ |
190
|
|
|
return $this->get('ongr_settings.settings_manager'); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.