Completed
Pull Request — master (#2165)
by Justin
06:55
created

WPSC_Purchase_Log_Page::controller_packing_slip()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 8
nop 0
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * WP eCommerce edit and view sales page functions
4
 *
5
 * These are the main WPSC sales page functions
6
 *
7
 * @package wp-e-commerce
8
 * @since 3.8.8
9
 */
10
11
class WPSC_Purchase_Log_Page {
12
	private $list_table;
13
	private $output;
14
	public $log_id = 0;
15
16
	/**
17
	 * WPSC_Purchase_Log
18
	 *
19
	 * @var WPSC_Purchase_Log object.
20
	 */
21
	public $log = null;
22
23
	/**
24
	 * Whether the purchase log can be modified.
25
	 *
26
	 * @var boolean
27
	 */
28
	protected $can_edit = false;
29
30
	public function __construct() {
31
		$controller = 'default';
32
		$controller_method = 'controller_default';
33
34
		// If individual purchase log, setup ID and action links.
35
		if ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
36
			$this->log_id = (int) $_REQUEST['id'];
37
			$this->log = new WPSC_Purchase_Log( $this->log_id );
38
			$this->can_edit = $this->log->can_edit();
39
		}
40
41
		if ( isset( $_REQUEST['c'] ) && method_exists( $this, 'controller_' . $_REQUEST['c'] ) ) {
42
			$controller = $_REQUEST['c'];
43
			$controller_method = 'controller_' . $controller;
44
		} elseif ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
45
			$controller = 'item_details';
46
			$controller_method = 'controller_item_details';
47
		}
48
49
		$this->$controller_method();
50
	}
51
52
	private function needs_update() {
53
		global $wpdb;
54
55
		if ( get_option( '_wpsc_purchlogs_3.8_updated' ) )
56
			return false;
57
58
		$c = $wpdb->get_var( "SELECT COUNT(*) FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE plugin_version IN ('3.6', '3.7')" );
59
		if ( $c > 0 )
60
			return true;
61
62
		update_option( '_wpsc_purchlogs_3.8_updated', true );
63
		return false;
64
	}
65
66
	public function controller_upgrade_purchase_logs_3_7() {
67
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_upgrade_purchase_logs_3_7' ) );
68
	}
69
70
	private function purchase_logs_fix_options( $id ) {
71
		?>
72
		<select name='<?php echo $id; ?>'>
73
			<option value='-1'><?php echo esc_html_x( 'Select an Option', 'Dropdown default when called in uniquename dropdown', 'wp-e-commerce' ); ?></option>
74
			<option value='billingfirstname'><?php esc_html_e( 'Billing First Name', 'wp-e-commerce' ); ?></option>
75
			<option value='billinglastname'><?php esc_html_e( 'Billing Last Name', 'wp-e-commerce' ); ?></option>
76
			<option value='billingaddress'><?php esc_html_e( 'Billing Address', 'wp-e-commerce' ); ?></option>
77
			<option value='billingcity'><?php esc_html_e( 'Billing City', 'wp-e-commerce' ); ?></option>
78
			<option value='billingstate'><?php esc_html_e( 'Billing State', 'wp-e-commerce' ); ?></option>
79
			<option value='billingcountry'><?php esc_html_e( 'Billing Country', 'wp-e-commerce' ); ?></option>
80
			<option value='billingemail'><?php esc_html_e( 'Billing Email', 'wp-e-commerce' ); ?></option>
81
			<option value='billingphone'><?php esc_html_e( 'Billing Phone', 'wp-e-commerce' ); ?></option>
82
			<option value='billingpostcode'><?php esc_html_e( 'Billing Post Code', 'wp-e-commerce' ); ?></option>
83
			<option value='shippingfirstname'><?php esc_html_e( 'Shipping First Name', 'wp-e-commerce' ); ?></option>
84
			<option value='shippinglastname'><?php esc_html_e( 'Shipping Last Name', 'wp-e-commerce' ); ?></option>
85
			<option value='shippingaddress'><?php esc_html_e( 'Shipping Address', 'wp-e-commerce' ); ?></option>
86
			<option value='shippingcity'><?php esc_html_e( 'Shipping City', 'wp-e-commerce' ); ?></option>
87
			<option value='shippingstate'><?php esc_html_e( 'Shipping State', 'wp-e-commerce' ); ?></option>
88
			<option value='shippingcountry'><?php esc_html_e( 'Shipping Country', 'wp-e-commerce' ); ?></option>
89
			<option value='shippingpostcode'><?php esc_html_e( 'Shipping Post Code', 'wp-e-commerce' ); ?></option>
90
		</select>
91
		<?php
92
	}
