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

s.php ➔ wpsc_purchlogs_has_customfields()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 6
nop 1
dl 0
loc 20
rs 7.7777
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wpsc_purchaselogs::the_purch_status() 0 3 1
A wpsc_purchaselogs::have_purch_status() 0 9 4
1
<?php
2
/**
3
 * WP eCommerce purchaselogs AND purchaselogs_items class
4
 *
5
 * These is the classes for the WP eCommerce purchase logs,
6
 * The purchaselogs class handles adding, removing and adjusting details in the purchaselogs,
7
 * The purchaselogs_items class handles adding, removing and adjusting individual item details in the purchaselogs,
8
 *
9
 * @package wp-e-commerce
10
 * @since 3.7
11
 * @subpackage wpsc-cart-classes
12
 */
13
class wpsc_purchaselogs {
0 ignored issues
show
Coding Style introduced by
Class name "wpsc_purchaselogs" is not in camel caps format
Loading history...
14
15
   var $earliest_timestamp;
16
   var $current_timestamp;
17
   var $earliest_year;
18
   var $current_year;
19
   var $form_data;
20
   var $purch_item_count;
21
   //individual purch log variables
22
   var $allpurchaselogs;
23
   var $currentitem = -1;
24
   var $purchitem;
25
   //used for purchase options
26
   var $currentstatus = -1;
27
   var $purch_status_count;
28
   var $allpurchaselogstatuses;
29
   var $purchstatus;
30
   //calculation of totals
31
   var $totalAmount;
32
   //used for csv
33
   var $current_start_timestamp;
34
   var $current_end_timestamp;
35
   var $start_timestamp;
36
   var $end_timestamp;
37
38
	/* Constructor function */
39
	public function __construct() {
40
		$this->getall_formdata();
41
		if ( !isset( $_GET['view_purchlogs_by'] ) && !isset( $_GET['purchlogs_searchbox'] ) ) {
42
			$dates = $this->getdates();
43
			$dates = array_slice( $dates, 0, 3 );
44
			if(isset($dates[2]['start']))
45
				$this->current_start_timestamp = $dates[2]['start'];
46
			$this->current_end_timestamp = $dates[0]['end'];
47
			$newlogs = $this->get_purchlogs( $dates );
48
			$_SESSION['newlogs'] = $newlogs;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
49
			$this->allpurchaselogs = $newlogs;
50
	  } else {
51
		 $this->getdates();
52
		 if ( isset( $_GET['view_purchlogs_by'] ) && isset( $_GET['view_purchlogs_by_status'] ) ) {
53
			$status = sanitize_text_field( $_GET['view_purchlogs_by_status'] );
54
			$viewby = sanitize_text_field( $_GET['view_purchlogs_by'] );
55
			if ( $viewby == 'all' ) {
56
			   $dates = $this->getdates();
57
			   $purchaselogs = $this->get_purchlogs( $dates, $status );
58
			   $_SESSION['newlogs'] = $purchaselogs;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
59
			   $this->allpurchaselogs = $purchaselogs;
60
			} elseif ( $viewby == '3mnths' ) {
61
			   $dates = $this->getdates();
62
63
			   $dates = array_slice( $dates, 0, 3 );
64
			   $this->current_start_timestamp = $dates[count($dates)-1]['start'];
65
			   $this->current_end_timestamp = $dates[0]['end'];
66
			   $newlogs = $this->get_purchlogs( $dates, $status );
67
			   $_SESSION['newlogs'] = $newlogs;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
68
			   $this->allpurchaselogs = $newlogs;
69
			} else {
70
			   $dates = explode( '_', $viewby );
71
			   $date[0]['start'] = $dates[0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$date was never initialized. Although not strictly required by PHP, it is generally a good practice to add $date = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
72
			   $date[0]['end'] = $dates[1];
73
			   $this->current_start_timestamp = $dates[0];
74
			   $this->current_end_timestamp = $dates[1];
75
			   $newlogs = $this->get_purchlogs( $date, $status );
76
			   $_SESSION['newlogs'] = $newlogs;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
77
			   $this->allpurchaselogs = $newlogs;
78
			}
79
		 }
80
	  }
81
	  $this->purch_item_count = count( $this->allpurchaselogs );
82
	  $statuses = $this->the_purch_item_statuses();
83
	  if ( isset( $_SESSION['newlogs'] ) ) {
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
84
		 $this->allpurchaselogs = $_SESSION['newlogs'];
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
85
		 $this->purch_item_count = count( $_SESSION['newlogs'] );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
86
	  }
87
88
	  return;
89
   }
90
91
   function get_purchlogs( $dates, $status='' ) {
0 ignored issues
show
introduced by
Expected 1 space between argument "$status" and equals sign; 0 found
Loading history...
introduced by
Expected 1 space between default value and equals sign for argument "$status";
Loading history...
92
	  global $wpdb;
93
	   $purchlog2 = array();
94
	  $orderby = apply_filters( 'wpsc_purchase_logs_orderby', "' ORDER BY `date` DESC" );
95
	  if ( $status == '' || $status == '-1' ) {
96
		 foreach ( (array)$dates as $date_pair ) {
97
			if ( ($date_pair['end'] >= $this->earliest_timestamp) && ($date_pair['start'] <= $this->current_timestamp) ) {
98
			   $sql = $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `date` BETWEEN %s AND %s {$orderby}", $date_pair['start'], $date_pair['end'] );
99
			   $purchase_logs = $wpdb->get_results( $sql );
100
			   array_push( $purchlog2, $purchase_logs );
101
			}
102
		 }
103
	  } else {
104
		 foreach ( (array)$dates as $date_pair ) {
105
			if ( ($date_pair['end'] >= $this->earliest_timestamp) && ($date_pair['start'] <= $this->current_timestamp) ) {
106
			   $sql = $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `date` BETWEEN %s AND %s AND `processed`=%s {$orderby}", $date_pair['start'], $date_pair['end'], $status );
107
			   $purchase_logs = $wpdb->get_results( $sql );
108
			   array_push( $purchlog2, $purchase_logs );
109
			}
110
		 }
111
	  }
112
	  $newarray = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
113
	  foreach ( $purchlog2 as $purch ) {
114
		 if ( is_array( $purch ) ) {
115
			foreach ( $purch as $log ) {
116
			   $newarray[] = $log;
117
			}
118
		 } else {
119
			exit( 'Else :' . print_r( $purch ) );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
120
		 }
121
	  }
122
	  $this->allpurchaselogs = $newarray;
123
	  $this->purch_item_count = count( $this->allpurchaselogs );
124
	  return $newarray;
125
   }
126
127
   function getall_formdata() {
128
	  global $wpdb;
129
	  $form_sql = "SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1';";
130
	  $form_data = $wpdb->get_results( $form_sql, ARRAY_A );
131
	  $this->form_data = $form_data;
132
	  return $form_data;
133
   }
134
135
   /*
136
	* This finds the earliest time in the shopping cart and sorts out the timestamp system for the month by month display
137
	* or if there was a filter applied use the filter to sort the dates.
138
	*/
139
140
   function getdates() {
141
	  global $wpdb, $purchlogs;
142
143
	  $earliest_record_sql = "SELECT MIN(`date`) AS `date` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `date`!=''";
144
	  $earliest_record = $wpdb->get_results( $earliest_record_sql, ARRAY_A );
145
146
	  $this->current_timestamp = time();
147
	  //if there are no reccords set the date to now.
148
	  $this->earliest_timestamp = ( isset( $earliest_record[0] ) && isset( $earliest_record[0]['date'] ) )?$earliest_record[0]['date']:time();
149
150
	  $this->current_year = date( "Y" );
151
	  $this->earliest_year = date( "Y", $this->earliest_timestamp );
152
153
	  $j = 0;
154
	  $date_list = array();
155
156
	  for ( $year = $this->current_year; $year >= $this->earliest_year; $year-- ) {
157
		 for ( $month = 12; $month >= 1; $month-- ) {
158
			$this->start_timestamp = mktime( 0, 0, 0, $month, 1, $year );
159
			$this->end_timestamp = mktime( 0, 0, 0, ($month + 1 ), 1, $year );
160
			if ( ($this->end_timestamp >= $this->earliest_timestamp) && ($this->start_timestamp <= $this->current_timestamp) ) {
161
			   $date_list[$j]['start'] = $this->start_timestamp;
162
			   $date_list[$j]['end'] = $this->end_timestamp;
163
			   $j++;
164
			}
165
		 }
166
	  }
167
	  if ( is_object( $purchlogs ) ) {
168
		 $purchlogs->current_start_timestamp = $purchlogs->earliest_timestamp;
169
		 $purchlogs->current_end_timestamp = $purchlogs->current_timestamp;
170
	  }
171
	  return $date_list;
172
   }
173
174
   function deletelog( $deleteid ) {
175
	global $wpdb;
176
	  if ( is_numeric( $deleteid ) ) {
177
178
		 $delete_log_form_sql = "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`='$deleteid'";
179
		 $cart_content = $wpdb->get_results( $delete_log_form_sql, ARRAY_A );
180
		 $wpdb->query( "DELETE FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`='$deleteid'" );
181
		 $wpdb->query( "DELETE FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "` WHERE `log_id` IN ('$deleteid')" );
182
		 $wpdb->query( "DELETE FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`='$deleteid' LIMIT 1" );
183
		 return '<div id="message" class="updated fade"><p>' . __( 'Thanks, the purchase log record has been deleted', 'wp-e-commerce' ) . '</p></div>';
184
	  }
185
   }
186
187
   //individual purchase log functions
188
   function next_purch_item() {
189
	  $this->currentitem++;
190
191
	  $this->purchitem = $this->allpurchaselogs[$this->currentitem];
192
	  return $this->purchitem;
193
   }
194
195
   function the_purch_item() {
196
	  $this->purchitem = $this->next_purch_item();
197
   }
198
199
   function have_purch_items() {
200
	  if ( $this->currentitem + 1 < $this->purch_item_count ) {
201
		 return true;
202
	  } else if ( $this->currentitem + 1 == $this->purch_item_count && $this->purch_item_count > 0 ) {
203
		 // Do some cleaning up after the loop,
204
		 $this->rewind_purch_items();
205
	  }
206
	  return false;
207
   }
208
209
   function rewind_purch_items() {
210
	  $this->currentitem = -1;
211
	  if ( $this->purch_item_count > 0 ) {
212
		 $this->purchitem = $this->allpurchaselogs[0];
213
	  }
214
   }
215
216
   function the_purch_item_statuses() {
217
	  global $wpdb, $wpsc_purchlog_statuses;
218
	  $this->purch_status_count = count( $wpsc_purchlog_statuses );
219
	  $this->allpurchaselogstatuses = $wpsc_purchlog_statuses;
220
	  return $wpsc_purchlog_statuses;
221
   }
222
223
   // purchase status loop functions
224
   function next_purch_status() {
225
	  $this->currentstatus++;
226
	  $this->purchstatus = $this->allpurchaselogstatuses[$this->currentstatus];
227
	  return $this->purchstatus;
228
   }
229
230
   function the_purch_status() {
231
	  $this->purchstatus = $this->next_purch_status();
232
   }
233
234
   function have_purch_status() {
235
	  if ( $this->currentstatus + 1 < $this->purch_status_count ) {
236
		 return true;
237
	  } else if ( $this->currentstatus + 1 == $this->purch_status_count && $this->purch_status_count > 0 ) {
238
		 // Do some cleaning up after the loop,
239
		 $this->rewind_purch_status();
240
	  }
241
	  return false;
242
   }
243
244
   function rewind_purch_status() {
245
	  $this->currentstatus = -1;
246
	  if ( $this->purch_status_count > 0 ) {
247
		 $this->purchstatus = $this->allpurchaselogstatuses[0];
248
	  }
249
   }
250
251
   function is_checked_status() {
252
	  if ( isset( $this->purchstatus['order'] ) && isset( $this->purchitem->processed ) && ($this->purchstatus['order'] == $this->purchitem->processed) ) {
253
		 return 'selected="selected"';
254
	  } else {
255
		 return '';
256
	  }
257
   }
258
259
   function the_purch_item_name() {
260
	  global $wpdb;
261
	  $i = 0;
262
263
	  if ( $this->form_data == null ) {
264
		 $this->getall_formdata();
265
	  }
266
267
	  $emailformid = 0;
268
	  $fNameformid = 0;
269
	  $lNameformid = 0;
270
271
	  foreach ( (array)$this->form_data as $formdata ) {
272
		 if ( in_array( 'billingemail', $formdata ) ) {
273
			$emailformid = $formdata['id'];
274
		 }
275
		 if ( in_array( 'billingfirstname', $formdata ) ) {
276
			$fNameformid = $formdata['id'];
277
		 }
278
		 if ( in_array( 'billinglastname', $formdata ) ) {
279
			$lNameformid = $formdata['id'];
280
		 }
281
		 $i++;
282
	  }
283
284
	  $sql = "SELECT value FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " WHERE log_id=" . $this->purchitem->id . " AND form_id=" . $emailformid;
285
	  $email = $wpdb->get_var( $sql );
286
287
	  $sql = "SELECT value FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " WHERE log_id=" . $this->purchitem->id . " AND form_id=" . $fNameformid;
288
	  $fname = $wpdb->get_var( $sql );
289
290
	  $sql = "SELECT value FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " WHERE log_id=" . $this->purchitem->id . " AND form_id=" . $lNameformid;
291
	  $lname = $wpdb->get_var( $sql );
292
293
	  $namestring = esc_html( $fname ) . ' ' . esc_html( $lname ) . ' (<a href="mailto:' . esc_attr( $email ) . '?subject=Message From ' . home_url() . '">' . esc_html( $email ) . '</a>) ';
294
295
	  if ( $fname == '' && $lname == '' && $email == '' ) {
296
		 $namestring = __('N/A', 'wp-e-commerce');
297
	  }
298
299
	  return $namestring;
300
   }
301
302
   function the_purch_item_details() {
303
	  global $wpdb;
304
	  $sql = "SELECT SUM(quantity) FROM " . WPSC_TABLE_CART_CONTENTS . " WHERE purchaseid=" . $this->purchitem->id;
305
	  $sum = $wpdb->get_var( $sql );
306
	  return $sum;
307
   }
308
309
	function search_purchlog_view( $searchterm ) {
310
		global $wpdb, $wp_version;
311
312
		if ( version_compare( $wp_version, '4.0', '>=' ) ) {
313
			$searchterm = '%' . $wpdb->esc_like( $searchterm ) . '%';
314
		} else {
315
			$searchterm = '%' . like_escape( $searchterm ) . '%';
316
		}
317
318
		$newlogs = $wpdb->get_results( $wpdb->prepare(
319
			"SELECT DISTINCT `" . WPSC_TABLE_PURCHASE_LOGS . "` . * FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "`
320
			LEFT JOIN `" . WPSC_TABLE_PURCHASE_LOGS . "`
321
			ON `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "`.`log_id` = `" . WPSC_TABLE_PURCHASE_LOGS . "`.`id`
322
			WHERE `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "`.`value` LIKE %s
323
			OR `" . WPSC_TABLE_PURCHASE_LOGS . "`.`transactid` = %s
324
			OR `" . WPSC_TABLE_PURCHASE_LOGS . "`.`track_id` LIKE %s",
325
			$searchterm
326
			)
327
		);
328
329
		$_SESSION['newlogs'] = $newlogs;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
330
331
		return $newlogs;
332
	}
333
334
}
335