21 Commits

Author SHA1 Message Date
1bd82eb40b some fix 2024-08-07 12:16:42 +03:00
4a12eeccfd some fix 2024-08-07 11:56:33 +03:00
71b2fdfb6b some fix 2024-08-06 12:10:26 +03:00
f53346610e add JasonTable class 2024-08-02 15:40:13 +03:00
67a3f5770e view custom ceil 2024-08-02 13:26:32 +03:00
6d5f6a8665 fix methods names 2024-08-02 12:06:57 +03:00
2c1db8573c custom handler 2024-08-02 11:44:34 +03:00
8d1dce0222 columns 2024-08-01 15:10:41 +03:00
433cc85f54 filters 2024-08-01 13:32:31 +03:00
39931b257b filter beta0.2 2024-08-01 12:50:16 +03:00
b7fe32b91d filter beta 2024-08-01 12:28:21 +03:00
320e1070ba custom column 2024-07-31 17:11:29 +03:00
8bc8a75a53 custom column beta 2024-07-31 16:32:44 +03:00
f5f6504545 pagination fix 2024-07-31 15:49:40 +03:00
647fe34d11 pagination 2024-07-31 14:51:39 +03:00
1714afcf3f pagination 2024-07-31 14:26:26 +03:00
d6d703aaa6 meta at hooks 2024-07-31 12:48:26 +03:00
b7fea4122c add hook afterPrint 2024-07-30 12:20:16 +03:00
48e6d645d9 update view json table 2024-07-23 15:17:16 +03:00
85c4a850b1 set action 2024-07-16 13:26:29 +03:00
02ded1a613 some fix 2024-07-16 13:20:48 +03:00
10 changed files with 430 additions and 116 deletions

18
composer.lock generated Normal file
View File

@ -0,0 +1,18 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "76e2cbfc39ced89c5f5aa46d0b1474c7",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "dev",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

View File

@ -2,14 +2,35 @@
"meta": {
"title": "forma 1",
"columns": {
"id": "ID",
"email": "Email",
"description": "Описание 1",
"description2": "Описание 2"
"description2": "Описание 2",
"status": "Статус"
},
"actions": ["view"],
"pagination": true,
"perPage": "5",
"currentPage": "1",
"showActionColumn": true,
"filter": true,
"total": 10,
"paginationPrefix": "/page",
"params": {"class": "table table-bordered", "border": "1"}
},
"filters": [
"email", "description"
],
"data": [
{"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "status": 1}
]
}

View File

@ -5,7 +5,42 @@ require_once "../vendor/autoload.php";
use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json');
$table = new ListJsonTable($json);
//$table->columns([
$table->columns([
'status' => function ($cell) {
return getStatusLabel()[$cell];
},
'email' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
'description' => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
]);
//$table->setBeforePrintCell(function ($key, $data) {
// return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
//});
$table->afterPrint(function ($meta) {
return "<div>After Print</div>";
});
$table->addColumn("Колонка 33", "k33", function ($id) {
return "my ID: " . $id;
});
$table->addColumn("Колонка 34", "k34", function ($id) {
return "some34";
});
$table->create();
$table->render();
function getStatusLabel(): array
{
return [
0 => "На модерации",
1 => "Активный",
2 => "Модератор",
99 => "Удален",
];
}

27
examples/view.json Normal file
View File

@ -0,0 +1,27 @@
{
"meta": {
"rows": {
"username": "Логин",
"email": "Email",
"created_at": "Создан",
"updated_at": "Обновлен"
},
"perPage": 10,
"currentPage": 1,
"baseUrl": "\/admin\/user",
"actions": [],
"params": {
"class": "table table-bordered",
"border": "2"
}
},
"data": {
"id": 1,
"username": "rrr",
"email": "rrr@mail.ru",
"password_hash": "sdgh46eyrtghsdret",
"role": 1,
"created_at": "15.07.2024",
"updated_at": null
}
}

20
examples/view.php Normal file
View File

@ -0,0 +1,20 @@
<?php
require_once "../vendor/autoload.php";
use Itguild\Tables\ViewJsonTable;
$json = file_get_contents('view.json');
$table = new ViewJsonTable($json);
//$table->rows([
$table->rows([
'username' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
'email' => function ($cell) {
return "<span style='color: firebrick'>$cell</span>";
}
]);
$table->create();
$table->render();

