1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\DataTables; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\Database\Order as DatabaseOrder; |
5
|
|
|
|
6
|
|
|
abstract class AbstractDataTablesDatabase implements \WebServCo\Framework\Interfaces\DataTablesInterface |
7
|
|
|
{ |
8
|
|
|
protected $db; |
9
|
|
|
|
10
|
|
|
abstract protected function getQuery($searchQueryPart, $orderQueryPart, $limitQuery); |
11
|
|
|
abstract protected function getRecordsTotalQuery(); |
12
|
|
|
|
13
|
|
|
public function __construct(\WebServCo\Framework\Interfaces\DatabaseInterface $db) |
14
|
|
|
{ |
15
|
|
|
$this->db = $db; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getResponse(Request $request) |
19
|
|
|
{ |
20
|
|
|
$params = []; |
21
|
|
|
$limitQuery = null; |
22
|
|
|
|
23
|
|
|
$columnArrayObject = $request->getColumns(); |
24
|
|
|
|
25
|
|
|
list($searchQueryPart, $searchParams) = $this->getSearchQueryPart($columnArrayObject); |
26
|
|
|
$params = array_merge($params, $searchParams); |
27
|
|
|
|
28
|
|
|
$orderArrayObject = $request->getOrder(); |
29
|
|
|
$orderQueryPart = $this->getOrderQueryPart($columnArrayObject, $orderArrayObject); |
30
|
|
|
|
31
|
|
|
$length = $request->getLength(); |
32
|
|
|
if (-1 != $length) { |
33
|
|
|
$limitQuery = 'LIMIT ?, ?'; |
34
|
|
|
$params[] = $request->getStart(); |
35
|
|
|
$params[] = $length; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$pdoStatement = $this->db->query( |
39
|
|
|
$this->getQuery($searchQueryPart, $orderQueryPart, $limitQuery), |
40
|
|
|
$params |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$data = $this->getData($columnArrayObject, $pdoStatement); |
44
|
|
|
|
45
|
|
|
$recordsFiltered = $this->getRecordsFiltered(); |
46
|
|
|
|
47
|
|
|
$recordsTotal = $this->getRecordsTotal($recordsFiltered, $searchQueryPart); |
48
|
|
|
|
49
|
|
|
return new Response( |
50
|
|
|
$request->getDraw(), |
51
|
|
|
$recordsTotal, |
52
|
|
|
$recordsFiltered, |
53
|
|
|
$data |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getData(ColumnArrayObject $columnArrayObject, \PDOStatement $pdoStatement) |
58
|
|
|
{ |
59
|
|
|
$data = []; |
60
|
|
|
while ($row = $pdoStatement->fetch(\PDO::FETCH_ASSOC)) { |
61
|
|
|
$item = []; |
62
|
|
|
foreach ($columnArrayObject as $column) { |
63
|
|
|
$name = $column->getData(); |
64
|
|
|
$item[$name] = isset($row[$name]) ? $row[$name] : null; |
65
|
|
|
} |
66
|
|
|
$data[] = $item; |
67
|
|
|
} |
68
|
|
|
return $data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getDatabaseColumnName($dataTablesColumnName) |
72
|
|
|
{ |
73
|
|
|
return $dataTablesColumnName; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getOrderQueryPart(ColumnArrayObject $columnArrayObject, OrderArrayObject $orderArrayObject) |
77
|
|
|
{ |
78
|
|
|
$query = null; |
79
|
|
|
$orderTotal = $orderArrayObject->count(); |
80
|
|
|
if (0 < $orderTotal) { |
81
|
|
|
$query = "ORDER BY"; |
82
|
|
|
$items = []; |
83
|
|
|
foreach ($orderArrayObject as $order) { |
84
|
|
|
if ($columnArrayObject[$order->getColumn()]->getOrderable()) { |
85
|
|
|
$dir = strtoupper($order->getDir()); |
86
|
|
|
$items[] = sprintf( |
87
|
|
|
' %s %s', |
88
|
|
|
$this->getDatabaseColumnName($columnArrayObject[$order->getColumn()]->getData()), |
89
|
|
|
in_array($dir, [DatabaseOrder::ASC, DatabaseOrder::DESC]) ? $dir : DatabaseOrder::ASC |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
$query .= implode(",", $items); |
94
|
|
|
} |
95
|
|
|
return $query; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getRecordsFiltered() |
99
|
|
|
{ |
100
|
|
|
return $this->db->getColumn("SELECT FOUND_ROWS()", [], 0); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getRecordsTotal($recordsFiltered, $searchQueryPart) |
104
|
|
|
{ |
105
|
|
|
if (empty($searchQueryPart)) { |
106
|
|
|
return $recordsFiltered; |
107
|
|
|
} |
108
|
|
|
return $this->db->getColumn( // grand total - query without the search, order, limits |
109
|
|
|
$this->getRecordsTotalQuery(), |
110
|
|
|
[] |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function getSearchQueryPart(ColumnArrayObject $columnArrayObject) |
115
|
|
|
{ |
116
|
|
|
$query = null; |
117
|
|
|
$params = []; |
118
|
|
|
foreach ($columnArrayObject as $column) { |
119
|
|
|
if ($column->getSearchable()) { |
120
|
|
|
$search = $column->getSearch(); |
121
|
|
|
$searchValue = $search->getValue(); |
122
|
|
|
if (!empty($searchValue)) { |
123
|
|
|
$query .= sprintf( |
124
|
|
|
" AND %s LIKE ?", |
125
|
|
|
$this->getDatabaseColumnName($column->getData()) |
126
|
|
|
); |
127
|
|
|
$params[] = sprintf('%%%s%%', $searchValue); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return [$query, $params]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|