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