117 lines
2.8 KiB
PHP
117 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace itguild\forms\widgets;
|
|
|
|
use itguild\forms\debug\Debug;
|
|
use itguild\forms\traits\CreateParams;
|
|
|
|
class TableJsonWidget extends BaseWidget
|
|
{
|
|
use CreateParams;
|
|
|
|
private string $html = '';
|
|
private string $json;
|
|
|
|
private int $count = 0;
|
|
private \Closure|false $beforePrintCell;
|
|
|
|
private array $data;
|
|
|
|
public function __construct(string $json)
|
|
{
|
|
$this->beforePrintCell = false;
|
|
$this->json = $json;
|
|
$this->data = json_decode($this->json, true);
|
|
}
|
|
|
|
public function beginTable(): void
|
|
{
|
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
|
$this->html = "<table $paramsStr>";
|
|
}
|
|
|
|
public function createThead(): void
|
|
{
|
|
if (!isset($this->data['meta']['columns'])){
|
|
return;
|
|
}
|
|
|
|
$this->html .= "<thead><tr>";
|
|
$this->html .= "<th>" . "ID" . "</th>";
|
|
foreach ($this->data['meta']['columns'] as $key => $column){
|
|
$this->html .= "<th>" . $column . "</th>";
|
|
}
|
|
$this->html .= "</tr></thead>";
|
|
}
|
|
|
|
public function createTbody(): void
|
|
{
|
|
if ($this->data['data']){
|
|
foreach ($this->data['data'] as $col){
|
|
$this->html .= "<tr>";
|
|
$this->count += 1;
|
|
$this->html .= "<td>" . $this->count . "</td>";
|
|
foreach ($col as $key => $row){
|
|
if ($this->issetColumn($key)){
|
|
if ($this->beforePrintCell){
|
|
$hook = $this->beforePrintCell;
|
|
$row = $hook($key, $row);
|
|
}
|
|
|
|
$this->html .= "<td>" . $row . "</td>";
|
|
}
|
|
}
|
|
$this->html .= "</tr>";
|
|
}
|
|
}
|
|
}
|
|
|
|
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 create()
|
|
{
|
|
$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>";
|
|
}
|
|
}
|
|
|
|
public function endTable(): void
|
|
{
|
|
$this->html .= "</table>";
|
|
}
|
|
|
|
public function render(): void
|
|
{
|
|
echo $this->html;
|
|
}
|
|
|
|
public function setBeforePrintCell(\Closure $closure): void
|
|
{
|
|
$this->beforePrintCell = $closure;
|
|
}
|
|
} |