|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models; |
|
4
|
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Facades\SessionHelper; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class DataComponent |
|
11
|
|
|
* @package hamburgscleanest\DataTables\Models |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class DataComponent { |
|
14
|
|
|
|
|
15
|
|
|
/** @var Builder */ |
|
16
|
|
|
protected $_queryBuilder; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Request */ |
|
19
|
|
|
protected $_request; |
|
20
|
|
|
|
|
21
|
|
|
/** @var bool */ |
|
22
|
|
|
protected $_rememberState = false; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $_rememberKey = 'global'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Everything that needs to be read when the state is remembered. |
|
29
|
|
|
* Is called before _afterInit(), so that session values can be overriden. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function _readFromSession() |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Use this function to save your state in the session. |
|
38
|
|
|
* This is called just before rendering, so all dynamically added stuff etc. is considered. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function _storeInSession() |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initalize fields after the query builder instance and the request is set. |
|
47
|
|
|
* |
|
48
|
|
|
* TODO: Refactor.. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function _afterInit() |
|
51
|
|
|
{ |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* You cannot count the data when ordering. |
|
57
|
|
|
* Disables ordering temporary. |
|
58
|
|
|
* |
|
59
|
|
|
* @return int |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getQueryCount(): int |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var \Illuminate\Database\Query\Builder $query */ |
|
64
|
2 |
|
$query = $this->_queryBuilder->getQuery(); |
|
65
|
|
|
|
|
66
|
2 |
|
$oldOrders = $query->orders; |
|
67
|
2 |
|
$oldLimit = $query->limit; |
|
68
|
2 |
|
$oldOffset = $query->offset; |
|
69
|
|
|
|
|
70
|
2 |
|
$query->orders = null; |
|
71
|
2 |
|
$query->limit = null; |
|
72
|
2 |
|
$query->offset = null; |
|
73
|
|
|
|
|
74
|
2 |
|
$dataCount = $query->count(); |
|
75
|
|
|
|
|
76
|
2 |
|
$query->orders = $oldOrders; |
|
77
|
2 |
|
$query->limit = $oldLimit; |
|
78
|
2 |
|
$query->offset = $oldOffset; |
|
79
|
|
|
|
|
80
|
2 |
|
return $dataCount; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Builder $queryBuilder |
|
85
|
|
|
* @param Request $request |
|
86
|
|
|
*/ |
|
87
|
8 |
|
public function init(Builder $queryBuilder, Request $request) |
|
88
|
|
|
{ |
|
89
|
8 |
|
$this->_request = $request; |
|
90
|
8 |
|
$this->_queryBuilder = $queryBuilder; |
|
91
|
|
|
|
|
92
|
8 |
|
if ($this->_rememberState) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->_readFromSession(); |
|
95
|
|
|
} |
|
96
|
8 |
|
$this->_afterInit(); |
|
97
|
8 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Remember the state of the data component. |
|
101
|
|
|
* |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function remember() |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->_rememberState = true; |
|
107
|
|
|
|
|
108
|
1 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Forget the state of the data component. |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function forget() |
|
117
|
|
|
{ |
|
118
|
1 |
|
$this->_rememberState = false; |
|
119
|
1 |
|
if ($this->_request !== null) |
|
120
|
|
|
{ |
|
121
|
1 |
|
SessionHelper::removeState($this->_request, $this->_rememberKey); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
4 |
|
public function transformData() |
|
128
|
|
|
{ |
|
129
|
4 |
|
if ($this->_rememberState) |
|
130
|
|
|
{ |
|
131
|
1 |
|
$this->_storeInSession(); |
|
132
|
|
|
} |
|
133
|
4 |
|
$this->_shapeData(); |
|
134
|
4 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Builder |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract protected function _shapeData(): Builder; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
abstract public function render(): string; |
|
145
|
|
|
} |