93
94
	public function display_upgrade_purchase_logs_3_7() {
95
		global $wpdb;
96
		$numChanged = 0;
97
		$numQueries = 0;
98
		$purchlog =  "SELECT DISTINCT id FROM `".WPSC_TABLE_PURCHASE_LOGS."` LIMIT 1";
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
99
		$id = $wpdb->get_var($purchlog);
100
		$usersql = "SELECT DISTINCT `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.value, `".WPSC_TABLE_CHECKOUT_FORMS."`.* FROM `".WPSC_TABLE_CHECKOUT_FORMS."` LEFT JOIN `".WPSC_TABLE_SUBMITTED_FORM_DATA."` ON `".WPSC_TABLE_CHECKOUT_FORMS."`.id = `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.`form_id` WHERE `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.log_id=".$id." ORDER BY `".WPSC_TABLE_CHECKOUT_FORMS."`.`checkout_order`" ;
101
		$formfields = $wpdb->get_results($usersql);
102
103
		if(count($formfields) < 1){
104
			$usersql = "SELECT DISTINCT  `".WPSC_TABLE_CHECKOUT_FORMS."`.* FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` != 'heading'";
105
			$formfields = $wpdb->get_results($usersql);
106
		}
107
108
		if(isset($_POST)){
109
			foreach($_POST as $key=>$value){
110
				if($value != '-1'){
111
					$complete = $wpdb->update(
112
				 WPSC_TABLE_CHECKOUT_FORMS,
113
				 array(
114
				'unique_name' => $value
115
				 ),
116
				 array(
117
				'id' => $key
118
				  ),
119
				 '%s',
120
				 '%d'
121
				 );
122
				}
123
				$numChanged++;
124
				$numQueries ++;
125
			}
126
127
			$sql = "UPDATE `".WPSC_TABLE_CHECKOUT_FORMS."` SET `unique_name`='delivertoafriend' WHERE `name` = '2. Shipping details'";
128
			$wpdb->query($sql);
129
130
			add_option('wpsc_purchaselogs_fixed',true);
131
		}
132
133
		include( 'includes/purchase-logs-page/upgrade.php' );
134
	}
135
136
	public function display_upgrade_purchase_logs_3_8() {
137
		?>
138
			<div class="wrap">
139
				<h2><?php echo esc_html( __('Sales', 'wp-e-commerce') ); ?> </h2>
140
				<div class="updated">
141
					<p><?php printf( __( 'Your purchase logs have been updated! <a href="%s">Click here</a> to return.' , 'wp-e-commerce' ), esc_url( remove_query_arg( 'c' ) ) ); ?></p>
142
				</div>
143
			</div>
144
		<?php
145
	}
146
147
	public function controller_upgrade_purchase_logs_3_8() {
148
		if ( $this->needs_update() )
149
			wpsc_update_purchase_logs();
150
151
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_upgrade_purchase_logs_3_8' ) );
152
	}
153
154
	function purchase_logs_pagination() {
155
		global $wpdb, $purchlogitem;
156
		$prev_id = $this->log->get_previous_log_id();
157
		$next_id = $this->log->get_next_log_id();
158
		?>
159
		<span class='tablenav'><span class='tablenav-pages'><span class='pagination-links'>
160
			<?php if ( $prev_id ) : ?>
161
				<a href='<?php echo esc_url( $this->get_purchase_log_url( $prev_id ) ); ?>' class='prev-page'>&lsaquo; <?php _e( 'Previous', 'wp-e-commerce' ); ?></a>
162
			<?php endif; ?>
163
164
			<?php if ( $next_id ) : ?>
165
				<a href='<?php echo esc_url( $this->get_purchase_log_url( $next_id ) ); ?>' class='next-page'><?php _e( 'Next', 'wp-e-commerce' ); ?> &rsaquo;</a>
166
			<?php endif; ?>
167
		</span></span></span>
168
		<?php
169
	}
