| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBooking\App\App; |
| 6 |
use FluentBooking\App\Services\CalendarService; |
| 7 |
|
| 8 |
class DataImporter |
| 9 |
{ |
| 10 |
public function importCalendar() |
| 11 |
{ |
| 12 |
$app = App::getInstance(); |
| 13 |
|
| 14 |
$data = $app->request->all(); |
| 15 |
|
| 16 |
if (empty($data['type'] || empty($data['user_id'] || empty($data['author_timezone'])))) { |
| 17 |
wp_send_json_error([ |
| 18 |
'message' => __('Please provide all required data', 'fluent-booking'), |
| 19 |
]); |
| 20 |
} |
| 21 |
|
| 22 |
$file = $app->request->file('file'); |
| 23 |
|
| 24 |
if (empty($file) || $file->getClientOriginalExtension() != 'json') { |
| 25 |
wp_send_json_error([ |
| 26 |
'message' => __('Invalid file. Please provide a valid JSON file', 'fluent-booking'), |
| 27 |
]); |
| 28 |
} |
| 29 |
|
| 30 |
$fileContent = $file->getContents(); |
| 31 |
|
| 32 |
$calendarData = wp_parse_args($data, json_decode($fileContent, true)); |
| 33 |
|
| 34 |
if (empty($calendarData)) { |
| 35 |
wp_send_json_error([ |
| 36 |
'message' => __('Invalid file. Please provide a valid JSON file', 'fluent-booking'), |
| 37 |
]); |
| 38 |
} |
| 39 |
|
| 40 |
$calendar = CalendarService::createCalendar($calendarData, false, true); |
| 41 |
|
| 42 |
if (is_wp_error($calendar)) { |
| 43 |
wp_send_json_error([ |
| 44 |
'message' => $calendar->get_error_message(), |
| 45 |
]); |
| 46 |
} |
| 47 |
|
| 48 |
wp_send_json([ |
| 49 |
'success' => true, |
| 50 |
'message' => __('Calendar imported successfully', 'fluent-booking'), |
| 51 |
]); |
| 52 |
} |
| 53 |
} |
| 54 |
|