PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.24
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.24
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Hooks / Handlers / DataExporter.php

DataExporter.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.24, at app/Hooks/Handlers/DataExporter.php

179 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Hooks\Handlers;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Models\Calendar;
7 use FluentBooking\App\Models\Availability;
8 use FluentBooking\App\Services\PermissionManager;
9
10 class DataExporter
11 {
12 public function exportCalendar()
13 {
14 $calendarId = (int)$_REQUEST['calendar_id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
15
16 if (!$calendarId) {
17 die(esc_html__('Please provide Calendar ID', 'fluent-booking'));
18 }
19
20 $calendar = Calendar::with(['metas', 'events' => function ($query) {
21 $query->with('event_metas');
22 }])->find($calendarId);
23
24 if (!$calendar) {
25 die(esc_html__('Calendar not found', 'fluent-booking'));
26 }
27
28 if (!PermissionManager::hasAllCalendarAccess() && !PermissionManager::hasCalendarAccess($calendar)) {
29 die(esc_html__('You do not have permission to export data', 'fluent-booking'));
30 }
31
32 $calendarData = $this->prepareCalendarExportData($calendar);
33
34 header('Content-Type: application/json');
35 header('Content-Disposition: attachment; filename=CluentBookingHostExport-' . $calendarId . '.json');
36 echo json_encode($calendarData, JSON_PRETTY_PRINT); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
37 exit();
38 }
39
40 public function exportBookingHosts()
41 {
42 if (!PermissionManager::hasAllCalendarAccess()) {
43 die(esc_html__('You do not have permission to export data', 'fluent-booking'));
44 }
45
46 $groupId = (int)$_REQUEST['group_id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
47
48 if (!$groupId) {
49 die(esc_html__('Please provide Group ID', 'fluent-booking'));
50 }
51
52 $attendees = Booking::where('group_id', $groupId)->get();
53
54 $csvData[] = [
55 'First Name',
56 'Last Name',
57 'Email',
58 'Message',
59 'Location Details',
60 'Source',
61 'Booking Type',
62 'Status',
63 'Source URL',
64 'Duration',
65 'Start Time',
66 'End Time',
67 'Payment Status',
68 'Payment Order Status',
69 'Payment Method',
70 'Currency',
71 'Total Amount',
72 'Order Created At',
73 'Transaction ID',
74 'Vendor Charge ID',
75 'Transaction Payment Method',
76 'Transaction Status',
77 'Transaction Total',
78 'Transaction Created At',
79 ];
80
81 foreach ($attendees as $attendee) {
82 $row = [
83 $attendee->first_name,
84 $attendee->last_name,
85 $attendee->email,
86 $attendee->message,
87 $attendee->getLocationAsText(),
88 $attendee->source,
89 $attendee->booking_type,
90 $attendee->status,
91 $attendee->source_url,
92 $attendee->slot_minutes,
93 $attendee->start_time,
94 $attendee->end_time,
95 $attendee->payment_status,
96 ];
97
98 if ($attendee->payment_status) {
99 $order = $attendee->payment_order;
100 if ($order) {
101 $order->load(['items', 'transaction']);
102 $row[] = $order->status;
103 $row[] = $order->payment_method;
104 $row[] = $order->currency;
105 $row[] = $order->total_amount / 100;
106 $row[] = $order->created_at;
107 $row[] = $order->transaction->id;
108 $row[] = $order->transaction->vendor_charge_id;
109 $row[] = $order->transaction->payment_method;
110 $row[] = $order->transaction->status;
111 $row[] = $order->transaction->total / 100;
112 $row[] = $order->transaction->created_at;
113 }
114 } else {
115 // Fill empty columns for payment related data if payment_status is false
116 $row = array_pad($row, 11, '');
117 }
118
119 $csvData[] = $row;
120 }
121
122 $csvData = apply_filters('fluent_booking/exporting_booking_data_csv', $csvData, $attendees);
123
124 $output = fopen('php://output', 'w');
125 header('Content-Type: text/csv');
126 header('Content-Disposition: attachment; filename=Booking-Event-Guests-' . $groupId . '.csv');
127
128 foreach ($csvData as $row) {
129 fputcsv($output, $row);
130 }
131
132 fclose($output); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
133 exit();
134 }
135
136 /*
137 * Prepare calendar data for export
138 * @param Calendar|int $calendar Calendar Model or ID
139 * @return array
140 */
141 public function prepareCalendarExportData($calendar = null)
142 {
143 if (is_numeric($calendar)) {
144 $calendar = Calendar::with(['metas', 'events' => function ($query) {
145 $query->with('event_metas');
146 }])->find($calendar);
147 } else if (is_null($calendar)) {
148 $calendar = Calendar::with(['metas', 'events' => function ($query) {
149 $query->with('event_metas');
150 }])->first();
151 }
152
153 if (!$calendar) {
154 return [];
155 }
156
157 $availabilities = [];
158
159 foreach ($calendar->events as $event) {
160 if (isset($availabilities[$event->availability_id])) {
161 continue;
162 }
163 $availability = Availability::find($event->availability_id);
164 if ($availability) {
165 $availabilities[$event->availability_id] = $availability;
166 }
167 }
168
169 $calendarData = $calendar->toArray();
170
171 $calendarData['data_type'] = 'host';
172 $calendarData['availabilities'] = $availabilities;
173
174 $calendarData = apply_filters('fluent_booking/exporting_calendar_data_json', $calendarData, $calendar);
175
176 return $calendarData;
177 }
178 }
179