This commit is contained in:
kali 2024-04-01 17:56:37 +03:00
parent 432b2547cf
commit a0458705fb
12 changed files with 308 additions and 17 deletions

View File

@ -0,0 +1,96 @@
<?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

@ -0,0 +1,101 @@
<?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;
/* layouts/simple.html.twig */
class __TwigTemplate_e48551b2ca758109aa7de7556ee07a2e extends Template
{
private $source;
private $macros = [];
public function __construct(Environment $env)
{
parent::__construct($env);
$this->source = $this->getSourceContext();
$this->parent = false;
$this->blocks = [
'title' => [$this, 'block_title'],
'content' => [$this, 'block_content'],
];
}
protected function doDisplay(array $context, array $blocks = [])
{
$macros = $this->macros;
// line 1
echo "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>";
// line 5
$this->displayBlock('title', $context, $blocks);
echo " </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\">
";
// line 15
$this->displayBlock('content', $context, $blocks);
// line 16
echo " </div>
</div>
</div>
</body>
</html>";
}
// line 5
public function block_title($context, array $blocks = [])
{
$macros = $this->macros;
}
// line 15
public function block_content($context, array $blocks = [])
{
$macros = $this->macros;
}
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
{
return "layouts/simple.html.twig";
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
{
return array ( 76 => 15, 70 => 5, 60 => 16, 58 => 15, 45 => 5, 39 => 1,);
}
public function getSourceContext()
{
return new Source("", "layouts/simple.html.twig", "/home/kali/php/untitled/views/layouts/simple.html.twig");
}
}

View File

@ -23,6 +23,7 @@ $router->get("/form-input/{id}", [\itguild\forms\app\controllers\FormInputContro
$router->get("/form-res/{id}", [\itguild\forms\app\controllers\FormResController::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->post("/form-save/{id}", [\itguild\forms\app\controllers\FormController::class, "saveAction"]);
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

View File

@ -8,9 +8,11 @@ use itguild\forms\debug\Debug;
use itguild\forms\mappers\JsonInputMapper;
use itguild\forms\inputs\BaseInput;
use itguild\forms\ActiveForm;
use itguild\forms\traits\CreateParams;
class JsonForm
{
use CreateParams;
private $jsonData;
private $html = '';
@ -22,8 +24,21 @@ class JsonForm
}
public function beginForm($params = []): string
{
$paramsString = $this->createParams($params);
return "<form $paramsString >";
}
public function endForm(): string
{
return "</form>";
}
public function convertHTML(): void
{
$this->html .= $this->beginForm($this->jsonData['meta']);
foreach ($this->jsonData['data'] as $item) {
@ -47,7 +62,7 @@ class JsonForm
}
}
$this->html .= $this->endForm();
//return $this->fetch($html);
}

View File

@ -5,22 +5,72 @@ namespace itguild\forms\app\controllers;
use itguild\forms\app\core\BaseController;
use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel;
use itguild\forms\debug\Debug;
use itguild\forms\Form;
use itguild\forms\JsonForm;
use Twig\TwigFunction;
class FormController extends BaseController
{
public function indexAction($id)
public function indexAction($id): void
{
// Инициализируем пустые массивы
$meta = [];
$formArr = [];
$formFieldsArr = [];
// Получение формы из БД и обработка мета данных формы
$form = FormModel::find($id);
if ($form->params) {
$meta = array_merge($meta, json_decode($form->params, true));
}
$meta['action'] = "/form-save/" . $form->id;
$meta['method'] = "POST";
$formArr['meta'] = $meta;
// Обработка полей формы
$fields = $form->fields;
Debug::dd($fields);
foreach ($fields as $field) {
$options = [];
$fieldArr = [];
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['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput";
$fieldArr['options'] = $options;
$formArr['data'][] = $fieldArr;
}
$formArr['data'][] = ['type' => "button", 'typeInput' => "submit","value" => "Отправить", "name" => "btn", "class" => "btn btn-primary"];
//Debug::dd($fields);
$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 saveAction($id): void
{
$form = FormResModel::Create(['form_id' => $id, 'data' => json_encode($_POST),]);
}
public function createFormAction()
{
$form = Form::Create([ 'title' => "dsds", 'status' => 1, 'params' => "", ]);
$form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]);
}
}

View File

@ -1,16 +1,24 @@
<?php
namespace itguild\forms\app\controllers;
namespace itguild\forms\app\controllers;
use itguild\forms\app\core\BaseController;
use itguild\forms\app\models\FormResModel;
use itguild\forms\debug\Debug;
use Illuminate\Http\Request;
class FormResController extends BaseController
{
public function indexAction($id)
public function formSetRes($id, $data)
{
Debug::prn(FormResModel::find($id)->first());
FormResModel::Create([
'form_id' => $id,
'data' => json_encode($data),
]);
return "Данные успешно сохранены в базе данных.";
}
}

View File

@ -9,15 +9,20 @@ class FormInputModel extends Model
protected $table = "form_input";
protected $fillable = [
'form_id', 'input_type_id', 'params'
'form_id', 'input_type_id', 'params', 'inputType'
];
protected $hidden = [
];
public function inputType()
public function inputType(): \Illuminate\Database\Eloquent\Relations\HasOne
{
return $this->hasOne(InputTypeModel::class, "id");
return $this->hasOne(InputTypeModel::class, "id", "input_type_id");
}
public function inputValue()
{
return $this->hasMany(InputValueModel::class, "form_input_id", "id");
}
}

View File

@ -18,6 +18,6 @@ class FormModel extends Model
public function fields()
{
return $this->hasMany(FormInputModel::class, "form_id");
return $this->hasMany(FormInputModel::class, "form_id", "id");
}
}

View File

@ -8,9 +8,11 @@ class ButtonBuilder
public static function build(string $name, array $params = [])
{
$value = $params['value'] ?? null;
$typeInput = $params['typeInput'] ?? null;
unset($params['value']);
unset($params['typeInput']);
return new Button(name: $name, value: $value, paramsArray: $params);
return new Button(name: $name, value: $value, typeInput: $typeInput, paramsArray: $params);
}
}

View File

@ -9,7 +9,7 @@ use itguild\forms\inputs\BaseInput;
class Button extends BaseInput
{
use CreateParams;
private $typeInput;
private string $name;
private string $value;
private array $paramsArray;
@ -19,8 +19,9 @@ class Button extends BaseInput
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value, array $paramsArray = [])
public function __construct(string $name, string $value, $typeInput, array $paramsArray = [])
{
$this->typeInput = $typeInput;
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->value = $value;
@ -34,7 +35,7 @@ class Button extends BaseInput
{
$paramsString = $this->createParams($this->paramsArray);
$label = "";
$button = "<input type='button' value='$this->value' name='$this->name' $paramsString>";
$button = "<input type='$this->typeInput' value='$this->value' name='$this->name' $paramsString>";
$this->createLabel();
@ -50,9 +51,9 @@ class Button extends BaseInput
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray = []): void
public static function build( string $name, string $value, string $typeInput, array $paramsArray = []): void
{
$button = new self($name, $value, $paramsArray);
$button = new self($name, $value, $typeInput, $paramsArray);
$button->create()->render();
}

View File

@ -2,6 +2,8 @@
namespace itguild\forms\traits;
use itguild\forms\debug\Debug;
trait CreateParams
{
@ -13,8 +15,10 @@ trait CreateParams
{
$paramsString = "";
foreach($data as $key => $param){
if(is_string($param)){
$paramsString .= $key . "='" . $param . "'";
}
}
return $paramsString;
}

View File

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