itstructure /
laravel-grid-view
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Itstructure\GridView; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Exception; |
||
| 7 | use Illuminate\Http\Request; |
||
| 8 | use Illuminate\Pagination\LengthAwarePaginator; |
||
| 9 | use Itstructure\GridView\Columns\{BaseColumn, CallbackColumn, DefaultColumn}; |
||
| 10 | use Itstructure\GridView\DataProviders\{BaseDataProvider, EloquentDataProvider}; |
||
| 11 | use Itstructure\GridView\Traits\Configurable; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class Grid |
||
| 15 | * @package Itstructure\GridView |
||
| 16 | */ |
||
| 17 | class Grid |
||
| 18 | { |
||
| 19 | use Configurable; |
||
| 20 | |||
| 21 | const INIT_PAGE_NUMBER = 1; |
||
| 22 | const INIT_ROWS_PER_PAGE = 10; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var EloquentDataProvider |
||
| 26 | */ |
||
| 27 | protected $dataProvider; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $useFilters = true; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $strictFilters = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array|Request|string |
||
| 41 | */ |
||
| 42 | protected $request; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var LengthAwarePaginator $paginator |
||
| 46 | */ |
||
| 47 | protected $paginator; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $paginatorOptions = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $page = self::INIT_PAGE_NUMBER; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | protected $rowsPerPage = self::INIT_ROWS_PER_PAGE; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $title; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $columnFields = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var BaseColumn[] |
||
| 76 | */ |
||
| 77 | protected $columnObjects = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $rowsFormAction = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $filtersFormAction = ''; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | protected $useSendButtonAnyway = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $sendButtonLabel; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $searchButtonLabel; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $resetButtonLabel; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | protected $tableBordered = true; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $tableStriped = true; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $tableHover = true; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var bool |
||
| 126 | */ |
||
| 127 | protected $tableSmall = true; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Grid constructor. |
||
| 131 | * @param array $config |
||
| 132 | * @throws Exception |
||
| 133 | */ |
||
| 134 | public function __construct(array $config) |
||
| 135 | { |
||
| 136 | $this->loadConfig($config); |
||
| 137 | $this->request = request(); |
||
| 138 | |||
| 139 | if (!($this->dataProvider instanceof BaseDataProvider)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 140 | throw new Exception('dataProvider must be instance of '.BaseDataProvider::class); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function render(): string |
||
| 148 | { |
||
| 149 | $this->applyColumnsConfig(); |
||
| 150 | |||
| 151 | $this->dataProvider->selectionConditions($this->request, $this->strictFilters); |
||
| 152 | |||
| 153 | $totalCount = $this->dataProvider->getCount(); |
||
| 154 | $pageNumber = $this->request->get($this->paginatorOptions['pageName'] ?? 'page', $this->page); |
||
| 155 | |||
| 156 | $this->paginator = new LengthAwarePaginator( |
||
| 157 | $this->dataProvider->get($this->rowsPerPage, $pageNumber), |
||
| 158 | $totalCount, |
||
| 159 | $this->rowsPerPage, |
||
| 160 | $pageNumber, |
||
| 161 | $this->paginatorOptions |
||
| 162 | ); |
||
| 163 | |||
| 164 | return view('grid_view::grid', [ |
||
| 165 | 'columnObjects' => $this->columnObjects, |
||
| 166 | 'useFilters' => $this->useFilters, |
||
| 167 | 'paginator' => $this->paginator, |
||
| 168 | 'title' => $this->title, |
||
| 169 | 'rowsFormAction' => $this->rowsFormAction, |
||
| 170 | 'filtersFormAction' => $this->filtersFormAction, |
||
| 171 | 'useSendButtonAnyway' => $this->useSendButtonAnyway, |
||
| 172 | 'searchButtonLabel' => $this->getSearchButtonLabel(), |
||
| 173 | 'resetButtonLabel' => $this->getResetButtonLabel(), |
||
| 174 | 'sendButtonLabel' => $this->getSendButtonLabel(), |
||
| 175 | 'tableBordered' => $this->tableBordered, |
||
| 176 | 'tableStriped' => $this->tableStriped, |
||
| 177 | 'tableHover' => $this->tableHover, |
||
| 178 | 'tableSmall' => $this->tableSmall |
||
| 179 | ])->render(); |
||
| 180 | } |
||
| 181 | |||
| 182 | protected function applyColumnsConfig(): void |
||
| 183 | { |
||
| 184 | foreach ($this->columnFields as $key => $config) { |
||
| 185 | |||
| 186 | $filterSubConfig = $this->useFilters ? [] : ['filter' => false]; |
||
| 187 | |||
| 188 | if (is_string($config)) { |
||
| 189 | $config = array_merge(['attribute' => $config], $filterSubConfig); |
||
| 190 | $this->fillColumnsObjects(new DefaultColumn($config)); |
||
| 191 | continue; |
||
| 192 | } |
||
| 193 | |||
| 194 | if (is_array($config)) { |
||
| 195 | $config = array_merge($config, $filterSubConfig); |
||
| 196 | |||
| 197 | if (isset($config['class']) && class_exists($config['class'])) { |
||
| 198 | $this->fillColumnsObjects(new $config['class']($config)); |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (isset($config['value']) && $config['value'] instanceof Closure) { |
||
| 203 | $this->fillColumnsObjects(new CallbackColumn($config)); |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->fillColumnsObjects(new DefaultColumn($config)); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param BaseColumn $columnObject |
||
| 214 | */ |
||
| 215 | protected function fillColumnsObjects(BaseColumn $columnObject): void |
||
| 216 | { |
||
| 217 | $this->columnObjects = array_merge($this->columnObjects, [$columnObject]); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | protected function getSearchButtonLabel(): string |
||
| 224 | { |
||
| 225 | return $this->searchButtonLabel ?? trans('grid_view::grid.search'); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | protected function getResetButtonLabel(): string |
||
| 232 | { |
||
| 233 | return $this->resetButtonLabel ?? trans('grid_view::grid.reset'); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | protected function getSendButtonLabel(): string |
||
| 240 | { |
||
| 241 | return $this->sendButtonLabel ?? trans('grid_view::grid.send'); |
||
| 242 | } |
||
| 243 | } |
||
| 244 |