beforePrintCell = false; $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->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 { if (!isset($this->data['meta']['columns'])) { return; } $this->html .= ""; $this->html .= ""; foreach ($this->data['meta']['columns'] as $key => $column) { if ($this->is_fillable($key)) { $this->html .= ""; } } $this->getCustomHeadColumn(); if ($this->showActionColumn) { $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) { $this->html .= ""; $this->count += 1; $id = $row["id"] ?? $this->count; $this->html .= ''; foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->beforePrintCell) { $hook = $this->beforePrintCell; $ceil = $hook($key, $ceil); } $this->html .= ""; } } $this->getCustomColumns($row["id"] ?? null); // if (isset($col['id'])) { // $newColumn = $this->getColumns($col['id']); // // $this->html .= ""; // } // else { // $this->html .= ""; // } 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 .= ""; } } 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 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 getColumns(int $id): string // { // $columns = []; // foreach ($this->actionsArray as $item) { // $objItem = new $item($this->baseUrl, $id); // $columns .= $objItem->fetch(); // } // // return $columns; // } 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 . "Действия
' . $id . '" . $ceil . "$newColumn
$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(\Closure $closure): void { $this->beforePrintCell = $closure; } }