108 lines
2.4 KiB
PHP
108 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace itguild\forms\table;
|
|
|
|
use itguild\forms\traits\CreateParams;
|
|
use itguild\forms\widgets\BaseWidget;
|
|
|
|
class ViewJsonTable
|
|
{
|
|
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->json = $json;
|
|
$this->data = json_decode($this->json, true);
|
|
$this->dataJson = json_decode($this->data['data']['data'], true);
|
|
}
|
|
|
|
|
|
|
|
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>";
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return $this->html;
|
|
}
|
|
|
|
private function issetColumn($column)
|
|
{
|
|
|
|
if (isset($this->data['meta']['columns'])){
|
|
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
|
|
if ($key === $column){
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function endTable()
|
|
{
|
|
$this->html .= "</table>";
|
|
$hookAfter = $this->afterPrintTable;
|
|
$this->html .= $hookAfter();
|
|
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->beginTable();
|
|
$this->createColum();
|
|
$this->endTable();
|
|
}
|
|
|
|
|
|
public function beforeTable(\Closure $closure): 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;
|
|
}
|
|
} |