338 lines
10 KiB
PHP
338 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Itguild\Tables;
|
|
|
|
use Exception;
|
|
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
|
use Itguild\Tables\ActionColumn\EditActionColumn;
|
|
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
|
use Itguild\Tables\traits\CreateParams;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
|
|
class ListJsonTable extends JasonTable
|
|
{
|
|
use CreateParams;
|
|
|
|
private string $json;
|
|
|
|
private int $count = 0;
|
|
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 = [];
|
|
|
|
#[NoReturn] public function __construct(string $json)
|
|
{
|
|
$this->beforePrintCell = [];
|
|
$this->json = $json;
|
|
$this->data = json_decode($this->json, true);
|
|
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
|
$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']);
|
|
$hookBefore = $this->beforePrintHook;
|
|
$this->html .= $hookBefore($this->data['meta']);
|
|
$this->html .= "<table $paramsStr>";
|
|
}
|
|
|
|
public function createThead(): void
|
|
{
|
|
$columnKeys = [];
|
|
if (!isset($this->data['meta']['columns'])) {
|
|
return;
|
|
}
|
|
|
|
$this->html .= "<thead><tr>";
|
|
if (!$this->issetColumn("id")) {
|
|
$this->html .= "<th>" . "ID" . "</th>";
|
|
$columnKeys[] = "id";
|
|
}
|
|
$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>";
|
|
}
|
|
}
|
|
|
|
public function createTbody(): void
|
|
{
|
|
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;
|
|
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) {
|
|
$cell = $this->getCustomCell($key, $cell);
|
|
}
|
|
$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"]);
|
|
|
|
$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
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
|
}
|
|
|
|
private function getCurrentFilter(string $column)
|
|
{
|
|
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;
|
|
}
|
|
|
|
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'])) {
|
|
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
|
|
if ($key === $column) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 (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'])) {
|
|
if (in_array($column, $this->data['meta']['fillable'])) {
|
|
return true;
|
|
}
|
|
} else {
|
|
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;
|
|
}
|
|
|
|
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);
|
|
$class = new $filter['class']([
|
|
'param' => $filter['param'],
|
|
'name' => $key,
|
|
'value' => $filter['value']
|
|
]);
|
|
$this->html .= $class->fetch();
|
|
}
|
|
else {
|
|
$this->html .= "<td></td>";
|
|
}
|
|
}
|
|
$this->html .= "</form></tr>";
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function create(): void
|
|
{
|
|
$this->setActions();
|
|
$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>";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function endTable(): void
|
|
{
|
|
$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 columns(array $data): void
|
|
{
|
|
foreach ($data as $key => $value) {
|
|
$this->beforePrintCell[$key] = $value;
|
|
}
|
|
}
|
|
|
|
} |