This commit is contained in:
kali 2024-04-04 17:47:39 +03:00
parent cfe5b3cca5
commit 38ec704a03
8 changed files with 305 additions and 25 deletions

View File

@ -0,0 +1,98 @@
<?php
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityNotAllowedTagError;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;
/* form/resultItem.html.twig */
class __TwigTemplate_94bc2d1f79a7d79af045673fc12da74a extends Template
{
private $source;
private $macros = [];
public function __construct(Environment $env)
{
parent::__construct($env);
$this->source = $this->getSourceContext();
$this->blocks = [
'title' => [$this, 'block_title'],
'content' => [$this, 'block_content'],
];
}
protected function doGetParent(array $context)
{
// line 1
return "layouts/simple.html.twig";
}
protected function doDisplay(array $context, array $blocks = [])
{
$macros = $this->macros;
$this->parent = $this->loadTemplate("layouts/simple.html.twig", "form/resultItem.html.twig", 1);
$this->parent->display($context, array_merge($this->blocks, $blocks));
}
// line 3
public function block_title($context, array $blocks = [])
{
$macros = $this->macros;
echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true);
}
// line 5
public function block_content($context, array $blocks = [])
{
$macros = $this->macros;
// line 6
echo " <h1 class=\"display-4\">";
echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true);
echo "</h1>
";
// line 8
echo twig_escape_filter($this->env, $this->env->getFunction('create_get_action')->getCallable()(), "html", null, true);
echo "
";
}
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
{
return "form/resultItem.html.twig";
}
/**
* @codeCoverageIgnore
*/
public function isTraitable()
{
return false;
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
{
return array ( 64 => 8, 58 => 6, 54 => 5, 47 => 3, 36 => 1,);
}
public function getSourceContext()
{
return new Source("", "form/resultItem.html.twig", "/home/kali/php/untitled/views/form/resultItem.html.twig");
}
}

View File

@ -67,6 +67,7 @@ class __TwigTemplate_e631c8331e0b3314f0fca4478f143546 extends Template
// line 9
echo twig_escape_filter($this->env, $this->env->getFunction('create_pagination')->getCallable()(), "html", null, true);
echo "
";
}

View File

@ -26,6 +26,8 @@ $router->get("/input-value/{id}", [\itguild\forms\app\controllers\InputValueCont
$router->post("/form-save/{id}", [\itguild\forms\app\controllers\FormController::class, "saveAction"]);
$router->get("/form-load/{id}", [\itguild\forms\app\controllers\FormController::class, "result"]);
$router->get("/form-load/{id}/{page}", [\itguild\forms\app\controllers\FormController::class, "result"]);
$router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::class, "getResultItem"]);
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

View File

