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\Entity\FormRequest; |
9
|
|
|
use AppBundle\Entity\Survey\Survey; |
10
|
|
|
use AppBundle\Exception\JsonHttpException; |
11
|
|
|
use AppBundle\Form\LoginType; |
12
|
|
|
use Mcfedr\JsonFormBundle\Controller\JsonController; |
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Doctrine\Common\Collections\Criteria; |
18
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
19
|
|
|
|
20
|
|
|
class DefaultController extends JsonController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Request $request |
24
|
|
|
* @Route("/login", name="api_login") |
25
|
|
|
* @Method("POST") |
26
|
|
|
* |
27
|
|
|
* @return JsonResponse |
28
|
|
|
*/ |
29
|
1 |
|
public function loginAction(Request $request) |
30
|
|
|
{ |
31
|
1 |
|
$userCredentials = new DtoUser(); |
32
|
|
|
|
33
|
1 |
|
$form = $this->createForm(LoginType::class, $userCredentials); |
34
|
|
|
|
35
|
1 |
|
$this->handleJsonForm($form, $request); |
36
|
|
|
|
37
|
1 |
|
$user = $this->getDoctrine()->getRepository('AppBundle:User') |
38
|
1 |
|
->findOneBy(['email' => $userCredentials->getEmail()]); |
39
|
|
|
|
40
|
1 |
|
if (!$user) { |
41
|
|
|
throw new JsonHttpException(400, 'Bad credentials'); |
42
|
|
|
} |
43
|
1 |
|
if (!$user->isEnabled()) { |
44
|
|
|
throw new JsonHttpException(400, 'Account is not enabled.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$result = $this->get('security.encoder_factory') |
48
|
1 |
|
->getEncoder($user) |
49
|
1 |
|
->isPasswordValid($user->getPassword(), $userCredentials->getPassword(), null); |
50
|
1 |
|
if (!$result) { |
51
|
|
|
throw new JsonHttpException(400, 'Bad credentials'); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$token = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
55
|
|
|
|
56
|
1 |
|
$em = $this->getDoctrine() |
57
|
1 |
|
->getManager(); |
58
|
1 |
|
$user->setApiToken($token); |
59
|
|
|
|
60
|
1 |
|
$em->persist($user); |
61
|
|
|
|
62
|
1 |
|
$em->flush(); |
63
|
|
|
|
64
|
1 |
|
$serializer = $this->get('serializer'); |
65
|
1 |
|
$json = $serializer->normalize( |
66
|
1 |
|
$user, |
67
|
1 |
|
null, |
68
|
1 |
|
array('groups' => array('Short')) |
69
|
|
|
); |
70
|
|
|
|
71
|
1 |
|
return $this->json( |
72
|
1 |
|
['user' => $json, 'X-AUTH-TOKEN' => $token] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Route("/dashboard") |
78
|
|
|
* @Method({"GET"}) |
79
|
|
|
* |
80
|
|
|
* @return JsonResponse |
81
|
|
|
*/ |
82
|
|
|
public function dashboardAction() |
83
|
|
|
{ |
84
|
|
|
$user = $this->getUser(); |
85
|
|
|
$events = $this->getDoctrine()->getRepository(Event::class)->selectNotExpiredByUser($this->getUser(), true); |
86
|
|
|
$requests = $user->getFormRequests(); |
87
|
|
|
$surveys = $user->getSurveys(); |
88
|
|
|
$surveys = $surveys->matching(Criteria::create()->where(Criteria::expr()->eq('status', 'current'))); |
89
|
|
|
$array = new ArrayCollection( |
90
|
|
|
array_merge($events, $requests->toArray(), $surveys->toArray()) |
91
|
|
|
); |
92
|
|
|
$news = $array->matching(Criteria::create()->orderBy(array('updatedAt' => Criteria::DESC))->setFirstResult(0) |
93
|
|
|
->setMaxResults(3)); |
94
|
|
|
$sortNews = array_fill_keys(['events', 'surveys', 'requests'], []); |
95
|
|
|
$calendar = $this->get('app.google_calendar'); |
96
|
|
|
foreach ($news as $new) { |
97
|
|
|
if ($new instanceof Event) { |
98
|
|
|
$sortNews['events'][] = new DtoEvent($calendar |
99
|
|
|
->getEventById($new->getGoogleId())); |
100
|
|
|
} |
101
|
|
|
if ($new instanceof Survey) { |
102
|
|
|
$sortNews['surveys'][] = $new; |
103
|
|
|
} |
104
|
|
|
if ($new instanceof FormRequest) { |
105
|
|
|
$sortNews['requests'][] = $new; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$googleEvents = []; |
109
|
|
|
foreach ($events as $event) { |
110
|
|
|
$googleEvents[] = $calendar |
111
|
|
|
->getEventById($event->getGoogleId()); |
112
|
|
|
} |
113
|
|
|
$events = []; |
114
|
|
|
foreach ($googleEvents as $event) { |
115
|
|
|
if ($event) { |
116
|
|
|
$events[] = new DtoEvent($event); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->json( |
121
|
|
|
['news' => $sortNews, 'events' => $events, 'surveys' => $surveys] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @Route("/news") |
127
|
|
|
* @Method({"GET"}) |
128
|
|
|
* |
129
|
|
|
* @return JsonResponse |
130
|
|
|
*/ |
131
|
|
|
public function newsAction() |
132
|
|
|
{ |
133
|
|
|
$user = $this->getUser(); |
134
|
|
|
$events = $user->getEvents(); |
135
|
|
|
$requests = $user->getFormRequests(); |
136
|
|
|
$surveys = $user->getSurveys(); |
137
|
|
|
$surveys = $surveys->matching(Criteria::create()->where(Criteria::expr()->eq('status', 'current'))); |
138
|
|
|
$array = new ArrayCollection( |
139
|
|
|
array_merge($events->toArray(), $requests->toArray(), $surveys->toArray()) |
140
|
|
|
); |
141
|
|
|
$news = $array->matching(Criteria::create()->orderBy(array('updatedAt' => Criteria::DESC))->setFirstResult(0) |
142
|
|
|
->setMaxResults(3)); |
143
|
|
|
$calendar = $this->get('app.google_calendar'); |
144
|
|
|
|
145
|
|
|
$sortNews = []; |
146
|
|
|
foreach ($news as $new) { |
147
|
|
|
if ($new instanceof Event) { |
148
|
|
|
$item = new DtoEvent($calendar |
149
|
|
|
->getEventById($new->getGoogleId())); |
150
|
|
|
$sortNews[] = [ |
151
|
|
|
'text' => 'New Event in your calendar: '. $item->getSummary(), |
152
|
|
|
'date' => $item->getStart()->getDateTime(), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
if ($new instanceof Survey) { |
157
|
|
|
$sortNews[] = [ |
158
|
|
|
'text' => 'You\'ve got a new' . $new->getType()->getName() . 'survey!', |
159
|
|
|
'date' => $new->getCreatedAt(), |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
if ($new instanceof FormRequest) { |
163
|
|
|
$sortNews[] = [ |
164
|
|
|
'text' => 'Your '. $new->getType() .' form has been approved!', |
165
|
|
|
'createdAt' => $new->getCreatedAt(), |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->json( |
171
|
|
|
['news' => $sortNews] |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|