170
171
	function purchase_logs_checkout_fields(){
172
		global $purchlogitem;
173
174
		if ( ! empty( $purchlogitem->additional_fields ) ) {
175
		?>
176
			<div class="metabox-holder">
177
				<div id="custom_checkout_fields" class="postbox">
178
					<h3 class='hndle'><?php esc_html_e( 'Additional Checkout Fields' , 'wp-e-commerce' ); ?></h3>
179
					<div class='inside'>
180
						<?php
181
						foreach( (array) $purchlogitem->additional_fields as $value ) {
182
							$value['value'] = maybe_unserialize ( $value['value'] );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
183
							if ( is_array( $value['value'] ) ) {
184
								?>
185
									<p><strong><?php echo $value['name']; ?> :</strong> <?php echo implode( stripslashes( $value['value'] ), ',' ); ?></p>
186
								<?php
187
							} else {
188
								$thevalue = esc_html( stripslashes( $value['value'] ));
189
								if ( empty( $thevalue ) ) {
190
									$thevalue = __( '<em>blank</em>', 'wp-e-commerce' );
191
								}
192
								?>
193
									<p><strong><?php echo $value['name']; ?> :</strong> <?php echo $thevalue; ?></p>
194
								<?php
195
							}
196
						}
197
						?>
198
					</div>
199
				</div>
200
			</div>
201
		<?php
202
		}
203
	}
204
205
	public function purchase_log_custom_fields(){
206
		if( wpsc_purchlogs_has_customfields() ){?>
207
			<div class='metabox-holder'>
208
				<div id='purchlogs_customfields' class='postbox'>
209
					<h3 class='hndle'><?php esc_html_e( 'Users Custom Fields' , 'wp-e-commerce' ); ?></h3>
210
					<div class='inside'>
211
						<?php $messages = wpsc_purchlogs_custommessages(); ?>
212
						<?php $files = wpsc_purchlogs_customfiles(); ?>
213
						<?php if(count($files) > 0){ ?>
214
							<h4><?php esc_html_e( 'Cart Items with Custom Files' , 'wp-e-commerce' ); ?>:</h4>
215
							<?php
216
							foreach($files as $file){
217
								echo $file;
218
							}
219
						}?>
220
						<?php if(count($messages) > 0){ ?>
221
							<h4><?php esc_html_e( 'Cart Items with Custom Messages' , 'wp-e-commerce' ); ?>:</h4>
222
							<?php
223
							foreach($messages as $message){
224
								echo esc_html( $message['title'] ) . ':<br />' . nl2br( esc_html( $message['message'] ) );
225
							}
226
						} ?>
227
					</div>
228
				</div>
229
			</div>
230
		<?php
231
		}
232
	}
233
234
	private function purchase_log_cart_items() {
235
		while( wpsc_have_purchaselog_details() ) : wpsc_the_purchaselog_item();
236
			self::purchase_log_cart_item( $this->can_edit );
237
		endwhile;
238
	}
239
240
	public static function purchase_log_cart_item( $can_edit = false ) {
241
		?>
242
		<tr class="purchase-log-line-item" id="purchase-log-item-<?php echo wpsc_purchaselog_details_id(); ?>" data-id="<?php echo wpsc_purchaselog_details_id(); ?>">
243
			<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
244
			<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
245
			<td>
246
				<?php if ( $can_edit ) : ?>
247
					<input type="number" step="1" min="0" autocomplete="off" name="wpsc_item_qty" class="wpsc_item_qty" placeholder="0" value="<?php echo wpsc_purchaselog_details_quantity(); ?>" size="4" class="quantity">
248
				<?php else: ?>
249
					<?php echo wpsc_purchaselog_details_quantity(); ?>
250
				<?php endif; ?>
251
			</td> <!-- QUANTITY! -->
252
			<td>
253
		 <?php
254
		echo wpsc_currency_display( wpsc_purchaselog_details_price() );
255
		do_action( 'wpsc_additional_sales_amount_info', wpsc_purchaselog_details_id() );
256
		 ?>
257
	 </td> <!-- PRICE! -->
258
			<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_shipping() ); ?></td> <!-- SHIPPING! -->
