PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.2.2
Order Tracking – WordPress Status Tracking Plugin v3.2.2
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.2.2, at includes/Export.class.php

474 lines 14.9 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'] ) ) {
34 add_action( 'admin_menu', array( $this, 'export_orders' ) );
35 }
36
37 add_action( 'admin_menu', array($this, 'register_install_screen' ));
38 }
39
40 public function register_install_screen() {
41 global $ewd_otp_controller;
42
43 add_submenu_page(
44 'ewd-otp-orders',
45 'Export Menu',
46 'Export',
47 $ewd_otp_controller->settings->get_setting( 'access-role' ),
48 'ewd-otp-export',
49 array($this, 'display_export_screen')
50 );
51
52 // This is required to enqueue style however, we are not registering/rendering this
53 require_once( EWD_OTP_PLUGIN_DIR . '/lib/simple-admin-pages/simple-admin-pages.php' );
54 $sap = sap_initialize_library(
55 $args = array(
56 'version' => '2.6.12',
57 'lib_url' => EWD_OTP_PLUGIN_URL . '/lib/simple-admin-pages/',
58 'theme' => 'purple',
59 )
60 );
61 $sap->add_page(
62 'submenu',
63 array(
64 'id' => 'ewd-otp-export',
65 'title' => __( 'Export', 'order-tracking' ),
66 'menu_title' => __( 'Export', 'order-tracking' ),
67 'parent_menu' => 'ewd-otp-orders',
68 'description' => ''
69 )
70 );
71 }
72
73 public function display_export_screen() {
74 global $ewd_otp_controller;
75
76 $export_permission = $ewd_otp_controller->permissions->check_permission( 'export' );
77
78 ?>
79 <div class="wrap sap-settings-page">
80 <h1>Export</h1>
81
82 <?php $this->display_messages(); ?>
83
84 <?php if ( $export_permission ) { ?>
85 <form method='post'>
86 <?php
87 wp_nonce_field( 'EWD_OTP_Export', 'EWD_OTP_Export_Nonce' );
88
89 // Fetch order status
90 $order_status_list = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
91 $customer_list = $this->get_customer_list();
92 $sales_rep_list = $this->get_sales_rep_list();
93
94 // currently only Order is available
95 $_POST['type-of-record'] = 'order';
96 ?>
97
98 <h2>Filters</h2>
99
100 <table class="form-table otp-export-filters" role="presentation">
101
102 <tr class="row type-of-record">
103 <th><?php _e( 'Type of Record', 'order-tracking' ); ?></th>
104 <td>
105 <fieldset>
106 <label for="type-of-record-order" class="sap-admin-input-container">
107 <input type="radio" name="type-of-record" value="order" id="type-of-record-order" <?php echo isset( $_POST['type-of-record'] ) && 'order' == $_POST['type-of-record'] ? 'checked' : ''; ?>>
108 <span class="sap-admin-radio-button"></span>
109 <span><?php _e( 'Order', 'order-tracking' ); ?></span>
110 </label>
111 <!-- <label for="type-of-record-customer" class="sap-admin-input-container">
112 <input type="radio" name="type-of-record" value="customer" id="type-of-record-customer" <?php echo isset($_POST['type-of-record']) && 'customer' == $_POST['type-of-record'] ? 'checked' : ''; ?>>
113 <span class="sap-admin-radio-button"></span>
114 <span>Customer</span>
115 </label>
116 <label for="type-of-record-sales-rep" class="sap-admin-input-container">
117 <input type="radio" name="type-of-record" value="sales-rep" id="type-of-record-sales-rep" <?php echo isset($_POST['type-of-record']) && 'sales-rep' == $_POST['type-of-record'] ? 'checked' : ''; ?>>
118 <span class="sap-admin-radio-button"></span>
119 <span>Sales Rep</span>
120 </label> -->
121 </fieldset>
122 </td>
123 </tr>
124
125 <tr class="row by-status">
126 <th><?php _e( 'Orders by Status', 'order-tracking' ); ?></th>
127 <td>
128 <fieldset>
129 <?php foreach ($order_status_list as $record): ?>
130 <label for="type-of-record-<?php echo $record->status;?>" class="sap-admin-input-container">
131 <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' : ''; ?>>
132 <span class="sap-admin-checkbox"></span>
133 <span><?php echo $record->status;?></span>
134 </label>
135 <?php endforeach ?>
136 </fieldset>
137 </td>
138 </tr>
139
140 <tr class="row date-range">
141 <th><?php _e( 'Orders from a Specific Date Range', 'order-tracking' ); ?></th>
142 <td>
143 <fieldset>
144 <label for="date-range-today" class="sap-admin-input-container">
145 <input type="radio" name="date_range" value="today" id="date-range-today" <?php echo isset( $_POST['date_range'] ) && 'today' == $_POST['date_range'] ? 'checked' : ''; ?>>
146 <span class="sap-admin-radio-button"></span>
147 <span><?php _e( sprintf( 'Today (%s)', date( get_option( 'date_format' ), strtotime( 'today' ) ) ), 'order-tracking' ); ?></span>
148 </label>
149 <label for="date-range-week" class="sap-admin-input-container">
150 <input type="radio" name="date_range" value="week" id="date-range-week" <?php echo isset( $_POST['date_range'] ) && 'week' == $_POST['date_range'] ? 'checked' : ''; ?>>
151 <span class="sap-admin-radio-button"></span>
152 <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>
153 </label>
154 <label for="date-range-past" class="sap-admin-input-container">
155 <input type="radio" name="date_range" value="past" id="date-range-past" <?php echo isset( $_POST['date_range'] ) && 'past' == $_POST['date_range'] ? 'checked' : ''; ?>>
156 <span class="sap-admin-radio-button"></span>
157 <span><?php _e( 'Past', 'order-tracking' ); ?></span>
158 </label>
159 <label for="date-range-from"><?php _e( 'From', 'order-tracking' ); ?></label>
160 <input type="date" name="start_date" id="date-range-from" value="<?php echo isset( $_POST['start_date'] ) ? $_POST['start_date'] : ''; ?>">
161 <label for="date-range-to"><?php _e( 'To', 'order-tracking' ); ?></label>
162 <input type="date" name="end_date" id="date-range-to" value="<?php echo isset( $_POST['end_date'] ) ? $_POST['end_date'] : ''; ?>">
163 </fieldset>
164 </td>
165 </tr>
166
167 <tr class="row customer-list">
168 <th><?php _e( 'Orders from specific Customer(s)', 'order-tracking' ); ?></th>
169 <td>
170 <fieldset>
171 <select name="order-of-customer[]" multiple>
172 <?php
173 foreach ($customer_list as $record) {
174 $selected = isset($_POST['order-of-customer']) && in_array($record->id, $_POST['order-of-customer']) ? 'selected' : '';
175 echo "<option value='{$record->id}' {$selected}>{$record->id} - {$record->name} ( {$record->email} )</option>";
176 }
177 ?>
178 </select>
179 </fieldset>
180 </td>
181 </tr>
182
183 <tr class="row sales-rep-list">
184 <th><?php _e( 'Orders for specific Sales Rep(s)', 'order-tracking' ); ?></th>
185 <td>
186 <fieldset>
187 <select name="order-of-sales-rep[]" multiple>
188 <?php
189 foreach ($sales_rep_list as $record) {
190 $selected = isset($_POST['order-of-sales-rep']) && in_array($record->id, $_POST['order-of-sales-rep']) ? 'selected' : '';
191 $l_name = !empty( $record->last_name ) ? " {$record->last_name}" : '';
192 echo "<option value='{$record->id}' {$selected}>{$record->id} - {$record->first_name}{$l_name} ( {$record->email} )</option>";
193 }
194 ?>
195 </select>
196 </fieldset>
197 </td>
198 </tr>
199
200 </table>
201
202 <input type='submit' name='ewd_otp_export' value='Export to Spreadsheet' class='button button-primary'>
203 &nbsp;
204 <a href="" class='button'><?php _e( 'Clear Form', 'order-tracking' ); ?></a>
205
206 </form>
207 <?php } else { ?>
208 <div class='ewd-otp-premium-locked'>
209 <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
210 </div>
211 <?php } ?>
212 </div>
213 <?php }
214
215 public function export_orders() {
216 global $ewd_otp_controller;
217
218 if ( $this->nonce_check and ! isset( $_POST['EWD_OTP_Export_Nonce'] ) ) {
219 return;
220 }
221
222 if ( $this->nonce_check and ! wp_verify_nonce( $_POST['EWD_OTP_Export_Nonce'], 'EWD_OTP_Export' ) ) {
223 return;
224 }
225
226 // Instantiate a new PHPExcel object
227 $spreadsheet = new Spreadsheet();
228 // Set the active Excel worksheet to sheet 0
229 $spreadsheet->setActiveSheetIndex(0);
230
231 $records = array('header' => array(), 'data' => array());
232 if( empty( $_POST['type-of-record'] ) or 'order' == $_POST['type-of-record'] ) {
233 $records = $this->get_order_data();
234 }
235 /*elseif ( 'customer' == $_POST['type-of-record'] ) {
236 $records = $this->get_customer_data();
237 }
238 elseif( 'sales-rep' == $_POST['type-of-record'] ) {
239 $records = $this->get_sales_reps();
240 }*/
241
242 if( 1 > count( $records['data'] ) ) {
243 $this->warning('No records found to export!');
244 return;
245 }
246
247 // Adding header
248 $row = 1; $col = 'A';
249 foreach ($records['header'] as $value) {
250 $spreadsheet->getActiveSheet()->setCellValue( $col.$row, $value );
251 $col++;
252 }
253
254 //start while loop to get data
255 $row = 2;
256 foreach ( $records['data'] as $record ) {
257 $col = 'A';
258 foreach ( $record as $value ) {
259 $spreadsheet->getActiveSheet()->setCellValue( $col.$row, $value );
260 $col++;
261 }
262 $row++;
263 }
264
265 // Redirect output to a client’s web browser (Excel5)
266 if ( ! isset( $format_type ) == 'csv' ) {
267
268 ob_clean();
269
270 header('Content-Type: application/vnd.ms-excel');
271 header('Content-Disposition: attachment;filename="orders_export.csv"');
272 header('Cache-Control: max-age=0');
273 $objWriter = new Csv($spreadsheet);
274 $objWriter->save('php://output');
275 }
276 else {
277
278 ob_clean();
279
280 header('Content-Type: application/vnd.ms-excel');
281 header('Content-Disposition: attachment;filename="orders_export.xls"');
282 header('Cache-Control: max-age=0');
283 $objWriter = new Xls($spreadsheet);
284 $objWriter->save('php://output');
285 }
286
287 die();
288 }
289
290 public function get_order_data()
291 {
292 global $ewd_otp_controller;
293
294 // Print out the regular order field labels
295 $order_header = array(
296 'Name',
297 'Number',
298 'Order Status',
299 'Order Status Updated (Read-Only)',
300 'Location',
301 'Display',
302 'Notes Public',
303 'Notes Private',
304 'Email',
305 'Customer',
306 'Sales Rep'
307 );
308
309 // Add custom fields to column headers
310 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
311 foreach ( $custom_fields as $custom_field ) {
312 $order_header[] = $custom_field->name;
313 }
314
315 $args = array(
316 'display' => true,
317 'orders_per_page' => -1,
318 );
319
320 // Order status
321 if( isset( $_POST['order-by-status'] ) && 0 < count( $_POST['order-by-status'] ) ) {
322 $args['status'] = array_map( 'sanitize_text_field', $_POST['order-by-status'] );
323 }
324
325 // Order for Date-range
326 if ( ! empty( $this->after ) ) {
327 // Used to let sales-rep and customers download their data from front-end
328 $args['after'] = $this->after;
329 }
330 elseif( isset( $_POST['date_range'] ) ) {
331 $args['date_range'] = sanitize_text_field( $_POST['date_range'] );
332 }
333 elseif( isset( $_POST['start_date'] ) || isset( $_POST['end_date'] ) ) {
334 // just to pass if in order_manager->prepare_args()
335 $args['date_range'] = 'custom';
336 $args['start_date'] = sanitize_text_field( $_POST['start_date'] );
337 $args['end_date'] = sanitize_text_field( $_POST['end_date'] );
338 }
339
340 // Order of Customer('s)
341 if ( ! empty( $this->customer_id ) ) {
342 // Used to let sales-rep and customers download their data from front-end
343 $args['customer'] = $this->customer_id;
344 }
345 elseif( isset( $_POST['order-of-customer'] ) && 0 < count( $_POST['order-of-customer'] ) ) {
346 $args['customer'] = array_map( 'sanitize_text_field', $_POST['order-of-customer'] );
347 }
348
349 // Order of Sales Rep('s)
350 if ( ! empty( $this->sales_rep_id ) ) {
351 // Used to let sales-rep and customers download their data from front-end
352 $args['sales_rep'] = $this->sales_rep_id;
353 }
354 elseif( isset( $_POST['order-of-sales-rep'] ) ) {
355 $args['sales_rep'] = array_map( 'sanitize_text_field', $_POST['order-of-sales-rep'] );
356 }
357
358 // fetching orders
359 $orders = $ewd_otp_controller->order_manager->get_matching_orders( $args );
360
361 $data = array();
362 foreach ($orders as $order) {
363 $record = array(
364 $order->name,
365 $order->number,
366 $order->status,
367 $order->status_updated,
368 $order->location,
369 ( $order->display ? 'Yes' : 'No' ),
370 $order->notes_public,
371 $order->notes_private,
372 $order->email,
373 max( $order->customer, 0 ),
374 max( $order->sales_rep, 0 ),
375 );
376
377 // Adding custom field data
378 foreach ( $custom_fields as $custom_field ) {
379 $record[] = $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $order->id );
380 }
381
382 $data[] = $record;
383 }
384
385 return array(
386 'header' => $order_header,
387 'data' => $data
388 );
389 }
390
391 public function get_sales_rep_data()
392 {
393 return array(
394 'header' => array(),
395 'data' => array()
396 );
397 }
398
399 public function get_customer_data()
400 {
401 return array(
402 'header' => array(),
403 'data' => array()
404 );
405 }
406
407 public function get_customer_list()
408 {
409 global $ewd_otp_controller;
410
411 $args = array(
412 'orderby' => 'Customer_Name',
413 'order' => 'asc',
414 'customers_per_page' => -1
415 );
416
417 return $ewd_otp_controller->customer_manager->get_matching_customers( $args );
418 }
419
420 public function get_sales_rep_list()
421 {
422 global $ewd_otp_controller;
423
424 $args = array(
425 'orderby' => 'Sales_Rep_First_Name',
426 'order' => 'asc',
427 'sales_reps_per_page' => -1
428 );
429
430 return $ewd_otp_controller->sales_rep_manager->get_matching_sales_reps( $args );
431 }
432
433 public function display_messages()
434 {
435 foreach ($this->messages as $type => $msgs) {
436 echo "<div class='notice notice-{$type}''>";
437 foreach ($msgs as $msg) {
438 echo "<p>{$msg}</p>";
439 }
440 echo '</div>';
441 }
442 }
443
444 public function warning( $msg )
445 {
446 if( !isset( $this->messages['warning'] ) ) {
447 $this->messages['warning'] = array();
448 }
449
450 $this->messages['warning'][] = $msg;
451 }
452
453 public function error( $msg )
454 {
455 if( !isset( $this->messages['error'] ) ) {
456 $this->messages['error'] = array();
457 }
458
459 $this->messages['error'][] = $msg;
460 }
461
462 public function success( $msg )
463 {
464 if( !isset( $this->messages['success'] ) ) {
465 $this->messages['success'] = array();
466 }
467
468 $this->messages['success'][] = $msg;
469 }
470
471 }
472
473
474