191 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			191 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Itguild\Tables;
 | |
| 
 | |
| use Itguild\Tables\ActionColumn\ActionColumn;
 | |
| use Itguild\Tables\ActionColumn\DeleteActionColumn;
 | |
| use Itguild\Tables\ActionColumn\EditActionColumn;
 | |
| use Itguild\Tables\ActionColumn\ViewActionColumn;
 | |
| use Itguild\Tables\traits\CreateParams;
 | |
| 
 | |
| class ListJsonTable
 | |
| {
 | |
|     use CreateParams;
 | |
| 
 | |
|     private string $html = '';
 | |
|     private string $json;
 | |
| 
 | |
|     private int $count = 0;
 | |
|     private \Closure|false $beforePrintCell;
 | |
|     private \Closure|false $beforePrint;
 | |
|     private string $baseUrl;
 | |
|     private array $data;
 | |
| 
 | |
|     private array $actionsArray = [];
 | |
|     private array $customActionsArray = [];
 | |
| 
 | |
|     public function __construct(string $json)
 | |
|     {
 | |
|         $this->beforePrintCell = false;
 | |
|         $this->beforePrint = function () {
 | |
|         };
 | |
|         $this->json = $json;
 | |
|         $this->data = json_decode($this->json, true);
 | |
|         $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
 | |
|         $this->setActions();
 | |
|     }
 | |
| 
 | |
|     public function beginTable(): void
 | |
|     {
 | |
|         $paramsStr = $this->createParams($this->data['meta']['params']);
 | |
|         $hook = $this->beforePrint;
 | |
|         $this->html .= $hook();
 | |
|         $this->html .= "<table $paramsStr>";
 | |
|     }
 | |
| 
 | |
|     public function beforePrint(\Closure $closure): void
 | |
|     {
 | |
|         $this->beforePrint = $closure;
 | |
|     }
 | |
| 
 | |
|     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) {
 | |
|             if ($this->is_fillable($key)) {
 | |
|                 $this->html .= "<th>" . $column . "</th>";
 | |
|             }
 | |
|         }
 | |
|         $this->html .= "<th>Действия</th></th></tr></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 $col) {
 | |
|                 $this->html .= "<tr>";
 | |
|                 $this->count += 1;
 | |
|                 $this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
 | |
|                 foreach ($col as $key => $row) {
 | |
|                     if ($this->issetColumn($key) and $this->is_fillable($key)) {
 | |
|                         if ($this->beforePrintCell) {
 | |
|                             $hook = $this->beforePrintCell;
 | |
|                             $row = $hook($key, $row);
 | |
|                         }
 | |
| 
 | |
|                         $this->html .= "<td>" . $row . "</td>";
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 $actions = $this->getActions($col["id"]);
 | |
| 
 | |
|                 $this->html .= "<td>$actions</td></tr>";
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function addAction(ActionColumn $actionColumn): void
 | |
|     {
 | |
|         $this->customActionsArray[] = $actionColumn;
 | |
|     }
 | |
| 
 | |
|     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 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 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;
 | |
|     }
 | |
| 
 | |
|     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;
 | |
|     }
 | |
| } |