1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\DataTables; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\Database\Order; |
|
|
|
|
5
|
|
|
|
6
|
|
|
abstract class AbstractDataTables |
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
|
|
|
$searchQuery = $this->getSearchQuery($columnArrayObject); |
27
|
|
|
|
28
|
|
|
$orderArrayObject = $request->getOrder(); |
29
|
|
|
$orderQuery = $this->getOrderQuery($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($searchQuery, $orderQuery, $limitQuery), |
40
|
|
|
$params |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$data = $this->getData($columnArrayObject, $pdoStatement); |
44
|
|
|
|
45
|
|
|
$recordsFiltered = $this->getRecordsFiltered(); |
46
|
|
|
|
47
|
|
|
$recordsTotal = $this->getRecordsTotal($recordsFiltered, $searchQuery); |
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 getOrderQuery(ColumnArrayObject $columnArrayObject, OrderArrayObject $orderArrayObject) |
72
|
|
|
{ |
73
|
|
|
$orderQuery = null; |
74
|
|
|
$orderTotal = $orderArrayObject->count(); |
75
|
|
|
if (0 < $orderTotal) { |
76
|
|
|
$orderQuery = "ORDER BY"; |
77
|
|
|
$items = []; |
78
|
|
|
foreach ($orderArrayObject as $order) { |
79
|
|
|
if ($columnArrayObject[$order->getColumn()]->getOrderable()) { |
80
|
|
|
$dir = strtoupper($order->getDir()); |
81
|
|
|
$items[] = sprintf( |
82
|
|
|
' %s %s', |
83
|
|
|
$this->getDatabaseColumnName($columnArrayObject[$order->getColumn()]->getData()), |
84
|
|
|
in_array($dir, [Order::ASC, Order::DESC]) ? $dir : Order::ASC |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$orderQuery .= implode(",", $items); |
89
|
|
|
} |
90
|
|
|
return $orderQuery; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getRecordsFiltered() |
94
|
|
|
{ |
95
|
|
|
return $this->db->getColumn("SELECT FOUND_ROWS()", [], 0); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getRecordsTotal($recordsFiltered, $searchQuery) |
99
|
|
|
{ |
100
|
|
|
if (empty($searchQuery)) { |
101
|
|
|
return $recordsFiltered; |
102
|
|
|
} |
103
|
|
|
return $this->db->getColumn( // grand total - query without the search, order, limits |
104
|
|
|
$this->getRecordsTotalQuery(), |
105
|
|
|
[] |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getSearchQuery(\WebServCo\Framework\DataTables\ColumnArrayObject $columnArrayObject) |
110
|
|
|
{ |
111
|
|
|
$searchQuery = null; |
112
|
|
|
foreach ($columnArrayObject as $column) { |
113
|
|
|
if ($column->getSearchable()) { |
114
|
|
|
$search = $column->getSearch(); |
115
|
|
|
$searchValue = $search->getValue(); |
116
|
|
|
if (!empty($searchValue)) { |
117
|
|
|
$searchQuery .= sprintf( |
118
|
|
|
" AND %s REGEXP '%s'", |
119
|
|
|
$this->getDatabaseColumnName($column->getData()), |
120
|
|
|
$searchValue |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $searchQuery; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: