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 Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class SettingsListController. Is used for managing settings in General env. |
21
|
|
|
*/ |
22
|
|
|
class SettingsController extends Controller |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
public function enableProfileAction($key) |
26
|
|
|
{ |
27
|
|
|
$profileName = $key; |
28
|
|
|
|
29
|
|
|
$cookie = $this->get('ongr_settings.cookie.active_profiles'); |
30
|
|
|
$currentActiveProfiles = $cookie->getValue(); |
31
|
|
|
|
32
|
|
|
if (is_array($currentActiveProfiles) && !array_intersect($currentActiveProfiles, [$profileName])) { |
33
|
|
|
$currentActiveProfiles[] = $profileName; |
34
|
|
|
$cookie->setValue($currentActiveProfiles); |
35
|
|
|
} else { |
36
|
|
|
$cookie->setValue([$profileName]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$settingsManager = $this->get('ongr_settings.settings_manager'); |
40
|
|
|
$settings = $settingsManager->getProfileSettings($profileName); |
41
|
|
|
|
42
|
|
|
return $this->render('ONGRSettingsBundle:Settings:enableProfile.html.twig', [ |
43
|
|
|
'profile' => $profileName, |
44
|
|
|
'profiles' => $settings, |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Renders settings list page. |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
|
|
public function listAction() |
54
|
|
|
{ |
55
|
|
|
return $this->render('ONGRSettingsBundle:Settings:list.html.twig'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Setting update action. |
60
|
|
|
* |
61
|
|
|
* @param Request $request |
62
|
|
|
* |
63
|
|
|
* @return JsonResponse |
64
|
|
|
*/ |
65
|
|
|
public function updateValueAction(Request $request) |
66
|
|
|
{ |
67
|
|
|
$name = $request->get('name'); |
68
|
|
|
$value = $request->get('value'); |
69
|
|
|
|
70
|
|
|
$manager = $this->get('ongr_settings.settings_manager'); |
71
|
|
|
$manager->update($name, ['value' => $value]); |
72
|
|
|
|
73
|
|
|
return new JsonResponse(['error' => false]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Setting delete action |
78
|
|
|
* |
79
|
|
|
* @param Request $request |
80
|
|
|
* |
81
|
|
|
* @return JsonResponse |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function deleteAction(Request $request) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$manager = $this->get('ongr_settings.settings_manager'); |
87
|
|
|
$manager->delete($request->get('name')); |
88
|
|
|
|
89
|
|
|
return new JsonResponse(['error' => false]); |
90
|
|
|
} catch (\Exception $e) { |
91
|
|
|
return new JsonResponse( |
92
|
|
|
[ |
93
|
|
|
'error' => true, |
94
|
|
|
'message' => 'Error occurred please try to delete setting again.' |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Submit action to create or edit setting if not exists. |
102
|
|
|
* |
103
|
|
|
* @param Request $request |
104
|
|
|
* |
105
|
|
|
* @return JsonResponse |
106
|
|
|
*/ |
107
|
|
|
public function submitAction(Request $request) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$manager = $this->get('ongr_settings.settings_manager'); |
111
|
|
|
$data = $request->get('setting'); |
112
|
|
|
|
113
|
|
|
if (!empty($data['value']) && !is_string($data['value'])) { |
114
|
|
|
$data['value'] = json_encode($data['value']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($request->get('force')) { |
118
|
|
|
$name = $request->get('name'); |
119
|
|
|
$manager->update($name, $data); |
120
|
|
|
} else { |
121
|
|
|
$manager->create($data); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return new JsonResponse(['error' => false]); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
return new JsonResponse( |
127
|
|
|
[ |
128
|
|
|
'error' => true, |
129
|
|
|
'message' => 'Error occurred! Something is wrong with provided data. '. |
130
|
|
|
'Please try to submit form again.' |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.