1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Api; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\DTO\DtoUser; |
6
|
|
|
use AppBundle\Entity\DTO\DtoEvent; |
7
|
|
|
use AppBundle\Exception\JsonHttpException; |
8
|
|
|
use AppBundle\Form\LoginType; |
9
|
|
|
use Mcfedr\JsonFormBundle\Controller\JsonController; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
use Doctrine\Common\Collections\Criteria; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
|
17
|
|
|
class DefaultController extends JsonController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Request $request |
21
|
|
|
* @Route("/login", name="api_login") |
22
|
|
|
* @Method("POST") |
23
|
|
|
* |
24
|
|
|
* @return JsonResponse |
25
|
|
|
*/ |
26
|
1 |
|
public function loginAction(Request $request) |
27
|
|
|
{ |
28
|
1 |
|
$userCredentials = new DtoUser(); |
29
|
|
|
|
30
|
1 |
|
$form = $this->createForm(LoginType::class, $userCredentials); |
31
|
|
|
|
32
|
1 |
|
$this->handleJsonForm($form, $request); |
33
|
|
|
|
34
|
1 |
|
$user = $this->getDoctrine()->getRepository('AppBundle:User') |
35
|
1 |
|
->findOneBy(['email' => $userCredentials->getEmail()]); |
36
|
|
|
|
37
|
1 |
|
if (!$user) { |
38
|
|
|
throw new JsonHttpException(400, 'Bad credentials'); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$result = $this->get('security.encoder_factory') |
42
|
1 |
|
->getEncoder($user) |
43
|
1 |
|
->isPasswordValid($user->getPassword(), $userCredentials->getPassword(), null); |
44
|
1 |
|
if (!$result) { |
45
|
|
|
throw new JsonHttpException(400, 'Bad credentials'); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$token = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
49
|
|
|
|
50
|
1 |
|
$em = $this->getDoctrine() |
51
|
1 |
|
->getManager(); |
52
|
1 |
|
$user->setApiToken($token); |
53
|
|
|
|
54
|
1 |
|
$em->persist($user); |
55
|
|
|
|
56
|
1 |
|
$em->flush(); |
57
|
|
|
|
58
|
1 |
|
$serializer = $this->get('serializer'); |
59
|
1 |
|
$json = $serializer->normalize( |
60
|
|
|
$user, |
61
|
1 |
|
null, |
62
|
1 |
|
array('groups' => array('Short')) |
63
|
|
|
); |
64
|
|
|
|
65
|
1 |
|
return $this->json( |
66
|
1 |
|
['user' => $json, 'X-AUTH-TOKEN' => $token] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @Route("/dashboard") |
72
|
|
|
* @Method({"GET"}) |
73
|
|
|
* |
74
|
|
|
* @return JsonResponse |
75
|
|
|
*/ |
76
|
|
|
public function dashboardAction() |
77
|
|
|
{ |
78
|
|
|
$user = $this->getUser(); |
79
|
|
|
$events = $user->getEvents(); |
80
|
|
|
$requests = $user->getFormRequests(); |
81
|
|
|
$surveys = $user->getSurveys(); |
82
|
|
|
$array = new ArrayCollection( |
83
|
|
|
array_merge($events->toArray(), $requests->toArray(), $surveys->toArray()) |
84
|
|
|
); |
85
|
|
|
$news = $array->matching(Criteria::create()->orderBy(array('updatedAt' => Criteria::DESC))->setFirstResult(0) |
86
|
|
|
->setMaxResults(3)); |
87
|
|
|
$events = $events->matching(Criteria::create()->setFirstResult(0)->setMaxResults(2)); |
88
|
|
|
$calendar = $this->get('app.google_calendar'); |
89
|
|
|
$googleEvents = []; |
90
|
|
|
foreach ($events as $event) { |
91
|
|
|
$googleEvents[] = $calendar |
92
|
|
|
->getEventById($event->getGoogleId()); |
93
|
|
|
} |
94
|
|
|
$events = []; |
95
|
|
|
foreach ($googleEvents as $event) { |
96
|
|
|
if ($event) { |
97
|
|
|
$events[] = new DtoEvent($event); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
$surveys = $surveys->matching(Criteria::create()->where(Criteria::expr()->eq('status', 'current'))); |
101
|
|
|
|
102
|
|
|
return $this->json( |
103
|
|
|
['news' => $news, 'events' => $events, 'surveys' => $surveys] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Route("/news") |
109
|
|
|
* @Method({"GET"}) |
110
|
|
|
* |
111
|
|
|
* @return JsonResponse |
112
|
|
|
*/ |
113
|
|
|
public function newsAction() |
114
|
|
|
{ |
115
|
|
|
$user = $this->getUser(); |
116
|
|
|
$events = $user->getEvents(); |
117
|
|
|
$requests = $user->getFormRequests(); |
118
|
|
|
$surveys = $user->getSurveys(); |
119
|
|
|
$array = new ArrayCollection( |
120
|
|
|
array_merge($events->toArray(), $requests->toArray(), $surveys->toArray()) |
121
|
|
|
); |
122
|
|
|
$news = $array->matching(Criteria::create()->orderBy(array('updatedAt' => Criteria::DESC))->setFirstResult(0) |
123
|
|
|
->setMaxResults(100)); |
124
|
|
|
|
125
|
|
|
return $this->json( |
126
|
|
|
['news' => $news] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|