tables/src/ListJsonTable.php

338 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-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
2024-08-02 15:40:13 +03:00
class ListJsonTable extends JasonTable
2024-05-27 12:11:56 +03:00
{
use CreateParams;
private string $json;
private int $count = 0;
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-23 14:52:22 +03:00
private bool $showFiltersRow = true;
private bool|array $filters = [];
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-07 12:16:42 +03:00
$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-23 14:52:22 +03:00
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
$this->filters = $this->data['filters'] ?? [];
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 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-08-08 14:21:47 +03:00
$columnKeys = $this->getColumnKeys();
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) {
2024-08-23 14:52:22 +03:00
$this->html .= "<th>Действия</th></th></tr>";
2024-07-31 17:11:29 +03:00
}
2024-08-23 14:52:22 +03:00
if($this->showFiltersRow){
2024-08-06 12:10:26 +03:00
$this->getFilters($columnKeys);
2024-08-23 14:52:22 +03:00
$this->html .= "</thead>";
2024-08-01 13:32:31 +03:00
}
2024-05-27 12:11:56 +03:00
}
public function createTbody(): void
{
if ($this->data['data']) {
$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-08-08 14:21:47 +03:00
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $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-08-06 12:10:26 +03:00
foreach ($row as $key => $cell) {
2024-05-27 12:11:56 +03:00
if ($this->issetColumn($key) and $this->is_fillable($key)) {
2024-08-07 12:16:42 +03:00
if($this->beforePrintCell) {
2024-08-06 12:10:26 +03:00
$cell = $this->getCustomCell($key, $cell);
2024-05-27 12:11:56 +03:00
}
2024-08-08 17:08:46 +03:00
$this->html .= "<td style='" . $this->getStyleFromCustomColumn($key) . "'>" . $cell . "</td>";
2024-05-27 12:11:56 +03:00
}
}
2024-07-31 17:11:29 +03:00
$this->getCustomColumns($row["id"] ?? null);
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-08-28 14:21:00 +03:00
private function getCurrentFilter(string $column)
2024-08-23 14:52:22 +03:00
{
2024-08-28 14:21:00 +03:00
if (is_array($this->beforePrintCell[$column])) {
if (isset($this->beforePrintCell[$column]['filter'])) {
if (is_array($this->beforePrintCell[$column]['filter'])) {
return $this->beforePrintCell[$column]['filter'];
}
}
}
return false;
2024-08-23 14:52:22 +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-08-08 17:08:46 +03:00
2024-08-08 14:21:47 +03:00
private function getColumnKeys(): array
2024-08-06 12:10:26 +03:00
{
2024-08-08 14:21:47 +03:00
$columnKeys = [];
2024-08-06 12:10:26 +03:00
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
return $columnKeys;
}
2024-08-08 14:21:47 +03:00
private function getFillableColumns(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$columnKeys[] = $key;
}
}
return $columnKeys;
}
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-12 15:39:26 +03:00
private function issetFilter(string $filter): bool
2024-08-01 12:50:16 +03:00
{
2024-08-01 13:32:31 +03:00
if (isset($this->data['filters'])) {
2024-08-12 15:39:26 +03:00
foreach ($this->data['filters'] as $currentFilter) {
2024-08-01 13:32:31 +03:00
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-12 15:39:26 +03:00
if (is_array($this->beforePrintCell[$filter])) {
if (isset($this->beforePrintCell[$filter]['filter'])) {
return true;
}
}
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-08-06 12:10:26 +03:00
private function getFilters(array $columnKeys): void
{
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
2024-08-28 14:24:25 +03:00
$filter = $this->getCurrentFilter($key);
$class = new $filter['class']([
'param' => $filter['param'],
2024-08-28 15:09:34 +03:00
'name' => $key,
'value' => $filter['value']
2024-08-23 14:52:22 +03:00
]);
2024-08-28 14:21:00 +03:00
$this->html .= $class->fetch();
2024-08-06 12:10:26 +03:00
}
else {
$this->html .= "<td></td>";
}
}
$this->html .= "</form></tr>";
}
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'],
2024-08-07 11:56:33 +03:00
'perPage' => $this->data['meta']['perPage'] ?? 10,
'currentPage' => $this->data['meta']['currentPage'] ?? 1,
'baseUrl' => $this->baseUrl ?? '',
'prefix' => $this->data['meta']['paginationPrefix'] ?? '/page',
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
}
2024-08-06 12:10:26 +03:00
public function columns(array $data): void
{
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
2024-08-08 14:24:53 +03:00
2024-05-27 12:11:56 +03:00
}