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