beforePrintCell = []; $this->json = $json; $this->data = json_decode($this->json, true); $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->pagination = $this->data['meta']['pagination'] ?? true; $this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true; $this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true; $this->filters = $this->data['filters'] ?? []; $this->beforePrintHook = function () { }; $this->afterPrintHook = function () { }; } public function beginTable(): void { $paramsStr = $this->createParams($this->data['meta']['params']); $hookBefore = $this->beforePrintHook; $this->html .= $hookBefore($this->data['meta']); $this->html .= ""; } public function createThead(): void { $columnKeys = []; if (!isset($this->data['meta']['columns'])) { return; } $this->html .= ""; if (!$this->issetColumn("id")) { $this->html .= ""; $columnKeys[] = "id"; } $columnKeys = $this->getColumnKeys(); $columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys()); $this->getCustomHeadColumn(); if ($this->showActionColumn) { $this->html .= ""; } if($this->showFiltersRow){ $this->getFilters($columnKeys); $this->html .= ""; } } public function createTbody(): void { if ($this->data['data']) { $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1); foreach ($this->data['data'] as $row) { //сортируем колнки согласно массиву колонок из json $row = array_merge(array_flip($this->getFillableColumns()), $row); $this->html .= ""; $this->count += 1; $id = $row["id"] ?? $this->count; if (!$this->issetColumn("id")) { $this->html .= ''; } foreach ($row as $key => $cell) { if ($this->issetColumn($key) and $this->is_fillable($key)) { if($this->beforePrintCell) { $cell = $this->getCustomCell($key, $cell); } $this->html .= ""; } } $this->getCustomColumns($row["id"] ?? null); if ($this->showActionColumn) { if (isset($row["id"])) { $actions = $this->getActions($row["id"]); $this->html .= ""; } else { $this->html .= ""; } } } } } public function addAction(string $actionColumn): void { $this->customActionsArray[] = $actionColumn; } public function addColumn(string $label, string $key, \Closure $closure): void { $this->customColumnsArray[] = ['label' => $label, "key" => $key, "handler" => $closure]; } private function setActions(): void { if (isset($this->data['meta']['actions'])) { foreach ($this->data['meta']['actions'] as $action) { switch ($action) { case 'view': $this->actionsArray[] = ViewActionColumn::class; break; case 'delete': $this->actionsArray[] = DeleteActionColumn::class; break; case 'edit': $this->actionsArray[] = EditActionColumn::class; break; } } } $this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray); } private function getCurrentFilter(string $column) { if (is_array($this->beforePrintCell[$column])) { if (isset($this->beforePrintCell[$column]['filter'])) { if (is_array($this->beforePrintCell[$column]['filter'])) { return $this->beforePrintCell[$column]['filter']; } } } return false; } public function getCustomColumns($id = null): void { foreach ($this->customColumnsArray as $item) { $this->html .= ""; } } public function getCustomHeadColumn(): void { foreach ($this->customColumnsArray as $item) { $this->html .= ""; } } protected function getCustomColumnKeys(): array { $keys = []; foreach ($this->customColumnsArray as $item) { $keys[] = $item['key']; } return $keys; } private function getColumnKeys(): array { $columnKeys = []; foreach ($this->data['meta']['columns'] as $key => $column) { if ($this->is_fillable($key)) { $this->html .= ""; $columnKeys[] = $key; } } return $columnKeys; } private function getFillableColumns(): array { $columnKeys = []; foreach ($this->data['meta']['columns'] as $key => $column) { if ($this->is_fillable($key)) { $columnKeys[] = $key; } } return $columnKeys; } private function issetColumn($column): bool { if (isset($this->data['meta']['columns'])) { foreach ($this->data['meta']['columns'] as $key => $currentColumn) { if ($key === $column) { return true; } } } return false; } private function issetFilter(string $filter): bool { if (isset($this->data['filters'])) { foreach ($this->data['filters'] as $currentFilter) { if (is_array($currentFilter)) { return false; } elseif (is_string($currentFilter)) { if ($currentFilter === $filter) { return true; } } } } if (is_array($this->beforePrintCell[$filter])) { if (isset($this->beforePrintCell[$filter]['filter'])) { return true; } } return false; } private function is_fillable($column): bool { if (isset($this->data['meta']['fillable'])) { if (in_array($column, $this->data['meta']['fillable'])) { return true; } } else { return true; } return false; } private function getActions(int $id): string { $actions = ""; foreach ($this->actionsArray as $item) { $objItem = new $item($this->baseUrl, $id); $actions .= $objItem->fetch(); } return $actions; } private function getFilters(array $columnKeys): void { $this->html .= ""; foreach ($columnKeys as $key){ if ($this->issetFilter($key)){ $filter = $this->getCurrentFilter($key); $class = new $filter['class']([ 'param' => $filter['param'], 'name' => $key, 'value' => $filter['value'] ]); $this->html .= $class->fetch(); } else { $this->html .= ""; } } $this->html .= ""; } /** * @throws Exception */ public function create(): void { $this->setActions(); $this->beginTable(); $this->createThead(); $this->createTbody(); $this->endTable(); } public function tableAction($json): void { $tableJson = json_decode($json, true); foreach ($tableJson as $key => $value) { echo ""; echo ""; echo ""; echo ""; } } /** * @throws Exception */ public function endTable(): void { $this->html .= "
" . "ID" . "Действия
' . $id . '" . $cell . "$actions
" . $item['handler']($id) . "" . $item['label'] . "" . $column . "
" . $key . "" . $value . "
"; $hookAfter = $this->afterPrintHook; $this->html .= $hookAfter($this->data['meta']); if ($this->pagination) { $options = [ 'countItem' => $this->data['meta']['total'], 'perPage' => $this->data['meta']['perPage'] ?? 10, 'currentPage' => $this->data['meta']['currentPage'] ?? 1, 'baseUrl' => $this->baseUrl ?? '', 'prefix' => $this->data['meta']['paginationPrefix'] ?? '/page', ]; $pagination = new Pagination($options); $pagination->create(); $this->html .= $pagination->fetch(); } } public function columns(array $data): void { foreach ($data as $key => $value) { $this->beforePrintCell[$key] = $value; } } }