@@ -6,9 +6,17 @@ class StanzaDelCittadinoBooking
6
6
7
7
const ENABLE_CACHE_KEY = 'sdc_booking_enabled ' ;
8
8
9
+ const CALENDAR_FILTER_CACHE_KEY = 'sdc_booking_calendar_filter ' ;
10
+
11
+ const STORE_AS_APPLICATION_CACHE_KEY = 'sdc_booking_as_application ' ;
12
+
13
+ private static $ useDraft = false ;
14
+
9
15
private static $ instance ;
10
16
11
- private function __construct (){}
17
+ private function __construct ()
18
+ {
19
+ }
12
20
13
21
public static function factory (): StanzaDelCittadinoBooking
14
22
{
@@ -72,6 +80,11 @@ public function storeConfig(int $office, int $service, int $place, array $calend
72
80
return false ;
73
81
}
74
82
83
+ public function getCalendar ($ id )
84
+ {
85
+ return StanzaDelCittadinoBridge::factory ()->instanceNewClient ()->getCalendar ($ id );
86
+ }
87
+
75
88
public function getCalendars (int $ service , int $ office , int $ place ): array
76
89
{
77
90
$ query = "SELECT calendars FROM ocbookingconfig WHERE service_id = $ service AND office_id = $ office AND place_id = $ place " ;
@@ -96,7 +109,7 @@ public function getOffices(int $service): array
96
109
$ query = "SELECT office_id, json_agg(json_build_object('place', place_id, 'calendars', calendars)) as data FROM ocbookingconfig WHERE service_id = $ service GROUP BY office_id " ;
97
110
$ rows = eZDB::instance ()->arrayQuery ($ query );
98
111
$ offices = [];
99
- foreach ($ rows as $ row ){
112
+ foreach ($ rows as $ row ) {
100
113
$ officeObject = eZContentObject::fetch ((int )$ row ['office_id ' ]);
101
114
if ($ officeObject instanceof eZContentObject) {
102
115
$ data = json_decode ($ row ['data ' ], true );
@@ -115,7 +128,7 @@ public function getOffices(int $service): array
115
128
],
116
129
'calendars ' => $ datum ['calendars ' ],
117
130
];
118
- if (isset ($ dataMap ['has_address ' ])){
131
+ if (isset ($ dataMap ['has_address ' ])) {
119
132
/** @var eZGmapLocation $address */
120
133
$ address = $ dataMap ['has_address ' ]->content ();
121
134
$ place ['address ' ] = [
@@ -211,9 +224,9 @@ public function getAvailabilities(array $calendars, string $month = null): array
211
224
StanzaDelCittadinoClient::$ processTimeout = 10 ;
212
225
$ client = StanzaDelCittadinoBridge::factory ()->instanceNewClient ();
213
226
$ currentMonth = date ('Y-m ' );
214
- if (!$ month || $ month == $ currentMonth ){
227
+ if (!$ month || $ month == $ currentMonth ) {
215
228
$ startDate = date ('Y-m-d ' );
216
- }else {
229
+ } else {
217
230
$ startDate = $ month . '-01 ' ;
218
231
}
219
232
$ startDateTime = DateTime::createFromFormat ('Y-m-d ' , $ startDate );
@@ -226,9 +239,15 @@ public function getAvailabilities(array $calendars, string $month = null): array
226
239
if ($ startDateTime instanceof DateTime) {
227
240
$ endDateTime = new DateTime (sprintf ('last day of %s ' , $ startDateTime ->format ('Y-m ' )));
228
241
$ response ['to ' ] = $ endDateTime ->format ('Y-m-d ' );
229
- $ remoteAvailabilities = $ client ->getCalendarsAvailabilities ($ calendars , $ startDate , $ endDateTime ->format ('Y-m-d ' ));
230
- foreach ($ remoteAvailabilities ['data ' ] as $ index => $ availability ){
231
- $ availability ['name ' ] = $ locale ->formatDate (DateTime::createFromFormat ('Y-m-d ' , $ availability ['date ' ])->format ('U ' ));
242
+ $ remoteAvailabilities = $ client ->getCalendarsAvailabilities (
243
+ $ calendars ,
244
+ $ startDate ,
245
+ $ endDateTime ->format ('Y-m-d ' )
246
+ );
247
+ foreach ($ remoteAvailabilities ['data ' ] as $ index => $ availability ) {
248
+ $ availability ['name ' ] = $ locale ->formatDate (
249
+ DateTime::createFromFormat ('Y-m-d ' , $ availability ['date ' ])->format ('U ' )
250
+ );
232
251
$ response ['availabilities ' ][$ index ] = $ availability ;
233
252
}
234
253
}
@@ -241,7 +260,7 @@ public function getAvailabilities(array $calendars, string $month = null): array
241
260
public function getAvailabilitiesByDay (array $ calendars , string $ day = null ): array
242
261
{
243
262
$ availabilities = [];
244
- if ($ day ){
263
+ if ($ day ) {
245
264
$ client = StanzaDelCittadinoBridge::factory ()->instanceNewClient ();
246
265
$ response = $ client ->getCalendarsAvailabilities ($ calendars , $ day );
247
266
$ availabilities = $ response ['data ' ];
@@ -250,17 +269,6 @@ public function getAvailabilitiesByDay(array $calendars, string $day = null): ar
250
269
return $ availabilities ;
251
270
}
252
271
253
- // public static function deleteDraftMeeting($meetingId)
254
- // {
255
- // try{
256
- // StanzaDelCittadinoBridge::factory()
257
- // ->instanceNewClient()
258
- // ->request('DELETE', '/api/meetings/'.$meetingId);
259
- // }catch (Throwable $e){
260
- // eZDebug::writeError($e->getMessage(), __METHOD__);
261
- // }
262
- // }
263
-
264
272
/**
265
273
* @param string $calendar
266
274
* @param string $date
@@ -270,19 +278,95 @@ public function getAvailabilitiesByDay(array $calendars, string $day = null): ar
270
278
* @return array
271
279
* @throws Exception
272
280
*/
273
- public function upsertDraftMeeting (string $ calendar , string $ date , string $ opening_hour , string $ slot , string $ meetingId = null ): array
281
+ public function upsertDraftMeeting (StanzaDelCittadinoBookingDTO $ dto ): array
274
282
{
275
- $ data = [
276
- 'calendar ' => $ calendar ,
277
- 'date ' => $ date ,
278
- 'opening_hour ' => $ opening_hour ,
279
- 'slot ' => $ slot ,
280
- 'meeting ' => $ meetingId ,
283
+ $ client = StanzaDelCittadinoBridge::factory ()->instanceNewClient ();
284
+ if (empty ($ dto ->getUserToken ())) {
285
+ $ authResponse = $ client ->request ('POST ' , '/api/session-auth ' );
286
+ $ dto ->setUserToken ($ authResponse ['token ' ]);
287
+ }
288
+ $ client ->setBearerToken ($ dto ->getUserToken ());
289
+ $ endpoint = '/it/meetings/new-draft ' ;
290
+ $ payload = $ dto ->toMeetingDraft ();
291
+ $ response = [
292
+ 'token ' => $ dto ->getUserToken (),
293
+ 'payload ' => $ payload ,
294
+ 'dto ' => $ dto ,
295
+ 'endpoint ' => $ endpoint ,
281
296
];
282
- return StanzaDelCittadinoBridge::factory ()
283
- ->instanceNewClient ()
284
- ->request ('POST ' , '/it/meetings/new-draft ' , $ data );
285
297
298
+ if (!self ::$ useDraft ){
299
+ $ response ['data ' ] = null ;
300
+ return $ response ;
301
+ }
302
+ try {
303
+ $ response ['data ' ] = $ client ->request ('POST ' , $ endpoint , $ payload );
304
+ }catch (Throwable $ e ){
305
+ $ response ['error ' ] = $ e ->getMessage ();
306
+ }
307
+
308
+ return $ response ;
309
+ }
310
+
311
+ public function bookMeeting (StanzaDelCittadinoBookingDTO $ dto ): array
312
+ {
313
+ return $ this ->isStoreMeetingAsApplication () ? $ this ->bookAsApplication ($ dto ) : $ this ->bookAsMeeting ($ dto );
314
+ }
315
+
316
+ private function bookAsApplication (StanzaDelCittadinoBookingDTO $ dto ): array
317
+ {
318
+ $ client = StanzaDelCittadinoBridge::factory ()->instanceNewClient ();
319
+ if (empty ($ dto ->getUserToken ())) {
320
+ $ authResponse = $ client ->request ('POST ' , '/api/session-auth ' );
321
+ $ dto ->setUserToken ($ authResponse ['token ' ]);
322
+ }
323
+ $ client ->setBearerToken ($ dto ->getUserToken ());
324
+ $ endpoint = '/api/applications ' ;
325
+ $ payload = $ dto ->toApplicationPayload ();
326
+ $ response = [
327
+ 'token ' => $ dto ->getUserToken (),
328
+ 'payload ' => $ payload ,
329
+ 'dto ' => $ dto ,
330
+ 'endpoint ' => $ endpoint ,
331
+ ];
332
+ try {
333
+ $ response ['data ' ] = $ client ->request ('POST ' , $ endpoint , $ payload );
334
+ }catch (Throwable $ e ){
335
+ $ response ['error ' ] = $ e ->getMessage ();
336
+ }
337
+
338
+ return $ response ;
339
+ }
340
+
341
+ private function bookAsMeeting (StanzaDelCittadinoBookingDTO $ dto ): array
342
+ {
343
+ $ client = StanzaDelCittadinoBridge::factory ()->instanceNewClient ();
344
+ if (empty ($ dto ->getUserToken ())) {
345
+ $ authResponse = $ client ->request ('POST ' , '/api/session-auth ' );
346
+ $ dto ->setUserToken ($ authResponse ['token ' ]);
347
+ }
348
+ $ client ->setBearerToken ($ dto ->getUserToken ());
349
+ if (self ::$ useDraft ) {
350
+ $ method = 'PUT ' ;
351
+ $ endpoint = '/api/meetings/ ' . $ dto ->getMeetingId ();
352
+ }else {
353
+ $ method = 'POST ' ;
354
+ $ endpoint = '/api/meetings ' ;
355
+ }
356
+ $ payload = $ dto ->toMeetingPayload ();
357
+ $ response = [
358
+ 'token ' => $ dto ->getUserToken (),
359
+ 'payload ' => $ payload ,
360
+ 'dto ' => $ dto ,
361
+ 'endpoint ' => $ endpoint ,
362
+ ];
363
+ try {
364
+ $ response ['data ' ] = $ client ->request ($ method , $ endpoint , $ payload );
365
+ }catch (Throwable $ e ){
366
+ $ response ['error ' ] = $ e ->getMessage ();
367
+ }
368
+
369
+ return $ response ;
286
370
}
287
371
288
372
public function getSteps (): array
@@ -348,4 +432,24 @@ public function getSteps(): array
348
432
],
349
433
];
350
434
}
435
+
436
+ public function useCalendarFilter (): bool
437
+ {
438
+ return (bool )$ this ->getStorage (self ::CALENDAR_FILTER_CACHE_KEY );
439
+ }
440
+
441
+ public function setUseCalendarFilter ($ enable )
442
+ {
443
+ $ this ->setStorage (self ::CALENDAR_FILTER_CACHE_KEY , (int )$ enable );
444
+ }
445
+
446
+ public function isStoreMeetingAsApplication (): bool
447
+ {
448
+ return (bool )$ this ->getStorage (self ::STORE_AS_APPLICATION_CACHE_KEY );
449
+ }
450
+
451
+ public function setStoreMeetingAsApplication ($ enable )
452
+ {
453
+ $ this ->setStorage (self ::STORE_AS_APPLICATION_CACHE_KEY , (int )$ enable );
454
+ }
351
455
}
0 commit comments