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
|
<?php include __DIR__ . "/../config/systemConfig.php"; include __DIR__ . "/base_request.php"; require_once __DIR__ . '/database.php'; $iniPath = __DIR__ . '/../config/database_config.ini'; if (!file_exists($iniPath)) { $response_body = array( "status" => 'error', "data" => "", "warning" => "", "error" => array("code" => "not_found_ini", "exception" => "Không tìm thấy file ini") ); $json = json_encode($response_body); die($json); }
$ini = parse_ini_file($iniPath); $table_prefix = $ini['table_prefix']; $table_terms = "`$table_prefix" . "terms`"; $table_term_taxonomy = "`$table_prefix" . "term_taxonomy`"; $table_term_relationships = "`$table_prefix" . "term_relationships`"; $table_termmeta = "`$table_prefix" . "termmeta`";
class get_data extends base_request { function __construct() { $this->database = new database(); }
function getTerm($page, $perpage, $taxonomy) { global $table_terms; $table_term_taxonomy = $GLOBALS['table_term_taxonomy']; $offset = $page * $perpage;
$res = array('data' => array()); $query = "SELECT T.term_id, T.name FROM $table_terms as T" . " INNER JOIN $table_term_taxonomy as TX ON T.term_id = TX.term_id" . " WHERE TX.taxonomy= '$taxonomy' LIMIT $offset, $perpage;";
// $dbContext = $GLOBALS['dbContext']; $dbContext = $GLOBALS['dbContext'];
if ($result = $dbContext->query($query)) { $res_array = $result; //->fetch_array(); if ($res_array) { foreach ($res_array as $item) { //$res['data'][] = $item;//array('id'=>$item, 'name'=>$item[1]); $res['data'][] = array('id' => $item['term_id'], 'name' => $item['name']); } } else $res = null; $result->free_result(); $this->success($res); } else { $this->error_db($dbContext, 'getTerm', $query); } }
function getCategory($page, $perpage) { $this->getTerm($page, $perpage, 'product_cat'); }
function getTags($page, $perpage) { $this->getTerm($page, $perpage, 'product_tag'); }
function get_attribute($page, $perpage) { global $table_woo_attribute_taxonomie; $offset = $page * $perpage; $query = "SELECT SQL_CALC_FOUND_ROWS attribute_id, attribute_name, attribute_label FROM $table_woo_attribute_taxonomie LIMIT $offset, $perpage;"; // $dbContext = $GLOBALS['dbContext']; $dbContext = $GLOBALS['dbContext']; if ($result = $dbContext->query($query)) { $res_array = $result; //->fetch_array(); $total_rows = $this->database->run_sql_get_single_col("SELECT FOUND_ROWS();"); $res['total'] = $total_rows; if ($res_array) { foreach ($res_array as $item) { $res['attributes'][] = array('id' => $item['attribute_id'], 'label' => $item['attribute_label'], 'slug' => $item['attribute_name']); } } else $res = null; $result->free_result(); $this->success($res); } else { $this->error_db($dbContext, 'get_attribute', $query); } }
function get_attribute_term($id_attribute, $page, $perpage) { global $table_woo_attribute_taxonomie; global $table_terms; $table_term_taxonomy = $GLOBALS['table_term_taxonomy']; $query = "SELECT attribute_name FROM $table_woo_attribute_taxonomie WHERE attribute_id = $id_attribute;"; $attribute_name = 'pa_' . $this->database->run_sql_get_single_col($query); if ($attribute_name != 'pa_') { $offset = $page * $perpage; $query = "SELECT SQL_CALC_FOUND_ROWS t.name, t.slug FROM $table_terms as t INNER JOIN $table_term_taxonomy as tx ON t.term_id = tx.term_id WHERE tx.taxonomy = '$attribute_name' LIMIT $offset, $perpage;"; // $dbContext = $GLOBALS['dbContext']; $dbContext = $GLOBALS['dbContext']; if ($result = $dbContext->query($query)) { $res_array = $result; //->fetch_array(); $total_rows = $this->database->run_sql_get_single_col("SELECT FOUND_ROWS();"); $res['total'] = $total_rows; if ($res_array) { foreach ($res_array as $item) { $res['terms'][] = array('name' => $item['name'], 'slug' => $item['slug']); } } else $res = null; $result->free_result(); $this->success($res); } else { $this->error_db($dbContext, 'get_attribute_term', $query); } } else { $this->success(array()); } }
function run_sql_get_single_col($dbContext, $query) { if ($result = $dbContext->query($query)) { $res_array = $result->fetch_array(); if ($res_array) $res = $res_array[0]; else $res = null; $result->free_result(); return $res; } else { $this->error_db($dbContext, 'run_sql_get_single_col', $query); } }
function run_sql($dbContext, $query) { if (!$dbContext->query($query)) { $this->error_db($dbContext, 'run_sql', $query); } } }
$get_data = new get_data();
if (isset($_GET['action']) && isset($_GET['page']) && isset($_GET['perpage'])) { $method = $_SERVER['REQUEST_METHOD']; $action = $_GET['action']; $page = $_GET['page']; $perpage = $_GET['perpage'];
switch ($action) { case 'get_category': $get_data->getCategory($page, $perpage); break; case 'get_tag': $get_data->getTags($page, $perpage); break; case 'get_attribute': $get_data->get_attribute($page, $perpage); break; case 'get_attribute_term': $id_attribute = $_GET['id'] ?? 0; $get_data->get_attribute_term($id_attribute, $page, $perpage); break; }
// if ($action == 'get_category') { // $get_data->getCategory($page, $perpage); // } } else { $get_data->error($get_data->create_error('not_found_param', 'Thieu param: action, page, perpage (bat dau tu 0)')); }
|