From a0458705fb18c8c482435540a5b9cf45fa367a36 Mon Sep 17 00:00:00 2001 From: kali Date: Mon, 1 Apr 2024 17:56:37 +0300 Subject: [PATCH] bd 1.0 --- .../1c/1c1a65045d16ca598e290fed477b7831.php | 96 +++++++++++++++++ .../b6/b6a9b94df97efc8dc9336c36ada05909.php | 101 ++++++++++++++++++ index.php | 1 + src/JsonForm.php | 17 ++- src/app/controllers/FormController.php | 56 +++++++++- src/app/controllers/FormResController.php | 12 ++- src/app/models/FormInputModel.php | 11 +- src/app/models/FormModel.php | 2 +- src/builders/ButtonBuilder.php | 4 +- src/inputs/Button.php | 11 +- src/traits/CreateParams.php | 6 +- views/form/form.html.twig | 8 ++ 12 files changed, 308 insertions(+), 17 deletions(-) create mode 100644 compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php create mode 100644 compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php create mode 100644 views/form/form.html.twig diff --git a/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php b/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php new file mode 100644 index 0000000..66be4ae --- /dev/null +++ b/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php @@ -0,0 +1,96 @@ +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 "

"; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + echo "

+"; + // 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"); + } +} diff --git a/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php b/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php new file mode 100644 index 0000000..fb0d5fb --- /dev/null +++ b/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php @@ -0,0 +1,101 @@ +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 " + + + + "; + // line 5 + $this->displayBlock('title', $context, $blocks); + echo " + + + + + +
+
+
+ "; + // line 15 + $this->displayBlock('content', $context, $blocks); + // line 16 + echo "
+ +
+
+ + +"; + } + + // 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"); + } +} diff --git a/index.php b/index.php index 104dccd..54b03df 100755 --- a/index.php +++ b/index.php @@ -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)); diff --git a/src/JsonForm.php b/src/JsonForm.php index b53c531..2e80e77 100755 --- a/src/JsonForm.php +++ b/src/JsonForm.php @@ -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 "
"; + } + + public function endForm(): string + { + return "
"; + + } + 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); } diff --git a/src/app/controllers/FormController.php b/src/app/controllers/FormController.php index 3338f51..c8d8b96 100644 --- a/src/app/controllers/FormController.php +++ b/src/app/controllers/FormController.php @@ -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' => "",]); } } \ No newline at end of file diff --git a/src/app/controllers/FormResController.php b/src/app/controllers/FormResController.php index f94c1f8..e15f6ad 100644 --- a/src/app/controllers/FormResController.php +++ b/src/app/controllers/FormResController.php @@ -1,16 +1,24 @@ first()); + FormResModel::Create([ + 'form_id' => $id, + 'data' => json_encode($data), + ]); + + return "Данные успешно сохранены в базе данных."; } + } \ No newline at end of file diff --git a/src/app/models/FormInputModel.php b/src/app/models/FormInputModel.php index 4c9c5d0..1c30597 100644 --- a/src/app/models/FormInputModel.php +++ b/src/app/models/FormInputModel.php @@ -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"); } } \ No newline at end of file diff --git a/src/app/models/FormModel.php b/src/app/models/FormModel.php index e444f8e..361cbac 100644 --- a/src/app/models/FormModel.php +++ b/src/app/models/FormModel.php @@ -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"); } } \ No newline at end of file diff --git a/src/builders/ButtonBuilder.php b/src/builders/ButtonBuilder.php index 51f6f65..0043ed8 100755 --- a/src/builders/ButtonBuilder.php +++ b/src/builders/ButtonBuilder.php @@ -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); } } \ No newline at end of file diff --git a/src/inputs/Button.php b/src/inputs/Button.php index a78e7b8..1f376b8 100755 --- a/src/inputs/Button.php +++ b/src/inputs/Button.php @@ -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 = ""; + $button = ""; $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(); } diff --git a/src/traits/CreateParams.php b/src/traits/CreateParams.php index 7a463ea..e19cb0e 100755 --- a/src/traits/CreateParams.php +++ b/src/traits/CreateParams.php @@ -2,6 +2,8 @@ namespace itguild\forms\traits; +use itguild\forms\debug\Debug; + trait CreateParams { @@ -13,7 +15,9 @@ trait CreateParams { $paramsString = ""; foreach($data as $key => $param){ - $paramsString .= $key . "='" . $param . "'"; + if(is_string($param)){ + $paramsString .= $key . "='" . $param . "'"; + } } return $paramsString; diff --git a/views/form/form.html.twig b/views/form/form.html.twig new file mode 100644 index 0000000..ef73269 --- /dev/null +++ b/views/form/form.html.twig @@ -0,0 +1,8 @@ +{% extends "layouts/simple.html.twig" %} + +{% block title %}{{ title }}{% endblock %} + +{% block content %} +

{{ title }}

+{{ create_form() }} +{% endblock %} \ No newline at end of file