beforePrintCell = false; $this->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->filters = $this->data['filters'] ?? false; $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 beforePrint(\Closure $closure): void { $this->beforePrintHook = $closure; } public function afterPrint(\Closure $closure): void { $this->afterPrintHook = $closure; } public function createThead(): void { $columnKeys = []; if (!isset($this->data['meta']['columns'])) { return; } $this->html .= ""; if (!$this->issetColumn("id")) { $this->html .= ""; $columnKeys[] = "id"; } foreach ($this->data['meta']['columns'] as $key => $column) { if ($this->is_fillable($key)) { $this->html .= ""; $columnKeys[] = $key; } } $columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys()); $this->getCustomHeadColumn(); if ($this->showActionColumn) { $this->html .= ""; } if ($this->filters) { $this->html .= ""; foreach ($columnKeys as $key){ if ($this->issetFilter($key)){ $this->html .= ""; } else { $this->html .= ""; } } $this->html .= ""; } } public function createTbody(): void { if ($this->data['data']) { // if($this->filter) // { // foreach ($this->data['meta']['columns'] as $col) { // $this->html .= ""; $this->count += 1; $id = $row["id"] ?? $this->count; if (!$this->issetColumn("id")) { $this->html .= ''; } foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { foreach ($this->beforePrintCell as $column => $closure) { // if ($this->beforePrintCell) { // $hook = $this->beforePrintCell; // $ceil = $hook($key, $ceil); // } if ($key == $column) { $hook = $closure; $ceil = $hook($ceil); } } $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); } 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 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($filter): bool { if (isset($this->data['filters'])) { foreach ($this->data['filters'] as $key => $currentFilter) { if (is_array($currentFilter)) { return false; } elseif (is_string($currentFilter)) { if ($currentFilter === $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; } /** * @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" . "" . $column . "Действия
"; // foreach ($this->data['meta']['filters'] as $filter) { // if ($this->issetFilter($filter)) // { // $filters = new Filter($filter, ) // } // } // // // } // } $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1); foreach ($this->data['data'] as $row) { $this->html .= "
' . $id . '" . $ceil . "$actions
" . $item['handler']($id) . "" . $item['label'] . "
" . $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'], 'currentPage' => $this->data['meta']['currentPage'], 'baseUrl' => $this->baseUrl, 'prefix' => $this->data['meta']['paginationPrefix'], ]; $pagination = new Pagination($options); $pagination->create(); $this->html .= $pagination->fetch(); } } public function render(): void { echo $this->html; } public function setBeforePrintCell(string $col, \Closure $closure): void { $this->beforePrintCell[$col] = $closure; } public function columns(array $data): void { foreach ($data as $key => $value) { $this->setBeforePrintCell($key, $value); } } }