11
11
12
12
namespace FOS \UserBundle \Controller ;
13
13
14
+ use FOS \UserBundle \FOSUserEvents ;
15
+ use FOS \UserBundle \Event \FilterGroupResponseEvent ;
16
+ use FOS \UserBundle \Event \FormEvent ;
14
17
use Symfony \Component \DependencyInjection \ContainerAware ;
18
+ use Symfony \Component \HttpFoundation \Request ;
15
19
use Symfony \Component \HttpFoundation \RedirectResponse ;
16
20
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
17
21
@@ -36,51 +40,91 @@ public function listAction()
36
40
/**
37
41
* Show one group
38
42
*/
39
- public function showAction ($ groupname )
43
+ public function showAction ($ groupName )
40
44
{
41
- $ group = $ this ->findGroupBy ('name ' , $ groupname );
45
+ $ group = $ this ->findGroupBy ('name ' , $ groupName );
42
46
43
47
return $ this ->container ->get ('templating ' )->renderResponse ('FOSUserBundle:Group:show.html. ' .$ this ->getEngine (), array ('group ' => $ group ));
44
48
}
45
49
46
50
/**
47
51
* Edit one group, show the edit form
48
52
*/
49
- public function editAction ($ groupname )
53
+ public function editAction (Request $ request , $ groupName )
50
54
{
51
- $ group = $ this ->findGroupBy ('name ' , $ groupname );
52
- $ form = $ this -> container -> get ( ' fos_user.group.form ' );
53
- $ formHandler = $ this ->container ->get ('fos_user.group.form.handler ' );
55
+ $ group = $ this ->findGroupBy ('name ' , $ groupName );
56
+ /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
57
+ $ formFactory = $ this ->container ->get ('fos_user.group.form.factory ' );
54
58
55
- $ process = $ formHandler ->process ($ group );
56
- if ($ process ) {
57
- $ this ->setFlash ('fos_user_success ' , 'group.flash.updated ' );
58
- $ groupUrl = $ this ->container ->get ('router ' )->generate ('fos_user_group_show ' , array ('groupname ' => $ group ->getName ()));
59
+ $ form = $ formFactory ->createForm ();
60
+ $ form ->setData ($ group );
59
61
60
- return new RedirectResponse ($ groupUrl );
62
+ if ($ request ->isMethod ('POST ' )) {
63
+ $ form ->bind ($ request );
64
+
65
+ if ($ form ->isValid ()) {
66
+ /** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
67
+ $ groupManager = $ this ->container ->get ('fos_user.group_manager ' );
68
+ /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
69
+ $ dispatcher = $ this ->container ->get ('event_dispatcher ' );
70
+
71
+ $ event = new FormEvent ($ form , $ request );
72
+ $ dispatcher ->dispatch (FOSUserEvents::GROUP_EDIT_SUCCESS , $ event );
73
+
74
+ $ groupManager ->updateGroup ($ group );
75
+
76
+ if (null === $ response = $ event ->getResponse ()) {
77
+ $ url = $ this ->container ->get ('router ' )->generate ('fos_user_group_show ' , array ('groupName ' => $ group ->getName ()));
78
+ $ response = new RedirectResponse ($ url );
79
+ }
80
+
81
+ $ dispatcher ->dispatch (FOSUserEvents::GROUP_EDIT_COMPLETED , new FilterGroupResponseEvent ($ group , $ request , $ response ));
82
+
83
+ return $ response ;
84
+ }
61
85
}
62
86
63
87
return $ this ->container ->get ('templating ' )->renderResponse ('FOSUserBundle:Group:edit.html. ' .$ this ->getEngine (), array (
64
88
'form ' => $ form ->createview (),
65
- 'groupname ' => $ group ->getName (),
89
+ 'group_name ' => $ group ->getName (),
66
90
));
67
91
}
68
92
69
93
/**
70
94
* Show the new form
71
95
*/
72
- public function newAction ()
96
+ public function newAction (Request $ request )
73
97
{
74
- $ form = $ this ->container ->get ('fos_user.group.form ' );
75
- $ formHandler = $ this ->container ->get ('fos_user.group.form.handler ' );
98
+ /** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
99
+ $ groupManager = $ this ->container ->get ('fos_user.group_manager ' );
100
+ /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
101
+ $ formFactory = $ this ->container ->get ('fos_user.group.form.factory ' );
102
+
103
+ $ group = $ groupManager ->createGroup ('' );
104
+ $ form = $ formFactory ->createForm ();
105
+ $ form ->setData ($ group );
106
+
107
+ if ($ request ->isMethod ('POST ' )) {
108
+ $ form ->bind ($ request );
109
+
110
+ if ($ form ->isValid ()) {
111
+ /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
112
+ $ dispatcher = $ this ->container ->get ('event_dispatcher ' );
113
+
114
+ $ event = new FormEvent ($ form , $ request );
115
+ $ dispatcher ->dispatch (FOSUserEvents::GROUP_CREATE_SUCCESS , $ event );
76
116
77
- $ process = $ formHandler ->process ();
78
- if ($ process ) {
79
- $ this ->setFlash ('fos_user_success ' , 'group.flash.created ' );
80
- $ parameters = array ('groupname ' => $ form ->getData ('group ' )->getName ());
81
- $ url = $ this ->container ->get ('router ' )->generate ('fos_user_group_show ' , $ parameters );
117
+ $ groupManager ->updateGroup ($ group );
82
118
83
- return new RedirectResponse ($ url );
119
+ if (null === $ response = $ event ->getResponse ()) {
120
+ $ url = $ this ->container ->get ('router ' )->generate ('fos_user_group_show ' , array ('groupName ' => $ group ->getName ()));
121
+ $ response = new RedirectResponse ($ url );
122
+ }
123
+
124
+ $ dispatcher ->dispatch (FOSUserEvents::GROUP_CREATE_COMPLETED , new FilterGroupResponseEvent ($ group , $ request , $ response ));
125
+
126
+ return $ response ;
127
+ }
84
128
}
85
129
86
130
return $ this ->container ->get ('templating ' )->renderResponse ('FOSUserBundle:Group:new.html. ' .$ this ->getEngine (), array (
@@ -91,13 +135,18 @@ public function newAction()
91
135
/**
92
136
* Delete one group
93
137
*/
94
- public function deleteAction ($ groupname )
138
+ public function deleteAction (Request $ request , $ groupName )
95
139
{
96
- $ group = $ this ->findGroupBy ('name ' , $ groupname );
140
+ $ group = $ this ->findGroupBy ('name ' , $ groupName );
97
141
$ this ->container ->get ('fos_user.group_manager ' )->deleteGroup ($ group );
98
- $ this ->setFlash ('fos_user_success ' , 'group.flash.deleted ' );
99
142
100
- return new RedirectResponse ($ this ->container ->get ('router ' )->generate ('fos_user_group_list ' ));
143
+ $ response = new RedirectResponse ($ this ->container ->get ('router ' )->generate ('fos_user_group_list ' ));
144
+
145
+ /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
146
+ $ dispatcher = $ this ->container ->get ('event_dispatcher ' );
147
+ $ dispatcher ->dispatch (FOSUserEvents::GROUP_CREATE_COMPLETED , new FilterGroupResponseEvent ($ group , $ request , $ response ));
148
+
149
+ return $ response ;
101
150
}
102
151
103
152
/**
@@ -106,7 +155,7 @@ public function deleteAction($groupname)
106
155
* @param string $key property name
107
156
* @param mixed $value property value
108
157
*
109
- * @throws NotFoundException if user does not exist
158
+ * @throws NotFoundHttpException if user does not exist
110
159
* @return \FOS\UserBundle\Model\GroupInterface
111
160
*/
112
161
protected function findGroupBy ($ key , $ value )
@@ -126,13 +175,4 @@ protected function getEngine()
126
175
{
127
176
return $ this ->container ->getParameter ('fos_user.template.engine ' );
128
177
}
129
-
130
- /**
131
- * @param string $action
132
- * @param string $value
133
- */
134
- protected function setFlash ($ action , $ value )
135
- {
136
- $ this ->container ->get ('session ' )->setFlash ($ action , $ value );
137
- }
138
178
}
0 commit comments