Completed
Push — master ( 65b406...199b8a )
by Julito
14:24 queued 12s
created

AccountController::homeAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Repository\IllustrationRepository;
7
use Chamilo\ThemeBundle\Model\UserInterface;
8
use Chamilo\UserBundle\Form\ProfileType;
9
use Chamilo\UserBundle\Repository\UserRepository;
10
use FOS\UserBundle\Model\UserManagerInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * Class UserController.
17
 *
18
 * @Route("/account")
19
 *
20
 * @author Julio Montoya <[email protected]>
21
 */
22
class AccountController extends BaseController
23
{
24
25
    /**
26
     * @Route("/home", methods={"GET"}, name="chamilo_core_account_home")
27
     *
28
     * @param string $username
29
     */
30
    public function homeAction()
31
    {
32
        $user = $this->getUser();
33
34
        if (!is_object($user) || !$user instanceof UserInterface) {
35
            throw $this->createAccessDeniedException('This user does not have access to this section');
36
        }
37
38
        return $this->render('@ChamiloCore/Account/home.html.twig', ['user' => $user]);
39
    }
40
41
    /**
42
     * @Route("/edit", methods={"GET", "POST"}, name="chamilo_core_account_edit")
43
     *
44
     * @param string $username
45
     */
46
    public function editAction(Request $request, UserManagerInterface $userManager, IllustrationRepository $illustrationRepository)
47
    {
48
        $user = $this->getUser();
49
50
        if (!is_object($user) || !$user instanceof UserInterface) {
51
            throw $this->createAccessDeniedException('This user does not have access to this section');
52
        }
53
54
        $form = $this->createForm(ProfileType::class, $user);
55
        $form->setData($user);
56
57
        $form->handleRequest($request);
58
59
        if ($form->isSubmitted() && $form->isValid()) {
60
            $illustration = $form['illustration']->getData();
61
            if ($illustration) {
62
                $file = $illustrationRepository->addIllustrationToUser($this->getUser(), $illustration);
63
                $em = $illustrationRepository->getEntityManager();
64
                $em->persist($file);
65
                $em->flush();
66
            }
67
68
            $userManager->updateUser($user);
69
70
            $this->addFlash('success', $this->trans('Updated'));
71
            $url = $this->generateUrl('chamilo_core_account_home', ['username' => $user->getUsername()]);
72
            $response = new RedirectResponse($url);
73
74
            return $response;
75
        }
76
77
        return $this->render('@ChamiloCore/Account/edit.html.twig', ['form' => $form->createView(), 'user' => $user]);
78
    }
79
}
80