This commit is contained in:
kali 2024-04-05 18:47:38 +03:00
parent 38ec704a03
commit 569c9348ba
10 changed files with 130 additions and 43 deletions

View File

@ -3,6 +3,8 @@ ini_set("display_errors", 1);
error_reporting(-1);
const ROOT_PATH = __DIR__;
const BASE_URL = "http://forms.local";
const VIEW_PATH = __DIR__ . "/views";
const VIEW_CACHE_PATH = __DIR__ . "/compilation_cache";
@ -24,9 +26,10 @@ $router->get("/form-res/{id}", [\itguild\forms\app\controllers\FormResController
$router->get("/input-type/{id}", [\itguild\forms\app\controllers\InputTypeController::class, "indexAction"]);
$router->get("/input-value/{id}", [\itguild\forms\app\controllers\InputValueController::class, "indexAction"]);
$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-result/{id}", [\itguild\forms\app\controllers\FormController::class, "result"]);
$router->get("/form-result/{id}/{page}", [\itguild\forms\app\controllers\FormController::class, "result"]);
$router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::class, "getResultItem"]);
$router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteItem"]);
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());

View File

@ -2,21 +2,16 @@
namespace itguild\forms\app\controllers;
use Exception;
use itguild\forms\app\core\BaseController;
use itguild\forms\app\models\FormInputModel;
use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel;
use itguild\forms\app\models\InputValueModel;
use itguild\forms\app\models\User;
use itguild\forms\debug\Debug;
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 itguild\forms\table\ListJsonTable;
use itguild\forms\table\Pagination;
use itguild\forms\table\ViewJsonTable;
use Twig\TwigFunction;
@ -105,18 +100,21 @@ class FormController extends BaseController
$formParams = json_decode($form->params, true);
$perPage = $formParams['meta']['perpage'] ?? "10";
$fillable = $formParams["meta"]["fillable"] ?? null;
$actions = $formParams["meta"]["actions"] ?? null;
$offset = ($page - 1) * $perPage;
$countItems = FormResModel::count();
$table = FormResModel::where("form_id", $id)->limit(3)->offset($offset)->get();
$table = FormResModel::where("form_id", $id)->where("status", FormResModel::STATUS_ACTIVE)->limit(3)->offset($offset)->get();
foreach ($table as $key => $item) {
$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){
if ($field->input_type_id === 6) {
$fillable = $formParams["meta"]["fillable"] ?? null;
$columnsToMod[] = $field->name;
}
@ -132,15 +130,16 @@ class FormController extends BaseController
$meta["fillable"] = $fillable;
$meta['params'] = ["class" => "table table-bordered", "border" => "1"];
$meta['currentPage'] = $page;
$meta['baseUrl'] = "http://forms.local/";
$meta['actions'] = $actions;
$meta['baseUrl'] = BASE_URL;
$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);
$table = new ListJsonTable($tableRes);
$table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form){
@ -160,7 +159,7 @@ class FormController extends BaseController
$this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems, $form) {
$pagination = new PaginationWidget($countItems, $perPage, $page, "http://forms.local/form-load/$form->id");
$pagination = new Pagination($countItems, $perPage, $page, BASE_URL . "/form-result/" . $form->id);
$pagination->create();
$pagination->render();
@ -169,11 +168,18 @@ class FormController extends BaseController
}
public function deleteItem($id)
{
$item = FormResModel::find($id)->update(["status" => FormResModel::STATUS_ARCHIVE]);
header( 'Location: ' . '' );
exit;
}
public function getResultItem($id)
{
$resultForm = FormResModel::find($id);
$formID = $resultForm->form_id;
$form = FormModel::find($formID);
$meta = [];
$columnsToMod = [];
@ -209,7 +215,7 @@ class FormController extends BaseController
$this->view->addFunction(new TwigFunction("create_get_action", function () use ($tableRes, $columnsToMod, $form) {
$pagination = new ResultWidget($tableRes);
$pagination = new ViewJsonTable($tableRes);
$pagination->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) {
if (in_array($column, $columnsToMod)) {
$res = InputValueModel::find($data);

View File

@ -6,10 +6,12 @@ use Illuminate\Database\Eloquent\Model;
class FormResModel extends Model
{
const STATUS_ACTIVE = 1;
const STATUS_ARCHIVE = 2;
protected $table = "form_res";
protected $fillable = [
'form_id', 'data'
'form_id', 'data', 'status'
];
protected $hidden = [

View File

@ -0,0 +1,19 @@
<?php
namespace itguild\forms\table\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();
}

View File

@ -0,0 +1,14 @@
<?php
namespace itguild\forms\table\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> ";
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace itguild\forms\table\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> ";
}
}

View File

@ -1,13 +1,14 @@
<?php
namespace itguild\forms\widgets;
namespace itguild\forms\table;
use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel;
use itguild\forms\debug\Debug;
use itguild\forms\table\ActionColumn\DeleteActionColumn;
use itguild\forms\table\ActionColumn\ViewActionColumn;
use itguild\forms\traits\CreateParams;
use itguild\forms\widgets\BaseWidget;
class TableJsonWidget extends BaseWidget
class ListJsonTable
{
use CreateParams;
@ -19,12 +20,15 @@ class TableJsonWidget extends BaseWidget
private string $baseUrl;
private array $data;
private array $actionsArray = [];
public function __construct(string $json)
{
$this->beforePrintCell = false;
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? null;
$this->setActions();
}
public function beginTable(): void
@ -42,7 +46,7 @@ class TableJsonWidget extends BaseWidget
$this->html .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)){
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
}
}
@ -57,7 +61,7 @@ class TableJsonWidget extends BaseWidget
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>';
$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) {
@ -68,7 +72,26 @@ class TableJsonWidget extends BaseWidget
$this->html .= "<td>" . $row . "</td>";
}
}
$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>";
$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;
}
}
}
}
@ -95,6 +118,17 @@ class TableJsonWidget extends BaseWidget
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();

View File

@ -1,10 +1,10 @@
<?php
namespace itguild\forms\widgets;
namespace itguild\forms\table;
use itguild\forms\debug\Debug;
use itguild\forms\widgets\BaseWidget;
class PaginationWidget extends BaseWidget
class Pagination
{
private string $html = "";

View File

@ -1,10 +1,11 @@
<?php
namespace itguild\forms\widgets;
namespace itguild\forms\table;
use itguild\forms\debug\Debug;
use itguild\forms\traits\CreateParams;
class ResultWidget extends BaseWidget
use itguild\forms\widgets\BaseWidget;
class ViewJsonTable
{
use CreateParams;
@ -22,10 +23,12 @@ class ResultWidget extends BaseWidget
$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>";
}

View File

@ -1,8 +0,0 @@
<?php
namespace itguild\forms\widgets;
class BaseWidget
{
}