From 3f6e5ffcec7fb25a7c240bc072ee10ab0ac9d810 Mon Sep 17 00:00:00 2001 From: kali Date: Mon, 27 May 2024 12:11:56 +0300 Subject: [PATCH] first table --- ActionColumn/ActionColumn.php | 20 +++ ActionColumn/DeleteActionColumn.php | 14 +++ ActionColumn/EditActionColumn.php | 16 +++ ActionColumn/ViewActionColumn.php | 14 +++ ListJsonTable.php | 184 ++++++++++++++++++++++++++++ Pagination.php | 69 +++++++++++ ViewJsonTable.php | 108 ++++++++++++++++ actionBtn/ActionBtn.php | 22 ++++ actionBtn/ToDeleteBtn.php | 13 ++ actionBtn/ToEditBtn.php | 13 ++ actionBtn/ToListBtn.php | 13 ++ 11 files changed, 486 insertions(+) create mode 100644 ActionColumn/ActionColumn.php create mode 100644 ActionColumn/DeleteActionColumn.php create mode 100644 ActionColumn/EditActionColumn.php create mode 100644 ActionColumn/ViewActionColumn.php create mode 100644 ListJsonTable.php create mode 100644 Pagination.php create mode 100644 ViewJsonTable.php create mode 100644 actionBtn/ActionBtn.php create mode 100644 actionBtn/ToDeleteBtn.php create mode 100644 actionBtn/ToEditBtn.php create mode 100644 actionBtn/ToListBtn.php diff --git a/ActionColumn/ActionColumn.php b/ActionColumn/ActionColumn.php new file mode 100644 index 0000000..e3b30f9 --- /dev/null +++ b/ActionColumn/ActionColumn.php @@ -0,0 +1,20 @@ +baseUrl = $baseUrl; + $this->id = $id; + } + + abstract public function fetch(); + + +} \ No newline at end of file diff --git a/ActionColumn/DeleteActionColumn.php b/ActionColumn/DeleteActionColumn.php new file mode 100644 index 0000000..2e3589e --- /dev/null +++ b/ActionColumn/DeleteActionColumn.php @@ -0,0 +1,14 @@ +baseUrl . $this->prefix . $this->id; + return " Удалить "; + } +} \ No newline at end of file diff --git a/ActionColumn/EditActionColumn.php b/ActionColumn/EditActionColumn.php new file mode 100644 index 0000000..25b2a69 --- /dev/null +++ b/ActionColumn/EditActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Редактировать "; + } + +} \ No newline at end of file diff --git a/ActionColumn/ViewActionColumn.php b/ActionColumn/ViewActionColumn.php new file mode 100644 index 0000000..eff82b7 --- /dev/null +++ b/ActionColumn/ViewActionColumn.php @@ -0,0 +1,14 @@ +baseUrl . $this->prefix . $this->id; + return " Просмотр "; + } +} \ No newline at end of file diff --git a/ListJsonTable.php b/ListJsonTable.php new file mode 100644 index 0000000..db9312f --- /dev/null +++ b/ListJsonTable.php @@ -0,0 +1,184 @@ +beforePrintCell = false; + $this->json = $json; + $this->data = json_decode($this->json, true); + $this->baseUrl = $this->data['meta']['baseUrl'] ?? null; + $this->setActions(); + } + + public function beginTable(): void + { + $paramsStr = $this->createParams($this->data['meta']['params']); + $hook = $this->beforePrint; + $this->html .= $hook(); + $this->html .= ""; + } + + public function beforePrint(\Closure $closure): void + { + $this->beforePrint = $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->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 $col) { + $this->html .= ""; + $this->count += 1; + $this->html .= ''; + foreach ($col as $key => $row) { + if ($this->issetColumn($key) and $this->is_fillable($key)) { + if ($this->beforePrintCell) { + $hook = $this->beforePrintCell; + $row = $hook($key, $row); + } + + $this->html .= ""; + } + } + + $actions = $this->getActions($col["id"]); + + $this->html .= ""; + } + } + } + + 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; + } + } + } + } + + private function issetColumn($column) + { + 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 getActions(int $id): string + { + $actions = ""; + foreach ($this->actionsArray as $item) { + $objItem = new $item($this->baseUrl, $id); + $actions .= $objItem->fetch(); + } + + return $actions; + } + + public function create() + { + $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 ""; + } + } + + public function endTable(): void + { + $this->html .= "
" . "ID" . "" . $column . "Действия
' . $this->count . '" . $row . "$actions
" . $key . "" . $value . "
"; + } + + public function render(): void + { + echo $this->html; + } + + public function setBeforePrintCell(\Closure $closure): void + { + $this->beforePrintCell = $closure; + } +} \ No newline at end of file diff --git a/Pagination.php b/Pagination.php new file mode 100644 index 0000000..c6d51a4 --- /dev/null +++ b/Pagination.php @@ -0,0 +1,69 @@ +countItem = $countItem; + $this->perPage = $perPage; + $this->currentPage = $currentPage; + $this->baseUrl = $baseUrl; + + $this->countPages = ceil($this->countItem / $perPage); + } + + public function create() + { + $prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null; + $next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null; + $btns = $prev ? "
  • $prev
  • " : ""; + $btns .= "
  • $this->currentPage
  • "; + $btns .= $next ? "
  • $next
  • " : ""; + + $this->html = str_replace('{btns}', $btns, $this->getTemplate()); + $this->html = str_replace('{previous_link}', $this->baseUrl . "/1", $this->html); + $this->html = str_replace('{next_link}', $this->baseUrl . "/" . $this->countPages, $this->html); + } + + public function render(): void + { + echo $this->html; + } + + public function fetch() + { + return $this->html; + } + + private function getTemplate() + { + return ''; + } +} \ No newline at end of file diff --git a/ViewJsonTable.php b/ViewJsonTable.php new file mode 100644 index 0000000..fe9818e --- /dev/null +++ b/ViewJsonTable.php @@ -0,0 +1,108 @@ +json = $json; + $this->data = json_decode($this->json, true); + $this->dataJson = json_decode($this->data['data']['data'], true); + } + + + + public function beginTable(): void + { + $paramsStr = $this->createParams($this->data['meta']['params']); + $hook = $this->beforePrintTable; + $this->html = $hook(); + $this->html .= ""; + } + public function createColum(): string + { + foreach ($this->data['meta']['columns'] as $key => $column){ + if ($this->issetColumn($key)){ + if ($this->beforePrintCell){ + $hook = $this->beforePrintCell; + $this->dataJson[$key] = $hook($key, $this->dataJson[$key]); + } + + $this->html .= ""; + + } + } + + + return $this->html; + } + + private function issetColumn($column) + { + + if (isset($this->data['meta']['columns'])){ + foreach ($this->data['meta']['columns'] as $key => $currentColumn){ + if ($key === $column){ + return true; + } + } + } + + return false; + } + + public function endTable() + { + $this->html .= "
    " . $column . ": " . $this->dataJson[$key] . "
    "; + $hookAfter = $this->afterPrintTable; + $this->html .= $hookAfter(); + + } + + public function create() + { + $this->beginTable(); + $this->createColum(); + $this->endTable(); + } + + + public function beforeTable(\Closure $closure): void + { + $this->beforePrintTable = $closure; + } + + public function afterTable(\Closure $closure): void + { + $this->afterPrintTable = $closure; + } + + public function render() + { + + echo $this->html; + + } + + public function setBeforePrintCell(\Closure $closure): void + { + $this->beforePrintCell = $closure; + } +} \ No newline at end of file diff --git a/actionBtn/ActionBtn.php b/actionBtn/ActionBtn.php new file mode 100644 index 0000000..5b4256f --- /dev/null +++ b/actionBtn/ActionBtn.php @@ -0,0 +1,22 @@ +baseUrl = $baseUrl; + $this->id = $id; + + return $this; + } + + abstract public function fetch(); + +} \ No newline at end of file diff --git a/actionBtn/ToDeleteBtn.php b/actionBtn/ToDeleteBtn.php new file mode 100644 index 0000000..2cc332f --- /dev/null +++ b/actionBtn/ToDeleteBtn.php @@ -0,0 +1,13 @@ +baseUrl$this->prefix$this->id' style='margin: 3px'>Удалить"; + } +} \ No newline at end of file diff --git a/actionBtn/ToEditBtn.php b/actionBtn/ToEditBtn.php new file mode 100644 index 0000000..f75e4aa --- /dev/null +++ b/actionBtn/ToEditBtn.php @@ -0,0 +1,13 @@ +baseUrl$this->prefix$this->id' style='margin: 3px'>Редактировать"; + } +} \ No newline at end of file diff --git a/actionBtn/ToListBtn.php b/actionBtn/ToListBtn.php new file mode 100644 index 0000000..4228b30 --- /dev/null +++ b/actionBtn/ToListBtn.php @@ -0,0 +1,13 @@ +baseUrl$this->prefix$this->id' style='margin: 3px'>Список"; + } +} \ No newline at end of file