/
ttr-db-admin-tbl.php
171 lines (146 loc) · 4.24 KB
/
ttr-db-admin-tbl.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
<?php
// Add WP_List_Table
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
// Admin Tutor Table
class Tutor_list extends WP_List_Table {
public function __construct() {
parent::__construct(array(
'singular' => __("Tutor", "ttr-db"),
'plural' => __("Tutors", "ttr-db"),
'ajax' => false
));
}
/* DISPLAY METHODS */
// Empty Table Message
public function no_items() {
_e("No Tutors Found", "ttr-db");
}
// Default Column Display Method
public function column_default($item, $column_name) {
return stripslashes($item[$column_name]);
}
// Name Column Method
public function column_name($item) {
$name = stripslashes($item['lname']." ".$item['fname']." ".$item['mname']);
// Remove Item
$url_del = sprintf("?page=%s&action=%s&id=%s", esc_attr($_REQUEST['page']), 'del-itm', $item['id']);
$url_del = wp_nonce_url($url_del, 'del-tutor_'.$item['id']);
$actions = array(
"edit" => sprintf("<a href=\"?page=%s&type=%s&id=%s\">".__("Edit", "ttr-db")."</a>", esc_attr($_REQUEST['page']), 'edit', $item['id']),
"delete" => "<a href=\"{$url_del}\">".__("Delete","ttr-db")."</a>"
);
return $name.$this->row_actions($actions);
}
// Age Column Method
public function column_age($item) {
return $item['min_age']." - ".$item['max_age'];
}
// Checkbox Column Method
public function column_cb($item) {
return sprintf("<input type=\"checkbox\" name=\"bulk-sel[]\" value=\"%s\">", $item['id']);
}
// @returns column slugs and titles
function get_columns() {
$columns = array(
'cb' => "<input type=\"checkbox\" >",
'name' => __("Name", "ttr-db"),
'age' => __("Age", "ttr-db"),
'province' => __("Province","ttr-db"),
'city' => __("City","ttr-db"),
'subject' => __("Subject", "ttr-db")
);
return $columns;
}
// @returns row actions array
public function get_bulk_actions() {
$act = array(
'bulk-delete' => __('Delete','ttr-db')
);
return $act;
}
// Prosses Requests
public function process_bulk_action () {
$post_action = $this->current_action();
switch ($post_action) {
case "add":
$nonce = esc_attr($_REQUEST['_wpnonce']);
if (!wp_verify_nonce($nonce, "add-new-tutor")) {
die("Failed Security Check");
} else {
TTR_db::add_tutor(
$_POST['fname'],
$_POST['mname'],
$_POST['lname'],
$_POST['min_age'],
$_POST['max_age'],
//$_POST['address'],
$_POST['province'],
$_POST['city'],
TTR_db::merge_subjects($_POST['subject']),
$_POST['descp']
);
}
break;
case "upt":
$nonce = esc_attr($_REQUEST['_wpnonce']);
if (!wp_verify_nonce($nonce, "edit-tutor-data_".$_POST['id'])) {
die("Failed Security Check");
} else {
TTR_db::upt_tutor(
$_POST['id'],
$_POST['fname'],
$_POST['mname'],
$_POST['lname'],
$_POST['min_age'],
$_POST['max_age'],
//$_POST['address'],
$_POST['province'],
$_POST['city'],
TTR_db::merge_subjects($_POST['subject']),
$_POST['descp']
);
}
break;
case "del-itm":
$nonce = esc_attr($_REQUEST['_wpnonce']);
if (!wp_verify_nonce($nonce, 'del-tutor_'.$_GET['id'])) {
die("Failed Security Check");
} else {
TTR_db::rm_tutor($_GET['id']);
}
break;
case "import_csv":
echo get_attached_file($_POST['f_id']);
break;
case "bulk-delete":
$del_ids = esc_sql($_POST['bulk-sel']);
foreach ($del_ids as $i) {
TTR_db::rm_tutor($i);
}
break;
}
}
public function prepare_items() {
/* Generate Headers */
$this->_column_headers = array(
$this->get_columns(), // (Array) Column Slugs and Titles
array(), // (Array) Hidden Fields
array(), // (Array) Sortable Columns
'fname' // (String) Slug of column which displays actions (edit, view, etc.)
);
// Write Bulk Action Prossesing
$this->process_bulk_action();
// Sets Pagination Data
$per_page = 10; //TODO make changable
$cur_page = $this->get_pagenum();
$this->set_pagination_args(array(
'total_items' => TTR_db::get_count(),
'per_page' => $per_page
));
// Sets Tutors For Database
$this->items = TTR_db::get_tutors($per_page, $cur_page);
}
}
?>