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