Compare commits

..

24 Commits

Author SHA1 Message Date
5f46431d45 fix filter row 2024-10-11 16:31:11 +03:00
02a3e52b7d fix pagination 2024-09-24 15:42:55 +03:00
d747203d99 custom action 2024-09-02 22:34:25 +03:00
ad18d94489 table filter fix 2024-08-29 13:13:51 +03:00
453917ea1a table filter fix 2024-08-29 13:08:43 +03:00
1f2b93baf1 table filter fix 2024-08-29 12:41:17 +03:00
5cb5ac839d table params fix 2024-08-29 12:07:54 +03:00
cf2cada74e fix date format 2024-08-29 11:49:46 +03:00
c816a7f14b some fix 2024-08-29 11:36:44 +03:00
0960a7b1fd fix filter 2024-08-28 15:39:35 +03:00
f20f408f12 add value to filter 2024-08-28 15:09:34 +03:00
4a15305505 some fix 2024-08-28 14:24:25 +03:00
5064042929 fix filter 2024-08-28 14:21:00 +03:00
f03ec200ac some fix filters 2024-08-28 12:12:52 +03:00
b4dc2f6ab1 filter class 2024-08-23 14:52:22 +03:00
77a306a0f6 add custom columns filter 2024-08-22 13:08:25 +03:00
af1b996766 filter 2024-08-22 11:53:13 +03:00
96b249eb51 add integer format 2024-08-13 14:30:00 +03:00
19e994ef38 filetrs 2024-08-12 15:39:26 +03:00
38d5deb36a formats and style 2024-08-08 17:08:46 +03:00
d0f136e484 add formats 2024-08-08 16:31:26 +03:00
7dbff619f5 merge 2024-08-08 14:24:53 +03:00
02fa193652 column format 2024-08-08 14:21:47 +03:00
a2284e3bf2 columns update 2024-08-08 13:26:45 +03:00
17 changed files with 386 additions and 98 deletions

View File

@ -4,6 +4,7 @@
"columns": {
"id": "ID",
"email": "Email",
"created_at": "Дата создания",
"description": "Описание 1",
"description2": "Описание 2",
"status": "Статус"
@ -13,24 +14,22 @@
"perPage": "5",
"currentPage": "1",
"showActionColumn": true,
"filter": true,
"showFiltersRow": true,
"total": 10,
"paginationPrefix": "/page",
"params": {"class": "table table-bordered", "border": "1"}
},
"filters": [
"email", "description"
],
"filters": ["email", "status"],
"data": [
{"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}
{"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}
]
}

View File

@ -1,27 +1,64 @@
<?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([
$table->columns([
'status' => function ($cell) {
"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>";
},
'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>";
});
@ -31,6 +68,9 @@ $table->addColumn("Колонка 33", "k33", function ($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();

View File

@ -1,48 +0,0 @@
<?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>';
}
}

19
src/Filter/Filter.php Normal file
View 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();
}

View 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>";
}
}

View 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>";
}
}

View 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>";
}
}

View 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
View 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,
];
}
}

View File

@ -4,7 +4,7 @@ namespace Itguild\Tables;
class JasonTable
{
protected string $html = "";
public string $html = "";
protected \Closure|array|false $beforePrintCell;
protected \Closure|false $afterPrintHook;
protected \Closure|false $beforePrintHook;
@ -23,12 +23,25 @@ class JasonTable
public function getCustomCell(string $key, string|null $cell)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $currentKey => $closure) {
if ($key == $currentKey) {
$hook = $closure;
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);
@ -46,4 +59,36 @@ class JasonTable
{
$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;
}
}

View File

