to composer
This commit is contained in:
183
src/ListJsonTable.php
Normal file
183
src/ListJsonTable.php
Normal file
@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
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 = [];
|
||||
|
||||
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 . "/form-item/" . $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>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
68
src/Pagination.php
Normal file
68
src/Pagination.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
|
||||
class Pagination
|
||||
{
|
||||
|
||||
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">«</span>
|
||||
</a>
|
||||
</li>
|
||||
{btns}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{next_link}" aria-label="Next">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>';
|
||||
}
|
||||
}
|
107
src/ViewJsonTable.php
Normal file
107
src/ViewJsonTable.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables;
|
||||
|
||||
use Itguild\Tables\traits\CreateParams;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
22
src/actionBtn/ActionBtn.php
Normal file
22
src/actionBtn/ActionBtn.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\actionBtn;
|
||||
|
||||
abstract class ActionBtn
|
||||
{
|
||||
|
||||
protected string $baseUrl;
|
||||
protected string $prefix;
|
||||
|
||||
protected int $id;
|
||||
public function __construct(string $baseUrl, int $id)
|
||||
{
|
||||
$this->baseUrl = $baseUrl;
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract public function fetch();
|
||||
|
||||
}
|
13
src/actionBtn/ToDeleteBtn.php
Normal file
13
src/actionBtn/ToDeleteBtn.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\actionBtn;
|
||||
|
||||
class ToDeleteBtn extends ActionBtn
|
||||
{
|
||||
protected string $prefix = "/form-delete/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
return "<a class='btn btn-danger' href='$this->baseUrl$this->prefix$this->id' style='margin: 3px'>Удалить</a>";
|
||||
}
|
||||
}
|
13
src/actionBtn/ToEditBtn.php
Normal file
13
src/actionBtn/ToEditBtn.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\actionBtn;
|
||||
|
||||
class ToEditBtn extends ActionBtn
|
||||
{
|
||||
protected string $prefix = "/form-edit/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
return "<a class='btn btn-warning' href='$this->baseUrl$this->prefix$this->id' style='margin: 3px'>Редактировать</a>";
|
||||
}
|
||||
}
|
13
src/actionBtn/ToListBtn.php
Normal file
13
src/actionBtn/ToListBtn.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\actionBtn;
|
||||
|
||||
class ToListBtn extends ActionBtn
|
||||
{
|
||||
protected string $prefix = "/form-result/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
return "<a class='btn btn-primary' href='$this->baseUrl$this->prefix$this->id' style='margin: 3px'>Список</a>";
|
||||
}
|
||||
}
|
20
src/actionColumn/ActionColumn.php
Normal file
20
src/actionColumn/ActionColumn.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\ActionColumn;
|
||||
|
||||
abstract class ActionColumn
|
||||
{
|
||||
protected string $baseUrl;
|
||||
protected string $prefix;
|
||||
|
||||
protected int $id;
|
||||
public function __construct(string $baseUrl, int $id)
|
||||
{
|
||||
$this->baseUrl = $baseUrl;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
abstract public function fetch();
|
||||
|
||||
|
||||
}
|
14
src/actionColumn/DeleteActionColumn.php
Normal file
14
src/actionColumn/DeleteActionColumn.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\ActionColumn;
|
||||
|
||||
class DeleteActionColumn extends ActionColumn
|
||||
{
|
||||
protected string $prefix = "/form-delete/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
|
||||
}
|
||||
}
|
16
src/actionColumn/EditActionColumn.php
Normal file
16
src/actionColumn/EditActionColumn.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\ActionColumn;
|
||||
|
||||
class EditActionColumn extends ActionColumn
|
||||
{
|
||||
|
||||
protected string $prefix = "/form-edit/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-primary'>Редактировать</a> ";
|
||||
}
|
||||
|
||||
}
|
14
src/actionColumn/ViewActionColumn.php
Normal file
14
src/actionColumn/ViewActionColumn.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\ActionColumn;
|
||||
|
||||
class ViewActionColumn extends ActionColumn
|
||||
{
|
||||
protected string $prefix = "/form-item/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-primary'>Просмотр</a> ";
|
||||
}
|
||||
}
|
21
src/traits/CreateOption.php
Executable file
21
src/traits/CreateOption.php
Executable file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\traits;
|
||||
|
||||
trait CreateOption
|
||||
{
|
||||
/**
|
||||
* @param array $options
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
public function createOption(array $options, $value = null): string
|
||||
{
|
||||
$optionsString = "";
|
||||
foreach ($options as $val => $title){
|
||||
$selected = (int)$value === $val ? "selected" : "";
|
||||
$optionsString .= "<option $selected value='$val'>$title</option>";
|
||||
}
|
||||
return $optionsString;
|
||||
}
|
||||
}
|
24
src/traits/CreateParams.php
Executable file
24
src/traits/CreateParams.php
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\Tables\traits;
|
||||
|
||||
trait CreateParams
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function createParams(array $data = []): string
|
||||
{
|
||||
$paramsString = "";
|
||||
foreach($data as $key => $param){
|
||||
if(is_string($param)){
|
||||
$paramsString .= $key . "='" . $param . "'";
|
||||
}
|
||||
}
|
||||
|
||||
return $paramsString;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user