Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
5f46431d45 | |||
02a3e52b7d | |||
d747203d99 | |||
ad18d94489 | |||
453917ea1a | |||
1f2b93baf1 | |||
5cb5ac839d | |||
cf2cada74e | |||
c816a7f14b | |||
0960a7b1fd | |||
f20f408f12 | |||
4a15305505 | |||
5064042929 | |||
f03ec200ac | |||
b4dc2f6ab1 | |||
77a306a0f6 | |||
af1b996766 | |||
96b249eb51 | |||
19e994ef38 | |||
38d5deb36a | |||
d0f136e484 | |||
7dbff619f5 | |||
02fa193652 | |||
a2284e3bf2 | |||
9e7c83b680 | |||
b374744261 | |||
c6918c85b3 | |||
d98ddded07 | |||
1bd82eb40b | |||
4a12eeccfd | |||
71b2fdfb6b | |||
f53346610e | |||
67a3f5770e | |||
6d5f6a8665 | |||
2c1db8573c | |||
8d1dce0222 | |||
433cc85f54 | |||
39931b257b | |||
b7fe32b91d | |||
320e1070ba | |||
8bc8a75a53 | |||
f5f6504545 | |||
647fe34d11 | |||
1714afcf3f | |||
d6d703aaa6 | |||
b7fea4122c | |||
48e6d645d9 | |||
85c4a850b1 |
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,34 @@
|
||||
"meta": {
|
||||
"title": "forma 1",
|
||||
"columns": {
|
||||
"id": "ID",
|
||||
"email": "Email",
|
||||
"created_at": "Дата создания",
|
||||
"description": "Описание 1",
|
||||
"description2": "Описание 2"
|
||||
"description2": "Описание 2",
|
||||
"status": "Статус"
|
||||
},
|
||||
"actions": ["view"],
|
||||
"pagination": true,
|
||||
"perPage": "5",
|
||||
"currentPage": "1",
|
||||
"showActionColumn": true,
|
||||
"showFiltersRow": true,
|
||||
"total": 10,
|
||||
"paginationPrefix": "/page",
|
||||
"params": {"class": "table table-bordered", "border": "1"}
|
||||
},
|
||||
"filters": ["email", "status"],
|
||||
"data": [
|
||||
{"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
|
||||
|
||||
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"},
|
||||
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": "1"},
|
||||
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "created_at": "19.06.2024", "status": 2},
|
||||
{"id": 4,"email":"fas4@mail.ru","description":"fafd <b>dsfgsd</b> vcbgdfs","description2":"ffdghdas", "created_at": "20.06.2024", "status": 1},
|
||||
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "created_at": "21.06.2024", "status": 1},
|
||||
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "created_at": "22.06.2024", "status": 1},
|
||||
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "created_at": "23.06.2024", "status": 0},
|
||||
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "created_at": "24.06.2024", "status": 1},
|
||||
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "created_at": "25.06.2024", "status": 99},
|
||||
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "created_at": "25.06.2024", "status": 1}
|
||||
]
|
||||
}
|
@ -1,11 +1,86 @@
|
||||
<?php
|
||||
|
||||
require_once "../vendor/autoload.php";
|
||||
ini_set("display_errors", true);
|
||||
error_reporting(-1);
|
||||
|
||||
require_once "../vendor/autoload.php";
|
||||
|
||||
use Itguild\Tables\Filter\InputDateFilter;
|
||||
use Itguild\Tables\Filter\InputTextFilter;
|
||||
use Itguild\Tables\Filter\SelectFilter;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
|
||||
$json = file_get_contents('simple.json');
|
||||
|
||||
$table = new ListJsonTable($json);
|
||||
|
||||
$table->columns([
|
||||
"created_at" => [
|
||||
"format" => "date:Y-m-d",
|
||||
'filter' => [
|
||||
'class' => InputDateFilter::class,
|
||||
]
|
||||
],
|
||||
'description' => [
|
||||
"format" => "html",
|
||||
"style" => ["width" => "300px"],
|
||||
"filter" => [
|
||||
'class' => InputTextFilter::class,
|
||||
'value' => 'value'
|
||||
],
|
||||
"value" => function ($cell) {
|
||||
return "<span style='color: sienna'>$cell</span>";
|
||||
}
|
||||
],
|
||||
'description2' => [
|
||||
"format" => "html",
|
||||
"filter" => [
|
||||
'class' => SelectFilter::class,
|
||||
'param' => ['black', 'red', 'green', 'blue', 'yellow'],
|
||||
'value' => 'red'
|
||||
],
|
||||
],
|
||||
'status' => [
|
||||
"filter" => [
|
||||
'class' => SelectFilter::class,
|
||||
'param' => getStatusLabel(),
|
||||
'value' => 'Активный'
|
||||
],
|
||||
"value" => function ($cell) {
|
||||
return getStatusLabel()[$cell];
|
||||
}],
|
||||
'k33' => [
|
||||
"format" => "integer",
|
||||
'filter' => [
|
||||
'class' => \Itguild\Tables\Filter\InputRangeFilter::class,
|
||||
'param' => ['min' => 0, 'max' => 10],
|
||||
]
|
||||
],
|
||||
'email' => function ($cell) {
|
||||
return "<span style='color: aqua'>$cell</span>";
|
||||
},
|
||||
]);
|
||||
$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->addAction(function($row, $url){
|
||||
return "<a href='mailto:". $row['email'] ."'>Написать</a>";
|
||||
});
|
||||
$table->create();
|
||||
$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();
|
19
src/Filter/Filter.php
Normal file
19
src/Filter/Filter.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\Filter;
|
||||
|
||||
abstract class Filter
|
||||
{
|
||||
public string $html = '';
|
||||
public string|array $param;
|
||||
public string $name;
|
||||
public string $value;
|
||||
public function __construct(array $source)
|
||||
{
|
||||
$this->param = $source['param'] ?? '';
|
||||
$this->name = $source['name'];
|
||||
$this->value = $source['value'] ?? '';
|
||||
}
|
||||
|
||||
abstract public function fetch();
|
||||
}
|
12
src/Filter/InputDateFilter.php
Normal file
12
src/Filter/InputDateFilter.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\Filter;
|
||||
|
||||
class InputDateFilter extends Filter
|
||||
{
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return "<td><input type='date' name='$this->name' value ='$this->value'></td>";
|
||||
}
|
||||
}
|
23
src/Filter/InputRangeFilter.php
Normal file
23
src/Filter/InputRangeFilter.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\Filter;
|
||||
|
||||
use Itguild\Tables\Filter\Filter;
|
||||
|
||||
class InputRangeFilter extends Filter
|
||||
{
|
||||
private int $min;
|
||||
private int $max;
|
||||
|
||||
public function __construct(array $source)
|
||||
{
|
||||
parent::__construct($source);
|
||||
$this->min = $this->param['min'] ?? 0;
|
||||
$this->max = $this->param['max'] ?? 100;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return "<td>" . $this->min . " <input type='range' name='$this->name' min= '$this->min' max='$this->max' value ='$this->value'>" . $this->max . "</td>";
|
||||
}
|
||||
}
|
11
src/Filter/InputTextFilter.php
Normal file
11
src/Filter/InputTextFilter.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\Filter;
|
||||
|
||||
class InputTextFilter extends \Itguild\Tables\Filter\Filter
|
||||
{
|
||||
public function fetch()
|
||||
{
|
||||
return "<td><input type='text' name='$this->name' value ='$this->value'></td>";
|
||||
}
|
||||
}
|
23
src/Filter/SelectFilter.php
Normal file
23
src/Filter/SelectFilter.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\Filter;
|
||||
|
||||
use Itguild\Tables\Filter\Filter;
|
||||
|
||||
class SelectFilter extends Filter
|
||||
{
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
$this->html = "<td><select name='$this->name'>";
|
||||
foreach ($this->param as $value) {
|
||||
if ($value === $this->value) {
|
||||
$this->html .= "<option value='$value' selected>$value</option>";
|
||||
} else {
|
||||
$this->html .= "<option value='$value'>$value</option>";
|
||||
}
|
||||
}
|
||||
$this->html .= "value='$this->value'</select></td>";
|
||||
return $this->html;
|
||||
}
|
||||
}
|
25
src/FormatMapper.php
Normal file
25
src/FormatMapper.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
use Itguild\Tables\formats\DateFormat;
|
||||
use Itguild\Tables\formats\HtmlFormat;
|
||||
use Itguild\Tables\formats\IntegerFormat;
|
||||
use Itguild\Tables\formats\PhoneNumberFormat;
|
||||
use Itguild\Tables\formats\TextFormat;
|
||||
|
||||
class FormatMapper
|
||||
{
|
||||
|
||||
public static function getFormat(): array
|
||||
{
|
||||
return [
|
||||
'text' => TextFormat::class,
|
||||
'date' => DateFormat::class,
|
||||
'html' => HtmlFormat::class,
|
||||
'phoneNumber' => PhoneNumberFormat::class,
|
||||
'integer' => IntegerFormat::class,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
94
src/JasonTable.php
Normal file
94
src/JasonTable.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
class JasonTable
|
||||
{
|
||||
public 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)) {
|
||||
if (isset($this->beforePrintCell[$key])) {
|
||||
if (is_array($this->beforePrintCell[$key])){
|
||||
if (!isset($this->beforePrintCell[$key]['format'])){
|
||||
$this->beforePrintCell[$key]['format'] = "text";
|
||||
}
|
||||
$format = explode(":", $this->beforePrintCell[$key]['format']);
|
||||
$formatClass = FormatMapper::getFormat()[$format[0]];
|
||||
$cell = $formatClass::fetch($cell, $format[1] ?? "");
|
||||
if (isset($this->beforePrintCell[$key]['value'])){
|
||||
$hook = $this->beforePrintCell[$key]['value'];
|
||||
$cell = $hook($cell);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$hook = $this->beforePrintCell[$key];
|
||||
$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;
|
||||
}
|
||||
|
||||
protected function getParamFromCustomColumn(string $column, string $paramName)
|
||||
{
|
||||
if (is_array($this->beforePrintCell)) {
|
||||
if (isset($this->beforePrintCell[$column])) {
|
||||
if (is_array($this->beforePrintCell[$column])){
|
||||
if (isset($this->beforePrintCell[$column][$paramName])){
|
||||
return $this->beforePrintCell[$column][$paramName];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getStyleFromCustomColumn(string $column)
|
||||
{
|
||||
$styleStr = '';
|
||||
$style = $this->getParamFromCustomColumn($column, "style");
|
||||
if (is_array($style)){
|
||||
foreach ($style as $key => $value){
|
||||
$styleStr .= $key . ": " . $value . ";";
|
||||
}
|
||||
}
|
||||
else {
|
||||
$styleStr = $style;
|
||||
}
|
||||
|
||||
return $styleStr;
|
||||
}
|
||||
|
||||
}
|
@ -2,100 +2,130 @@
|
||||
|
||||
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\Filter\InputTextFilter;
|
||||
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 $showFiltersRow = true;
|
||||
private bool|array $filters = [];
|
||||
|
||||
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->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
|
||||
$this->filters = $this->data['filters'] ?? [];
|
||||
$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>";
|
||||
$this->html .= "<th>" . "ID" . "</th>";
|
||||
foreach ($this->data['meta']['columns'] as $key => $column) {
|
||||
if ($this->is_fillable($key)) {
|
||||
$this->html .= "<th>" . $column . "</th>";
|
||||
}
|
||||
if (!$this->issetColumn("id")) {
|
||||
$this->html .= "<th>" . "ID" . "</th>";
|
||||
$columnKeys[] = "id";
|
||||
}
|
||||
$columnKeys = array_merge($columnKeys, $this->getColumnKeys());
|
||||
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
||||
$this->getCustomHeadColumn();
|
||||
if ($this->showActionColumn) {
|
||||
$this->html .= "<th>Действия</th></th></tr>";
|
||||
}
|
||||
if ($this->showFiltersRow) {
|
||||
$this->getFilters($columnKeys);
|
||||
$this->html .= "</thead>";
|
||||
}
|
||||
$this->html .= "<th>Действия</th></th></tr></thead>";
|
||||
}
|
||||
|
||||
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) {
|
||||
//сортируем колнки согласно массиву колонок из json
|
||||
$row = array_merge(array_flip($this->getFillableColumns()), $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);
|
||||
if ($key === "id" and $cell === 0) {
|
||||
$cell = $this->count;
|
||||
}
|
||||
$cell = $this->getCustomCell($key, $cell);
|
||||
}
|
||||
|
||||
$this->html .= "<td>" . $row . "</td>";
|
||||
$this->html .= "<td style='" . $this->getStyleFromCustomColumn($key) . "'>" . $cell . "</td>";
|
||||
}
|
||||
}
|
||||
$this->getCustomColumns($row["id"] ?? null);
|
||||
if ($this->showActionColumn) {
|
||||
if (isset($row["id"])) {
|
||||
$actions = $this->getActions($row);
|
||||
|
||||
$actions = $this->getActions($col["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
|
||||
public function addAction(string|\Closure $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 +146,66 @@ class ListJsonTable
|
||||
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
||||
}
|
||||
|
||||
private function getCurrentFilter(string $column)
|
||||
{
|
||||
if (isset($this->beforePrintCell[$column]) and is_array($this->beforePrintCell[$column])) {
|
||||
if (isset($this->beforePrintCell[$column]['filter'])) {
|
||||
if (isset($this->beforePrintCell[$column]['filter']) and is_array($this->beforePrintCell[$column]['filter'])) {
|
||||
return $this->beforePrintCell[$column]['filter'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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 = [];
|
||||
foreach ($this->data['meta']['columns'] as $key => $column) {
|
||||
if ($this->is_fillable($key)) {
|
||||
$this->html .= "<th>" . $column . "</th>";
|
||||
$columnKeys[] = $key;
|
||||
}
|
||||
}
|
||||
return $columnKeys;
|
||||
}
|
||||
|
||||
private function getFillableColumns(): array
|
||||
{
|
||||
$columnKeys = [];
|
||||
foreach ($this->data['meta']['columns'] as $key => $column) {
|
||||
if ($this->is_fillable($key)) {
|
||||
$columnKeys[] = $key;
|
||||
}
|
||||
}
|
||||
return $columnKeys;
|
||||
}
|
||||
|
||||
private function issetColumn($column): bool
|
||||
{
|
||||
if (isset($this->data['meta']['columns'])) {
|
||||
@ -129,6 +219,27 @@ class ListJsonTable
|
||||
return false;
|
||||
}
|
||||
|
||||
private function issetFilter(string $filter): bool
|
||||
{
|
||||
if (isset($this->data['filters'])) {
|
||||
foreach ($this->data['filters'] as $currentFilter) {
|
||||
if (is_array($currentFilter)) {
|
||||
return false;
|
||||
} elseif (is_string($currentFilter)) {
|
||||
if ($currentFilter === $filter) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($this->beforePrintCell[$filter]) and is_array($this->beforePrintCell[$filter])) {
|
||||
if (isset($this->beforePrintCell[$filter]['filter'])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function is_fillable($column): bool
|
||||
{
|
||||
if (isset($this->data['meta']['fillable'])) {
|
||||
@ -142,19 +253,55 @@ class ListJsonTable
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getActions(int $id): string
|
||||
private function getActions(array $row): string
|
||||
{
|
||||
$actions = "";
|
||||
foreach ($this->actionsArray as $item) {
|
||||
$objItem = new $item($this->baseUrl, $id);
|
||||
$actions .= $objItem->fetch();
|
||||
if (is_string($item)) {
|
||||
$objItem = new $item($this->baseUrl, $row['id']);
|
||||
$actions .= $objItem->fetch();
|
||||
} else {
|
||||
$actions .= $item($row, $this->baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
$filter = $this->getCurrentFilter($key);
|
||||
$params = [
|
||||
'param' => $filter['param'] ?? '',
|
||||
'name' => $key,
|
||||
'value' => $filter['value'] ?? '',
|
||||
];
|
||||
if ($filter) {
|
||||
$class = new $filter['class']($params);
|
||||
$this->html .= $class->fetch();
|
||||
} else {
|
||||
$class = new InputTextFilter($params);
|
||||
$this->html .= $class->fetch();
|
||||
}
|
||||
} else {
|
||||
$this->html .= "<td></td>";
|
||||
}
|
||||
}
|
||||
if ($this->showActionColumn) {
|
||||
$this->html .= "<td></td>";
|
||||
}
|
||||
$this->html .= "</form></tr>";
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(): void
|
||||
{
|
||||
$this->setActions();
|
||||
$this->beginTable();
|
||||
$this->createThead();
|
||||
$this->createTbody();
|
||||
@ -163,7 +310,6 @@ class ListJsonTable
|
||||
|
||||
public function tableAction($json): void
|
||||
{
|
||||
|
||||
$tableJson = json_decode($json, true);
|
||||
|
||||
foreach ($tableJson as $key => $value) {
|
||||
@ -174,18 +320,34 @@ class ListJsonTable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function endTable(): void
|
||||
{
|
||||
$this->html .= "</table>";
|
||||
$hookAfter = $this->afterPrintHook;
|
||||
$this->html .= $hookAfter($this->data['meta']);
|
||||
|
||||
if ($this->pagination && $this->data['data']) {
|
||||
$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;
|
||||
|
||||
|
||||
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">
|
||||
|
@ -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 = [];
|
||||
$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']);
|
||||
$hook = $this->beforePrintTable;
|
||||
$this->html = $hook();
|
||||
$this->html .= "<table $paramsStr>";
|
||||
}
|
||||
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 .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
|
||||
|
||||
}
|
||||
//Хук перед выводом ячейки
|
||||
if (isset($this->beforePrintHook)){
|
||||
$hook = $this->beforePrintHook;
|
||||
$this->html .= $hook();
|
||||
}
|
||||
|
||||
|
||||
$this->html .= "<table $paramsStr>";
|
||||
}
|
||||
public function createRows(): string
|
||||
{
|
||||
foreach ($this->data['meta']['rows'] as $key => $row){
|
||||
if ($this->issetRow($key)){
|
||||
if ($this->beforePrintCell) {
|
||||
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
|
||||
}
|
||||
$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>";
|
||||
$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->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;
|
||||
}
|
||||
}
|
||||
}
|
10
src/formats/BaseFormat.php
Normal file
10
src/formats/BaseFormat.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
abstract class BaseFormat
|
||||
{
|
||||
|
||||
abstract static function fetch(string|null $data, string $options = "");
|
||||
|
||||
}
|
17
src/formats/DateFormat.php
Normal file
17
src/formats/DateFormat.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class DateFormat extends BaseFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
static function fetch(?string $data, string $options = ""): ?string
|
||||
{
|
||||
return (new DateTimeImmutable($data ?? ""))->format($options) ?? null;
|
||||
}
|
||||
}
|
14
src/formats/HtmlFormat.php
Normal file
14
src/formats/HtmlFormat.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
use Itguild\Tables\formats\BaseFormat;
|
||||
|
||||
class HtmlFormat extends BaseFormat
|
||||
{
|
||||
|
||||
static function fetch(?string $data, string $options = ""): string
|
||||
{
|
||||
return (string)$data;
|
||||
}
|
||||
}
|
14
src/formats/IntegerFormat.php
Normal file
14
src/formats/IntegerFormat.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
use Itguild\Tables\formats\BaseFormat;
|
||||
|
||||
class IntegerFormat extends BaseFormat
|
||||
{
|
||||
|
||||
static function fetch(?string $data, string $options = "")
|
||||
{
|
||||
return intval($data);
|
||||
}
|
||||
}
|
14
src/formats/PhoneNumberFormat.php
Normal file
14
src/formats/PhoneNumberFormat.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
use Itguild\Tables\formats\BaseFormat;
|
||||
|
||||
class PhoneNumberFormat extends BaseFormat
|
||||
{
|
||||
|
||||
static function fetch(?string $data, string $options = "")
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
13
src/formats/TextFormat.php
Normal file
13
src/formats/TextFormat.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\formats;
|
||||
|
||||
class TextFormat extends BaseFormat
|
||||
{
|
||||
|
||||
public static function fetch($data, string $options = ""): string
|
||||
{
|
||||
return strip_tags((string)$data);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user