hamburgscleanest /
data-tables
| 1 | <?php |
||
| 2 | |||
| 3 | namespace hamburgscleanest\DataTables\Models; |
||
| 4 | |||
| 5 | use hamburgscleanest\DataTables\Facades\SessionHelper; |
||
| 6 | use Illuminate\Database\Eloquent\Builder; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class DataComponent |
||
| 10 | * @package hamburgscleanest\DataTables\Models |
||
| 11 | */ |
||
| 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 | 11 | public function getQueryCount() : int |
|
| 30 | { |
||
| 31 | /** @var \Illuminate\Database\Query\Builder $query */ |
||
| 32 | 11 | $query = $this->_dataTable->query()->getQuery(); |
|
| 33 | |||
| 34 | 11 | [$oldOrders, $oldLimit, $oldOffset] = [$query->orders, $query->limit, $query->offset]; |
|
| 35 | 11 | $query->orders = $query->limit = $query->offset = null; |
|
| 36 | |||
| 37 | 11 | $dataCount = $query->count(); |
|
| 38 | |||
| 39 | 11 | $query->orders = $oldOrders; |
|
| 40 | 11 | $query->limit = $oldLimit; |
|
| 41 | 11 | $query->offset = $oldOffset; |
|
| 42 | |||
| 43 | 11 | return $dataCount; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param DataTable $dataTable |
||
| 48 | * |
||
| 49 | * @return DataComponent |
||
| 50 | */ |
||
| 51 | 34 | public function init(DataTable $dataTable) : DataComponent |
|
| 52 | { |
||
| 53 | 34 | $this->_dataTable = $dataTable; |
|
| 54 | |||
| 55 | 34 | if ($this->_rememberState) |
|
| 56 | { |
||
| 57 | 3 | $this->_readFromSession(); |
|
| 58 | } |
||
| 59 | |||
| 60 | 34 | $this->_afterInit(); |
|
| 61 | |||
| 62 | 34 | 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 |
|
| 70 | { |
||
| 71 | |||
| 72 | 2 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Initalize fields after the query builder instance is set. |
||
| 76 | */ |
||
| 77 | 7 | protected function _afterInit() : void |
|
| 78 | { |
||
| 79 | |||
| 80 | 7 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Remember the state of the data component. |
||
| 84 | * |
||
| 85 | * @return DataComponent |
||
| 86 | */ |
||
| 87 | 3 | public function remember() : DataComponent |
|
| 88 | { |
||
| 89 | 3 | $this->_rememberState = true; |
|
| 90 | |||
| 91 | 3 | return $this; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Forget the state of the data component. |
||
| 96 | * |
||
| 97 | * @return DataComponent |
||
| 98 | */ |
||
| 99 | 1 | public function forget() : DataComponent |
|
| 100 | { |
||
| 101 | 1 | $this->_rememberState = false; |
|
| 102 | 1 | SessionHelper::removeState($this->_rememberKey); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 103 | |||
| 104 | 1 | return $this; |
|
| 105 | } |
||
| 106 | |||
| 107 | 18 | public function transformData() : void |
|
| 108 | { |
||
| 109 | 18 | if ($this->_rememberState) |
|
| 110 | { |
||
| 111 | 2 | $this->_storeInSession(); |
|
| 112 | } |
||
| 113 | 18 | $this->_shapeData(); |
|
| 114 | 18 | } |
|
| 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 |
|
| 121 | { |
||
| 122 | |||
| 123 | 1 | } |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 32 | public function getName() : string |
|
| 129 | { |
||
| 130 | 32 | return \mb_strtolower(\class_basename($this)); |
|
| 131 | } |
||
| 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 | } |