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\Document\Setting; |
15
|
|
|
use ONGR\SettingsBundle\Settings\General\SettingsManager; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
17
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
22
|
|
|
use Symfony\Component\Yaml\Parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SettingsManager controller responsible for CRUD actions from frontend for settings. |
26
|
|
|
* |
27
|
|
|
* @package ONGR\SettingsBundle\Controller |
28
|
|
|
*/ |
29
|
|
|
class SettingsManagerController extends Controller |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Action for saving/setting setting values. |
33
|
|
|
* |
34
|
|
|
* @param Request $request |
35
|
|
|
* |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
|
|
public function setSettingAction(Request $request) |
39
|
|
|
{ |
40
|
|
|
$data = json_decode($request->request->get('data'), true); |
41
|
|
|
$manager = $this->getSettingsManager(); |
42
|
|
|
$parser = new Parser(); |
43
|
|
|
$value = $data['setting_value']; |
44
|
|
|
$type = $data['setting_type']; |
45
|
|
|
$name = htmlentities($data['setting_name']); |
46
|
|
|
$profiles = $data['setting_profiles']; |
47
|
|
|
$description = htmlentities($data['setting_description']); |
48
|
|
|
$response = []; |
49
|
|
|
$response['error'] = ''; |
50
|
|
|
|
51
|
|
|
switch ($type) { |
52
|
|
|
case 'bool': |
53
|
|
|
$value == 'true' ? $value = true : $value = false; |
54
|
|
|
break; |
55
|
|
|
case 'string': |
56
|
|
|
$value = htmlentities($value); |
57
|
|
|
break; |
58
|
|
|
case 'object': |
59
|
|
|
try { |
60
|
|
|
$value = json_encode($parser->parse($value)); |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
$response['error'] = 'Passed setting value does not contain valid yaml'; |
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, $description, $type, $value, $profile); |
75
|
|
|
} |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
$response['error'] = 'Insertion failed: '.$e->getMessage(); |
78
|
|
|
} |
79
|
|
|
return new JsonResponse(json_encode($response)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Action for rendering single setting edit page. |
84
|
|
|
* |
85
|
|
|
* @param Request $request |
86
|
|
|
* @param string $name |
87
|
|
|
* @param string $profile |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
* @throws NotFoundHttpException |
91
|
|
|
*/ |
92
|
|
|
public function editAction(Request $request, $name, $profile) |
93
|
|
|
{ |
94
|
|
|
$setting = $this->getSettingsManager()->get($name, $profile, false, $request->query->get('type', 'string')); |
95
|
|
|
|
96
|
|
|
return $this->render( |
97
|
|
|
'ONGRSettingsBundle:Settings:edit1.html.twig', |
98
|
|
|
[ |
99
|
|
|
'setting' => $setting, |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Action for Angularjs to edit settings. |
106
|
|
|
* |
107
|
|
|
* @param Request $request |
108
|
|
|
* @param string $name |
109
|
|
|
* @param string $profile |
110
|
|
|
* |
111
|
|
|
* @return Response |
112
|
|
|
* @throws NotFoundHttpException |
113
|
|
|
*/ |
114
|
|
|
public function ngEditAction(Request $request, $name, $profile) |
115
|
|
|
{ |
116
|
|
|
$content = $request->getContent(); |
117
|
|
|
if (empty($content)) { |
118
|
|
|
return new Response(Response::$statusTexts[400], 400); |
119
|
|
|
} |
120
|
|
|
$content = json_decode($content, true); |
121
|
|
|
if ($content === null || empty($content['setting'])) { |
122
|
|
|
return new Response(Response::$statusTexts[400], 400); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$type = isset($content['setting']['type']) ? $content['setting']['type'] : 'string'; |
126
|
|
|
$value = $content['setting']['data']['value']; |
127
|
|
|
if ($type == Setting::TYPE_BOOLEAN) { |
128
|
|
|
$value == 'true' ? $value = true : $value = false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$manager = $this->getSettingsManager(); |
132
|
|
|
$model = $manager->get($name, $profile, false, $type); |
133
|
|
|
$model->setType($type); |
134
|
|
|
|
135
|
|
|
$model->setData((object)['value' => $value]); |
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 Request $request |
168
|
|
|
* @param string $name |
169
|
|
|
* @param string $from |
170
|
|
|
* |
171
|
|
|
* @return Response |
172
|
|
|
* @throws NotFoundHttpException |
173
|
|
|
*/ |
174
|
|
|
public function copyAction(Request $request, $name, $from) |
175
|
|
|
{ |
176
|
|
|
$to = json_decode($request->request->get('to_profiles'), true); |
177
|
|
|
|
178
|
|
|
$settingsManager = $this->getSettingsManager(); |
179
|
|
|
|
180
|
|
|
$setting = $settingsManager->get($name, $from); |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
foreach ($to as $profile) { |
184
|
|
|
if ($from != $profile) { |
185
|
|
|
$this->getSettingsManager()->duplicate($setting, $profile); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return new Response(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return SettingsManager |
194
|
|
|
*/ |
195
|
|
|
protected function getSettingsManager() |
196
|
|
|
{ |
197
|
|
|
return $this->get('ongr_settings.settings_manager'); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|