|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JhFlexiTime\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use JhFlexiTime\Repository\UserSettingsRepositoryInterface; |
|
6
|
|
|
use JhUser\Repository\UserRepositoryInterface; |
|
7
|
|
|
use Zend\Mvc\Controller\AbstractRestfulController; |
|
8
|
|
|
use Zend\Validator\Date as DateValidator; |
|
9
|
|
|
use Zend\View\Model\JsonModel; |
|
10
|
|
|
use JhFlexiTime\Service\TimeCalculatorService; |
|
11
|
|
|
use JhFlexiTime\Service\BookingService; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class BookingRestController |
|
15
|
|
|
* @package JhFlexiTime\Controller |
|
16
|
|
|
* @author Aydin Hassan <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class BookingRestController extends AbstractRestfulController |
|
19
|
|
|
{ |
|
20
|
|
|
use GetSetDateTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \JhFlexiTime\Service\BookingService |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $bookingService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \JhFlexiTime\Service\TimeCalculatorService |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $timeCalculatorService; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var UserRepositoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $userRepository; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var UserSettingsRepositoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $userSettingsRepository; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param BookingService $bookingService |
|
44
|
|
|
* @param TimeCalculatorService $timeCalculatorService |
|
45
|
|
|
* @param UserRepositoryInterface $userRepository |
|
46
|
|
|
* @param UserSettingsRepositoryInterface $userSettingsRepository |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( |
|
49
|
|
|
BookingService $bookingService, |
|
50
|
|
|
TimeCalculatorService $timeCalculatorService, |
|
51
|
|
|
UserRepositoryInterface $userRepository, |
|
52
|
|
|
UserSettingsRepositoryInterface $userSettingsRepository |
|
53
|
|
|
) { |
|
54
|
|
|
$this->bookingService = $bookingService; |
|
55
|
|
|
$this->timeCalculatorService = $timeCalculatorService; |
|
56
|
|
|
$this->userRepository = $userRepository; |
|
57
|
|
|
$this->userSettingsRepository = $userSettingsRepository; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return JsonModel |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getList() |
|
64
|
|
|
{ |
|
65
|
|
|
$month = $this->params()->fromQuery('m', false); |
|
66
|
|
|
$year = $this->params()->fromQuery('y', false); |
|
67
|
|
|
$userId = $this->params()->fromQuery('user', false); |
|
68
|
|
|
$period = $this->getDate($month, $year); |
|
69
|
|
|
|
|
70
|
|
|
if ($userId && $this->isGranted('flexi-time.readOthers')) { |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$user = $this->userRepository->find($userId); |
|
73
|
|
|
if (!$user) { |
|
74
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
75
|
|
|
'success' => false, |
|
76
|
|
|
'message' => 'User does not exist', |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$userSettings = $this->userSettingsRepository->findOneByUser($user); |
|
84
|
|
|
$records = $this->bookingService->getUserBookingsForMonth($user, $period); |
|
85
|
|
|
$pagination = $this->bookingService->getPagination($period); |
|
86
|
|
|
$totals = $this->timeCalculatorService->getTotals($user, $userSettings->getFlexStartDate(), $period); |
|
87
|
|
|
|
|
88
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
89
|
|
|
'bookings' => [ |
|
90
|
|
|
'records' => $records, |
|
91
|
|
|
'totals' => $totals, |
|
92
|
|
|
'user' => $user, |
|
93
|
|
|
], |
|
94
|
|
|
'pagination' => $pagination, |
|
95
|
|
|
'date' => $period, |
|
96
|
|
|
'today' => new \DateTime("today"), |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int $id |
|
102
|
|
|
* @return JsonModel |
|
103
|
|
|
*/ |
|
104
|
|
|
public function get($id) |
|
105
|
|
|
{ |
|
106
|
|
|
$id = $this->parseIdCriteria($id); |
|
107
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
108
|
|
|
'booking' => $this->bookingService->getBookingByUserAndDate($id['user'], $id['date']), |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
* @return JsonModel |
|
115
|
|
|
*/ |
|
116
|
|
|
public function create($data) |
|
117
|
|
|
{ |
|
118
|
|
|
$booking = $this->bookingService->create($data); |
|
119
|
|
|
|
|
120
|
|
|
if (is_array($booking)) { |
|
121
|
|
|
$booking['success'] = false; |
|
122
|
|
|
return new JsonModel($booking); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser()); |
|
126
|
|
|
$monthTotals = $this->timeCalculatorService->getTotals( |
|
127
|
|
|
$booking->getUser(), |
|
128
|
|
|
$userSettings->getFlexStartDate(), |
|
129
|
|
|
$booking->getDate() |
|
130
|
|
|
); |
|
131
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
132
|
|
|
'success' => true, |
|
133
|
|
|
'booking' => $booking, |
|
134
|
|
|
'monthTotals' => $monthTotals, |
|
135
|
|
|
'weekTotals' => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate()) |
|
136
|
|
|
]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param int $id |
|
141
|
|
|
* @param array $data |
|
142
|
|
|
* @return JsonModel |
|
143
|
|
|
*/ |
|
144
|
|
|
public function update($id, $data) |
|
145
|
|
|
{ |
|
146
|
|
|
$id = $this->parseIdCriteria($id); |
|
147
|
|
|
$booking = $this->bookingService->update($id['user'], $id['date'], $data); |
|
148
|
|
|
|
|
149
|
|
|
if (is_array($booking)) { |
|
150
|
|
|
$booking['success'] = false; |
|
151
|
|
|
return new JsonModel($booking); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser()); |
|
155
|
|
|
$monthTotals = $this->timeCalculatorService->getTotals( |
|
156
|
|
|
$booking->getUser(), |
|
157
|
|
|
$userSettings->getFlexStartDate(), |
|
158
|
|
|
$booking->getDate() |
|
159
|
|
|
); |
|
160
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
161
|
|
|
'success' => true, |
|
162
|
|
|
'booking' => $booking, |
|
163
|
|
|
'monthTotals' => $monthTotals, |
|
164
|
|
|
'weekTotals' => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate()) |
|
165
|
|
|
]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param int $id |
|
170
|
|
|
* @return JsonModel |
|
171
|
|
|
*/ |
|
172
|
|
|
public function delete($id) |
|
173
|
|
|
{ |
|
174
|
|
|
$id = $this->parseIdCriteria($id); |
|
175
|
|
|
$booking = $this->bookingService->delete($id['user'], $id['date']); |
|
176
|
|
|
if (is_array($booking)) { |
|
177
|
|
|
$booking['success'] = false; |
|
178
|
|
|
return new JsonModel($booking); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser()); |
|
182
|
|
|
$monthTotals = $this->timeCalculatorService->getTotals( |
|
183
|
|
|
$booking->getUser(), |
|
184
|
|
|
$userSettings->getFlexStartDate(), |
|
185
|
|
|
$booking->getDate() |
|
186
|
|
|
); |
|
187
|
|
|
return new JsonModel([ |
|
|
|
|
|
|
188
|
|
|
'success' => true, |
|
189
|
|
|
'monthTotals' => $monthTotals, |
|
190
|
|
|
'weekTotals' => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate()) |
|
191
|
|
|
]); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param $id |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
public function parseIdCriteria($id) |
|
199
|
|
|
{ |
|
200
|
|
|
$idParts = explode("-", $id); |
|
201
|
|
|
$date = new \JhFlexiTime\DateTime\DateTime(); |
|
202
|
|
|
$date->setTimestamp($idParts[0]); |
|
203
|
|
|
return ['date' => $date,'user' => $idParts[1]]; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: