forked from interspace-vn/accesstrade-coupon-pro
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coupons_list.php
274 lines (245 loc) · 8.06 KB
/
coupons_list.php
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
<?php
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Nhymxu_AT_Coupon_List extends WP_List_Table
{
public $active_filter = '';
public $filters = [];
public $search_key = '';
/**
* Prepare the items for the table to process
*
* @return Void
*/
public function prepare_items()
{
global $wpdb;
$this->search_key = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
// Get filters data
$results = $wpdb->get_results( "SELECT type FROM {$wpdb->prefix}coupons GROUP BY type" );
if( !empty( $results ) ) {
foreach( $results as $row ) {
$this->filters[] = $row->type;
}
}
if ( isset( $_REQUEST['filter_merchant'] ) && in_array( $_REQUEST['filter_merchant'], $this->filters ) ) {
$this->active_filter = $_REQUEST['filter_merchant'];
} else {
$this->active_filter = '';
}
// END Get filters data
$perPage = 50;
$currentPage = $this->get_pagenum();
$data = $this->table_data( $perPage, $currentPage );
$totalItems = $this->get_number_of_records();
$this->set_pagination_args( [
'total_items' => $totalItems,
'per_page' => $perPage
] );
$this->_column_headers = [$columns, $hidden, $sortable];
$this->items = $data;
}
private function get_number_of_records() {
global $wpdb;
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}coupons";
if( $this->active_filter != '' ) {
$sql .= ' WHERE type = "'. $_REQUEST['filter_merchant'] .'"';
}
if( $this->search_key != '' ) {
if( $this->active_filter == '' ) {
$sql .= ' WHERE';
} else {
$sql .= ' AND';
}
$sql .= ' (title LIKE "%'. $this->search_key .'%" OR code LIKE "%'. $this->search_key .'%")';
}
return $wpdb->get_var( $sql );
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = [
'cb' => '<input type="checkbox">',
'title' => 'Tiêu đề',
'type' => 'Merchant',
'code' => 'Mã giảm giá',
'exp' => 'Ngày hết hạn',
'note' => 'Ghi chú',
'save' => 'Giảm'
];
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return [];
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return [
'title' => ['title', false],
'type' => ['type', false],
'exp' => ['exp', false],
];
}
/**
* Get the table data
*
* @return Array
*/
private function table_data( $per_page = 50, $page_number = 1 )
{
global $wpdb;
$sql = "SELECT id, title, type, code, exp, note, save FROM {$wpdb->prefix}coupons";
if( $this->active_filter != '' ) {
$sql .= ' WHERE type = "'. $this->active_filter .'"';
}
if( $this->search_key != '' ) {
if( $this->active_filter == '' ) {
$sql .= ' WHERE';
} else {
$sql .= ' AND';
}
$sql .= ' (title LIKE "%'. $this->search_key .'%" OR code LIKE "%'. $this->search_key .'%")';
}
if ( !empty( $_REQUEST['orderby'] ) ) {
$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
}
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$results = $wpdb->get_results( $sql, ARRAY_A );
return $results;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
switch( $column_name ) {
case 'title':
case 'code':
case 'type':
case 'exp':
case 'note':
case 'save':
return $item[ $column_name ];
default:
return print_r( $item, true ) ;
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b )
{
// Set defaults
$orderby = 'title';
$order = 'asc';
// If orderby is set, use this as the sort column
if(!empty($_GET['orderby']))
{
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if(!empty($_GET['order']))
{
$order = $_GET['order'];
}
$result = strcmp( $a[$orderby], $b[$orderby] );
if($order === 'asc')
{
return $result;
}
return -$result;
}
/**
* Allow filter per merchant
*/
function extra_tablenav( $which ) {
?>
<?php if( 'bottom' == $which ): ?>
<div class="alignleft nhymxu-bulk_delete">
<button class="button" id="nhymxu-bulk_delete">Xóa nhiều</button>
</div>
<?php endif; ?>
<div class="alignleft actions">
<?php if ( 'top' == $which && !empty( $this->filters ) ): ?>
<select id="filter_merchant" name="filter_merchant">
<option value="">Tất cả merchant</option>
<?php foreach ( $this->filters as $merchant ): ?>
<option value="<?=esc_attr( $merchant );?>" <?=( $this->active_filter == $merchant ) ? 'selected' : '' ;?>><?=esc_attr( $merchant );?></option>
<?php endforeach; ?>
</select>
<input id="btn-filter" type="submit" class="button" value="Lọc">
<?php endif; ?>
</div>
<?php
}
/**
* Get value for checkbox column.
*
* @param object $item A row's data.
* @return string Text to be placed inside the column <td>.
*/
protected function column_cb( $item ) {
$output = '<label class="screen-reader-text" for="coupon_' . $item['id'] . '">Chọn' . $item['title'] . '</label>';
$output .= '<input type="checkbox" class="input_coupon_bulk_action" name="coupons[]" id="coupon_'. $item['id'] .'" value="'. $item['id'] .'">';
return $output;
}
public function get_bulk_actions() {
return [];
/*
* on hitting apply in bulk actions the url paramas are set as
* ?action=bulk-delete&paged=1&action2=-1
*
* action and action2 are set based on the triggers above and below the table
*/
$actions = ['bulk-delete' => 'Xóa coupon'];
return $actions;
}
/*
* Method for rendering the title column.
* Adds row action links to the title column.
* e.g. url/admin.php?page=accesstrade_coupon_addnew&coupon_id=1
*/
protected function column_title( $item ) {
$admin_page_url = admin_url('admin.php');
// row action to view usermeta.
$query_args_editcoupon = array(
'page' => wp_unslash( 'accesstrade_coupon_addnew' ),
'coupon_id' => absint( $item['id']),
);
$editcoupon_link = esc_url( add_query_arg( $query_args_editcoupon, $admin_page_url ) );
$actions['edit_coupon'] = '<a href="' . $editcoupon_link . '">Sửa</a>';
$actions['delete_coupon'] = '<a href="javascript:void(0);" onclick="nhymxu_delete_coupon(\''. $item['id'] .'\', \''. $item['code'] .'\');">Xóa</a>';
// similarly add row actions for add usermeta.
//$_GET$row_value = '<strong>' . $item['title'] . '</strong>';
$row_value = $item['title'];
return $row_value . $this->row_actions( $actions );
}
}