Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
9e7c83b680 | |||
b374744261 | |||
c6918c85b3 | |||
d98ddded07 | |||
1bd82eb40b | |||
4a12eeccfd | |||
71b2fdfb6b | |||
f53346610e | |||
67a3f5770e | |||
6d5f6a8665 | |||
2c1db8573c | |||
8d1dce0222 | |||
433cc85f54 | |||
39931b257b | |||
b7fe32b91d | |||
320e1070ba | |||
8bc8a75a53 | |||
f5f6504545 | |||
647fe34d11 | |||
1714afcf3f | |||
d6d703aaa6 | |||
b7fea4122c | |||
48e6d645d9 | |||
85c4a850b1 | |||
02ded1a613 | |||
c648bd37b6 | |||
cf347f1dc2 | |||
4bdd883d67 | |||
f9fc6b9f17 | |||
338555d62c |
18
composer.lock
generated
Normal file
18
composer.lock
generated
Normal 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"
|
||||||
|
}
|
@ -2,14 +2,35 @@
|
|||||||
"meta": {
|
"meta": {
|
||||||
"title": "forma 1",
|
"title": "forma 1",
|
||||||
"columns": {
|
"columns": {
|
||||||
|
"id": "ID",
|
||||||
"email": "Email",
|
"email": "Email",
|
||||||
"description": "Описание 1",
|
"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"}
|
"params": {"class": "table table-bordered", "border": "1"}
|
||||||
},
|
},
|
||||||
|
"filters": [
|
||||||
|
"email", "description"
|
||||||
|
],
|
||||||
"data": [
|
"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}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -5,7 +5,42 @@ require_once "../vendor/autoload.php";
|
|||||||
use Itguild\Tables\ListJsonTable;
|
use Itguild\Tables\ListJsonTable;
|
||||||
|
|
||||||
$json = file_get_contents('simple.json');
|
$json = file_get_contents('simple.json');
|
||||||
|
|
||||||
$table = new ListJsonTable($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->create();
|
||||||
$table->render();
|
$table->render();
|
||||||
|
|
||||||
|
|
||||||
|
function getStatusLabel(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
0 => "На модерации",
|
||||||
|
1 => "Активный",
|
||||||
|
2 => "Модератор",
|
||||||
|
99 => "Удален",
|
||||||
|
];
|
||||||
|
}
|
27
examples/view.json
Normal file
27
examples/view.json
Normal 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
20
examples/view.php
Normal 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();
|
@ -8,8 +8,11 @@ abstract class ActionColumn
|
|||||||
protected string $prefix;
|
protected string $prefix;
|
||||||
|
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function __construct(string $baseUrl, int $id)
|
public function __construct(string $baseUrl, int $id, string $prefix = '')
|
||||||
{
|
{
|
||||||
|
if (!empty($prefix)){
|
||||||
|
$this->prefix = $prefix;
|
||||||
|
}
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
|
|
||||||
class DeleteActionColumn extends ActionColumn
|
class DeleteActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
protected string $prefix = "/form-delete/";
|
protected string $prefix = "/delete/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
@ -5,7 +5,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
class EditActionColumn extends ActionColumn
|
class EditActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
|
|
||||||
protected string $prefix = "/form-edit/";
|
protected string $prefix = "/edit/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
|
|
||||||
class ViewActionColumn extends ActionColumn
|
class ViewActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
protected string $prefix = "/form-item/";
|
protected string $prefix = "/view/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
48
src/Filter.php
Normal file
48
src/Filter.php
Normal 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
49
src/JasonTable.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,91 +2,120 @@
|
|||||||
|
|
||||||
namespace Itguild\Tables;
|
namespace Itguild\Tables;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\EditActionColumn;
|
use Itguild\Tables\ActionColumn\EditActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
||||||
use Itguild\Tables\traits\CreateParams;
|
use Itguild\Tables\traits\CreateParams;
|
||||||
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
|
||||||
class ListJsonTable
|
class ListJsonTable extends JasonTable
|
||||||
{
|
{
|
||||||
use CreateParams;
|
use CreateParams;
|
||||||
|
|
||||||
private string $html = '';
|
|
||||||
private string $json;
|
private string $json;
|
||||||
|
|
||||||
private int $count = 0;
|
private int $count = 0;
|
||||||
private \Closure|false $beforePrintCell;
|
|
||||||
private \Closure|false $beforePrint;
|
|
||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
private array $data;
|
private array $data;
|
||||||
|
|
||||||
private array $actionsArray = [];
|
private bool $pagination = true;
|
||||||
|
private bool $showActionColumn = true;
|
||||||
|
private bool|array $filters = false;
|
||||||
|
|
||||||
public function __construct(string $json)
|
private array $actionsArray = [];
|
||||||
|
private array $customActionsArray = [];
|
||||||
|
private array $customColumnsArray = [];
|
||||||
|
|
||||||
|
#[NoReturn] public function __construct(string $json)
|
||||||
{
|
{
|
||||||
$this->beforePrintCell = false;
|
$this->beforePrintCell = [];
|
||||||
$this->beforePrint = function (){};
|
|
||||||
$this->json = $json;
|
$this->json = $json;
|
||||||
$this->data = json_decode($this->json, true);
|
$this->data = json_decode($this->json, true);
|
||||||
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
$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
|
public function beginTable(): void
|
||||||
{
|
{
|
||||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||||
$hook = $this->beforePrint;
|
$hookBefore = $this->beforePrintHook;
|
||||||
$this->html .= $hook();
|
$this->html .= $hookBefore($this->data['meta']);
|
||||||
$this->html .= "<table $paramsStr>";
|
$this->html .= "<table $paramsStr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function beforePrint(\Closure $closure): void
|
|
||||||
{
|
|
||||||
$this->beforePrint = $closure;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createThead(): void
|
public function createThead(): void
|
||||||
{
|
{
|
||||||
|
$columnKeys = [];
|
||||||
if (!isset($this->data['meta']['columns'])) {
|
if (!isset($this->data['meta']['columns'])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->html .= "<thead><tr>";
|
$this->html .= "<thead><tr>";
|
||||||
|
if (!$this->issetColumn("id")) {
|
||||||
$this->html .= "<th>" . "ID" . "</th>";
|
$this->html .= "<th>" . "ID" . "</th>";
|
||||||
foreach ($this->data['meta']['columns'] as $key => $column) {
|
$columnKeys[] = "id";
|
||||||
if ($this->is_fillable($key)) {
|
|
||||||
$this->html .= "<th>" . $column . "</th>";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$columnKeys = $this->getColumnKeys($columnKeys);
|
||||||
|
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
||||||
|
$this->getCustomHeadColumn();
|
||||||
|
if ($this->showActionColumn) {
|
||||||
$this->html .= "<th>Действия</th></th></tr></thead>";
|
$this->html .= "<th>Действия</th></th></tr></thead>";
|
||||||
}
|
}
|
||||||
|
if ($this->filters) {
|
||||||
|
$this->getFilters($columnKeys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function createTbody(): void
|
public function createTbody(): void
|
||||||
{
|
{
|
||||||
if ($this->data['data']) {
|
if ($this->data['data']) {
|
||||||
|
|
||||||
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
|
$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->html .= "<tr>";
|
||||||
$this->count += 1;
|
$this->count += 1;
|
||||||
$this->html .= '<td><a href=' . $this->baseUrl . "/form-item/" . $col["id"] . '>' . $this->count . '</a></td>';
|
$id = $row["id"] ?? $this->count;
|
||||||
foreach ($col as $key => $row) {
|
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->issetColumn($key) and $this->is_fillable($key)) {
|
||||||
if ($this->beforePrintCell) {
|
if($this->beforePrintCell) {
|
||||||
$hook = $this->beforePrintCell;
|
$cell = $this->getCustomCell($key, $cell);
|
||||||
$row = $hook($key, $row);
|
|
||||||
}
|
}
|
||||||
|
$this->html .= "<td>" . $cell . "</td>";
|
||||||
$this->html .= "<td>" . $row . "</td>";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->getCustomColumns($row["id"] ?? null);
|
||||||
$actions = $this->getActions($col["id"]);
|
if ($this->showActionColumn) {
|
||||||
|
if (isset($row["id"])) {
|
||||||
|
$actions = $this->getActions($row["id"]);
|
||||||
|
|
||||||
$this->html .= "<td>$actions</td></tr>";
|
$this->html .= "<td>$actions</td></tr>";
|
||||||
|
} else {
|
||||||
|
$this->html .= "<td></td></tr>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
private function setActions(): void
|
||||||
{
|
{
|
||||||
@ -105,9 +134,45 @@ class ListJsonTable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function issetColumn($column)
|
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'])) {
|
if (isset($this->data['meta']['columns'])) {
|
||||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
|
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
|
||||||
@ -120,14 +185,30 @@ class ListJsonTable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function is_fillable($column): bool
|
private function issetFilter($filter): bool
|
||||||
{
|
{
|
||||||
if (isset($this->data['meta']['fillable'])){
|
if (isset($this->data['filters'])) {
|
||||||
if (in_array($column, $this->data['meta']['fillable'])) {
|
foreach ($this->data['filters'] as $key => $currentFilter) {
|
||||||
|
if (is_array($currentFilter)) {
|
||||||
|
return false;
|
||||||
|
} elseif (is_string($currentFilter)) {
|
||||||
|
if ($currentFilter === $filter) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,8 +226,26 @@ class ListJsonTable
|
|||||||
return $actions;
|
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->beginTable();
|
||||||
$this->createThead();
|
$this->createThead();
|
||||||
$this->createTbody();
|
$this->createTbody();
|
||||||
@ -155,7 +254,6 @@ class ListJsonTable
|
|||||||
|
|
||||||
public function tableAction($json): void
|
public function tableAction($json): void
|
||||||
{
|
{
|
||||||
|
|
||||||
$tableJson = json_decode($json, true);
|
$tableJson = json_decode($json, true);
|
||||||
|
|
||||||
foreach ($tableJson as $key => $value) {
|
foreach ($tableJson as $key => $value) {
|
||||||
@ -166,18 +264,33 @@ class ListJsonTable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function endTable(): void
|
public function endTable(): void
|
||||||
{
|
{
|
||||||
$this->html .= "</table>";
|
$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;
|
foreach ($data as $key => $value) {
|
||||||
|
$this->beforePrintCell[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setBeforePrintCell(\Closure $closure): void
|
|
||||||
{
|
|
||||||
$this->beforePrintCell = $closure;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,6 +3,9 @@
|
|||||||
namespace Itguild\Tables;
|
namespace Itguild\Tables;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use http\Message;
|
||||||
|
|
||||||
class Pagination
|
class Pagination
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -14,17 +17,24 @@ class Pagination
|
|||||||
|
|
||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
|
|
||||||
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct(array $options)
|
||||||
{
|
{
|
||||||
$this->countItem = $countItem;
|
if (!$options['countItem']) {
|
||||||
$this->perPage = $perPage;
|
throw new Exception(message: "countItem is not valid");
|
||||||
$this->currentPage = $currentPage;
|
}
|
||||||
$this->baseUrl = $baseUrl;
|
$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;
|
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
|
||||||
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
||||||
@ -42,12 +52,12 @@ class Pagination
|
|||||||
echo $this->html;
|
echo $this->html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch()
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
return $this->html;
|
return $this->html;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTemplate()
|
private function getTemplate(): string
|
||||||
{
|
{
|
||||||
return '<nav aria-label="Page navigation example">
|
return '<nav aria-label="Page navigation example">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
|
@ -4,60 +4,54 @@ namespace Itguild\Tables;
|
|||||||
|
|
||||||
use Itguild\Tables\traits\CreateParams;
|
use Itguild\Tables\traits\CreateParams;
|
||||||
|
|
||||||
class ViewJsonTable
|
class ViewJsonTable extends JasonTable
|
||||||
{
|
{
|
||||||
use CreateParams;
|
use CreateParams;
|
||||||
|
|
||||||
private array $data;
|
private array $data;
|
||||||
|
|
||||||
private string $html = "";
|
|
||||||
private string $json;
|
private string $json;
|
||||||
|
|
||||||
private \Closure|false $beforePrintCell;
|
|
||||||
private \Closure|false $beforePrintTable;
|
|
||||||
|
|
||||||
private \Closure|false $afterPrintTable;
|
|
||||||
|
|
||||||
private array $dataJson;
|
private array $dataJson;
|
||||||
public function __construct($json)
|
public function __construct($json)
|
||||||
{
|
{
|
||||||
|
$this->beforePrintCell = [];
|
||||||
$this->json = $json;
|
$this->json = $json;
|
||||||
$this->data = json_decode($this->json, true);
|
$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
|
public function beginTable(): void
|
||||||
{
|
{
|
||||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||||
$hook = $this->beforePrintTable;
|
|
||||||
$this->html = $hook();
|
//Хук перед выводом ячейки
|
||||||
|
if (isset($this->beforePrintHook)){
|
||||||
|
$hook = $this->beforePrintHook;
|
||||||
|
$this->html .= $hook();
|
||||||
|
}
|
||||||
|
|
||||||
$this->html .= "<table $paramsStr>";
|
$this->html .= "<table $paramsStr>";
|
||||||
}
|
}
|
||||||
public function createColum(): string
|
public function createRows(): string
|
||||||
{
|
{
|
||||||
foreach ($this->data['meta']['columns'] as $key => $column){
|
foreach ($this->data['meta']['rows'] as $key => $row){
|
||||||
if ($this->issetColumn($key)){
|
if ($this->issetRow($key)){
|
||||||
if ($this->beforePrintCell){
|
if ($this->beforePrintCell) {
|
||||||
$hook = $this->beforePrintCell;
|
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
|
||||||
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
|
|
||||||
}
|
}
|
||||||
|
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
|
||||||
$this->html .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $this->html;
|
return $this->html;
|
||||||
}
|
}
|
||||||
|
private function issetRow($column): bool
|
||||||
private function issetColumn($column)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isset($this->data['meta']['columns'])){
|
if (isset($this->data['meta']['rows'])){
|
||||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
|
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
|
||||||
if ($key === $column){
|
if ($key === $column){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -67,41 +61,28 @@ class ViewJsonTable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function endTable()
|
public function endTable(): void
|
||||||
{
|
{
|
||||||
$this->html .= "</table>";
|
$this->html .= "</table>";
|
||||||
$hookAfter = $this->afterPrintTable;
|
|
||||||
$this->html .= $hookAfter();
|
|
||||||
|
|
||||||
|
if(isset($this->afterPrintHook)){
|
||||||
|
$hookAfter = $this->afterPrintHook;
|
||||||
|
$this->html .= $hookAfter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(): void
|
||||||
{
|
{
|
||||||
$this->beginTable();
|
$this->beginTable();
|
||||||
$this->createColum();
|
$this->createRows();
|
||||||
$this->endTable();
|
$this->endTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function beforeTable(\Closure $closure): void
|
public function rows(array $data): void
|
||||||
{
|
{
|
||||||
$this->beforePrintTable = $closure;
|
foreach ($data as $key => $value) {
|
||||||
|
$this->beforePrintCell[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function afterTable(\Closure $closure): void
|
|
||||||
{
|
|
||||||
$this->afterPrintTable = $closure;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function render()
|
|
||||||
{
|
|
||||||
|
|
||||||
echo $this->html;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setBeforePrintCell(\Closure $closure): void
|
|
||||||
{
|
|
||||||
$this->beforePrintCell = $closure;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user