class-affiliates-totals.php
30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
<?php
/**
* class-affiliates-totals.php
*
* Copyright (c) 2010 - 2014 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates
* @since 2.7.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'Affiliates_Totals' ) ) {
/**
* Totals admin.
*/
class Affiliates_Totals {
const NONCE = 'totals-nonce';
const NONCE_1 = 'totals-nonce-1';
const NONCE_2 = 'totals-nonce-2';
const SET_FILTERS = 'set-filters';
const SET_RPP = 'set-rpp';
const SET_PAGE = 'set-page';
const TOTALS_PER_PAGE = 10;
public static function view() {
global $wpdb, $affiliates_options;
$output = '';
$today = date( 'Y-m-d', time() );
if ( !current_user_can( AFFILIATES_ACCESS_AFFILIATES ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
if ( isset ( $_GET['action'] ) ) {
switch( $_GET['action'] ) {
case 'close_referrals' :
$params = array(
'tables' => array(
'referrals' => _affiliates_get_tablename( 'referrals' ),
'affiliates' => _affiliates_get_tablename( 'affiliates' ),
'affiliates_users' => _affiliates_get_tablename( 'affiliates_users' ),
'users' => $wpdb->users,
)
);
$params = array_merge( $_GET, $params );
echo self::update_status( AFFILIATES_REFERRAL_STATUS_CLOSED, $params );
die();
break;
}
}
$from_date = $affiliates_options->get_option( 'totals_from_date', null );
$from_datetime = null;
$thru_date = $affiliates_options->get_option( 'totals_thru_date', null );
$thru_datetime = null;
$referral_status = $affiliates_options->get_option( 'totals_referral_status', null );
$currency_id = $affiliates_options->get_option( 'totals_currency_id', null );
if ( isset( $_POST['clear_filters'] ) || isset( $_POST['submitted'] ) ) {
if ( !wp_verify_nonce( $_POST[self::NONCE], self::SET_FILTERS ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
}
if ( isset( $_POST['clear_filters'] ) ) {
$affiliates_options->delete_option( 'totals_from_date' );
$affiliates_options->delete_option( 'totals_thru_date' );
$affiliates_options->delete_option( 'totals_referral_status' );
$affiliates_options->delete_option( 'totals_currency_id' );
$from_date = null;
$from_datetime = null;
$thru_date = null;
$thru_datetime = null;
$referral_status = null;
$currency_id = null;
} else if ( isset( $_POST['submitted'] ) ) {
if ( !empty( $_POST['from_date'] ) ) {
$from_date = date( 'Y-m-d', strtotime( $_POST['from_date'] ) );
$affiliates_options->update_option( 'totals_from_date', $from_date );
} else {
$from_date = null;
$affiliates_options->delete_option( 'totals_from_date' );
}
if ( !empty( $_POST['thru_date'] ) ) {
$thru_date = date( 'Y-m-d', strtotime( $_POST['thru_date'] ) );
$affiliates_options->update_option( 'totals_thru_date', $thru_date );
} else {
$thru_date = null;
$affiliates_options->delete_option( 'totals_thru_date' );
}
if ( $from_date && $thru_date ) {
if ( strtotime( $from_date ) > strtotime( $thru_date ) ) {
$thru_date = null;
$affiliates_options->delete_option( 'totals_thru_date' );
}
}
if ( !empty( $_POST['referral_status'] ) && ( $referral_status = Affiliates_Utility::verify_referral_status_transition( $_POST['referral_status'], $_POST['referral_status'] ) ) ) {
$affiliates_options->update_option( 'totals_referral_status', $referral_status );
} else {
$referral_status = null;
$affiliates_options->delete_option( 'totals_referral_status' );
}
if ( !empty( $_POST['currency_id'] ) && ( $currency_id = Affiliates_Utility::verify_currency_id( $_POST['currency_id'] ) ) ) {
$affiliates_options->update_option( 'totals_currency_id', $currency_id );
} else {
$currency_id = null;
$affiliates_options->delete_option( 'totals_currency_id' );
}
}
if ( isset( $_POST['row_count'] ) ) {
if ( !wp_verify_nonce( $_POST[self::NONCE_1], self::SET_RPP ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
}
if ( isset( $_POST['paged'] ) ) {
if ( !wp_verify_nonce( $_POST[self::NONCE_2], self::SET_PAGE ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'affiliate_id', $current_url );
$referrals_table = _affiliates_get_tablename( 'referrals' );
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
$output .= '<div class="totals">';
$output .= '<h1>';
$output .= __( 'Totals', AFFILIATES_PLUGIN_DOMAIN );
$output .= '</h1>';
$row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
if ($row_count <= 0) {
$row_count = $affiliates_options->get_option( 'totals_per_page', self::TOTALS_PER_PAGE );
} else {
$affiliates_options->update_option('totals_per_page', $row_count );
}
$offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
if ( $offset < 0 ) {
$offset = 0;
}
$paged = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 0;
if ( $paged < 0 ) {
$paged = 0;
}
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
switch ( $orderby ) {
case 'affiliate_id' :
case 'name' :
case 'user_login' :
case 'email' :
case 'total' :
case 'currency_id' :
break;
default:
$orderby = 'name';
}
$order = isset( $_GET['order'] ) ? $_GET['order'] : null;
switch ( $order ) {
case 'asc' :
case 'ASC' :
$switch_order = 'DESC';
break;
case 'desc' :
case 'DESC' :
$switch_order = 'ASC';
break;
default:
$order = 'ASC';
$switch_order = 'DESC';
}
$filters = array( " 1=%d " );
$filter_params = array( 1 );
// We have the desired dates from the user's point of view, i.e. in her timezone.
// If supported, adjust the dates for the site's timezone:
if ( $from_date ) {
$from_datetime = DateHelper::u2s( $from_date );
}
if ( $thru_date ) {
$thru_datetime = DateHelper::u2s( $thru_date, 24*3600 );
}
if ( $from_datetime && $thru_datetime ) {
$filters[] = " r.datetime >= %s AND r.datetime < %s ";
$filter_params[] = $from_datetime;
$filter_params[] = $thru_datetime;
} else if ( $from_datetime ) {
$filters[] = " r.datetime >= %s ";
$filter_params[] = $from_datetime;
} else if ( $thru_datetime ) {
$filters[] = " r.datetime < %s ";
$filter_params[] = $thru_datetime;
}
if ( $referral_status ) {
$filters[] = " r.status = %s ";
$filter_params[] = $referral_status;
}
if ( $currency_id ) {
$filters[] = " r.currency_id = %s ";
$filter_params[] = $currency_id;
}
if ( !empty( $filters ) ) {
$filters = " WHERE " . implode( " AND ", $filters );
} else {
$filters = '';
}
$having = '';
// note double select to obtain number of rows (otherwise group counts are obtained)
$count = $wpdb->get_var( $wpdb->prepare(
"
SELECT COUNT(*) FROM (
SELECT r.affiliate_id
FROM $referrals_table r
LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id
LEFT JOIN $affiliates_users_table au ON a.affiliate_id = au.affiliate_id
LEFT JOIN $wpdb->users u on au.user_id = u.ID
$filters
GROUP BY r.affiliate_id, r.currency_id
$having
) tmp
",
$filter_params
) );
if ( $count > $row_count ) {
$paginate = true;
} else {
$paginate = false;
}
$pages = ceil ( $count / $row_count );
if ( $paged > $pages ) {
$paged = $pages;
}
if ( $paged != 0 ) {
$offset = ( $paged - 1 ) * $row_count;
}
$results = $wpdb->get_results( $wpdb->prepare(
"
SELECT a.*, u.user_login, SUM(r.amount) as total, r.currency_id
FROM $referrals_table r
LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id
LEFT JOIN $affiliates_users_table au ON a.affiliate_id = au.affiliate_id
LEFT JOIN $wpdb->users u on au.user_id = u.ID
$filters
GROUP BY r.affiliate_id, r.currency_id
$having
ORDER BY $orderby $order LIMIT $row_count OFFSET $offset
",
$filter_params
) );
$column_display_names = array(
'affiliate_id' => __( 'Id', AFFILIATES_PLUGIN_DOMAIN ),
'name' => __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ),
'email' => __( 'Email', AFFILIATES_PLUGIN_DOMAIN ),
'user_login' => __( 'Username', AFFILIATES_PLUGIN_DOMAIN ),
'total' => __( 'Total', AFFILIATES_PLUGIN_DOMAIN ),
'currency_id' => __( 'Currency', AFFILIATES_PLUGIN_DOMAIN )
);
$output .= '<div class="totals-overview">';
$mp_params = "";
if ( !empty( $from_date ) ) {
$mp_params .= "&from_date=" . urlencode( $from_date );
}
if ( !empty( $thru_date ) ) {
$mp_params .= "&thru_date=" . urlencode( $thru_date );
}
if ( !empty( $referral_status ) ) {
$mp_params .= "&referral_status=" . urlencode( $referral_status );
}
if ( !empty( $currency_id ) ) {
$mp_params .= "¤cy_id=" . urlencode( $currency_id );
}
if ( !empty( $orderby ) ) {
$mp_params .= "&orderby=" . urlencode( $orderby );
}
if ( !empty( $order ) ) {
$mp_params .= "&order=" . urlencode( $order );
}
$output .= '<style type="text/css">';
$output .= '.close-referrals img, .close-referrals span.label { vertical-align: middle; }';
$output .= '</style>';
$output .= '<div class="manage">';
$output .= '<p>';
$output .=
"<a title='" . __( 'Click to close these referrals', AFFILIATES_PLUGIN_DOMAIN ) . "' " .
"class='button close-referrals' " .
"href='" . esc_url( $current_url ) . "&action=close_referrals" . $mp_params . "'>" .
"<img class='icon' alt='" . __( 'Close referrals', AFFILIATES_PLUGIN_DOMAIN) . "' src='". AFFILIATES_PLUGIN_URL ."images/closed.png'/>" .
"<span class='label'>" . __( 'Close Referrals', AFFILIATES_PLUGIN_DOMAIN) . "</span>" .
"</a>";
$output .= "</p>";
$output .= '</div>';
$status_descriptions = array(
AFFILIATES_REFERRAL_STATUS_ACCEPTED => __( 'Accepted', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_CLOSED => __( 'Closed', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_PENDING => __( 'Pending', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_REJECTED => __( 'Rejected', AFFILIATES_PLUGIN_DOMAIN ),
);
$status_select = '<label class="referral-status-filter" for="referral_status">' . __('Referral Status', AFFILIATES_PLUGIN_DOMAIN ) . '</label>';
$status_select .= ' ';
$status_select .= '<select class="referral-status-filter" name="referral_status">';
$status_select .= '<option value="" ' . ( empty( $referral_status ) ? ' selected="selected" ' : '' ) . '>--</option>';
foreach ( $status_descriptions as $key => $label ) {
$selected = $key == $referral_status ? ' selected="selected" ' : '';
$status_select .= '<option ' . $selected . ' value="' . esc_attr( $key ) . '">' . $label . '</option>';
}
$status_select .= '</select>';
$currencies = $wpdb->get_results( "SELECT DISTINCT(currency_id) FROM $referrals_table WHERE currency_id IS NOT NULL" );
$currency_select = '<label class="currency-id-filter" for="currency_id">' . __( 'Currency', AFFILIATES_PLUGIN_DOMAIN ) . '</label>';
$currency_select .= ' ';
$currency_select .= '<select class="currency-id-filter" name="currency_id">';
$currency_select .= '<option value="" ' . ( empty( $currency_id ) ? ' selected="selected" ' : '' ) . '>--</option>';
foreach ( $currencies as $currency ) {
$selected = $currency->currency_id == $currency_id ? ' selected="selected" ' : '';
$currency_select .= '<option ' . $selected . ' value="' . esc_attr( $currency->currency_id ) . '">' . $currency->currency_id . '</option>';
}
$currency_select .= '</select>';
$output .=
'<div class="filters">' .
'<label class="description" for="setfilters">' . __( 'Filters', AFFILIATES_PLUGIN_DOMAIN ) . '</label>' .
'<form id="setfilters" action="" method="post">' .
'<p>' .
$status_select .
' ' .
$currency_select .
'</p>' .
'<p>' .
'<label class="from-date-filter" for="from_date">' . __( 'From', AFFILIATES_PLUGIN_DOMAIN ) . '</label>' .
'<input class="datefield from-date-filter" name="from_date" type="text" value="' . esc_attr( $from_date ) . '"/>'.
'<label class="thru-date-filter" for="thru_date">' . __( 'Until', AFFILIATES_PLUGIN_DOMAIN ) . '</label>' .
'<input class="datefield thru-date-filter" name="thru_date" type="text" class="datefield" value="' . esc_attr( $thru_date ) . '"/>'.
'</p>
<p>' .
wp_nonce_field( self::SET_FILTERS, self::NONCE, true, false ) .
'<input class="button" type="submit" value="' . __( 'Apply', AFFILIATES_PLUGIN_DOMAIN ) . '"/>' .
'<input class="button" type="submit" name="clear_filters" value="' . __( 'Clear', AFFILIATES_PLUGIN_DOMAIN ) . '"/>' .
'<input type="hidden" value="submitted" name="submitted"/>' .
'</p>' .
'</form>' .
'</div>';
$output .= '
<div class="page-options">
<form id="setrowcount" action="" method="post">
<div>
<label for="row_count">' . __('Results per page', AFFILIATES_PLUGIN_DOMAIN ) . '</label>' .
'<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" />
' . wp_nonce_field( self::SET_RPP, self::NONCE_1, true, false ) . '
<input class="button" type="submit" value="' . __( 'Apply', AFFILIATES_PLUGIN_DOMAIN ) . '"/>
</div>
</form>
</div>
';
if ( $paginate ) {
require_once( AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php' );
$pagination = new Affiliates_Pagination( $count, null, $row_count );
$output .= '<form id="posts-filter" method="post" action="">';
$output .= '<div>';
$output .= wp_nonce_field( self::SET_PAGE, self::NONCE_2, true, false );
$output .= '</div>';
$output .= '<div class="tablenav top">';
$output .= $pagination->pagination( 'top' );
$output .= '</div>';
$output .= '</form>';
}
$output .= '
<table class="wp-list-table widefat fixed" cellspacing="0">
<thead>
<tr>
';
foreach ( $column_display_names as $key => $column_display_name ) {
$options = array(
'orderby' => $key,
'order' => $switch_order
);
$class = $key;
if ( !in_array($key, array( 'edit', 'remove', 'links' ) ) ) {
if ( strcmp( $key, $orderby ) == 0 ) {
$lorder = strtolower( $order );
$class = "$key manage-column sorted $lorder";
} else {
$class = "$key manage-column sortable";
}
$column_display_name = '<a href="' . esc_url( add_query_arg( $options, $current_url ) ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
}
$output .= "<th scope='col' class='$class'>$column_display_name</th>";
}
$output .= '</tr>
</thead>
<tbody>
';
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
$name_suffix = '';
$class_deleted = '';
if ( $is_deleted = ( strcmp( $result->status, 'deleted' ) == 0 ) ) {
$class_deleted = ' deleted ';
$name_suffix .= " " . __( '(removed)', AFFILIATES_PLUGIN_DOMAIN );
}
$class_inoperative = '';
if ( $is_inoperative = ! ( ( $result->from_date <= $today ) && ( $result->thru_date == null || $result->thru_date >= $today ) ) ) {
$class_inoperative = ' inoperative ';
$name_suffix .= " " . __( '(inoperative)', AFFILIATES_PLUGIN_DOMAIN );
}
$output .= '<tr class="' . $class_deleted . $class_inoperative . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
$output .= "<td class='affiliate-id'>";
if ( affiliates_encode_affiliate_id( $result->affiliate_id ) != $result->affiliate_id ) {
$output .= '<span class="encoded-hint" title="' . affiliates_encode_affiliate_id( $result->affiliate_id ) . '">' . $result->affiliate_id . '</span>';
} else {
$output .= $result->affiliate_id;
}
$output .= "</td>";
$output .= "<td class='affiliate-name'>" . stripslashes( wp_filter_nohtml_kses( $result->name ) ) . $name_suffix . "</td>";
$output .= "<td class='affiliate-email'>" . $result->email . "</td>";
$output .= "<td class='affiliate-user-login'>" . $result->user_login . "</td>";
$output .= "<td class='total'>$result->total</td>";
$output .= "<td class='currency-id'>$result->currency_id</td>";
$output .= '</tr>';
}
} else {
$output .= '<tr><td colspan="' . count( $column_display_names ) . '">' . __( 'There are no results.', AFFILIATES_PLUGIN_DOMAIN ) . '</td></tr>';
}
$output .= '</tbody>';
$output .= '</table>';
if ( $paginate ) {
$pagination = new Affiliates_Pagination($count, null, $row_count);
$output .= '<div class="tablenav bottom">';
$output .= $pagination->pagination( 'bottom' );
$output .= '</div>';
}
$output .= '</div>'; // .totals-overview
$output .= '</div>'; // .totals
echo $output;
affiliates_footer();
}
public static function update_status( $new_status, $params = null ) {
global $wpdb;
$output = "";
$from_date = isset( $params['from_date'] ) ? $params['from_date'] : null;
$from_datetime = $from_date ? DateHelper::u2s( $from_date ) : null;
$thru_date = isset( $params['thru_date'] ) ? $params['thru_date'] : null;
$thru_datetime = $thru_date ? DateHelper::u2s( $thru_date, 24*3600 ) : null;
$referral_status = isset( $params['referral_status'] ) ? Affiliates_Utility::verify_referral_status_transition( $params['referral_status'], $params['referral_status'] ) : null;
$currency_id = isset( $params['currency_id'] ) ? Affiliates_Utility::verify_currency_id( $params['currency_id'] ) : null;
$orderby = isset( $params['orderby'] ) ? $params['orderby'] : null;
$order = isset( $params['order'] ) ? $params['order'] : null;
switch ( $orderby ) {
case 'affiliate_id' :
case 'name' :
case 'email' :
$orderby = 'a.' . $orderby;
break;
case 'user_login' :
$orderby = 'au.' . $orderby;
break;
case 'currency_id' :
$orderby = 'r.' . $orderby;
break;
default:
$orderby = 'a.name';
}
switch ( $order ) {
case 'asc' :
case 'ASC' :
case 'desc' :
case 'DESC' :
break;
default:
$order = 'ASC';
}
if ( isset( $params['tables'] ) ) {
$output .= "<h1>" . __( "Closing referrals", AFFILIATES_PLUGIN_DOMAIN ) . "</h1>";
$output .= "<div class='closing-referrals-overview'>";
$affiliates_table = $params['tables']['affiliates'];
$affiliates_users_table = $params['tables']['affiliates_users'];
$referrals_table = $params['tables']['referrals'];
$users_table = $params['tables']['users'];
$filters = array( " 1=%d " );
$filter_params = array( 1 );
if ( $from_datetime && $thru_datetime ) {
$filters[] = " r.datetime >= %s AND r.datetime < %s ";
$filter_params[] = $from_datetime;
$filter_params[] = $thru_datetime;
} else if ( $from_datetime ) {
$filters[] = " r.datetime >= %s ";
$filter_params[] = $from_datetime;
} else if ( $thru_datetime ) {
$filters[] = " r.datetime < %s ";
$filter_params[] = $thru_datetime;
}
if ( $referral_status ) {
$filters[] = " r.status = %s ";
$filter_params[] = $referral_status;
}
if ( $currency_id ) {
$filters[] = " r.currency_id = %s ";
$filter_params[] = $currency_id;
}
if ( !empty( $filters ) ) {
$filters = " WHERE " . implode( " AND ", $filters );
} else {
$filters = '';
}
$order_by = '';
if ( $orderby && $order ) {
$order_by .= " ORDER BY $orderby $order ";
}
$step = isset( $params['step'] ) ? intval( $params['step'] ) : 1;
switch ( $step ) {
case 1 :
$results = $wpdb->get_results( $wpdb->prepare(
"
SELECT a.*, r.*, u.user_login
FROM $referrals_table r
LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id
LEFT JOIN $affiliates_users_table au ON a.affiliate_id = au.affiliate_id
LEFT JOIN $users_table u on au.user_id = u.ID
$filters
$order_by
",
$filter_params
) );
$output .= "<div class='manage'>";
$output .= "<div class='warning'>";
$output .= "<p>";
$output .= "<strong>";
$output .= __( "Please review the list of referrals that will be <em>closed</em>.", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</strong>";
$output .= "</p>";
$output .= "</div>"; // .warning
$output .= "<p>";
$output .= __( "Usually only referrals that are <em>accepted</em> and have been paid out should be <em>closed</em>. If there are unwanted or too many referrals shown, restrict your filter settings.", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</p>";
$output .= "<p>";
$output .= __( "If these referrals can be closed, click the confirmation button below.", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</p>";
$output .= "</div>";
$output .= '<div id="referrals-overview" class="referrals-overview">';
$output .= self::render_results( $results );
$output .= '</div>'; // .referrals-overview
if ( count( $results > 0 ) ) {
$mp_params = "";
if ( !empty( $from_date ) ) {
$mp_params .= "&from_date=" . urlencode( $from_date );
}
if ( !empty( $thru_date ) ) {
$mp_params .= "&thru_date=" . urlencode( $thru_date );
}
if ( !empty( $referral_status ) ) {
$mp_params .= "&referral_status=" . urlencode( $referral_status );
}
if ( !empty( $currency_id ) ) {
$mp_params .= "¤cy_id=" . urlencode( $currency_id );
}
if ( !empty( $orderby ) ) {
$mp_params .= "&orderby=" . urlencode( $orderby );
}
if ( !empty( $order ) ) {
$mp_params .= "&order=" . urlencode( $order );
}
$output .= '<div class="manage confirm">';
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$current_url = remove_query_arg( 'action', $current_url );
$current_url = remove_query_arg( 'affiliate_id', $current_url );
$output .= '<style type="text/css">';
$output .= '.close-referrals img, .close-referrals .label { vertical-align: middle; }';
$output .= '</style>';
$output .= "<p>";
$output .= __( "Close these referrals by clicking:", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</p>";
$output .=
"<a title='" . __( 'Click to close these referrals', AFFILIATES_PLUGIN_DOMAIN ) . "' " .
"class='close-referrals button' " .
"href='" . esc_url( $current_url ) . "&action=close_referrals&step=2" . $mp_params . "'>" .
"<img class='icon' alt='" . __( 'Close referrals', AFFILIATES_PLUGIN_DOMAIN) . "' src='". AFFILIATES_PLUGIN_URL ."images/closed.png'/>" .
"<span class='label'>" . __( 'Close Referrals', AFFILIATES_PLUGIN_DOMAIN) . "</span>" .
"</a>";
$output .= "<div class='warning'>";
$output .= "<p>";
$output .= "<strong>";
$output .= __( "This action can not be undone*.", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</strong>";
$output .= "</p>";
$output .= "<p>";
$output .= "<span style='font-size:0.8em;'>";
$output .= __( "*To undo, each referral would have to be set to the desired status individually.", AFFILIATES_PLUGIN_DOMAIN );
$output .= "</span>";
$output .= "</p>";
$output .= "</div>"; // .warning
$output .= '</div>'; // .manage.confirm
}
break; // step 1 - ask for confirmation confirmation
case 2 :
// try to make the changes
$results = $wpdb->get_results( $wpdb->prepare(
"
SELECT a.*, r.*, u.user_login
FROM $referrals_table r
LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id
LEFT JOIN $affiliates_users_table au ON a.affiliate_id = au.affiliate_id
LEFT JOIN $users_table u on au.user_id = u.ID
$filters
$order_by
",
$filter_params
) );
$updated = array();
$omitted = array();
$failed = array();
foreach ( $results as $result ) {
if ( $s = Affiliates_Utility::verify_referral_status_transition( $result->status, $new_status ) ) {
if ( $wpdb->query( $wpdb->prepare(
"UPDATE $referrals_table SET status = %s WHERE affiliate_id = %d AND post_id = %d AND datetime = %s ",
$s,
$result->affiliate_id,
$result->post_id,
$result->datetime
) ) ) {
$result->status = $s;
$updated[] = $result;
} else {
$failed[] = $result;
}
} else {
$omitted[] = $result;
}
}
// always show at least the updated table because this will
// also give information if no results have been updated
$status_descriptions = array(
AFFILIATES_REFERRAL_STATUS_ACCEPTED => __( 'Accepted', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_CLOSED => __( 'Closed', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_PENDING => __( 'Pending', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_REJECTED => __( 'Rejected', AFFILIATES_PLUGIN_DOMAIN ),
);
$output .= "<h2>" . __( "Updated", AFFILIATES_PLUGIN_DOMAIN ) . "</h2>";
$output .= "<p>";
$output .= sprintf( __( "These referrals have been updated to <em>%s</em>.", AFFILIATES_PLUGIN_DOMAIN ), ( isset( $status_descriptions[$new_status] ) ? $status_descriptions[$new_status] : $new_status ) );
$output .= "</p>";
$output .= self::render_results( $updated );
if ( count( $omitted ) > 0 ) {
$output .= "<h2>" . __( "Omitted", AFFILIATES_PLUGIN_DOMAIN ) . "</h2>";
$output .= "<p>";
$output .= sprintf( __( "These referrals have been omitted because their status must not be changed to <em>%s</em>.", AFFILIATES_PLUGIN_DOMAIN ), ( isset( $status_descriptions[$new_status] ) ? $status_descriptions[$new_status] : $new_status ) );
$output .= "</p>";
$output .= self::render_results( $omitted );
}
if ( count( $failed ) > 0 ) {
$output .= "<h2>" . __( "Failed", AFFILIATES_PLUGIN_DOMAIN ) . "</h2>";
$output .= "<p>";
$output .= sprintf( __( "These referrals could not be updated to <em>%s</em>.", AFFILIATES_PLUGIN_DOMAIN ), ( isset( $status_descriptions[$new_status] ) ? $status_descriptions[$new_status] : $new_status ) );
$output .= "</p>";
$output .= self::render_results( $failed );
}
break; // step 2 -commit changes
}
$output .= "</div>";// .closing-referrals-overview
}
return $output;
}
public static function render_results( $results ) {
$output = "";
$column_display_names = array(
'datetime' => __( 'Date', AFFILIATES_PLUGIN_DOMAIN ),
'post_title' => __( 'Post', AFFILIATES_PLUGIN_DOMAIN ),
'name' => __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ),
'amount' => __( 'Amount', AFFILIATES_PLUGIN_DOMAIN ),
'currency_id' => __( 'Currency', AFFILIATES_PLUGIN_DOMAIN ),
'status' => __( 'Status', AFFILIATES_PLUGIN_DOMAIN )
);
$status_descriptions = array(
AFFILIATES_REFERRAL_STATUS_ACCEPTED => __( 'Accepted', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_CLOSED => __( 'Closed', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_PENDING => __( 'Pending', AFFILIATES_PLUGIN_DOMAIN ),
AFFILIATES_REFERRAL_STATUS_REJECTED => __( 'Rejected', AFFILIATES_PLUGIN_DOMAIN ),
);
$status_icons = array(
AFFILIATES_REFERRAL_STATUS_ACCEPTED => "<img class='icon' alt='" . __( 'Accepted', AFFILIATES_PLUGIN_DOMAIN) . "' src='" . AFFILIATES_PLUGIN_URL . "images/accepted.png'/>",
AFFILIATES_REFERRAL_STATUS_CLOSED => "<img class='icon' alt='" . __( 'Closed', AFFILIATES_PLUGIN_DOMAIN) . "' src='" . AFFILIATES_PLUGIN_URL . "images/closed.png'/>",
AFFILIATES_REFERRAL_STATUS_PENDING => "<img class='icon' alt='" . __( 'Pending', AFFILIATES_PLUGIN_DOMAIN) . "' src='" . AFFILIATES_PLUGIN_URL . "images/pending.png'/>",
AFFILIATES_REFERRAL_STATUS_REJECTED => "<img class='icon' alt='" . __( 'Rejected', AFFILIATES_PLUGIN_DOMAIN) . "' src='" . AFFILIATES_PLUGIN_URL . "images/rejected.png'/>",
);
$output .= '<table id="referrals" class="referrals wp-list-table widefat fixed" cellspacing="0">';
$output .= "<thead>";
$output .= "<tr>";
foreach ( $column_display_names as $key => $column_display_name ) {
$output .= "<th scope='col'>$column_display_name</th>";
}
$output .= "</tr>";
$output .= "</thead>";
$output .= "<tbody>";
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
$output .= '<tr class="details-referrals ' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
$output .= '<td class="datetime">' . DateHelper::s2u( $result->datetime ) . '</td>';
$title = get_the_title( $result->post_id );
$output .= '<td class="post_title">' . wp_filter_nohtml_kses( $title ) . '</td>';
$output .= "<td class='name'>" . stripslashes( wp_filter_nohtml_kses( $result->name ) ) . "</td>";
$output .= "<td class='amount'>" . stripslashes( wp_filter_nohtml_kses( $result->amount ) ) . "</td>";
$output .= "<td class='currency_id'>" . stripslashes( wp_filter_nohtml_kses( $result->currency_id ) ) . "</td>";
$output .= "<td class='status'>";
$output .= isset( $status_icons[$result->status] ) ? $status_icons[$result->status] : '';
$output .= isset( $status_descriptions[$result->status] ) ? $status_descriptions[$result->status] : '';
$output .= "</td>";
$output .= '</tr>';
}
} else {
$output .= '<tr><td colspan="' . count( $column_display_names ) . '">' . __('There are no results.', AFFILIATES_PLUGIN_DOMAIN ) . '</td></tr>';
}
$output .= '</tbody>';
$output .= '</table>';
return $output;
}
}
} // if