48
src/Filter.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace Itguild\Tables;
class Filter
{
// private array $columnsForFilter;
private string $column;
private string $html = "";
private string $baseUrl;
public function __construct(array $columns, string $baseUrl)
{
$this->columnsForFilter = $columns;
$this->baseUrl = $baseUrl;
}
public function create(): void
{
$this->html = str_replace('{action_link}', $this->baseUrl, $this->getTemplate());
$this->html = str_replace('{column}', 'id', $this->html);
}
public function render(): void
{
echo $this->html;
}
public function fetch(): string
{
return $this->html;
}
public function getColumnsForFilter(): array
{
return $this->columnsForFilter;
}
public function getTemplate(): string
{
return '<nav aria-label="Filters example">
<form action="{action_link}">
<input type="text" name="{column}">
</nav>';
}
}

49
src/JasonTable.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace Itguild\Tables;
class JasonTable
{
protected string $html = "";
protected \Closure|array|false $beforePrintCell;
protected \Closure|false $afterPrintHook;
protected \Closure|false $beforePrintHook;
public function render(): void
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
public function getCustomCell(string $key, string|null $cell)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $currentKey => $closure) {
if ($key == $currentKey) {
$hook = $closure;
$cell = $hook($cell);
}
}
} else {
$hook = $this->beforePrintCell;
$cell = $hook($key, $cell);
}
return $cell;
}
public function afterPrint(\Closure $closure): void
{
$this->afterPrintHook = $closure;
}
public function beforePrint(\Closure $closure): void
{
$this->beforePrintHook = $closure;
}
}

View File