@ -15,6 +15,7 @@ use itguild\forms\exceptions\FormNotFoundException;
use itguild\forms\Form;
use itguild\forms\JsonForm;
use itguild\forms\widgets\PaginationWidget;
use itguild\forms\widgets\ResultWidget;
use itguild\forms\widgets\TableJsonWidget;
use Twig\TwigFunction;
@ -27,7 +28,6 @@ class FormController extends BaseController
$meta = [];
$formArr = [];
$formFieldsArr = [];
// Получение формы из БД и обработка мета данных формы
$form = FormModel::find($id);
@ -86,6 +86,7 @@ class FormController extends BaseController
$form = FormResModel::Create(['form_id' => $id, 'data' => json_encode($_POST),]);
}
public function createFormAction()
{
$form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]);
@ -103,34 +104,40 @@ class FormController extends BaseController
$form = FormModel::find($id);
$formParams = json_decode($form->params, true);
$perPage = $formParams['meta']['perpage'] ?? "10";
$fillable = $formParams["meta"]["fillable"] ?? null;
$offset = ($page - 1) * $perPage;
$countItems = FormResModel::count();
$table = FormResModel::where("form_id", $id)->limit(3)->offset($offset)->get();
foreach ($table as $key => $item) {
$data[] = json_decode($item->data, true);
$data[] = array_merge(json_decode($item->data, true), ["id" => $item->id]);
}
if ($form->fields) {
foreach ($form->fields as $field) {
if ($field->input_type_id === 6){
$columnsToMod[] = $field->name;
}
$columns[$field->name] = $field->label;
}
}
}
}
$meta['title'] = "Form ID: " . $id;
$meta['columns'] = $columns;
$meta["perPage"] = $perPage;
$meta["fillable"] = $fillable;
$meta['params'] = ["class" => "table table-bordered", "border" => "1"];
$meta['currentPage'] = $page;
$meta['baseUrl'] = "http://forms.local/";
$tableArr["meta"] = $meta;
$tableArr["data"] = $data;
//Debug::prn($tableArr);
$tableRes = json_encode($tableArr);
$this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes, $columnsToMod, $form) {
$table = new TableJsonWidget($tableRes);
@ -151,14 +158,71 @@ class FormController extends BaseController
}));
$this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems) {
$this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems, $form) {
$pagination = new PaginationWidget($countItems, $perPage, $page, "http://forms.local/form-load/1");
$pagination = new PaginationWidget($countItems, $perPage, $page, "http://forms.local/form-load/$form->id");
$pagination->create();
$pagination->render();
}));
echo $this->view->render('form/result.html.twig', ['title' => $tableArr['meta']["title"]]);
echo $this->view->render('form/result.html.twig', ['title' => $tableArr["meta"]["title"]]);
}
public function getResultItem($id)
{
$resultForm = FormResModel::find($id);
$formID = $resultForm->form_id;
$form = FormModel::find($formID);
$meta = [];
$columnsToMod = [];
$columns = [];
$resultData = $resultForm->data;
if ($form->fields) {
foreach ($form->fields as $field) {
if ($field->input_type_id === 6){
$columnsToMod[] = $field->name;
}
$columns[$field->name] = $field->label;
}
}
$meta['title'] = "ITEM ID: " . $id;
$meta['columns'] = $columns;
$meta['params'] = ["class" => "table table-bordered", "border" => "1"];
$tableArr["meta"] = $meta;
$tableArr["data"] = $resultForm;
$tableRes = json_encode($tableArr);
$this->view->addFunction(new TwigFunction("create_get_action", function () use ($tableRes, $columnsToMod, $form) {
$pagination = new ResultWidget($tableRes);
$pagination->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) {
if (in_array($column, $columnsToMod)) {
$res = InputValueModel::find($data);
return $res->value ?? "";
}
return $data;
});
$pagination->create();
$pagination->render();
}));
echo $this->view->render('form/resultItem.html.twig', ['title' => $tableArr["meta"]["title"]]);
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace itguild\forms\widgets;
use itguild\forms\debug\Debug;
use itguild\forms\traits\CreateParams;
class ResultWidget extends BaseWidget
{
use CreateParams;
private array $data;
private string $html = "";
private string $json;
private \Closure|false $beforePrintCell;
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']);
$this->html = "<table $paramsStr>";
}
public function createColum()
{
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";
}
public function create()
{
$this->beginTable();
$this->createColum();
$this->endTable();
}
public function render()
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
}

View File

@ -2,6 +2,8 @@
namespace itguild\forms\widgets;
use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel;
use itguild\forms\debug\Debug;
use itguild\forms\traits\CreateParams;
@ -14,7 +16,7 @@ class TableJsonWidget extends BaseWidget
private int $count = 0;
private \Closure|false $beforePrintCell;
private string $baseUrl;
private array $data;
public function __construct(string $json)
@ -22,6 +24,7 @@ class TableJsonWidget extends BaseWidget
$this->beforePrintCell = false;
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? null;
}
public function beginTable(): void
@ -32,28 +35,32 @@ class TableJsonWidget extends BaseWidget
public function createThead(): void
{
if (!isset($this->data['meta']['columns'])){
if (!isset($this->data['meta']['columns'])) {
return;
}
$this->html .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column){
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)){
$this->html .= "<th>" . $column . "</th>";
}
$this->html .= "</tr></thead>";
}
$this->html .= "<th>Действия</th></th></tr></thead>";
}
public function createTbody(): void
{
if ($this->data['data']){
foreach ($this->data['data'] as $col){
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>" . $this->count . "</td>";
foreach ($col as $key => $row){
if ($this->issetColumn($key)){
if ($this->beforePrintCell){
$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);
}
@ -61,16 +68,16 @@ class TableJsonWidget extends BaseWidget
$this->html .= "<td>" . $row . "</td>";
}
}
$this->html .= "</tr>";
$this->html .= "<td><a href='" . $this->baseUrl . "form-item/" . $col["id"] . "' style='margin-right: 20px'>Просмотр</a><a href='#' style='margin-right: 20px'>Удалить</a></td></tr>";
}
}
}
private function issetColumn($column)
{
if (isset($this->data['meta']['columns'])){
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
if ($key === $column){
if (isset($this->data['meta']['columns'])) {
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
if ($key === $column) {
return true;
}
}
@ -79,6 +86,15 @@ class TableJsonWidget extends BaseWidget
return false;
}
private function is_fillable($column)
{
if (in_array($column, $this->data['meta']['fillable'])) {
return true;
}
return false;
}
public function create()
{
$this->beginTable();

View File

@ -7,4 +7,5 @@
{{ create_table() }}
{{ create_pagination() }}
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends "layouts/simple.html.twig" %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
<h1 class="display-4">{{ title }}</h1>
{{ create_get_action() }}
{% endblock %}