forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootGrid.php
97 lines (84 loc) · 3.6 KB
/
bootGrid.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
<?php
class BootGrid {
static function getSqlFromPost($searchFieldsNames = array(), $keyPrefix = "", $alternativeOrderBy = "", $doNotSearch=false, $FIND_IN_SET = "") {
if(empty($doNotSearch)){
$sql = self::getSqlSearchFromPost($searchFieldsNames);
}else{
$sql = "";
}
if(empty($_POST['sort']) && !empty($_GET['order'][0]['dir'])){
$index = intval($_GET['order'][0]['column']);
$_GET['columns'][$index]['data'];
$_POST['sort'][$_GET['columns'][$index]['data']] = $_GET['order'][0]['dir'];
}
if(!empty($FIND_IN_SET)){
$sql .= " ORDER BY FIND_IN_SET({$FIND_IN_SET}) DESC ";
}else if (!empty($_POST['sort'])) {
$orderBy = array();
foreach ($_POST['sort'] as $key => $value) {
$direction = "ASC";
if(strtoupper($value)==="DESC"){
$direction = "DESC";
}
$key = preg_replace("/[^A-Za-z0-9._ ]/", '', $key);
if($key=='order'){
$key = '`order`';
}
$orderBy[] = " {$keyPrefix}{$key} {$direction} ";
}
$sql .= " ORDER BY ".implode(",", $orderBy);
} else {
$sql .= $alternativeOrderBy;
}
if (empty($_POST['rowCount']) && !empty($_GET['length'])) {
$_POST['rowCount'] = intval($_GET['length']);
}
if (empty($_POST['current']) && !empty($_GET['start'])) {
$_POST['current'] = ($_GET['start'] / $_GET['length']) + 1;
} else if (empty($_POST['current']) && isset($_GET['start'])) {
$_POST['current'] = 1;
}
$_POST['current'] = intval(@$_POST['current']);
$_POST['rowCount'] = intval(@$_POST['rowCount']);
if(!empty($_POST['rowCount']) && $_POST['rowCount']>0){
if(empty($_POST['current'])){
$_POST['current'] = 1;
}
$_POST['rowCount'] = intval($_POST['rowCount']);
$_POST['current'] = intval($_POST['current']);
$current = intval(($_POST['current']-1)*$_POST['rowCount']);
$current = $current<0?0:$current;
$sql .= " LIMIT $current, {$_POST['rowCount']} ";
}else{
$_POST['current'] = 0;
$_POST['rowCount'] = 0;
}
return $sql;
}
static function getSqlSearchFromPost($searchFieldsNames = array(), $connection = "AND") {
$sql = "";
if (!empty($_GET['searchPhrase'])) {
$_POST['searchPhrase'] = $_GET['searchPhrase'];
} else if (!empty($_GET['search']['value'])) {
$_POST['searchPhrase'] = $_GET['search']['value'];
}else if (!empty($_GET['q'])) {
$_POST['searchPhrase'] = $_GET['q'];
}
if(!empty($_POST['searchPhrase'])){
global $global;
$search = $global['mysqli']->real_escape_string(xss_esc($_POST['searchPhrase']));
$like = array();
foreach ($searchFieldsNames as $value) {
$like[] = " {$value} LIKE '%{$search}%' ";
// for accent insensitive
$like[] = " CONVERT(CAST({$value} as BINARY) USING utf8) LIKE '%{$search}%' ";
}
if(!empty($like)){
$sql .= " {$connection} (". implode(" OR ", $like).")";
}else{
$sql .= " {$connection} 1=1 ";
}
}
return $sql;
}
}