1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Controller\MembersArea; |
4
|
|
|
|
5
|
|
|
use Application\Entity\UserActionEntity; |
6
|
|
|
use Application\Form\Type\User\SettingsType; |
7
|
|
|
use Application\Form\Type\User\PasswordType; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Borut Balažek <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MyController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Application $app |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function indexAction(Application $app) |
23
|
|
|
{ |
24
|
|
|
return $app->redirect( |
25
|
|
|
$app['url_generator']->generate('members-area.my.profile') |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Application $app |
31
|
|
|
* |
32
|
|
|
* @return Response |
33
|
|
|
*/ |
34
|
|
|
public function profileAction(Application $app) |
35
|
|
|
{ |
36
|
|
|
return new Response( |
37
|
|
|
$app['twig']->render( |
38
|
|
|
'contents/members-area/my/profile.html.twig' |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Request $request |
45
|
|
|
* @param Application $app |
46
|
|
|
* |
47
|
|
|
* @return Response |
48
|
|
|
*/ |
49
|
|
|
public function settingsAction(Request $request, Application $app) |
50
|
|
|
{ |
51
|
|
|
$userOld = clone $app['user']; |
52
|
|
|
$userOldArray = $userOld->toArray(false); |
53
|
|
|
|
54
|
|
|
$form = $app['form.factory']->create( |
55
|
|
|
new SettingsType(), |
56
|
|
|
$app['user'] |
57
|
|
|
); |
58
|
|
|
$newEmailCode = $request->query->get('new_email_code'); |
59
|
|
|
|
60
|
|
|
if ($newEmailCode) { |
61
|
|
|
$userByNewEmailCode = $app['orm.em'] |
62
|
|
|
->getRepository('Application\Entity\UserEntity') |
63
|
|
|
->findOneByNewEmailCode($newEmailCode) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
if ( |
67
|
|
|
$userByNewEmailCode && |
68
|
|
|
$userByNewEmailCode === $app['user'] |
69
|
|
|
) { |
70
|
|
|
$app['user'] |
71
|
|
|
->setNewEmailCode(null) |
72
|
|
|
->setEmail($app['user']->getNewEmail()) |
73
|
|
|
->setNewEmail(null) |
74
|
|
|
; |
75
|
|
|
$app['orm.em']->persist($app['user']); |
76
|
|
|
$app['orm.em']->flush(); |
77
|
|
|
|
78
|
|
|
$app['application.mailer'] |
79
|
|
|
->swiftMessageInitializeAndSend(array( |
80
|
|
|
'subject' => $app['name'].' - '.$app['translator']->trans('Email change confirmation'), |
81
|
|
|
'to' => array($app['user']->getEmail()), |
82
|
|
|
'body' => 'emails/users/new-email-confirmation.html.twig', |
83
|
|
|
'templateData' => array( |
84
|
|
|
'user' => $app['user'], |
85
|
|
|
), |
86
|
|
|
)) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$app['flashbag']->add( |
90
|
|
|
'success', |
91
|
|
|
$app['translator']->trans( |
92
|
|
|
'You have successfully changed to your new email address!' |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} else { |
96
|
|
|
$app['flashbag']->add( |
97
|
|
|
'warning', |
98
|
|
|
$app['translator']->trans( |
99
|
|
|
'The new email code is invalid. Please request your new password again!' |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $app->redirect( |
105
|
|
|
$app['url_generator']->generate('members-area.my.settings') |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($request->getMethod() == 'POST') { |
110
|
|
|
$form->handleRequest($request); |
111
|
|
|
|
112
|
|
|
if ($form->isValid()) { |
113
|
|
|
$userEntity = $form->getData(); |
114
|
|
|
|
115
|
|
|
if ($userEntity->getProfile()->getRemoveImage()) { |
116
|
|
|
$userEntity->getProfile()->setImageUrl(null); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/*** Image ***/ |
120
|
|
|
$userEntity |
121
|
|
|
->getProfile() |
122
|
|
|
->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
123
|
|
|
->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
124
|
|
|
->imageUpload() |
125
|
|
|
; |
126
|
|
|
$app['orm.em']->persist($userEntity); |
127
|
|
|
|
128
|
|
|
if ($userOld->getEmail() !== $userEntity->getEmail()) { |
129
|
|
|
$userEntity |
130
|
|
|
->setNewEmailCode(md5(uniqid(null, true))) |
131
|
|
|
->setNewEmail($userEntity->getEmail()) |
132
|
|
|
->setEmail($userOld->getEmail()) |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
$app['application.mailer'] |
136
|
|
|
->swiftMessageInitializeAndSend(array( |
137
|
|
|
'subject' => $app['name'].' - '.$app['translator']->trans('Email change'), |
138
|
|
|
'to' => array($userEntity->getNewEmail()), |
139
|
|
|
'body' => 'emails/users/new-email.html.twig', |
140
|
|
|
'templateData' => array( |
141
|
|
|
'user' => $userEntity, |
142
|
|
|
), |
143
|
|
|
)) |
144
|
|
|
; |
145
|
|
|
|
146
|
|
|
$app['flashbag']->add( |
147
|
|
|
'success', |
148
|
|
|
$app['translator']->trans( |
149
|
|
|
'Please confirm your new password, by clicking the confirmation link we just sent you to the new email address!' |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$userActionEntity = new UserActionEntity(); |
155
|
|
|
$userActionEntity |
156
|
|
|
->setUser($userEntity) |
157
|
|
|
->setKey('user.settings.change') |
158
|
|
|
->setMessage('User has changed his settings!') |
159
|
|
|
->setData(array( |
160
|
|
|
'old' => $userOldArray, |
161
|
|
|
'new' => $userEntity->toArray(false), |
162
|
|
|
)) |
163
|
|
|
->setIp($app['request']->getClientIp()) |
164
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
165
|
|
|
; |
166
|
|
|
$app['orm.em']->persist($userActionEntity); |
167
|
|
|
|
168
|
|
|
$app['orm.em']->flush(); |
169
|
|
|
|
170
|
|
|
$app['flashbag']->add( |
171
|
|
|
'success', |
172
|
|
|
$app['translator']->trans( |
173
|
|
|
'Your settings were successfully saved!' |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
return $app->redirect( |
178
|
|
|
$app['url_generator']->generate('members-area.my.settings') |
179
|
|
|
); |
180
|
|
|
} else { |
181
|
|
|
$app['orm.em']->refresh($app['user']); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return new Response( |
186
|
|
|
$app['twig']->render( |
187
|
|
|
'contents/members-area/my/settings.html.twig', |
188
|
|
|
array( |
189
|
|
|
'form' => $form->createView(), |
190
|
|
|
) |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param Request $request |
197
|
|
|
* @param Application $app |
198
|
|
|
* |
199
|
|
|
* @return Response |
200
|
|
|
*/ |
201
|
|
|
public function passwordAction(Request $request, Application $app) |
202
|
|
|
{ |
203
|
|
|
$form = $app['form.factory']->create( |
204
|
|
|
new PasswordType(), |
205
|
|
|
$app['user'] |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
if ($request->getMethod() == 'POST') { |
209
|
|
|
$form->handleRequest($request); |
210
|
|
|
|
211
|
|
|
if ($form->isValid()) { |
212
|
|
|
$userEntity = $form->getData(); |
213
|
|
|
|
214
|
|
|
if ($userEntity->getPlainPassword()) { |
215
|
|
|
$userEntity->setPlainPassword( |
216
|
|
|
$userEntity->getPlainPassword(), |
217
|
|
|
$app['security.encoder_factory'] |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$app['orm.em']->persist($userEntity); |
221
|
|
|
|
222
|
|
|
$userActionEntity = new UserActionEntity(); |
223
|
|
|
$userActionEntity |
224
|
|
|
->setUser($userEntity) |
225
|
|
|
->setKey('user.password.change') |
226
|
|
|
->setMessage('User has changed his password!') |
227
|
|
|
->setIp($app['request']->getClientIp()) |
228
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
229
|
|
|
; |
230
|
|
|
$app['orm.em']->persist($userActionEntity); |
231
|
|
|
|
232
|
|
|
$app['orm.em']->flush(); |
233
|
|
|
|
234
|
|
|
$app['flashbag']->add( |
235
|
|
|
'success', |
236
|
|
|
$app['translator']->trans( |
237
|
|
|
'Your password was successfully changed!' |
238
|
|
|
) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return new Response( |
245
|
|
|
$app['twig']->render( |
246
|
|
|
'contents/members-area/my/password.html.twig', |
247
|
|
|
array( |
248
|
|
|
'form' => $form->createView(), |
249
|
|
|
) |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param Request $request |
256
|
|
|
* @param Application $app |
257
|
|
|
* |
258
|
|
|
* @return Response |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function actionsAction(Request $request, Application $app) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$limitPerPage = $request->query->get('limit_per_page', 20); |
263
|
|
|
$currentPage = $request->query->get('page'); |
264
|
|
|
|
265
|
|
|
$userActionResults = $app['orm.em'] |
266
|
|
|
->createQueryBuilder() |
267
|
|
|
->select('ua') |
268
|
|
|
->from('Application\Entity\UserActionEntity', 'ua') |
269
|
|
|
->where('ua.user = ?1') |
270
|
|
|
->setParameter(1, $app['user']) |
271
|
|
|
; |
272
|
|
|
|
273
|
|
|
$pagination = $app['paginator']->paginate( |
274
|
|
|
$userActionResults, |
275
|
|
|
$currentPage, |
276
|
|
|
$limitPerPage, |
277
|
|
|
array( |
278
|
|
|
'route' => 'members-area.my.actions', |
279
|
|
|
'defaultSortFieldName' => 'ua.timeCreated', |
280
|
|
|
'defaultSortDirection' => 'desc', |
281
|
|
|
'searchFields' => array( |
282
|
|
|
'ua.key', |
283
|
|
|
'ua.ip', |
284
|
|
|
), |
285
|
|
|
) |
286
|
|
|
); |
287
|
|
|
|
288
|
|
|
return new Response( |
289
|
|
|
$app['twig']->render( |
290
|
|
|
'contents/members-area/my/actions.html.twig', |
291
|
|
|
array( |
292
|
|
|
'pagination' => $pagination, |
293
|
|
|
) |
294
|
|
|
) |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
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.