-
Notifications
You must be signed in to change notification settings - Fork 36
/
SettingsController.php
98 lines (79 loc) · 2.87 KB
/
SettingsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Craue\ConfigBundle\Controller;
use Craue\ConfigBundle\CacheAdapter\CacheAdapterInterface;
use Craue\ConfigBundle\Entity\SettingInterface;
use Craue\ConfigBundle\Form\ModifySettingsForm;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2023 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class SettingsController extends AbstractController {
public function modifyAction(CacheAdapterInterface $cache, FormFactoryInterface $formFactory, Request $request,
SessionInterface $session, Environment $twig, EntityManagerInterface $em, TranslatorInterface $translator) {
$repo = $em->getRepository($this->container->getParameter('craue_config.entity_name'));
$allStoredSettings = $repo->findAll();
$formData = [
'settings' => $allStoredSettings,
];
$form = $formFactory->create(ModifySettingsForm::class, $formData);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// update the cache
foreach ($formData['settings'] as $formSetting) {
$storedSetting = $this->getSettingByName($allStoredSettings, $formSetting->getName());
if ($storedSetting !== null) {
$cache->set($storedSetting->getName(), $storedSetting->getValue());
}
}
$em->flush();
if ($session instanceof Session) {
$session->getFlashBag()->set('notice', $translator->trans('settings_changed', [], 'CraueConfigBundle'));
}
return $this->redirectToRoute($this->container->getParameter('craue_config.redirectRouteAfterModify'));
}
}
return new Response($twig->render('@CraueConfig/Settings/modify.html.twig', [
'form' => $form->createView(),
'sections' => $this->getSections($allStoredSettings),
]));
}
/**
* @param SettingInterface[] $settings
* @return string[] (may also contain a null value)
*/
protected function getSections(array $settings) {
$sections = [];
foreach ($settings as $setting) {
$section = $setting->getSection();
if (!in_array($section, $sections, true)) {
$sections[] = $section;
}
}
sort($sections);
return $sections;
}
/**
* @param SettingInterface[] $settings
* @param string $name
* @return SettingInterface|null
*/
protected function getSettingByName(array $settings, $name) {
foreach ($settings as $setting) {
if ($setting->getName() === $name) {
return $setting;
}
}
return null;
}
}