This commit is contained in:
kali
2024-04-03 18:25:38 +03:00
parent 09b5167a1a
commit cfe5b3cca5
17 changed files with 227 additions and 147 deletions

View File

@ -0,0 +1,69 @@
<?php
namespace itguild\forms\widgets;
use itguild\forms\debug\Debug;
class PaginationWidget extends BaseWidget
{
private string $html = "";
private int $perPage = 10;
private int $countItem;
private int $currentPage = 1;
private int $countPages;
private string $baseUrl;
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
{
$this->countItem = $countItem;
$this->perPage = $perPage;
$this->currentPage = $currentPage;
$this->baseUrl = $baseUrl;
$this->countPages = ceil($this->countItem / $perPage);
}
public function create()
{
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
$btns = $prev ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$prev'>$prev</a></li>" : "";
$btns .= "<li class='page-item'><a class='page-link active' href='#'>$this->currentPage</a></li>";
$btns .= $next ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$next'>$next</a></li>" : "";
$this->html = str_replace('{btns}', $btns, $this->getTemplate());
$this->html = str_replace('{previous_link}', $this->baseUrl . "/1", $this->html);
$this->html = str_replace('{next_link}', $this->baseUrl . "/" . $this->countPages, $this->html);
}
public function render(): void
{
echo $this->html;
}
public function fetch()
{
return $this->html;
}
private function getTemplate()
{
return '<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="{previous_link}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{btns}
<li class="page-item">
<a class="page-link" href="{next_link}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>';
}
}

View File

@ -12,10 +12,14 @@ class TableJsonWidget extends BaseWidget
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);
}
@ -33,6 +37,7 @@ class TableJsonWidget extends BaseWidget
}
$this->html .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column){
$this->html .= "<th>" . $column . "</th>";
}
@ -44,8 +49,15 @@ class TableJsonWidget extends BaseWidget
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>";
}
}
@ -97,4 +109,9 @@ class TableJsonWidget extends BaseWidget
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
}