This commit is contained in:
kali 2024-04-15 17:46:33 +03:00
parent 569c9348ba
commit 87deac2576
23 changed files with 652 additions and 75 deletions

View File

@ -0,0 +1,97 @@
<?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/form.html.twig */
class __TwigTemplate_1782f108c6541d589defb1221fcbfccb 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/form.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 7
echo twig_escape_filter($this->env, $this->env->getFunction('create_form')->getCallable()(), "html", null, true);
echo "
";
}
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
{
return "form/form.html.twig";
}
/**
* @codeCoverageIgnore
*/
public function isTraitable()
{
return false;
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
{
return array ( 63 => 7, 58 => 6, 54 => 5, 47 => 3, 36 => 1,);
}
public function getSourceContext()
{
return new Source("", "form/form.html.twig", "/home/kali/php/untitled/views/form/form.html.twig");
}
}

View File

@ -64,4 +64,16 @@
"class": "btn btn-primary" "class": "btn btn-primary"
} }
] ]
}
{
"meta": {
"perpage": 3,
"actions": ["view", "edit", "delete"],
"fillable": ["description", "phone", "telega", "city"],
"status" : 1
},
"params": {
"class": "form-control",
"id": "form"
}
} }

0
in1.php Normal file
View File

View File

@ -26,11 +26,12 @@ $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-type/{id}", [\itguild\forms\app\controllers\InputTypeController::class, "indexAction"]);
$router->get("/input-value/{id}", [\itguild\forms\app\controllers\InputValueController::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->post("/form-save/{id}", [\itguild\forms\app\controllers\FormController::class, "saveAction"]);
$router->get("/form-result/{id}", [\itguild\forms\app\controllers\FormController::class, "result"]); $router->get("/form-result/{id}/{page}?", [\itguild\forms\app\controllers\FormController::class, "resultAction"]);
$router->get("/form-result/{id}/{page}", [\itguild\forms\app\controllers\FormController::class, "result"]); $router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::class, "viewAction"]);
$router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::class, "getResultItem"]); $router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteAction"]);
$router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteItem"]); $router->get("/form-edit/{id}", [\itguild\forms\app\controllers\FormController::class, "editAction"]);
$router->get("/admin/create-form", [\itguild\forms\app\controllers\AdminController::class, "createAction"]);
$router->post("/admin/save-form", [\itguild\forms\app\controllers\AdminController::class, "saveForm"]);
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); $dispatcher = new Phroute\Phroute\Dispatcher($router->getData());
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

View File

@ -0,0 +1,37 @@
<?php
namespace itguild\forms\app\controllers;
use itguild\forms\ActiveForm;
use itguild\forms\app\core\BaseController;
use itguild\forms\app\models\FormModel;
use itguild\forms\core\cg_view\CgView;
class AdminController extends BaseController
{
public string $html;
public function createAction(): void
{
$view = new CgView();
$view->viewPath = VIEW_PATH;
$view->layout = "/layouts/admin_layout.php";
$view->render("/admin/index.php", ['title' => "Создать форму", 'form' => new ActiveForm()]);
}
public function saveForm()
{
$title = $_POST["title"];
$params = $_POST["params"];
if (isset($title) and isset($params)) {
$form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => $params]);
echo "Форма сохранена!";
}
}
}

View File

