1 | <?php |
||
12 | abstract class DataComponent { |
||
13 | |||
14 | /** @var bool */ |
||
15 | protected $_rememberState = false; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $_rememberKey = 'global'; |
||
19 | |||
20 | /** @var DataTable */ |
||
21 | protected $_dataTable; |
||
22 | |||
23 | /** |
||
24 | * You cannot count the data when ordering. |
||
25 | * Disables ordering temporary. |
||
26 | * |
||
27 | * @return int |
||
28 | */ |
||
29 | 8 | public function getQueryCount() : int |
|
30 | { |
||
31 | /** @var \Illuminate\Database\Query\Builder $query */ |
||
32 | 8 | $query = $this->_dataTable->query()->getQuery(); |
|
33 | |||
34 | 8 | [$oldOrders, $oldLimit, $oldOffset] = [$query->orders, $query->limit, $query->offset]; |
|
35 | 8 | $query->orders = $query->limit = $query->offset = null; |
|
36 | |||
37 | 8 | $dataCount = $query->count(); |
|
38 | |||
39 | 8 | $query->orders = $oldOrders; |
|
40 | 8 | $query->limit = $oldLimit; |
|
41 | 8 | $query->offset = $oldOffset; |
|
42 | |||
43 | 8 | return $dataCount; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param DataTable $dataTable |
||
48 | * |
||
49 | * @return DataComponent |
||
50 | */ |
||
51 | 32 | public function init(DataTable $dataTable) : DataComponent |
|
52 | { |
||
53 | 32 | $this->_dataTable = $dataTable; |
|
54 | |||
55 | 32 | if ($this->_rememberState) |
|
56 | { |
||
57 | 3 | $this->_readFromSession(); |
|
58 | } |
||
59 | |||
60 | 32 | $this->_afterInit(); |
|
61 | |||
62 | 32 | return $this; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Everything that needs to be read when the state is remembered. |
||
67 | * Is called before _afterInit(), so that session values can be overriden. |
||
68 | */ |
||
69 | 2 | protected function _readFromSession() : void |
|
73 | |||
74 | /** |
||
75 | * Initalize fields after the query builder instance is set. |
||
76 | */ |
||
77 | 7 | protected function _afterInit() : void |
|
81 | |||
82 | /** |
||
83 | * Remember the state of the data component. |
||
84 | * |
||
85 | * @return DataComponent |
||
86 | */ |
||
87 | 3 | public function remember() : DataComponent |
|
93 | |||
94 | /** |
||
95 | * Forget the state of the data component. |
||
96 | * |
||
97 | * @return DataComponent |
||
98 | */ |
||
99 | 1 | public function forget() : DataComponent |
|
106 | |||
107 | 15 | public function transformData() : void |
|
115 | |||
116 | /** |
||
117 | * Use this function to save your state in the session. |
||
118 | * This is called just before rendering, so all dynamically added stuff etc. is considered. |
||
119 | */ |
||
120 | 1 | protected function _storeInSession() : void |
|
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | 30 | public function getName() : string |
|
132 | |||
133 | /** |
||
134 | * @return Builder |
||
135 | */ |
||
136 | abstract protected function _shapeData() : Builder; |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | abstract public function render() : string; |
||
142 | } |