Completed
Pull Request — master (#2165)
by Justin
05:16
created

WPSC_Purchase_Log_Page::controller_item_details()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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