|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xdaysaysay\AdminBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\UserBundle\Model\UserInterface; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
7
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
8
|
|
|
use Symfony\Component\Form\FormError; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Xdaysaysay\AdminBundle\Form\Type\UserType; |
|
11
|
|
|
use Xdaysaysay\AdminBundle\FormTrait\FormTrait; |
|
12
|
|
|
use Xdaysaysay\UserBundle\Entity\User; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class UserController |
|
16
|
|
|
* @package Xdaysaysay\AdminBundle\Controller |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
use FormTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* UserController constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->entityClassName = 'Xdaysaysay\UserBundle\Entity\User'; |
|
28
|
|
|
$this->repositoryName = 'XdaysaysayUserBundle:User'; |
|
29
|
|
|
$this->twigFormDirectory = 'XdaysaysayAdminBundle:User'; |
|
30
|
|
|
$this->formRoute = 'user'; |
|
31
|
|
|
$this->translation = 'user'; |
|
32
|
|
|
$this->formType = 'Xdaysaysay\AdminBundle\Form\Type\UserType'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Displays a form to edit an existing User entity. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* @param int $id |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
44
|
|
|
* @throws \LogicException |
|
45
|
|
|
* @throws \InvalidArgumentException |
|
46
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
47
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
48
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
49
|
|
|
* @throws \OutOfBoundsException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function editAction(Request $request, $id) |
|
52
|
|
|
{ |
|
53
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
54
|
|
|
|
|
55
|
|
|
$entity = $em->getRepository('XdaysaysayUserBundle:User')->find($id); |
|
56
|
|
|
|
|
57
|
|
|
if (!$entity) { |
|
58
|
|
|
throw $this->createNotFoundException('Unable to find User entity.'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$form = $this->createNewEditForm($entity); |
|
62
|
|
|
|
|
63
|
|
|
$form->handleRequest($request); |
|
64
|
|
|
|
|
65
|
|
|
if ($form->isValid()) { |
|
66
|
|
|
if ($form->get('password')->getData() !== '') { |
|
67
|
|
|
$entity->setPlainPassword($form->get('password')->getData()); |
|
68
|
|
|
} |
|
69
|
|
|
$this->get('fos_user.user_manager')->updateUser($entity); |
|
70
|
|
|
|
|
71
|
|
|
$this->addFlash('success', $this->get('translator')->trans('admin.user.flash.edit', [], 'admin')); |
|
72
|
|
|
|
|
73
|
|
|
return $this->redirect($this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()])); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->render( |
|
77
|
|
|
'@XdaysaysayAdmin/User/edit.html.twig', [ |
|
78
|
|
|
'entity' => $entity, |
|
79
|
|
|
'form' => $form->createView(), |
|
80
|
|
|
] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Displays a form to edit an existing User entity. |
|
86
|
|
|
* |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
92
|
|
|
* @throws \LogicException |
|
93
|
|
|
* @throws \InvalidArgumentException |
|
94
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
95
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
96
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
97
|
|
|
* @throws \OutOfBoundsException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function newAction(Request $request) |
|
100
|
|
|
{ |
|
101
|
|
|
$entity = $this->get('fos_user.user_manager')->createUser(); |
|
102
|
|
|
|
|
103
|
|
|
$form = $this->createNewEditForm($entity, true); |
|
104
|
|
|
|
|
105
|
|
|
$form->handleRequest($request); |
|
106
|
|
|
|
|
107
|
|
|
$user = $this->getDoctrine()->getRepository('XdaysaysayUserBundle:User')->findOneBy([ |
|
108
|
|
|
'username' => $form->get('username')->getData(), |
|
109
|
|
|
]); |
|
110
|
|
|
|
|
111
|
|
|
if ($user) { |
|
112
|
|
|
$form->get('username')->addError(new FormError($this->get('translator')->trans('admin.user.flash.username_already_exists', [], 'admin'))); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$user = $this->getDoctrine()->getRepository('XdaysaysayUserBundle:User')->findOneBy([ |
|
116
|
|
|
'email' => $form->get('email')->getData(), |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
if ($user) { |
|
120
|
|
|
$form->get('email')->addError(new FormError($this->get('translator')->trans('admin.user.flash.email_already_exists', [], 'admin'))); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($form->isValid()) { |
|
124
|
|
|
$entity->setPlainPassword($form->get('password')->getData()); |
|
125
|
|
|
$this->get('fos_user.user_manager')->updateUser($entity); |
|
126
|
|
|
|
|
127
|
|
|
$this->addFlash('success', $this->get('translator')->trans('admin.user.flash.create', [], 'admin')); |
|
128
|
|
|
|
|
129
|
|
|
return $this->redirect($this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()])); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this->render( |
|
133
|
|
|
'@XdaysaysayAdmin/User/new.html.twig', [ |
|
134
|
|
|
'entity' => $entity, |
|
135
|
|
|
'form' => $form->createView(), |
|
136
|
|
|
] |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Creates a form to create a User entity. |
|
143
|
|
|
* |
|
144
|
|
|
* @param UserInterface $entity The entity |
|
145
|
|
|
* @param boolean $newUser Define if the for is for a new user entity |
|
146
|
|
|
* |
|
147
|
|
|
* @return \Symfony\Component\Form\Form The form |
|
148
|
|
|
* |
|
149
|
|
|
* @throws \InvalidArgumentException |
|
150
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
151
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
152
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
153
|
|
|
*/ |
|
154
|
|
|
private function createNewEditForm(UserInterface $entity, $newUser = false) |
|
155
|
|
|
{ |
|
156
|
|
|
$action = $this->generateUrl('xdaysaysay_admin_user_new'); |
|
157
|
|
|
$submitText = $this->get('translator')->trans('admin.common.form.create', [], 'admin'); |
|
158
|
|
|
|
|
159
|
|
|
if ($entity->getId() !== null) { |
|
160
|
|
|
$action = $this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()]); |
|
161
|
|
|
$submitText = $this->get('translator')->trans('admin.common.form.update', [], 'admin'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$form = $this->createForm( |
|
165
|
|
|
UserType::class, $entity, [ |
|
166
|
|
|
'action' => $action, |
|
167
|
|
|
'method' => 'PUT', |
|
168
|
|
|
'new_user' => $newUser, |
|
169
|
|
|
] |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$form->add( |
|
173
|
|
|
'submit', SubmitType::class, [ |
|
174
|
|
|
'label' => $submitText, |
|
175
|
|
|
'attr' => [ |
|
176
|
|
|
'class' => 'btn btn-primary', |
|
177
|
|
|
], |
|
178
|
|
|
] |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
return $form; |
|
182
|
|
|
} |
|
183
|
|
|
} |