1
1
<?php
2
2
namespace DataTables \Adapters ;
3
+
3
4
use Phalcon \Paginator \Adapter \QueryBuilder as PQueryBuilder ;
4
5
5
- class QueryBuilder extends AdapterInterface{
6
+ class QueryBuilder extends AdapterInterface {
7
+ /**
8
+ * @var \Phalcon\Mvc\Model\Query\BuilderInterface
9
+ */
6
10
protected $ builder ;
7
11
12
+ /**
13
+ * @var array
14
+ */
15
+ protected $ typeMapping
16
+ = [
17
+ "equal " => array (
18
+ "clause " => "%s = %s " ,
19
+ "value " => "%s " ,
20
+ ),
21
+ "like " => array (
22
+ "clause " => "%s LIKE %s " ,
23
+ "value " => "%s " ,
24
+ ),
25
+ "greater " => array (
26
+ "clause " => "%s > %s " ,
27
+ "value " => "%s " ,
28
+ ),
29
+ "greater_equal " => array (
30
+ "clause " => "%s >= %s " ,
31
+ "value " => "%s "
32
+ ),
33
+ "lower " => array (
34
+ "clause " => "%s < %s " ,
35
+ "value " => "%s "
36
+ ),
37
+ "lower_equal " => array (
38
+ "clause " => "%s <= %s " ,
39
+ "value " => "%s "
40
+ ),
41
+ "not_equal " => array (
42
+ "clause " => "%s != %s " ,
43
+ "value " => "%s "
44
+ ),
45
+ "not_like " => array (
46
+ "clause " => "%s NOT LIKE %s " ,
47
+ "value " => "%s "
48
+ ),
49
+ "in " => array (
50
+ "clause " => " %s IN (%s) " ,
51
+ "value " => "%s "
52
+ ),
53
+ "not_in " => array (
54
+ "clause " => "%s NOT IN (%s) " ,
55
+ "value " => "%s "
56
+ ),
57
+ "between " => array (
58
+ "clause " => "%s BETWEEN %s AND %s " ,
59
+ "value " => "%s "
60
+ ),
61
+ "not_between " => array (
62
+ "clause " => "%s NOT BETWEEN %s AND %s " ,
63
+ "value " => "%s "
64
+ ),
65
+ "regex " => array (
66
+ "clause " => "%s REGEX %s " ,
67
+ "value " => "%s "
68
+ ),
69
+ "not_regex " => array (
70
+ "clause " => "%s NOT REGEX %s " ,
71
+ "value " => "%s "
72
+ ),
73
+ ];
74
+
8
75
public function setBuilder ($ builder ) {
9
76
$ this ->builder = $ builder ;
10
77
}
11
78
12
79
public function getResponse () {
13
- $ builder = new PQueryBuilder ([
14
- 'builder ' => $ this ->builder ,
15
- 'limit ' => 1 ,
16
- 'page ' => 1 ,
17
- ]);
80
+ $ builder = new PQueryBuilder (
81
+ [
82
+ 'builder ' => $ this ->builder ,
83
+ 'limit ' => 1 ,
84
+ 'page ' => 1 ,
85
+ ]
86
+ );
18
87
19
88
$ total = $ builder ->getPaginate ();
20
89
21
- $ this ->bind ('global_search ' , function ($ column , $ search ) {
90
+ $ this ->bind (
91
+ 'global_search ' , function ($ column , $ search ) {
22
92
$ this ->builder ->orWhere ("{$ column } LIKE ?0 " , ["% {$ search }% " ]);
23
- });
93
+ }
94
+ );
24
95
25
- $ this ->bind ('column_search ' , function ($ column , $ search ) {
96
+ $ this ->bind (
97
+ 'column_search ' , function ($ column , $ search ) {
26
98
$ this ->builder ->andWhere ("{$ column } LIKE :key_ {$ column }: " , ["key_ {$ column }" => "% {$ search }% " ]);
27
- });
28
-
99
+ }
100
+ );
29
101
30
- $ this ->bind ('external_search ' , function ($ column , $ type , $ search ) {
31
- $ this ->builder ->andWhere ("{$ column } LIKE :key_ {$ column }: " , ["key_ {$ column }" => "% {$ search }% " ]);
32
- });
102
+ $ this ->bind (
103
+ 'external_search ' , function ($ column , $ type , $ search ) {
33
104
105
+ $ type = mb_strtolower ($ type );
106
+ $ search = str_replace ('* ' , '% ' , $ search );
107
+ if (array_key_exists ($ type , $ this ->typeMapping )) {
108
+ $ clause = sprintf ($ this ->typeMapping [$ type ]['clause ' ], $ column , ':key_ ' . $ column . ": " );
109
+ $ search = sprintf ($ this ->typeMapping [$ type ]['value ' ], $ search );
110
+ } else {
111
+ throw new \Exception ('no valid search type ' . $ type );
112
+ }
113
+ $ this ->builder ->andWhere ($ clause , ["key_ {$ column }" => $ search ]);
114
+ }
115
+ );
34
116
35
- $ this ->bind ('order ' , function ($ order ) {
117
+ $ this ->bind (
118
+ 'order ' , function ($ order ) {
36
119
$ this ->builder ->orderBy (implode (', ' , $ order ));
37
- });
120
+ }
121
+ );
38
122
39
- $ builder = new PQueryBuilder ([
40
- 'builder ' => $ this ->builder ,
41
- 'limit ' => $ this ->parser ->getLimit (),
42
- 'page ' => $ this ->parser ->getPage (),
43
- ]);
123
+ $ builder = new PQueryBuilder (
124
+ [
125
+ 'builder ' => $ this ->builder ,
126
+ 'limit ' => $ this ->parser ->getLimit (),
127
+ 'page ' => $ this ->parser ->getPage (),
128
+ ]
129
+ );
44
130
45
131
$ filtered = $ builder ->getPaginate ();
46
-
47
- return $ this ->formResponse ([
48
- 'total ' => $ total ->total_items ,
49
- 'filtered ' => $ filtered ->total_items ,
50
- 'data ' => $ filtered ->items ->toArray (),
51
- ]);
132
+
133
+ return $ this ->formResponse (
134
+ [
135
+ 'total ' => $ total ->total_items ,
136
+ 'filtered ' => $ filtered ->total_items ,
137
+ 'data ' => $ filtered ->items ->toArray (),
138
+ 'phql ' => $ this ->builder ->getPhql ()
139
+ ]
140
+ );
52
141
}
142
+
53
143
}
0 commit comments