/
UserController.php
106 lines (89 loc) · 2.96 KB
/
UserController.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
99
100
101
102
103
104
105
106
<?php
namespace Club\UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class UserController extends Controller
{
/**
* @Template()
* @Route("/user", name="user")
*/
public function indexAction()
{
$user = $this->getUser();
$form = $this->createForm(new \Club\UserBundle\Form\User(), $user);
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
$this->get('session')->setFlash('notice', $this->get('translator')->trans('Your changes are saved.'));
return $this->redirect($this->generateUrl('user'));
}
}
return array(
'user' => $user,
'form' => $form->createView()
);
}
/**
* @Template()
* @Route("/user/reset")
*/
public function resetAction()
{
$user = $this->get('security.context')->getToken()->getUser();
$form = $this->createFormBuilder($user)
->add('password', 'repeated', array(
'type' => 'password',
'first_name' => 'Password',
'second_name' => 'Password again',
'required' => false
))
->getForm();
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$reset = $em->getRepository('ClubUserBundle:ResetPassword')->findOneBy(array(
'user' => $user->getId()
));
$em->remove($reset);
$em->flush();
$this->get('session')->setFlash('notice', $this->get('translator')->trans('Your changes are saved.'));
return $this->redirect($this->generateUrl('user'));
}
}
return array(
'user' => $user,
'form' => $form->createView()
);
}
protected function getUser()
{
$user = $this->get('security.context')->getToken()->getUser();
$em = $this->getDoctrine()->getEntityManager();
if (!$user->getProfile()->getProfileAddress()) {
$address = new \Club\UserBundle\Entity\ProfileAddress();
$address->setContactType('home');
$address->setProfile($user->getProfile());
$user->getProfile()->setProfileAddress($address);
}
if (!$user->getProfile()->getProfilePhone()) {
$phone = new \Club\UserBundle\Entity\ProfilePhone();
$phone->setContactType('home');
$phone->setProfile($user->getProfile());
$user->getProfile()->setProfilePhone($phone);
}
if (!$user->getProfile()->getProfileEmail()) {
$email = new \Club\UserBundle\Entity\ProfileEmail();
$email->setContactType('home');
$email->setProfile($user->getProfile());
$user->getProfile()->setProfileEmail($email);
}
return $user;
}
}