@ -6,6 +6,7 @@ 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;
@ -21,7 +22,8 @@ class ListJsonTable extends JasonTable
private bool $pagination = true;
private bool $showActionColumn = true;
private bool|array $filters = false;
private bool $showFiltersRow = true;
private bool|array $filters = [];
private array $actionsArray = [];
private array $customActionsArray = [];
@ -35,7 +37,8 @@ class ListJsonTable extends JasonTable
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->pagination = $this->data['meta']['pagination'] ?? true;
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
$this->filters = $this->data['filters'] ?? false;
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
$this->filters = $this->data['filters'] ?? [];
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
@ -62,14 +65,15 @@ class ListJsonTable extends JasonTable
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
$columnKeys = $this->getColumnKeys($columnKeys);
$columnKeys = array_merge($columnKeys, $this->getColumnKeys());
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr></thead>";
$this->html .= "<th>Действия</th></th></tr>";
}
if ($this->filters) {
if ($this->showFiltersRow) {
$this->getFilters($columnKeys);
$this->html .= "</thead>";
}
}
@ -78,6 +82,8 @@ class ListJsonTable extends JasonTable
if ($this->data['data']) {
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $row) {
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $row);
$this->html .= "<tr>";
$this->count += 1;
$id = $row["id"] ?? $this->count;
@ -86,16 +92,19 @@ class ListJsonTable extends JasonTable
}
foreach ($row as $key => $cell) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if($this->beforePrintCell) {
if ($this->beforePrintCell) {
if ($key === "id" and $cell === 0) {
$cell = $this->count;
}
$cell = $this->getCustomCell($key, $cell);
}
$this->html .= "<td>" . $cell . "</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["id"]);
$actions = $this->getActions($row);
$this->html .= "<td>$actions</td></tr>";
} else {
@ -106,7 +115,7 @@ class ListJsonTable extends JasonTable
}
}
public function addAction(string $actionColumn): void
public function addAction(string|\Closure $actionColumn): void
{
$this->customActionsArray[] = $actionColumn;
}
@ -137,6 +146,18 @@ class ListJsonTable extends JasonTable
$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) {
@ -161,8 +182,10 @@ class ListJsonTable extends JasonTable
return $keys;
}
private function getColumnKeys(array $columnKeys): array
private function getColumnKeys(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
@ -172,6 +195,17 @@ class ListJsonTable extends JasonTable
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'])) {
@ -185,10 +219,10 @@ class ListJsonTable extends JasonTable
return false;
}
private function issetFilter($filter): bool
private function issetFilter(string $filter): bool
{
if (isset($this->data['filters'])) {
foreach ($this->data['filters'] as $key => $currentFilter) {
foreach ($this->data['filters'] as $currentFilter) {
if (is_array($currentFilter)) {
return false;
} elseif (is_string($currentFilter)) {
@ -198,7 +232,11 @@ class ListJsonTable extends JasonTable
}
}
}
if (isset($this->beforePrintCell[$filter]) and is_array($this->beforePrintCell[$filter])) {
if (isset($this->beforePrintCell[$filter]['filter'])) {
return true;
}
}
return false;
}
@ -215,12 +253,16 @@ class ListJsonTable extends JasonTable
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);
if (is_string($item)) {
$objItem = new $item($this->baseUrl, $row['id']);
$actions .= $objItem->fetch();
} else {
$actions .= $item($row, $this->baseUrl);
}
}
return $actions;
@ -229,14 +271,28 @@ class ListJsonTable extends JasonTable
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>";
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 {
} else {
$this->html .= "<td></td>";
}
}
if ($this->showActionColumn) {
$this->html .= "<td></td>";
}
$this->html .= "</form></tr>";
}
@ -273,7 +329,7 @@ class ListJsonTable extends JasonTable
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter($this->data['meta']);
if ($this->pagination) {
if ($this->pagination && $this->data['data']) {
$options = [
'countItem' => $this->data['meta']['total'],
'perPage' => $this->data['meta']['perPage'] ?? 10,
@ -293,4 +349,5 @@ class ListJsonTable extends JasonTable
$this->beforePrintCell[$key] = $value;
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Itguild\Tables\formats;
abstract class BaseFormat
{
abstract static function fetch(string|null $data, string $options = "");
}

View 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;
}
}

View 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;
}
}

View 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);
}
}

View 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;
}
}

View 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);
}
}