@ -6,65 +6,44 @@ use itguild\forms\app\core\BaseController;
use itguild\forms\app\models\FormModel; use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel; use itguild\forms\app\models\FormResModel;
use itguild\forms\app\models\InputValueModel; use itguild\forms\app\models\InputValueModel;
use itguild\forms\debug\Debug;
use itguild\forms\exceptions\FormNotFoundException; use itguild\forms\exceptions\FormNotFoundException;
use itguild\forms\exceptions\FormResultNotFoundException;
use itguild\forms\Form; use itguild\forms\Form;
use itguild\forms\JsonForm; use itguild\forms\JsonForm;
use itguild\forms\table\actionBtn\ToDeleteBtn;
use itguild\forms\table\actionBtn\ToEditBtn;
use itguild\forms\table\actionBtn\ToListBtn;
use itguild\forms\table\ListJsonTable; use itguild\forms\table\ListJsonTable;
use itguild\forms\table\Pagination; use itguild\forms\table\Pagination;
use itguild\forms\table\ViewJsonTable; use itguild\forms\table\ViewJsonTable;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\TwigFunction; use Twig\TwigFunction;
class FormController extends BaseController class FormController extends BaseController
{ {
/**
* @throws SyntaxError
* @throws FormNotFoundException
* @throws RuntimeError
* @throws LoaderError
*/
public function indexAction($id): void public function indexAction($id): void
{ {
// Инициализируем пустые массивы
$meta = [];
$formArr = [];
$formFieldsArr = [];
// Получение формы из БД и обработка мета данных формы
$form = FormModel::find($id); $form = FormModel::find($id);
if (!$form) { if (!$form) {
throw new FormNotFoundException($id); throw new FormNotFoundException($id);
} }
if (isset($form->params)) { $formArr = $this->createFormArr($form);
$allParams = json_decode($form->params, true);
$meta = array_merge($meta, $allParams['params']);
}
$meta['action'] = "/form-save/" . $form->id;
$meta['method'] = "POST";
$formArr['meta'] = $meta;
// Обработка полей формы
if ($form->fields) {
$fields = $form->fields;
foreach ($fields as $field) {
$options = [];
$fieldArr = [];
$params = [];
if ($field->inputValue) {
foreach ($field->inputValue as $value) {
$options[$value['id']] = $value['value'];
}
}
if ($field->params) {
$fieldArr = array_merge($fieldArr, json_decode($field->params, true));
}
$fieldArr['name'] = $field->name ?? $fieldArr['name'];
$fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput";
$fieldArr['options'] = $options;
$formArr['data'][] = $fieldArr;
}
}
$formArr['data'][] = ['type' => "button", 'typeInput' => "submit", "value" => "Отправить", "name" => "", "class" => "btn btn-primary"]; $formArr['data'][] = ['type' => "button", 'typeInput' => "submit", "value" => "Отправить", "name" => "", "class" => "btn btn-primary"];
//Debug::dd($fields); //Debug::dd($formArr);
$this->view->addFunction(new TwigFunction("create_form", function () use ($formArr) { $this->view->addFunction(new TwigFunction("create_form", function () use ($formArr) {
$form = new JsonForm(json_encode($formArr)); $form = new JsonForm(json_encode($formArr));
@ -78,7 +57,17 @@ class FormController extends BaseController
public function saveAction($id): void public function saveAction($id): void
{ {
$form = FormResModel::Create(['form_id' => $id, 'data' => json_encode($_POST),]); if (isset($_POST["form_res_id"])) {
$formResId = $_POST["form_res_id"];
unset($_POST["form_res_id"]);
FormResModel::where("id", $formResId)->update(["data" => json_encode($_POST)]);
} else {
$formRes = FormResModel::Create(['form_id' => $id, 'data' => json_encode($_POST),]);
$formResId = $formRes->id;
}
header('Location: ' . "/form-item/$formResId");
exit;
} }
@ -87,7 +76,7 @@ class FormController extends BaseController
$form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]); $form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]);
} }
public function result($id, $page = 1) public function resultAction($id, $page = 1): void
{ {
$meta = []; $meta = [];
@ -102,22 +91,22 @@ class FormController extends BaseController
$fillable = $formParams["meta"]["fillable"] ?? null; $fillable = $formParams["meta"]["fillable"] ?? null;
$actions = $formParams["meta"]["actions"] ?? null; $actions = $formParams["meta"]["actions"] ?? null;
$offset = ($page - 1) * $perPage; $offset = ($page - 1) * $perPage;
$countItems = FormResModel::count(); $countItems = FormResModel::where("status", 1)->count();
$table = FormResModel::where("form_id", $id)->where("status", FormResModel::STATUS_ACTIVE)->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) { foreach ($table as $key => $item) {
$data[] = array_merge(json_decode($item->data, true), ["id" => $item->id]); $data[] = array_merge(json_decode($item->data, true), ["id" => $item->id]);
} }
if ($form->fields) { if ($form->fields) {
foreach ($form->fields as $field) { foreach ($form->fields as $field) {
if ($field->input_type_id === 6) { if ($field->input_type_id === 6) {
$fillable = $formParams["meta"]["fillable"] ?? null; $fillable = $formParams["meta"]["fillable"] ?? null;
$columnsToMod[] = $field->name; $columnsToMod[] = $field->name;
} }
$columns[$field->name] = $field->label; $columns[$field->name] = $field->label;
@ -137,24 +126,28 @@ class FormController extends BaseController
$tableArr["data"] = $data; $tableArr["data"] = $data;
$tableRes = json_encode($tableArr); $tableRes = json_encode($tableArr);
$this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes, $columnsToMod, $form) { $this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes, $columnsToMod, $form, $countItems) {
$table = new ListJsonTable($tableRes); $table = new ListJsonTable($tableRes);
$table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form){ $table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) {
if (in_array($column, $columnsToMod)){ if (in_array($column, $columnsToMod)) {
$res = InputValueModel::find($data); $res = InputValueModel::find($data);
return $res->value ?? ""; return $res->value ?? "";
} }
return $data; return $data;
}); });
$table->beforePrint(function () use($countItems){
return "<h5 class='display-10 fw-bold'>Количество записей: " . $countItems . "</h5>";
});
$table->create(); $table->create();
$table->render(); $table->render();
})); }));
$this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems, $form) { $this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems, $form) {
@ -168,15 +161,45 @@ class FormController extends BaseController
} }
public function deleteItem($id) public function deleteAction($id): void
{ {
$item = FormResModel::find($id)->update(["status" => FormResModel::STATUS_ARCHIVE]); $item = FormResModel::find($id);
header( 'Location: ' . '' ); $item->update(["status" => FormResModel::STATUS_ARCHIVE]);
header('Location: ' . "/form-result/$item->form_id");
exit; exit;
}
public function editAction($id): void
{
$res = FormResModel::find($id);
if (!$res) {
throw new FormResultNotFoundException($id);
}
$form = FormModel::find($res->form_id);
if (!$form) {
throw new FormNotFoundException($id);
}
$formArr = $this->createFormArr($form, $res);
$formArr["data"][] = ["type" => "hidden", "value" => $id, "name" => "form_res_id"];
$formArr['data'][] = ['type' => "button", 'typeInput' => "submit", "value" => "Редактировать", "name" => "", "class" => "btn btn-primary"];
//Debug::dd($formArr);
$this->view->addFunction(new TwigFunction("create_form", function () use ($formArr) {
$form = new JsonForm(json_encode($formArr));
$form->convertHTML();
$form->render();
}));
echo $this->view->render('form/form.html.twig', ['title' => $form->title]);
} }
public function getResultItem($id) public function viewAction($id): void
{ {
$resultForm = FormResModel::find($id); $resultForm = FormResModel::find($id);
$formID = $resultForm->form_id; $formID = $resultForm->form_id;
@ -189,7 +212,7 @@ class FormController extends BaseController
if ($form->fields) { if ($form->fields) {
foreach ($form->fields as $field) { foreach ($form->fields as $field) {
if ($field->input_type_id === 6){ if ($field->input_type_id === 6) {
$columnsToMod[] = $field->name; $columnsToMod[] = $field->name;
} }
@ -203,7 +226,7 @@ class FormController extends BaseController
$meta['title'] = "ITEM ID: " . $id; $meta['title'] = "ITEM ID: " . $id;
$meta['columns'] = $columns; $meta['columns'] = $columns;
$meta['params'] = ["class" => "table table-bordered", "border" => "1"]; $meta['params'] = ["class" => "table table-bordered", "border" => "1"];
$meta["actions"] = ["view", "edit", "delete"];
$tableArr["meta"] = $meta; $tableArr["meta"] = $meta;
$tableArr["data"] = $resultForm; $tableArr["data"] = $resultForm;
@ -211,12 +234,10 @@ class FormController extends BaseController
$tableRes = json_encode($tableArr); $tableRes = json_encode($tableArr);
$this->view->addFunction(new TwigFunction("create_get_action", function () use ($tableRes, $columnsToMod, $form, $resultForm) {
$table = new ViewJsonTable($tableRes);
$this->view->addFunction(new TwigFunction("create_get_action", function () use ($tableRes, $columnsToMod, $form) { $table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) {
$pagination = new ViewJsonTable($tableRes);
$pagination->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) {
if (in_array($column, $columnsToMod)) { if (in_array($column, $columnsToMod)) {
$res = InputValueModel::find($data); $res = InputValueModel::find($data);
return $res->value ?? ""; return $res->value ?? "";
@ -224,11 +245,95 @@ class FormController extends BaseController
return $data; return $data;
}); });
$pagination->create(); $table->beforeTable(function () use($form, $resultForm){
$pagination->render(); $btn = (new ToListBtn(BASE_URL, $form->id))->fetch();
$btn .= (new ToEditBtn(BASE_URL, $resultForm->id))->fetch();
$btn .= (new ToDeleteBtn(BASE_URL, $resultForm->id))->fetch();
return $btn;
});
$table->afterTable(function () use ($resultForm){
$timeCreate = "<h5 class='display-10'>Создано: " . $resultForm->created_at . "</h5>";
$timeUpdate = "<h5 class='display-10'>Обновлено: " . $resultForm->updated_at . "</h5>";;
return $timeCreate . $timeUpdate;
});
$table->create();
$table->render();
})); }));
echo $this->view->render('form/resultItem.html.twig', ['title' => $tableArr["meta"]["title"]]); echo $this->view->render('form/resultItem.html.twig', ['title' => $tableArr["meta"]["title"]]);
} }
private function createFormArr(FormModel $form, FormResModel|false $formResult = false): array
{
// Инициализируем пустые массивы
$meta = [];
$formArr = [];
$formFieldsArr = [];
$resArr = [];
// Получение формы из БД и обработка мета данных формы
if ($formResult) {
$resArr = json_decode($formResult->data, true);
}
if (isset($form->params)) {
$allParams = json_decode($form->params, true);
$meta = array_merge($meta, $allParams['params']);
}
$meta['action'] = "/form-save/" . $form->id;
$meta['method'] = "POST";
$formArr['meta'] = $meta;
$formResults = FormResModel::where("form_id", $form->id)->where("id",);
// Обработка полей формы
if ($form->fields) {
$fields = $form->fields;
foreach ($fields as $field) {
$options = [];
$fieldArr = [];
$params = [];
if ($field->inputValue) {
foreach ($field->inputValue as $value) {
$options[$value['id']] = $value['value'];
}
}
if ($field->params) {
$fieldArr = array_merge($fieldArr, json_decode($field->params, true));
}
$fieldArr['name'] = $field->name ?? $fieldArr['name'];
if ($field->input_type_id === 4) {
if ($resArr[$fieldArr['name']] === $fieldArr['value']) {
$fieldArr['checked'] = "true";
}
}
if ($field->input_type_id === 1) {
if ($resArr[$fieldArr['name']] === $fieldArr['value']) {
$fieldArr['checked'] = "true";
}
}
if ($formResult && $field->input_type_id !== 4) {
$fieldArr['value'] = $resArr[$fieldArr['name']] ?? null;
}
$fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput";
$fieldArr['options'] = $options;
$formArr['data'][] = $fieldArr;
}
}
return $formArr;
}
} }