259
			<?php if( wpec_display_product_tax() ): ?>
260
				<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_tax() ); ?></td> <!-- TAX! -->
261
			<?php endif; ?>
262
			<!-- <td><?php echo wpsc_currency_display( wpsc_purchaselog_details_discount() ); ?></td> --> <!-- DISCOUNT! -->
263
			<td class="amount"><?php echo wpsc_currency_display( wpsc_purchaselog_details_total() ); ?></td> <!-- TOTAL! -->
264
			<?php if ( $can_edit ) : ?>
265
				<td class="remove">
266
					<div class="wpsc-remove-row">
267
						<button type="button" class="wpsc-remove-item-button"><span style="color:#a00;" class="dashicons dashicons-dismiss"></span> <?php esc_html_e( 'Remove Item', 'wp-e-commerce' ); ?></button>
268
					</div>
269
				</td> <!-- REMOVE! -->
270
			<?php endif; ?>
271
		</tr>
272
		<?php
273
		do_action( 'wpsc_additional_sales_item_info', wpsc_purchaselog_details_id() );
274
	}
275
276
	public function controller_item_details() {
277
		if (
278
			! isset( $_REQUEST['id'] )
279
			|| ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) )
280
			|| ! $this->log->exists()
281
		) {
282
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
283
		}
284
285
		$this->log->init_items();
286
287
		$columns = array(
288
			'title'    => __( 'Name', 'wp-e-commerce' ),
289
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
290
			'quantity' => __( 'Quantity','wp-e-commerce' ),
291
			'price'    => __( 'Price', 'wp-e-commerce' ),
292
			'shipping' => __( 'Item Shipping', 'wp-e-commerce'),
293
		);
294
295
		if ( wpec_display_product_tax() ) {
296
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
297
		}
298
299
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
300
301
		if ( $this->can_edit ) {
302
			$columns['remove'] = '';
303
		}
304
305
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
306
307
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_purchase_log' ) );
308
		add_action( 'wpsc_purchlogitem_metabox_start', array( $this, 'purchase_log_custom_fields' ) );
309
	}
310
311
	public function controller_packing_slip() {
312
313
		if ( ! isset( $_REQUEST['id'] ) || ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) ) ) {
314
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
315
		}
316
317
		$this->log->init_items();
318
319
		$columns = array(
320
			'title'    => __( 'Item Name', 'wp-e-commerce' ),
321
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
322
			'quantity' => __( 'Quantity', 'wp-e-commerce' ),
323
			'price'    => __( 'Price', 'wp-e-commerce' ),
324
			'shipping' => __( 'Item Shipping','wp-e-commerce' ),
325
		);
326
327
		if ( wpec_display_product_tax() ) {
328
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
329
		}
330
331
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
332
333
		$cols = count( $columns ) - 2;
334
335
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
336
337
		if ( file_exists( get_stylesheet_directory() . '/wpsc-packing-slip.php' ) ) {
338
			$packing_slip_file = get_stylesheet_directory() . '/wpsc-packing-slip.php';
339
		} else {
340
			$packing_slip_file = 'includes/purchase-logs-page/packing-slip.php';
341
		}
342
343
		$packing_slip_file = apply_filters( 'wpsc_packing_packing_slip_path', $packing_slip_file );
344
345
		include( $packing_slip_file );
346
347
		exit;
348
	}
349
350
	public function controller_default() {
351
		//Create an instance of our package class...
352
		$this->list_table = new WPSC_Purchase_Log_List_Table();
353
		$this->process_bulk_action();
354
		$this->list_table->prepare_items();
355
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_list_table' ) );
356
	}
357
358
	public function display_purchase_log() {
359
		$cols = 4;
360
		if ( wpec_display_product_tax() ) {
361
			$cols++;
362
		}
363
364
		if ( $this->can_edit ) {
365
			$cols++;
366
		}
367
368
		$receipt_sent = ! empty( $_GET['sent'] );
369
		$receipt_not_sent = isset( $_GET['sent'] ) && ! $_GET['sent'];
370
		include( 'includes/purchase-logs-page/item-details.php' );
371
372
		global $wp_scripts;
373
374
		wp_enqueue_script( 'wp-backbone' );
375
376
		if ( isset( $wp_scripts->registered['wp-e-commerce-purchase-logs'] ) ) {
377
			// JS needed for modal
378
			$wp_scripts->registered['wp-e-commerce-purchase-logs']->deps[] = 'wp-backbone';
379
		}
380
381
		add_action( 'admin_footer', 'find_posts_div' );
382
	}
383
384
	public function download_csv() {
385
		$_REQUEST['rss_key'] = 'key';
386
		wpsc_purchase_log_csv();
387
	}
388
389
	public function process_bulk_action() {
390
		global $wpdb;
391
		$current_action = $this->list_table->current_action();
392
393
		do_action( 'wpsc_sales_log_process_bulk_action', $current_action );
394
395
		if ( ! $current_action || ( 'download_csv' != $current_action && empty( $_REQUEST['post'] ) ) ) {
396
			if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
397
				wp_redirect( esc_url_raw( remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'action', 'action2' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ) );
398
				exit;
399
			}
400
401
			unset( $_REQUEST['post'] );
402
			return;
403
		}
404
405
		if ( 'download_csv' == $current_action ) {
406
			$this->download_csv();
407
			exit;
408
		}
409
410
		$sendback = remove_query_arg( array(
411
			'_wpnonce',
412
			'_wp_http_referer',
413
			'action',
414
			'action2',
415
			'confirm',
416
			'post',
417
			'last_paged'
418
		) );
419
420
		if ( 'delete' == $current_action ) {
421
422
			// delete action
423
			if ( empty( $_REQUEST['confirm'] ) ) {
424
				$this->list_table->disable_search_box();
425
				$this->list_table->disable_bulk_actions();
426
				$this->list_table->disable_sortable();
427
				$this->list_table->disable_month_filter();
428
				$this->list_table->disable_views();
429
				$this->list_table->set_per_page(0);
430
				add_action( 'wpsc_purchase_logs_list_table_before', array( $this, 'action_list_table_before' ) );
431
				return;
432
			} else {
433
				if ( empty( $_REQUEST['post'] ) )
434
					return;
435
436
				$ids = array_map( 'intval', $_REQUEST['post'] );
437
438
				foreach ( $ids as $id ) {
439
					$log = new WPSC_Purchase_Log( $id );
440
					$log->delete();
441
				}
442
443
				$sendback = add_query_arg( array(
444
					'paged'   => $_REQUEST['last_paged'],
445
					'deleted' => count( $_REQUEST['post'] ),
446
				), $sendback );
447
448
			}
449
		}
450
451
		// change status actions
452
		if ( is_numeric( $current_action ) && ! empty( $_REQUEST['post'] ) ) {
453
454
			foreach ( $_REQUEST['post'] as $id )
455
				wpsc_purchlog_edit_status( $id, $current_action );
456
457
			$sendback = add_query_arg( array(
458
				'updated' => count( $_REQUEST['post'] ),
459
			), $sendback );
460
		}
461
462
		wp_redirect( esc_url_raw( $sendback ) );
463
		exit;
464
	}
465
466
	public function action_list_table_before() {
467
		include( 'includes/purchase-logs-page/bulk-delete-confirm.php' );
468
	}
469
470
	public function display_list_table() {
471
		if ( ! empty( $this->output ) ) {
472
			echo $this->output;
473
			return;
474
		}
475
476
		include( 'includes/purchase-logs-page/list-table.php' );
477
	}
478
479
	private function get_purchase_log_url( $id ) {
480
		$location = add_query_arg( array(
481
			'page' => 'wpsc-purchase-logs',
482
			'c'    => 'item_details',
483
			'id'   => $id,
484
		), admin_url( 'index.php' ) );
485
486
		return esc_url( $location );
487
	}
488
489
}
490