tables/src/ListJsonTable.php

337 lines
10 KiB
PHP
Raw Normal View History

2024-05-27 12:11:56 +03:00
<?php
2024-05-27 12:59:44 +03:00
namespace Itguild\Tables;
2024-05-27 12:11:56 +03:00
2024-07-31 15:49:40 +03:00
use Exception;
2024-07-15 17:19:30 +03:00
use Itguild\Tables\ActionColumn\ActionColumn;
2024-05-27 12:59:44 +03:00
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\traits\CreateParams;
2024-07-30 12:20:16 +03:00
use JetBrains\PhpStorm\NoReturn;
2024-05-27 12:11:56 +03:00
class ListJsonTable
{
use CreateParams;
private string $html = '';
private string $json;
private int $count = 0;
2024-08-01 15:10:41 +03:00
// private \Closure|false $beforePrintCell;
private array $beforePrintCell;
2024-07-30 12:20:16 +03:00
private \Closure|false $beforePrintHook;
private \Closure|false $afterPrintHook;
2024-05-27 12:11:56 +03:00
private string $baseUrl;
private array $data;
2024-07-31 14:51:39 +03:00
private bool $pagination = true;
2024-07-31 17:11:29 +03:00
private bool $showActionColumn = true;
2024-08-01 13:32:31 +03:00
private bool|array $filters = false;
2024-07-31 14:51:39 +03:00
2024-05-27 12:11:56 +03:00
private array $actionsArray = [];
2024-07-15 17:19:30 +03:00
private array $customActionsArray = [];
2024-07-31 16:32:44 +03:00
private array $customColumnsArray = [];
2024-05-27 12:11:56 +03:00
2024-07-30 12:20:16 +03:00
#[NoReturn] public function __construct(string $json)
2024-05-27 12:11:56 +03:00
{
2024-08-01 15:10:41 +03:00
// $this->beforePrintCell = false;
$this->beforePrintCell = [];
2024-05-27 12:11:56 +03:00
$this->json = $json;
$this->data = json_decode($this->json, true);
2024-05-27 12:59:44 +03:00
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
2024-07-31 14:51:39 +03:00
$this->pagination = $this->data['meta']['pagination'] ?? true;
2024-07-31 17:11:29 +03:00
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
2024-08-01 13:32:31 +03:00
$this->filters = $this->data['filters'] ?? false;
2024-07-30 12:20:16 +03:00
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
2024-05-27 12:11:56 +03:00
}
public function beginTable(): void
{
$paramsStr = $this->createParams($this->data['meta']['params']);
2024-07-30 12:20:16 +03:00
$hookBefore = $this->beforePrintHook;
2024-07-31 12:48:26 +03:00
$this->html .= $hookBefore($this->data['meta']);
2024-05-27 12:11:56 +03:00
$this->html .= "<table $paramsStr>";
}
public function beforePrint(\Closure $closure): void
{
2024-07-30 12:20:16 +03:00
$this->beforePrintHook = $closure;
}
public function afterPrint(\Closure $closure): void
{
$this->afterPrintHook = $closure;
2024-05-27 12:11:56 +03:00
}
public function createThead(): void
{
2024-08-01 13:32:31 +03:00
$columnKeys = [];
2024-05-27 12:11:56 +03:00
if (!isset($this->data['meta']['columns'])) {
return;
}
$this->html .= "<thead><tr>";
2024-08-01 13:32:31 +03:00
if (!$this->issetColumn("id")) {
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
2024-05-27 12:11:56 +03:00
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
2024-08-01 13:32:31 +03:00
$columnKeys[] = $key;
2024-05-27 12:11:56 +03:00
}
}
2024-08-01 13:32:31 +03:00
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
2024-07-31 17:11:29 +03:00
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr></thead>";
}
2024-08-01 13:32:31 +03:00
if ($this->filters) {
$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>";
}
2024-05-27 12:11:56 +03:00
}
public function createTbody(): void
{
if ($this->data['data']) {
2024-08-01 12:28:21 +03:00
// if($this->filter)
// {
2024-08-01 12:50:16 +03:00
// foreach ($this->data['meta']['columns'] as $col) {
// $this->html .= "<tr><td>";
// foreach ($this->data['meta']['filters'] as $filter) {
// if ($this->issetFilter($filter))
// {
// $filters = new Filter($filter, )
// }
// }
2024-08-01 12:28:21 +03:00
//
2024-08-01 12:50:16 +03:00
//
// }
2024-08-01 12:28:21 +03:00
// }
2024-05-27 12:11:56 +03:00
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
2024-07-31 17:11:29 +03:00
foreach ($this->data['data'] as $row) {
2024-05-27 12:11:56 +03:00
$this->html .= "<tr>";
$this->count += 1;
2024-07-31 17:11:29 +03:00
$id = $row["id"] ?? $this->count;
2024-08-01 13:32:31 +03:00
if (!$this->issetColumn("id")) {
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
}
2024-07-31 17:11:29 +03:00
foreach ($row as $key => $ceil) {
2024-05-27 12:11:56 +03:00
if ($this->issetColumn($key) and $this->is_fillable($key)) {
2024-08-01 15:10:41 +03:00
foreach ($this->beforePrintCell as $column => $closure) {
// if ($this->beforePrintCell) {
// $hook = $this->beforePrintCell;
// $ceil = $hook($key, $ceil);
// }
if ($key == $column) {
$hook = $closure;
$ceil = $hook($ceil);
}
2024-05-27 12:11:56 +03:00
}
2024-07-31 17:11:29 +03:00
$this->html .= "<td>" . $ceil . "</td>";
2024-05-27 12:11:56 +03:00
}
}
2024-07-31 17:11:29 +03:00
$this->getCustomColumns($row["id"] ?? null);
2024-08-01 12:50:16 +03:00
2024-07-31 17:11:29 +03:00
if ($this->showActionColumn) {
if (isset($row["id"])) {
$actions = $this->getActions($row["id"]);
2024-07-31 16:32:44 +03:00
2024-07-31 17:11:29 +03:00
$this->html .= "<td>$actions</td></tr>";
} else {
$this->html .= "<td></td></tr>";
}
2024-07-30 12:20:16 +03:00
}
2024-05-27 12:11:56 +03:00
}
}
}
2024-07-16 13:20:48 +03:00
public function addAction(string $actionColumn): void
2024-07-15 17:19:30 +03:00
{
$this->customActionsArray[] = $actionColumn;
}
2024-07-31 17:11:29 +03:00
public function addColumn(string $label, string $key, \Closure $closure): void
{
$this->customColumnsArray[] = ['label' => $label, "key" => $key, "handler" => $closure];
}
2024-07-31 16:32:44 +03:00
2024-05-27 12:11:56 +03:00
private function setActions(): void
{
if (isset($this->data['meta']['actions'])) {
foreach ($this->data['meta']['actions'] as $action) {
switch ($action) {
case 'view':
$this->actionsArray[] = ViewActionColumn::class;
break;
case 'delete':
$this->actionsArray[] = DeleteActionColumn::class;
break;
case 'edit':
$this->actionsArray[] = EditActionColumn::class;
break;
}
}
}
2024-07-15 17:19:30 +03:00
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
2024-05-27 12:11:56 +03:00
}
2024-07-31 17:11:29 +03:00
public function getCustomColumns($id = null): void
{
foreach ($this->customColumnsArray as $item) {
2024-08-01 13:32:31 +03:00
$this->html .= "<td>" . $item['handler']($id) . "</td>";
2024-07-31 17:11:29 +03:00
}
}
public function getCustomHeadColumn(): void
{
foreach ($this->customColumnsArray as $item) {
2024-08-01 13:32:31 +03:00
$this->html .= "<th>" . $item['label'] . "</th>";
2024-07-31 17:11:29 +03:00
}
}
2024-08-01 15:10:41 +03:00
protected function getCustomColumnKeys(): array
2024-08-01 13:32:31 +03:00
{
$keys = [];
foreach ($this->customColumnsArray as $item) {
$keys[] = $item['key'];
}
return $keys;
}
2024-07-15 17:19:30 +03:00
private function issetColumn($column): bool
2024-05-27 12:11:56 +03:00
{
if (isset($this->data['meta']['columns'])) {
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
if ($key === $column) {
return true;
}
}
}
return false;
}
2024-08-01 12:50:16 +03:00
private function issetFilter($filter): bool
{
2024-08-01 13:32:31 +03:00
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;
}
2024-08-01 12:50:16 +03:00
}
}
}
2024-08-01 13:32:31 +03:00
2024-08-01 12:50:16 +03:00
return false;
}
2024-05-27 12:11:56 +03:00
private function is_fillable($column): bool
{
2024-07-15 15:54:11 +03:00
if (isset($this->data['meta']['fillable'])) {
2024-05-27 12:11:56 +03:00
if (in_array($column, $this->data['meta']['fillable'])) {
return true;
}
2024-07-15 15:54:11 +03:00
} else {
2024-05-27 12:11:56 +03:00
return true;
}
return false;
}
private function getActions(int $id): string
{
$actions = "";
foreach ($this->actionsArray as $item) {
$objItem = new $item($this->baseUrl, $id);
$actions .= $objItem->fetch();
}
return $actions;
}
2024-07-31 15:49:40 +03:00
/**
* @throws Exception
*/
2024-07-16 13:26:29 +03:00
public function create(): void
2024-05-27 12:11:56 +03:00
{
2024-07-16 13:26:29 +03:00
$this->setActions();
2024-05-27 12:11:56 +03:00
$this->beginTable();
$this->createThead();
$this->createTbody();
$this->endTable();
}
public function tableAction($json): void
{
$tableJson = json_decode($json, true);
foreach ($tableJson as $key => $value) {
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>" . $value . "</td>";
echo "</tr>";
}
}
2024-07-31 15:49:40 +03:00
/**
* @throws Exception
*/
2024-05-27 12:11:56 +03:00
public function endTable(): void
{
$this->html .= "</table>";
2024-07-30 12:20:16 +03:00
$hookAfter = $this->afterPrintHook;
2024-07-31 12:48:26 +03:00
$this->html .= $hookAfter($this->data['meta']);
2024-07-31 14:51:39 +03:00
2024-07-31 17:11:29 +03:00
if ($this->pagination) {
2024-07-31 15:49:40 +03:00
$options = [
'countItem' => $this->data['meta']['total'],
'perPage' => $this->data['meta']['perPage'],
'currentPage' => $this->data['meta']['currentPage'],
'baseUrl' => $this->baseUrl,
2024-07-31 17:11:29 +03:00
'prefix' => $this->data['meta']['paginationPrefix'],
2024-07-31 15:49:40 +03:00
];
$pagination = new Pagination($options);
2024-07-31 14:51:39 +03:00
$pagination->create();
$this->html .= $pagination->fetch();
}
2024-05-27 12:11:56 +03:00
}
public function render(): void
{
echo $this->html;
}
2024-08-01 15:10:41 +03:00
public function setBeforePrintCell(string $col, \Closure $closure): void
{
$this->beforePrintCell[$col] = $closure;
}
public function columns(array $data): void
2024-05-27 12:11:56 +03:00
{
2024-08-01 15:10:41 +03:00
foreach ($data as $key => $value) {
$this->setBeforePrintCell($key, $value);
}
2024-05-27 12:11:56 +03:00
}
}