PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.5.0
Order Tracking – WordPress Status Tracking Plugin v3.5.0
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / Export.class.php

Export.class.php in Order Tracking – WordPress Status Tracking Plugin 3.5.0, at includes/Export.class.php

592 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class to export orders created by the plugin
5 */
6
7 if ( !defined( 'ABSPATH' ) )
8 exit;
9
10 if (!class_exists('ComposerAutoloaderInit4618f5c41cf5e27cc7908556f031e4d4')) { require_once EWD_OTP_PLUGIN_DIR . '/lib/PHPSpreadsheet/vendor/autoload.php'; }
11 use PhpOffice\PhpSpreadsheet\Spreadsheet;
12 use PhpOffice\PhpSpreadsheet\Writer\Xls;
13 use PhpOffice\PhpSpreadsheet\Writer\Csv;
14 class ewdotpExport {
15
16 // Set whether a valid nonce is needed before exporting orders
17 public $nonce_check = true;
18
19 /**
20 * all the messages to display
21 * array(
22 * 'error' => 'Some error!',
23 * 'info' => 'Some info msg',
24 * 'success' => 'Some success',
25 * 'warning' => 'Some warning!'
26 * )
27 * @var array
28 */
29 public $messages = array();
30
31 public function __construct() {
32
33 if ( isset( $_POST['ewd_otp_export'] ) ) { add_action( 'admin_menu', array( $this, 'run_export' ) ); }
34
35 add_action( 'admin_menu', array($this, 'register_install_screen' ));
36 }
37
38 public function register_install_screen() {
39 global $ewd_otp_controller;
40
41 add_submenu_page(
42 'ewd-otp-orders',
43 'Export Menu',
44 'Export',
45 $ewd_otp_controller->settings->get_setting( 'access-role' ),
46 'ewd-otp-export',
47 array($this, 'display_export_screen')
48 );
49
50 // This is required to enqueue style however, we are not registering/rendering this
51 require_once( EWD_OTP_PLUGIN_DIR . '/lib/simple-admin-pages/simple-admin-pages.php' );
52 $sap = sap_initialize_library(
53 $args = array(
54 'version' => '2.7.4',
55 'lib_url' => EWD_OTP_PLUGIN_URL . '/lib/simple-admin-pages/',
56 'theme' => 'purple',
57 )
58 );
59 $sap->add_page(
60 'submenu',
61 array(
62 'id' => 'ewd-otp-export',
63 'title' => __( 'Export', 'order-tracking' ),
64 'menu_title' => __( 'Export', 'order-tracking' ),
65 'parent_menu' => 'ewd-otp-orders',
66 'description' => ''
67 )
68 );
69 }
70
71 public function display_export_screen() {
72 global $ewd_otp_controller;
73
74 $export_permission = $ewd_otp_controller->permissions->check_permission( 'export' );
75
76 ?>
77 <div class="wrap sap-settings-page">
78 <h1>Export</h1>
79
80 <?php $this->display_messages(); ?>
81
82 <?php if ( $export_permission ) { ?>
83 <form method='post'>
84 <?php
85 wp_nonce_field( 'EWD_OTP_Export', 'EWD_OTP_Export_Nonce' );
86
87 // Fetch order status
88 $order_status_list = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
89 $customer_list = $this->get_customer_list();
90 $sales_rep_list = $this->get_sales_rep_list();
91
92 // set type being exported to orders if not set
93 $_POST['type-of-record'] = isset( $_POST['type-of-record'] ) ? $_POST['type-of-record'] : 'order';
94 ?>
95
96 <table class="form-table ewd-otp-export-filters" role="presentation">
97
98 <tr class="row type-of-record">
99 <th><?php _e( 'Type of Record', 'order-tracking' ); ?></th>
100 <td>
101 <fieldset>
102 <label for="type-of-record-order" class="sap-admin-input-container">
103 <input type="radio" name="type-of-record" value="order" id="type-of-record-order" <?php echo $_POST['type-of-record'] == 'order' ? 'checked' : ''; ?>>
104 <span class="sap-admin-radio-button"></span>
105 <span><?php _e( 'Order', 'order-tracking' ); ?></span>
106 </label>
107 <label for="type-of-record-customer" class="sap-admin-input-container">
108 <input type="radio" name="type-of-record" value="customer" id="type-of-record-customer" <?php echo $_POST['type-of-record'] == 'customer' ? 'checked' : ''; ?>>
109 <span class="sap-admin-radio-button"></span>
110 <span>Customer</span>
111 </label>
112 <label for="type-of-record-sales-rep" class="sap-admin-input-container">
113 <input type="radio" name="type-of-record" value="sales-rep" id="type-of-record-sales-rep" <?php echo $_POST['type-of-record'] == 'sales-rep' ? 'checked' : ''; ?>>
114 <span class="sap-admin-radio-button"></span>
115 <span>Sales Rep</span>
116 </label>
117 </fieldset>
118 </td>
119 </tr>
120
121 <tr class="row by-status <?php echo $_POST['type-of-record'] != 'order' ? 'ewd-otp-hidden' : ''; ?>" >
122 <th><?php _e( 'Orders by Status', 'order-tracking' ); ?></th>
123 <td>
124 <fieldset>
125 <?php foreach ($order_status_list as $record): ?>
126 <label for="type-of-record-<?php echo esc_attr( $record->status );?>" class="sap-admin-input-container">
127 <input type="checkbox" name="order-by-status[]" value="<?php echo $record->status;?>" id="type-of-record-<?php echo $record->status;?>" <?php echo isset($_POST['order-by-status']) && in_array($record->status, $_POST['order-by-status']) ? 'checked' : ''; ?>>
128 <span class="sap-admin-checkbox"></span>
129 <span><?php echo esc_html( $record->status );?></span>
130 </label>
131 <?php endforeach ?>
132 </fieldset>
133 </td>
134 </tr>
135
136 <tr class="row date-range <?php echo $_POST['type-of-record'] != 'order' ? 'ewd-otp-hidden' : ''; ?>">
137 <th><?php _e( 'Orders from a Specific Date Range', 'order-tracking' ); ?></th>
138 <td>
139 <fieldset>
140 <label for="date-range-today" class="sap-admin-input-container">
141 <input type="radio" name="date_range" value="today" id="date-range-today" <?php echo isset( $_POST['date_range'] ) && 'today' == $_POST['date_range'] ? 'checked' : ''; ?>>
142 <span class="sap-admin-radio-button"></span>
143 <span><?php _e( sprintf( 'Today (%s)', date( get_option( 'date_format' ), strtotime( 'today' ) ) ), 'order-tracking' ); ?></span>
144 </label>
145 <label for="date-range-week" class="sap-admin-input-container">
146 <input type="radio" name="date_range" value="week" id="date-range-week" <?php echo isset( $_POST['date_range'] ) && 'week' == $_POST['date_range'] ? 'checked' : ''; ?>>
147 <span class="sap-admin-radio-button"></span>
148 <span><?php _e( sprintf( 'This Week (%s - %s)', date( get_option( 'date_format' ), strtotime( 'monday this week' ) ), date( get_option( 'date_format' ), strtotime( 'sunday this week' ) ) ), 'order-tracking' ); ?></span>
149 </label>
150 <label for="date-range-past" class="sap-admin-input-container">
151 <input type="radio" name="date_range" value="past" id="date-range-past" <?php echo isset( $_POST['date_range'] ) && 'past' == $_POST['date_range'] ? 'checked' : ''; ?>>
152 <span class="sap-admin-radio-button"></span>
153 <span><?php _e( 'Past', 'order-tracking' ); ?></span>
154 </label>
155 <label for="date-range-from"><?php _e( 'From', 'order-tracking' ); ?></label>
156 <input type="date" name="start_date" id="date-range-from" value="<?php echo isset( $_POST['start_date'] ) ? esc_attr__( $_POST['start_date'] ) : ''; ?>">
157 <label for="date-range-to"><?php _e( 'To', 'order-tracking' ); ?></label>
158 <input type="date" name="end_date" id="date-range-to" value="<?php echo isset( $_POST['end_date'] ) ? esc_attr__( $_POST['end_date'] ) : ''; ?>">
159 </fieldset>
160 </td>
161 </tr>
162
163 <tr class="row customer-list <?php echo $_POST['type-of-record'] != 'order' ? 'ewd-otp-hidden' : ''; ?>">
164 <th><?php _e( 'Orders from specific Customer(s)', 'order-tracking' ); ?></th>
165 <td>
166 <fieldset>
167 <select name="order-of-customer[]" multiple>
168 <?php
169 foreach ($customer_list as $record) {
170 $selected = isset($_POST['order-of-customer']) && in_array($record->id, $_POST['order-of-customer']) ? 'selected' : '';
171 echo "<option value='{$record->id}' {$selected}>{$record->id} - {$record->name} ( {$record->email} )</option>";
172 }
173 ?>
174 </select>
175 </fieldset>
176 </td>
177 </tr>
178
179 <tr class="row sales-rep-list <?php echo $_POST['type-of-record'] != 'order' ? 'ewd-otp-hidden' : ''; ?>">
180 <th><?php _e( 'Orders for specific Sales Rep(s)', 'order-tracking' ); ?></th>
181 <td>
182 <fieldset>
183 <select name="order-of-sales-rep[]" multiple>
184 <?php
185 foreach ($sales_rep_list as $record) {
186 $selected = isset($_POST['order-of-sales-rep']) && in_array($record->id, $_POST['order-of-sales-rep']) ? 'selected' : '';
187 $l_name = !empty( $record->last_name ) ? " {$record->last_name}" : '';
188 echo "<option value='{$record->id}' {$selected}>{$record->id} - {$record->first_name}{$l_name} ( {$record->email} )</option>";
189 }
190 ?>
191 </select>
192 </fieldset>
193 </td>
194 </tr>
195
196 </table>
197
198 <input type='submit' name='ewd_otp_export' value='Export to Spreadsheet' class='button button-primary'>
199 &nbsp;
200 <a href="" class='button'><?php _e( 'Clear Form', 'order-tracking' ); ?></a>
201
202 </form>
203 <?php } else { ?>
204 <div class='ewd-otp-premium-locked'>
205 <a href="https://www.etoilewebdesign.com/license-payment/?Selected=OTP&Quantity=1&utm_source=otp_export" target="_blank">Upgrade</a> to the premium version to use this feature
206 </div>
207 <?php } ?>
208 </div>
209 <?php }
210
211
212 public function run_export() {
213 global $ewd_otp_controller;
214
215 if ( $this->nonce_check and ! isset( $_POST['EWD_OTP_Export_Nonce'] ) ) {
216 return;
217 }
218
219 if ( $this->nonce_check and ! wp_verify_nonce( $_POST['EWD_OTP_Export_Nonce'], 'EWD_OTP_Export' ) ) {
220 return;
221 }
222
223 // Instantiate a new PHPExcel object
224 $spreadsheet = new Spreadsheet();
225 // Set the active Excel worksheet to sheet 0
226 $spreadsheet->setActiveSheetIndex(0);
227
228 $records = array('header' => array(), 'data' => array());
229
230 if ( empty( $_POST['type-of-record'] ) or 'order' == $_POST['type-of-record'] ) {
231 $records = $this->get_order_data();
232 }
233 elseif ( 'customer' == $_POST['type-of-record'] ) {
234 $records = $this->get_customer_data();
235 }
236 elseif( 'sales-rep' == $_POST['type-of-record'] ) {
237 $records = $this->get_sales_rep_data();
238 }
239
240 if( 1 > count( $records['data'] ) ) {
241 $this->warning('No records found to export!');
242 return;
243 }
244
245 // Adding header
246 $row = 1; $col = 'A';
247 foreach ($records['header'] as $value) {
248 $spreadsheet->getActiveSheet()->setCellValue( $col.$row, $value );
249 $col++;
250 }
251
252 //start while loop to get data
253 $row = 2;
254 foreach ( $records['data'] as $record ) {
255 $col = 'A';
256 foreach ( $record as $value ) {
257 $spreadsheet->getActiveSheet()->setCellValue( $col.$row, $value );
258 $col++;
259 }
260 $row++;
261 }
262
263 // Redirect output to a client’s web browser (Excel5)
264 if ( ! isset( $format_type ) == 'csv' ) {
265
266 ob_clean();
267
268 header('Content-Type: application/vnd.ms-excel');
269 header('Content-Disposition: attachment;filename="' . $records['filename'] . '.csv"');
270 header('Cache-Control: max-age=0');
271 $objWriter = new Csv($spreadsheet);
272 $objWriter->save('php://output');
273 }
274 else {
275
276 ob_clean();
277
278 header('Content-Type: application/vnd.ms-excel');
279 header('Content-Disposition: attachment;filename="' . $records['filename'] . '.xls"');
280 header('Cache-Control: max-age=0');
281 $objWriter = new Xls($spreadsheet);
282 $objWriter->save('php://output');
283 }
284
285 die();
286 }
287
288 public function get_order_data()
289 {
290 global $ewd_otp_controller;
291
292 // Print out the regular order field labels
293 $order_header = array(
294 'Name',
295 'Number',
296 'Order Status',
297 'Order Status Updated (Read-Only)',
298 'Location',
299 'Display',
300 'Notes Public',
301 'Notes Private',
302 'Email',
303 'Phone Number',
304 'Customer',
305 'Sales Rep'
306 );
307
308 // Add custom fields to column headers
309 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
310
311 foreach ( $custom_fields as $custom_field ) {
312
313 $order_header[] = $custom_field->name;
314 }
315
316 $args = array(
317 'display' => true,
318 'orders_per_page' => -1,
319 );
320
321 // Order status
322 if( isset( $_POST['order-by-status'] ) && 0 < count( $_POST['order-by-status'] ) ) {
323
324 $args['status'] = array_map( 'sanitize_text_field', $_POST['order-by-status'] );
325 }
326
327 // Order for Date-range
328 if ( ! empty( $this->after ) ) {
329
330 // Used to let sales-rep and customers download their data from front-end
331 $args['after'] = $this->after;
332 }
333 elseif( isset( $_POST['date_range'] ) ) {
334
335 $args['date_range'] = sanitize_text_field( $_POST['date_range'] );
336 }
337 elseif( isset( $_POST['start_date'] ) || isset( $_POST['end_date'] ) ) {
338
339 // just to pass if in order_manager->prepare_args()
340 $args['date_range'] = 'custom';
341 $args['start_date'] = sanitize_text_field( $_POST['start_date'] );
342 $args['end_date'] = sanitize_text_field( $_POST['end_date'] );
343 }
344
345 // Order of Customer('s)
346 if ( ! empty( $this->customer_id ) ) {
347
348 // Used to let sales-rep and customers download their data from front-end
349 $args['customer'] = $this->customer_id;
350 }
351 elseif( isset( $_POST['order-of-customer'] ) && 0 < count( $_POST['order-of-customer'] ) ) {
352
353 $args['customer'] = array_map( 'sanitize_text_field', $_POST['order-of-customer'] );
354 }
355
356 // Order of Sales Rep('s)
357 if ( ! empty( $this->sales_rep_id ) ) {
358
359 // Used to let sales-rep and customers download their data from front-end
360 $args['sales_rep'] = $this->sales_rep_id;
361 }
362 elseif( isset( $_POST['order-of-sales-rep'] ) ) {
363
364 $args['sales_rep'] = array_map( 'sanitize_text_field', $_POST['order-of-sales-rep'] );
365 }
366
367 // fetching orders
368 $orders = $ewd_otp_controller->order_manager->get_matching_orders( $args );
369
370 $data = array();
371
372 foreach ( $orders as $order ) {
373
374 $record = array(
375 $order->name,
376 $order->number,
377 $order->status,
378 $order->status_updated,
379 $order->location,
380 ( $order->display ? 'Yes' : 'No' ),
381 $order->notes_public,
382 $order->notes_private,
383 $order->email,
384 $order->phone_number,
385 max( $order->customer, 0 ),
386 max( $order->sales_rep, 0 ),
387 );
388
389 // Adding custom field data
390 foreach ( $custom_fields as $custom_field ) {
391
392 $record[] = $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $order->id );
393 }
394
395 $data[] = $record;
396 }
397
398 return array(
399 'header' => $order_header,
400 'data' => $data,
401 'filename' => 'order_export',
402 );
403 }
404
405 public function get_customer_data() {
406 global $ewd_otp_controller;
407
408 // Print out the regular customer field labels
409 $customer_header = array(
410 'Customer ID',
411 'Number',
412 'Name',
413 'Email',
414 'Sales Rep ID',
415 'WP ID',
416 'FEUP ID',
417 );
418
419 // Add custom fields to column headers
420 $custom_fields = $ewd_otp_controller->settings->get_customer_custom_fields();
421
422 foreach ( $custom_fields as $custom_field ) {
423
424 $customer_header[] = $custom_field->name;
425 }
426
427 $args = array(
428 'customers_per_page' => -1,
429 );
430
431 // fetching customers
432 $customers = $ewd_otp_controller->customer_manager->get_matching_customers( $args );
433
434 $data = array();
435
436 foreach ( $customers as $customer ) {
437
438 $record = array(
439 $customer->id,
440 $customer->number,
441 $customer->name,
442 $customer->email,
443 max( $customer->sales_rep, 0 ),
444 max( $customer->wp_id, 0 ),
445 max( $customer->feup_id, 0 ),
446 );
447
448 // Adding custom field data
449 foreach ( $custom_fields as $custom_field ) {
450
451 $record[] = $ewd_otp_controller->customer_manager->get_field_value( $custom_field->id, $customer->id );
452 }
453
454 $data[] = $record;
455 }
456
457 return array(
458 'header' => $customer_header,
459 'data' => $data,
460 'filename' => 'customer_export',
461 );
462 }
463
464 public function get_sales_rep_data() {
465 global $ewd_otp_controller;
466
467 // Print out the regular sales rep field labels
468 $sales_rep_header = array(
469 'Sales Rep ID',
470 'Number',
471 'First Name',
472 'Last Name',
473 'Email',
474 'Phone Number',
475 'WP ID',
476 );
477
478 // Add custom fields to column headers
479 $custom_fields = $ewd_otp_controller->settings->get_sales_rep_custom_fields();
480
481 foreach ( $custom_fields as $custom_field ) {
482
483 $sales_rep_header[] = $custom_field->name;
484 }
485
486 $args = array(
487 'sales_reps_per_page' => -1,
488 );
489
490 // fetching sales reps
491 $sales_reps = $ewd_otp_controller->sales_rep_manager->get_matching_sales_reps( $args );
492
493 $data = array();
494
495 foreach ( $sales_reps as $sales_rep ) {
496
497 $record = array(
498 $sales_rep->id,
499 $sales_rep->number,
500 $sales_rep->first_name,
501 $sales_rep->last_name,
502 $sales_rep->email,
503 $sales_rep->phone_number,
504 max( $sales_rep->wp_id, 0 ),
505 );
506
507 // Adding custom field data
508 foreach ( $custom_fields as $custom_field ) {
509
510 $record[] = $ewd_otp_controller->sales_rep_manager->get_field_value( $custom_field->id, $customer->id );
511 }
512
513 $data[] = $record;
514 }
515
516 return array(
517 'header' => $sales_rep_header,
518 'data' => $data,
519 'filename' => 'sales_rep_export',
520 );
521 }
522
523 public function get_customer_list() {
524 global $ewd_otp_controller;
525
526 $args = array(
527 'orderby' => 'Customer_Name',
528 'order' => 'asc',
529 'customers_per_page' => -1
530 );
531
532 return $ewd_otp_controller->customer_manager->get_matching_customers( $args );
533 }
534
535 public function get_sales_rep_list() {
536 global $ewd_otp_controller;
537
538 $args = array(
539 'orderby' => 'Sales_Rep_First_Name',
540 'order' => 'asc',
541 'sales_reps_per_page' => -1
542 );
543
544 return $ewd_otp_controller->sales_rep_manager->get_matching_sales_reps( $args );
545 }
546
547 public function display_messages() {
548
549 foreach ( $this->messages as $type => $msgs ) {
550
551 echo "<div class='notice notice-{$type}''>";
552 foreach ($msgs as $msg) {
553 echo "<p>{$msg}</p>";
554 }
555 echo '</div>';
556 }
557 }
558
559 public function warning( $msg ) {
560
561 if ( !isset( $this->messages['warning'] ) ) {
562
563 $this->messages['warning'] = array();
564 }
565
566 $this->messages['warning'][] = $msg;
567 }
568
569 public function error( $msg ) {
570
571 if ( !isset( $this->messages['error'] ) ) {
572
573 $this->messages['error'] = array();
574 }
575
576 $this->messages['error'][] = $msg;
577 }
578
579 public function success( $msg ) {
580
581 if ( !isset( $this->messages['success'] ) ) {
582
583 $this->messages['success'] = array();
584 }
585
586 $this->messages['success'][] = $msg;
587 }
588
589 }
590
591
592