1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BWC\Share\App; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
|
8
|
|
|
class DataTablesAdapter |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
public $echo; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
public $search; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
public $pageSize; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
public $offset; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
public $pageNum; |
24
|
|
|
|
25
|
|
|
/** @var array columnIndex=>ASC|DESC */ |
26
|
|
|
public $sortIndexInfo; |
27
|
|
|
|
28
|
|
|
/** @var array columnName=>bool */ |
29
|
|
|
public $sortInfo; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Request $request Where to read input parameters from |
34
|
|
|
* @param array $columns Optional list of column names from table need to build sortInfo, if omitted only sortIndexInfo would be available |
35
|
|
|
*/ |
36
|
|
|
function bind(Request $request, array $columns = null) { |
|
|
|
|
37
|
|
|
$this->echo = $request->get('sEcho'); |
38
|
|
|
$this->search = $request->get('sSearch'); |
39
|
|
|
$this->pageSize = intval($request->get('iDisplayLength')); |
40
|
|
|
$this->offset = intval($request->get('iDisplayStart')); |
41
|
|
|
$this->pageNum = $this->pageSize ? floor($this->offset / $this->pageSize)+1 : 1; |
|
|
|
|
42
|
|
|
$this->sortIndexInfo = array(); |
43
|
|
|
for ($i=0; $i<10; $i++) { |
44
|
|
|
$key = 'iSortCol_'.$i; |
45
|
|
|
if (!$request->query->has($key)) break; |
46
|
|
|
$sortColIdx = intval($request->get($key)); |
47
|
|
|
$sortDir = strtoupper($request->get('sSortDir_0')); |
48
|
|
|
if ($sortDir != 'ASC' && $sortDir != 'DESC') { |
49
|
|
|
$sortDir = 'ASC'; |
50
|
|
|
} |
51
|
|
|
$this->sortIndexInfo[] = array($sortColIdx, $sortDir); |
52
|
|
|
if ($columns && isset($columns[$sortColIdx])) { |
53
|
|
|
$this->sortInfo[$columns[$sortColIdx]] = $sortDir == 'ASC'; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param object[] $array |
62
|
|
|
* @param int|bool $totalRecords |
63
|
|
|
* @return Response |
64
|
|
|
*/ |
65
|
|
|
function getResponse(array $array, $totalRecords = false) { |
|
|
|
|
66
|
|
|
$data = new \stdClass(); |
67
|
|
|
if ($totalRecords) { |
68
|
|
|
$data->iTotalDisplayRecords = $totalRecords; |
69
|
|
|
$data->iTotalRecords = $totalRecords; |
70
|
|
|
} else { |
71
|
|
|
$data->iTotalDisplayRecords = count($array); |
72
|
|
|
$data->iTotalRecords = count($array); |
73
|
|
|
} |
74
|
|
|
//$data->iTotalRecords = $totalRecords === false ? $data->iTotalDisplayRecords : $totalRecords; // SELECT FOUND_ROWS() |
75
|
|
|
$data->sEcho = $this->echo; |
76
|
|
|
$data->aaData = $array; |
77
|
|
|
$json = json_encode($data); |
78
|
|
|
return new Response($json, 200, array('Content-Type'=>'application/json')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.