View File

@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
class FormModel extends Model class FormModel extends Model
{ {
const STATUS_ACTIVE = 1;
const STATUS_ARCHIVE = 2;
protected $table = "form"; protected $table = "form";
protected $fillable = [ protected $fillable = [

View File

@ -0,0 +1,17 @@
<?php
namespace itguild\forms\builders;
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\Hidden;
class HiddenBuilder
{
public static function build(string $name, array $params = []): Hidden
{
$value = $params['value'] ?? "";
unset($params['value']);
return new Hidden(name: $name, value: $value, paramsArray: $params);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace itguild\forms\core\cg_view;
use itguild\forms\debug\Debug;
class CgView
{
public string $viewPath = '';
public bool|string $layout = false;
public function __construct()
{
}
public function render(string $view, array $data = []): void
{
$content = $this->createContent($view, $data);
echo $content;
}
public function fetch(string $view, array $data = []): false|string
{
$content = $this->createContent($view, $data);
return $content;
}
private function createContent(string $view, array $data = []): false|string
{
ob_start();
foreach ($data as $key => $datum){
${"$key"} = $datum;
}
include ($this->viewPath . $view);
$content = ob_get_contents();
ob_end_clean ();
ob_start();
$file_content = $content;
if ($this->layout){
if (file_exists($this->viewPath . $this->layout)){
include ($this->viewPath . $this->layout);
$file_content = ob_get_contents();
}
}
ob_end_clean ();
return $file_content;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\exceptions;
class FormResultNotFoundException extends \Exception
{
function __construct($id)
{
$this -> message = "Ответ формы $id не найдена";
}
}

60
src/inputs/Hidden.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace itguild\forms\inputs;
use itguild\forms\templates\Simple\SimpleTemplate;
use itguild\forms\traits\CreateParams;
class Hidden extends BaseInput
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value = '', array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$hidden = "<input name='$this->name' type='hidden' value='$this->value' $paramsString >";
$label = "";
$this->createLabel();
$this->html = str_replace('{input}', $hidden, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray): void
{
$hidden = new self($name, $value, $paramsArray);
$hidden->create()->render();
}
}

View File

@ -5,6 +5,7 @@ namespace itguild\forms\mappers;
use itguild\forms\builders\Builder; use itguild\forms\builders\Builder;
use itguild\forms\builders\ButtonBuilder; use itguild\forms\builders\ButtonBuilder;
use itguild\forms\builders\CheckBoxBuilder; use itguild\forms\builders\CheckBoxBuilder;
use itguild\forms\builders\HiddenBuilder;
use itguild\forms\builders\LabelBuilder; use itguild\forms\builders\LabelBuilder;
use itguild\forms\builders\RadioButtonBuilder; use itguild\forms\builders\RadioButtonBuilder;
use itguild\forms\builders\SelectBuilder; use itguild\forms\builders\SelectBuilder;
@ -23,6 +24,7 @@ class JsonInputMapper
"select" => SelectBuilder::class, "select" => SelectBuilder::class,
"button" => ButtonBuilder::class, "button" => ButtonBuilder::class,
"checkbox" => CheckBoxBuilder::class, "checkbox" => CheckBoxBuilder::class,
"hidden" => HiddenBuilder::class,
]; ];
} }

View File

@ -6,6 +6,7 @@ abstract class ActionColumn
{ {
protected string $baseUrl; protected string $baseUrl;
protected string $prefix; protected string $prefix;
protected int $id; protected int $id;
public function __construct(string $baseUrl, int $id) public function __construct(string $baseUrl, int $id)
{ {

View File

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

View File

@ -4,6 +4,7 @@ namespace itguild\forms\table;
use itguild\forms\debug\Debug; use itguild\forms\debug\Debug;
use itguild\forms\table\ActionColumn\DeleteActionColumn; use itguild\forms\table\ActionColumn\DeleteActionColumn;
use itguild\forms\table\ActionColumn\EditActionColumn;
use itguild\forms\table\ActionColumn\ViewActionColumn; use itguild\forms\table\ActionColumn\ViewActionColumn;
use itguild\forms\traits\CreateParams; use itguild\forms\traits\CreateParams;
use itguild\forms\widgets\BaseWidget; use itguild\forms\widgets\BaseWidget;
@ -17,6 +18,7 @@ class ListJsonTable
private int $count = 0; private int $count = 0;
private \Closure|false $beforePrintCell; private \Closure|false $beforePrintCell;
private \Closure|false $beforePrint;
private string $baseUrl; private string $baseUrl;
private array $data; private array $data;
@ -34,7 +36,14 @@ class ListJsonTable
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$this->html = "<table $paramsStr>"; $hook = $this->beforePrint;
$this->html .= $hook();
$this->html .= "<table $paramsStr>";
}
public function beforePrint(\Closure $closure): void
{
$this->beforePrint = $closure;
} }
public function createThead(): void public function createThead(): void
@ -91,6 +100,9 @@ class ListJsonTable
case 'delete': case 'delete':
$this->actionsArray[] = DeleteActionColumn::class; $this->actionsArray[] = DeleteActionColumn::class;
break; break;
case 'edit':
$this->actionsArray[] = EditActionColumn::class;
break;
} }
} }
} }

