This repository was archived by the owner on Mar 17, 2023. It is now read-only.
forked from elcweb/KeyValueStoreBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefaultController.php
More file actions
104 lines (80 loc) · 2.96 KB
/
DefaultController.php
File metadata and controls
104 lines (80 loc) · 2.96 KB
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
99
100
101
102
103
<?php
namespace Elcweb\KeyValueStoreBundle\Controller;
use Elcweb\KeyValueStoreBundle\Entity\KeyValue;
use Elcweb\KeyValueStoreBundle\Form\KeyValueType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* @Route("/admin/keyvalue")
*/
class DefaultController extends Controller
{
/**
* @Route("/")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
// Show soft deleted users
$filters = $em->getFilters();
$filters->disable('softdeleteable');
$items = $em->getRepository('ElcwebKeyValueStoreBundle:KeyValue')->findAll();
return array('items' => $items);
}
/**
* @Route("/create")
* @Template()
*/
public function createAction(Request $request)
{
$keyValue = new KeyValue();
$form = $this->createForm(new KeyValueType(), $keyValue);
$result = $this->processForm($form, $request, $keyValue, 'created');
if ($result) {
return $this->redirect($this->generateUrl('elcweb_keyvaluestore_default_index'));
}
return array('form' => $form->createView(), 'actionName' => 'Create');
}
/**
* @Route("/edit/{key}")
* @Template("ElcwebKeyValueStoreBundle:Default:create.html.twig")
*/
public function editAction(Request $request, KeyValue $keyValue)
{
$form = $this->createForm(new KeyValueType(), $keyValue);
$result = $this->processForm($form, $request, $keyValue, 'updated');
if ($result) {
return $this->redirect($this->generateUrl('elcweb_keyvaluestore_default_index'));
}
return array('form' => $form->createView(), 'actionName' => 'Edit');
}
private function processForm($form, $request, $keyValue, $action = 'updated')
{
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$keyValue = $form->getData();
$dispatcher = $this->container->get('event_dispatcher');
$dispatcher->dispatch('elcweb.keyvalue.'.$action, new GenericEvent('', array('keyValue' => $keyValue)));
$this->get('session')->getFlashBag()->add('success', 'Saved.');
return true;
}
}
return false;
}
/**
* @Route("/delete/{key}")
*/
public function deleteAction(KeyValue $keyValue)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($keyValue);
$entityManager->flush();
$this->get('session')->getFlashBag()->add('success', "{$keyValue->getKey()} Removed");
return $this->redirect($this->generateUrl('elcweb_keyvaluestore_default_index'));
}
}