PluginProbe
Bookit — Booking & Appointment Calendar / trunk
Bookit — Booking & Appointment Calendar vtrunk
2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 All 61 releases
bookit / includes / classes / admin / ImportExportController.php

ImportExportController.php in Bookit — Booking & Appointment Calendar trunk, at includes/classes/admin/ImportExportController.php

396 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes\Admin;
4
5 use Bookit\Classes\Database\Categories;
6 use Bookit\Classes\Database\Services;
7 use Bookit\Classes\Database\Staff;
8 use Bookit\Classes\Database\Staff_Services;
9 use Bookit\Classes\Database\Staff_Working_Hours;
10 use Bookit\Classes\Database\Appointments;
11
12 class ImportExportController {
13
14 /**
15 * Export Bookit Data
16 */
17 public static function export() {
18 check_ajax_referer( 'bookit_export', 'nonce' );
19
20 if ( ! current_user_can( 'manage_options' ) ) {
21 return false;
22 }
23
24 $settings = SettingsController::get_settings();
25 $services = Services::get_all();
26 $categories = Categories::get_all();
27 $staff = Staff::get_all();
28 $export_data = array(
29 'Settings' => $settings,
30 'Categories' => $categories,
31 'Service' => $services,
32 'Staff' => $staff,
33 );
34 if ( $export_data ) {
35 $export_data = json_encode( $export_data );
36 header( 'Content-Description: File Transfer' );
37 header( 'Content-Disposition: attachment; filename=bookit_data.txt' );
38 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
39 echo sanitize_text_field( $export_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
40 die;
41 }
42 }
43
44 /**
45 * Export Excel Data
46 */
47 public static function export_excel() {
48 check_ajax_referer( 'bookit_export', 'nonce' );
49
50 if ( ! current_user_can( 'manage_options' ) ) {
51 return false;
52 }
53
54 $appointments = Appointments::export_all();
55 $data = array();
56 $headers = array(
57 'ID',
58 'CUSTOMER',
59 'CUSTOMER PHONE',
60 'STAFF',
61 'SERVICE',
62 'PRICE',
63 'DATE',
64 'TIME',
65 'PAYMENT',
66 'STATUS',
67 );
68
69 foreach ( $appointments as $key => $appointment ) {
70 foreach ( $headers as $header ) {
71 switch ( $header ) {
72 case 'ID':
73 $data[ $key ][ $header ] = $appointment['id'];
74 break;
75 case 'CUSTOMER':
76 $data[ $key ][ $header ] = $appointment['customer'];
77 break;
78 case 'CUSTOMER PHONE':
79 $data[ $key ][ $header ] = $appointment['customer_phone'];
80 break;
81 case 'STAFF':
82 $data[ $key ][ $header ] = $appointment['staff'];
83 break;
84 case 'SERVICE':
85 $data[ $key ][ $header ] = $appointment['service'];
86 break;
87 case 'PRICE':
88 $data[ $key ][ $header ] = $appointment['price'];
89 break;
90 case 'DATE':
91 $data[ $key ][ $header ] = date_i18n( get_option( 'date_format' ), $appointment['date_timestamp'] );
92 break;
93 case 'TIME':
94 $data[ $key ][ $header ] = date_i18n( get_option( 'time_format' ), $appointment['start_time'] ) . ' - ' . date_i18n( get_option( 'time_format' ), $appointment['end_time'] );
95 break;
96 case 'PAYMENT':
97 $data[ $key ][ $header ] = $appointment['payment_method'] . ' ' . $appointment['payment_status'];
98 break;
99 case 'STATUS':
100 $data[ $key ][ $header ] = $appointment['status'];
101 break;
102 }
103 }
104 }
105
106 self::export_appointment_excel( $data );
107 }
108
109 /**
110 * Export Data as Excel file
111 * @param $data
112 */
113 public static function export_appointment_excel( $data ) {
114 header( 'Content-Type: application/csv' );
115 header( 'Content-Disposition: attachment; filename="appintments_booket_data.csv";' );
116 ob_end_clean();
117 $handle = fopen( 'php://output', 'w' );
118 fputcsv( $handle, array_keys( $data['0'] ), ';' );
119 foreach ( $data as $value ) {
120 fputcsv( $handle, $value, ';' );
121 }
122 fclose( $handle );
123 ob_flush();
124 die;
125 }
126
127 /**
128 * Import Data file
129 *
130 * @since 2.5.0
131 */
132 public static function import() {
133 check_ajax_referer( 'bookit_import', 'nonce' );
134
135 if ( ! current_user_can( 'manage_options' ) ) {
136 return false;
137 }
138
139 if ( ! ( is_array( $_POST ) && is_array( $_FILES ) && defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
140 wp_send_json_error( [
141 'errors' => [ 'import_json' => esc_html_x( 'No data', 'Import error message', 'bookit' ) ],
142 'message' => esc_html_x( 'Error occurred!', 'Import error message', 'bookit' ),
143 ] );
144
145 return false;
146 }
147
148 if ( ! function_exists( 'wp_handle_upload' ) ) {
149 require_once ABSPATH . 'wp-admin/includes/file.php';
150 }
151
152 if ( empty( $_FILES['file'] ) ) {
153 wp_send_json_error( [
154 'errors' => [ 'import_json' => esc_html_x( 'File is empty', 'Import error message', 'bookit' ) ],
155 'message' => esc_html_x( 'Error occurred!', 'Import error message', 'bookit' ),
156 ] );
157
158 return false;
159 }
160
161 $file = $_FILES['file'];
162 $file_name = $file['name'];
163 $file_type = wp_check_filetype( basename( $file_name ) );
164 $allowed_file_types = [ 'text/plain' ];
165
166 if ( ! in_array( $file_type['type'], $allowed_file_types ) ) {
167 wp_send_json_error( [
168 'errors' => [ 'import_json' => esc_html_x( 'File type not allowed', 'Import error message', 'bookit' ) ],
169 'message' => esc_html_x( 'Error occurred!', 'Import error message', 'bookit' ),
170 ] );
171
172 return false;
173 }
174
175 // Disable file type check as it was already done.
176 $upload_overrides = [
177 'test_form' => false,
178 'test_type' => false,
179 'mimes' => [
180 'txt' => 'text/plain',
181 ],
182 ];
183
184 $file_info = wp_handle_upload( $file, $upload_overrides );
185
186 if ( ! $file_info || isset( $file_info['error'] ) ) {
187 wp_send_json_error( [
188 'errors' => [ 'import_json' => esc_html_x( 'Error occurred during file upload', 'Import error message', 'bookit' ) ],
189 'message' => esc_html_x( 'Error occurred!', 'Import error message', 'bookit' ),
190 ] );
191
192 return false;
193 }
194
195 $data = self::bookit_read_file( $file_info );
196 wp_send_json( $data, 200 );
197 }
198
199 /**
200 * Run Demo Import
201 */
202 public static function demo_import_apply() {
203 check_ajax_referer( 'bookit_import', 'nonce' );
204
205 if ( ! current_user_can( 'manage_options' ) ) {
206 return false;
207 }
208
209 $file = array( 'file' => BOOKIT_PATH . '/demo-sample/bookit.txt' );
210
211 if ( file_exists( $file['file'] ) ) {
212 $data = self::bookit_read_file( $file, true );
213 wp_send_json( $data, 200 );
214 } else {
215 wp_send_json_error(
216 array(
217 'errors' => array( 'demo_import' => __( 'File not found', 'bookit' ) ),
218 'message' => __( 'Error occurred!', 'bookit' ),
219 )
220 );
221 }
222 }
223
224 /**
225 * Read Import File
226 * @param $file
227 * @param bool $demo
228 *
229 * @return array
230 */
231 public static function bookit_read_file( $file, $demo = false ) {
232 $result = false;
233 $file_data = array();
234
235 if ( file_exists( $file['file'] ) || true == $demo ) {
236
237 $file_url = $file['file'];
238 $fileContents = file_get_contents( $file_url );
239 $json = json_decode( $fileContents, true );
240 if ( null !== $json ) {
241 $result = true;
242 update_option( 'bookit_import_file', $file_url );
243 $file_data['settings'] = 0;
244 $file_data['categories'] = count( $json['Categories'] );
245 $file_data['service'] = count( $json['Service'] );
246 $file_data['staff'] = count( $json['Staff'] );
247 }
248 }
249
250 return array(
251 'info' => $file_data,
252 'success' => $result,
253 );
254 }
255
256 /**
257 * Rum Demo Import
258 */
259 public static function demo_import_run() {
260 check_ajax_referer( 'bookit_import', 'nonce' );
261
262 if ( ! current_user_can( 'manage_options' ) ) {
263 return false;
264 }
265
266 $key = absint( intval( $_POST['key'] ) );
267 $file_url = get_option( 'bookit_import_file' );
268 $fileContents = file_get_contents( $file_url );
269 $json = json_decode( $fileContents, true );
270 $step = sanitize_text_field( $_POST['step'] );
271
272 if ( null !== $json ) {
273 $data = self::import_data( $json, $step, $key );
274 } else {
275 $data = array( 'success' => false );
276 }
277 wp_send_json( $data, 200 );
278 }
279
280
281 /**
282 * Import Data
283 *
284 * @param $json
285 * @param $step
286 * @param $key
287 *
288 * @return array
289 */
290 public static function import_data( $json, $step, $key ) {
291 $imported_categories = array();
292 $imported_services = array();
293
294 if ( 'categories' == $step && $json['Categories'] && ! empty( $json['Categories'][ $key ] ) ) {
295 $cat_key = $json['Categories'][ $key ]['id'];
296 $existing_category = Categories::get( 'name', $json['Categories'][ $key ]['name'] );
297
298 unset( $json['Categories'][ $key ]['id'] );
299
300 if ( empty( $existing_category ) ) {
301 Categories::insert( $json['Categories'][ $key ] );
302 }
303
304 $cat_id = ( empty( $existing_category ) ) ? Categories::insert_id() : $existing_category->id;
305 $imported_categories[ $cat_key ] = $cat_id;
306
307 if ( 0 == $key ) {
308 set_transient( 'bookit_categories', json_encode( $imported_categories ), 1000 );
309 }
310
311 if ( $key > 0 && get_transient( 'bookit_categories' ) ) {
312 $all_imported_categories = json_decode( get_transient( 'bookit_categories' ), true );
313 $all_imported_categories[ $cat_key ] = $cat_id;
314 set_transient( 'bookit_categories', json_encode( $all_imported_categories ), 1000 );
315 }
316 } elseif ( 'service' == $step && $json['Service'] && ! empty( $json['Service'][ $key ] ) ) {
317 $imported_categories = ( get_transient( 'bookit_categories' ) ) ? json_decode( get_transient( 'bookit_categories' ), true ) : array();
318 $json['Service'][ $key ]['category_id'] = $imported_categories[ $json['Service'][ $key ]['category_id'] ];
319 $service_id = $json['Service'][ $key ]['id'];
320
321 unset( $json['Service'][ $key ]['id'] );
322 unset( $json['Service'][ $key ]['icon_id'] );
323
324 Services::insert( $json['Service'][ $key ] );
325 $imported_services[ $service_id ] = Services::insert_id();
326
327 if ( 0 == $key ) {
328 set_transient( 'bookit_services', json_encode( $imported_services ), 1000 );
329 }
330
331 if ( $key > 0 && get_transient( 'bookit_services' ) ) {
332 $all_imported_services = json_decode( get_transient( 'bookit_services' ), true );
333 $all_imported_services[ $service_id ] = Services::insert_id();
334 set_transient( 'bookit_services', json_encode( $all_imported_services ), 1000 );
335 }
336 } elseif ( 'staff' == $step && $json['Staff'] ) {
337 unset( $json['Staff'][ $key ]['id'] );
338 $staff_imported_services = array();
339
340 if ( $json['Staff'][ $key ] ) {
341 unset( $json['Staff'][ $key ]['id'] );
342
343 if ( get_transient( 'bookit_services' ) ) {
344 $staff_imported_services = json_decode( get_transient( 'bookit_services' ), true );
345 }
346 self::staff_import( $json['Staff'][ $key ], $staff_imported_services );
347 }
348 }
349 return array(
350 'key' => $key += 1,
351 'success' => true,
352 );
353 }
354
355 /**
356 * Import Staff
357 *
358 * @param $data
359 * @param $imported_services
360 *
361 * @return mixed
362 */
363 public static function staff_import( $data, $imported_services ) {
364 $staff_services = json_decode( stripslashes( $data['staff_services'] ) );
365 $working_hours = json_decode( stripslashes( $data['working_hours'] ) );
366
367 unset( $data['staff_services'] );
368 unset( $data['working_hours'] );
369
370 Staff::insert( $data );
371 $id = Staff::insert_id();
372
373 foreach ( $working_hours as $working_hour ) {
374 $insert = array(
375 'staff_id' => $id,
376 'weekday' => $working_hour->weekday,
377 'start_time' => $working_hour->start_time,
378 'end_time' => $working_hour->end_time,
379 'break_from' => $working_hour->break_from,
380 'break_to' => $working_hour->break_to,
381 );
382 Staff_Working_Hours::insert( $insert );
383 }
384
385 foreach ( $staff_services as $staff_service ) {
386 $insert = array(
387 'staff_id' => $id,
388 'service_id' => ( $imported_services[ $staff_service->id ] ) ? $imported_services[ $staff_service->id ] : null,
389 'price' => number_format( (float) $staff_service->price, 2, '.', '' ),
390 );
391 Staff_Services::insert( $insert );
392 }
393 return $data;
394 }
395 }
396