View File

@ -15,6 +15,9 @@ class ViewJsonTable
private string $json; private string $json;
private \Closure|false $beforePrintCell; private \Closure|false $beforePrintCell;
private \Closure|false $beforePrintTable;
private \Closure|false $afterPrintTable;
private array $dataJson; private array $dataJson;
public function __construct($json) public function __construct($json)
@ -22,17 +25,18 @@ class ViewJsonTable
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->dataJson = json_decode($this->data['data']['data'], true); $this->dataJson = json_decode($this->data['data']['data'], true);
} }
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$this->html = "<table $paramsStr>"; $hook = $this->beforePrintTable;
$this->html = $hook();
$this->html .= "<table $paramsStr>";
} }
public function createColum() public function createColum(): string
{ {
foreach ($this->data['meta']['columns'] as $key => $column){ foreach ($this->data['meta']['columns'] as $key => $column){
if ($this->issetColumn($key)){ if ($this->issetColumn($key)){
@ -66,7 +70,10 @@ class ViewJsonTable
public function endTable() public function endTable()
{ {
$this->html .= "</table"; $this->html .= "</table>";
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
} }
public function create() public function create()
@ -76,6 +83,17 @@ class ViewJsonTable
$this->endTable(); $this->endTable();
} }
public function beforeTable(\Closure $closure): void
{
$this->beforePrintTable = $closure;
}
public function afterTable(\Closure $closure): void
{
$this->afterPrintTable = $closure;
}
public function render() public function render()
{ {

View File

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

View File

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

View File

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

View File

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

View File

@ -13,7 +13,7 @@ trait CreateOption
{ {
$optionsString = ""; $optionsString = "";
foreach ($options as $val => $title){ foreach ($options as $val => $title){
$selected = $value === $val ? "selected" : ""; $selected = (int)$value === $val ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>"; $optionsString .= "<option $selected value='$val'>$title</option>";
} }
return $optionsString; return $optionsString;

42
views/admin/index.php Normal file
View File

@ -0,0 +1,42 @@
<?php
/**
* @var \itguild\forms\ActiveForm $form ;
*/
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\TextArea;
?>
<?php echo $form->beginForm("/admin/save-form"); ?>
<?php $form->field(TextInput::class, name: "title", params: [
'class' => "form-control",
'placeholder' => 'Название формы'
])
->setLabel("Название формы")
->render();
$form->field(TextArea::class, name: "params", params: [
'class' => "form-control",
'placeholder' => 'Параметры формы'
])
->setLabel("Параметры формы")
->render();
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
<script>
$.ajax({
url:'/handler.php',
method: 'post',
dataType: 'html'
data: {element: 'value', abc:'test'},
success: function (data){
console.log(<<Sent successfully: >> + data);
}
});
</script>
<?php $form->endForm(); ?>

View File

@ -0,0 +1,25 @@
<?php
/**
* @var string $title;
* @var string $content;
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title ?></title>
<link rel="stylesheet" href="vendor/twbs/bootstrap/dist/css/bootstrap.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<?php echo $content ?>
</div>
</div>
</div>
</body>
</html>