@ -2,100 +2,121 @@
namespace Itguild\Tables;
use Itguild\Tables\ActionColumn\ActionColumn;
use Exception;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\traits\CreateParams;
use JetBrains\PhpStorm\NoReturn;
class ListJsonTable
class ListJsonTable extends JasonTable
{
use CreateParams;
private string $html = '';
private string $json;
private int $count = 0;
private \Closure|false $beforePrintCell;
private \Closure|false $beforePrint;
private string $baseUrl;
private array $data;
private bool $pagination = true;
private bool $showActionColumn = true;
private bool|array $filters = false;
private array $actionsArray = [];
private array $customActionsArray = [];
private array $customColumnsArray = [];
public function __construct(string $json)
#[NoReturn] public function __construct(string $json)
{
$this->beforePrintCell = false;
$this->beforePrint = function () {
};
$this->beforePrintCell = [];
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->setActions();
$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']);
$hook = $this->beforePrint;
$this->html .= $hook();
$hookBefore = $this->beforePrintHook;
$this->html .= $hookBefore($this->data['meta']);
$this->html .= "<table $paramsStr>";
}
public function beforePrint(\Closure $closure): void
{
$this->beforePrint = $closure;
}
public function createThead(): void
{
$columnKeys = [];
if (!isset($this->data['meta']['columns'])) {
return;
}
$this->html .= "<thead><tr>";
if (!$this->issetColumn("id")) {
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
}
$columnKeys[] = "id";
}
$columnKeys = $this->getColumnKeys($columnKeys);
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr></thead>";
}
if ($this->filters) {
$this->getFilters($columnKeys);
}
}
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) {
foreach ($this->data['data'] as $row) {
$this->html .= "<tr>";
$this->count += 1;
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
foreach ($col as $key => $row) {
$id = $row["id"] ?? $this->count;
if (!$this->issetColumn("id")) {
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
}
foreach ($row as $key => $cell) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if($this->beforePrintCell) {
$hook = $this->beforePrintCell;
$row = $hook($key, $row);
$cell = $this->getCustomCell($key, $cell);
}
$this->html .= "<td>" . $row . "</td>";
$this->html .= "<td>" . $cell . "</td>";
}
}
$actions = $this->getActions($col["id"]);
$this->getCustomColumns($row["id"] ?? null);
if ($this->showActionColumn) {
if (isset($row["id"])) {
$actions = $this->getActions($row["id"]);
$this->html .= "<td>$actions</td></tr>";
} else {
$this->html .= "<td></td></tr>";
}
}
}
}
}
public function addAction(ActionColumn $actionColumn): void
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'])) {
@ -116,6 +137,41 @@ class ListJsonTable
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
}
public function getCustomColumns($id = null): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<td>" . $item['handler']($id) . "</td>";
}
}
public function getCustomHeadColumn(): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<th>" . $item['label'] . "</th>";
}
}
protected function getCustomColumnKeys(): array
{
$keys = [];
foreach ($this->customColumnsArray as $item) {
$keys[] = $item['key'];
}
return $keys;
}
private function getColumnKeys(array $columnKeys): array
{
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function issetColumn($column): bool
{
if (isset($this->data['meta']['columns'])) {
@ -129,6 +185,23 @@ class ListJsonTable
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'])) {
@ -153,8 +226,26 @@ class ListJsonTable
return $actions;
}
public function create()
private function getFilters(array $columnKeys): void
{
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<td><input type='text' name='$key'></td>";
}
else {
$this->html .= "<td></td>";
}
}
$this->html .= "</form></tr>";
}
/**
* @throws Exception
*/
public function create(): void
{
$this->setActions();
$this->beginTable();
$this->createThead();
$this->createTbody();
@ -163,7 +254,6 @@ class ListJsonTable
public function tableAction($json): void
{
$tableJson = json_decode($json, true);
foreach ($tableJson as $key => $value) {
@ -174,18 +264,33 @@ class ListJsonTable
}
}
/**
* @throws Exception
*/
public function endTable(): void
{
$this->html .= "</table>";
$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 render(): void
public function columns(array $data): void
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}

View File

@ -3,6 +3,9 @@
namespace Itguild\Tables;
use Exception;
use http\Message;
class Pagination
{
@ -14,17 +17,24 @@ class Pagination
private string $baseUrl;
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
/**
* @throws Exception
*/
public function __construct(array $options)
{
$this->countItem = $countItem;
$this->perPage = $perPage;
$this->currentPage = $currentPage;
$this->baseUrl = $baseUrl;
if (!$options['countItem']) {
throw new Exception(message: "countItem is not valid");
}
$this->countItem = $options['countItem'];
$this->perPage = $options['perPage'] ?? 10;
$this->currentPage = $options['currentPage'] ?? 1;
$this->baseUrl = $options['baseUrl'] ?? '';
$this->baseUrl .= $options['prefix'] ?? '/page';
$this->countPages = ceil($this->countItem / $perPage);
$this->countPages = ceil($this->countItem / $this->perPage);
}
public function create()
public function create(): void
{
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
@ -42,12 +52,12 @@ class Pagination
echo $this->html;
}
public function fetch()
public function fetch(): string
{
return $this->html;
}
private function getTemplate()
private function getTemplate(): string
{
return '<nav aria-label="Page navigation example">
<ul class="pagination">

View File

@ -4,60 +4,54 @@ namespace Itguild\Tables;
use Itguild\Tables\traits\CreateParams;
class ViewJsonTable
class ViewJsonTable extends JasonTable
{
use CreateParams;
private array $data;
private string $html = "";
private string $json;
private \Closure|false $beforePrintCell;
private \Closure|false $beforePrintTable;
private \Closure|false $afterPrintTable;
private array $dataJson;
public function __construct($json)
{
$this->beforePrintCell = false;
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->dataJson = json_decode($this->data['data']['data'], true);
$this->dataJson = $this->data['data'];
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
}
public function beginTable(): void
{
$paramsStr = $this->createParams($this->data['meta']['params']);
//Хук перед выводом ячейки
if (isset($this->beforePrintTable)){
$hook = $this->beforePrintTable;
$this->html = $hook();
}
$this->html .= "<table $paramsStr>";
}
public function createColum(): string
public function createRows(): string
{
foreach ($this->data['meta']['columns'] as $key => $column){
if ($this->issetColumn($key)){
foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){
if ($this->beforePrintCell) {
$hook = $this->beforePrintCell;
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
}
$this->html .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
}
}
return $this->html;
}
private function issetColumn($column)
private function issetRow($column): bool
{
if (isset($this->data['meta']['columns'])){
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
if (isset($this->data['meta']['rows'])){
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
if ($key === $column){
return true;
}
@ -67,41 +61,28 @@ class ViewJsonTable
return false;
}
public function endTable()
public function endTable(): void
{
$this->html .= "</table>";
if(isset($this->afterPrintTable)){
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
}
}
public function create()
public function create(): void
{
$this->beginTable();
$this->createColum();
$this->createRows();
$this->endTable();
}
public function beforeTable(\Closure $closure): void
public function rows(array $data): 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;
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}