From cfe5b3cca57a2731b3fb3d257304737c5a1301ab Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 3 Apr 2024 18:25:38 +0300 Subject: [PATCH] bd 3.0 --- .../1c/1c1a65045d16ca598e290fed477b7831.php | 96 -------------- .../b6/b6a9b94df97efc8dc9336c36ada05909.php | 4 +- .../ea/ea1278337c261f39a1523ba336d5c786.php | 9 +- console.php | 2 +- form.json | 20 ++- index.php | 1 + src/app/controllers/FormController.php | 125 +++++++++++++----- src/app/models/FormInputModel.php | 2 +- src/app/models/FormModel.php | 2 +- src/exceptions/FormNotFoundException.php | 11 ++ src/inputs/RadioButton.php | 8 +- src/migrations/FormsInputMigration.php | 3 + src/widgets/PaginationWidget.php | 69 ++++++++++ src/widgets/TableJsonWidget.php | 17 +++ views/form/form.html.twig | 1 + views/form/result.html.twig | 2 + views/layouts/simple.html.twig | 2 + 17 files changed, 227 insertions(+), 147 deletions(-) delete mode 100644 compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php create mode 100644 src/exceptions/FormNotFoundException.php create mode 100644 src/widgets/PaginationWidget.php diff --git a/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php b/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php deleted file mode 100644 index 66be4ae..0000000 --- a/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php +++ /dev/null @@ -1,96 +0,0 @@ -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 index fb0d5fb..69844b3 100644 --- a/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php +++ b/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php @@ -60,6 +60,8 @@ class __TwigTemplate_e48551b2ca758109aa7de7556ee07a2e extends Template echo " + + @@ -91,7 +93,7 @@ class __TwigTemplate_e48551b2ca758109aa7de7556ee07a2e extends Template */ public function getDebugInfo() { - return array ( 76 => 15, 70 => 5, 60 => 16, 58 => 15, 45 => 5, 39 => 1,); + return array ( 78 => 15, 72 => 5, 60 => 16, 58 => 15, 45 => 5, 39 => 1,); } public function getSourceContext() diff --git a/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php b/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php index 6353f70..3f75964 100644 --- a/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php +++ b/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php @@ -58,10 +58,15 @@ class __TwigTemplate_e631c8331e0b3314f0fca4478f143546 extends Template echo "

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

+ "; - // line 7 + // line 8 echo twig_escape_filter($this->env, $this->env->getFunction('create_table')->getCallable()(), "html", null, true); echo " + "; + // line 9 + echo twig_escape_filter($this->env, $this->env->getFunction('create_pagination')->getCallable()(), "html", null, true); + echo " "; } @@ -86,7 +91,7 @@ class __TwigTemplate_e631c8331e0b3314f0fca4478f143546 extends Template */ public function getDebugInfo() { - return array ( 63 => 7, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); + return array ( 68 => 9, 64 => 8, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); } public function getSourceContext() diff --git a/console.php b/console.php index 34ef2d2..83db175 100755 --- a/console.php +++ b/console.php @@ -27,4 +27,4 @@ require_once "bootstrap/db.php"; //\itguild\forms\migrations\FormsInputMigration::up(); //\itguild\forms\migrations\FormsResMigration::up(); //\itguild\forms\migrations\InputsValueMigrations::up(); -DB::forms('form_input')-> get(); + diff --git a/form.json b/form.json index e5334ed..b4b7670 100755 --- a/form.json +++ b/form.json @@ -10,7 +10,9 @@ "placeholder": "Email", "id": "emailId", "label": { - "title": "Email", "for": "emailId", "class": "form-label" + "title": "Email", + "for": "emailId", + "class": "form-label" }, "class": "form-control" }, @@ -27,10 +29,16 @@ "class": "form-control", "id": "cityId", "label": { - "title": "Выберите город", "for": "cityId", "class": "form-label" + "title": "Выберите город", + "for": "cityId", + "class": "form-label" }, "value": 2, - "options": {"1": "Москва", "2": "Донецк", "3": "Ростов"} + "options": { + "1": "Москва", + "2": "Донецк", + "3": "Ростов" + } }, { "type": "textArea", @@ -44,7 +52,9 @@ "class": "", "id": "agreeId", "label": { - "title": "Согласен с правилами", "for": "agreeId", "class": "form-label" + "title": "Согласен с правилами", + "for": "agreeId", + "class": "form-label" } }, { @@ -54,4 +64,4 @@ "class": "btn btn-primary" } ] -} +} \ No newline at end of file diff --git a/index.php b/index.php index 4a2972e..796f217 100755 --- a/index.php +++ b/index.php @@ -25,6 +25,7 @@ $router->get("/input-type/{id}", [\itguild\forms\app\controllers\InputTypeContro $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"]); $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/app/controllers/FormController.php b/src/app/controllers/FormController.php index f369f4e..3269c56 100644 --- a/src/app/controllers/FormController.php +++ b/src/app/controllers/FormController.php @@ -2,14 +2,19 @@ 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\TableJsonWidget; use Twig\TwigFunction; @@ -25,30 +30,41 @@ class FormController extends BaseController // Получение формы из БД и обработка мета данных формы $form = FormModel::find($id); - if ($form->params) { - $meta = array_merge($meta, json_decode($form->params, true)); + + if (!$form) { + throw new FormNotFoundException($id); + } + + 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; // Обработка полей формы - $fields = $form->fields; - foreach ($fields as $field) { - $options = []; - $fieldArr = []; - if ($field->inputValue) { - foreach ($field->inputValue as $value) { - $options[$value['id']] = $value['value']; + 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)); + 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; } - $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"]; @@ -75,37 +91,74 @@ class FormController extends BaseController $form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]); } - public function result($id) + public function result($id, $page = 1) { - $meta = []; - $data = []; - $tableArr = []; - $table = FormResModel::where("form_id", $id)->get(); - foreach ($table as $key => $item){ + $meta = []; + $data = []; + $tableArr = []; + $columns = []; + $columnsToMod = []; - $data[] = json_decode($item->data, true); + $form = FormModel::find($id); + $formParams = json_decode($form->params, true); + $perPage = $formParams['meta']['perpage'] ?? "10"; + $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); + } + + 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'] = ["email" => "Email", "description" => "Описание 1", "description2" => "Описание 2"]; - $meta['params'] = ["class" => "table table-bordered", "border" => "1"]; + } - $tableArr["meta"] = $meta; - $tableArr["data"] = $data; - $tableRes = json_encode($tableArr); - - $this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes) { + $meta['title'] = "Form ID: " . $id; + $meta['columns'] = $columns; + $meta['params'] = ["class" => "table table-bordered", "border" => "1"]; - $table = new TableJsonWidget($tableRes); - $table->create(); - $table->render(); - })); + $tableArr["meta"] = $meta; + $tableArr["data"] = $data; - echo $this->view->render('form/result.html.twig', ['title' => $tableArr['meta']["title"]]); + $tableRes = json_encode($tableArr); + + $this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes, $columnsToMod, $form) { + + $table = new TableJsonWidget($tableRes); + + + $table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form){ + if (in_array($column, $columnsToMod)){ + $res = InputValueModel::find($data); + return $res->value ?? ""; + } + + return $data; + }); + $table->create(); + $table->render(); + + + + })); + + $this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems) { + + $pagination = new PaginationWidget($countItems, $perPage, $page, "http://forms.local/form-load/1"); + $pagination->create(); + $pagination->render(); + + })); + echo $this->view->render('form/result.html.twig', ['title' => $tableArr['meta']["title"]]); } } \ No newline at end of file diff --git a/src/app/models/FormInputModel.php b/src/app/models/FormInputModel.php index 1c30597..24eec06 100644 --- a/src/app/models/FormInputModel.php +++ b/src/app/models/FormInputModel.php @@ -9,7 +9,7 @@ class FormInputModel extends Model protected $table = "form_input"; protected $fillable = [ - 'form_id', 'input_type_id', 'params', 'inputType' + 'form_id', 'input_type_id', 'label', 'name', 'params', 'priority', 'inputType' ]; protected $hidden = [ diff --git a/src/app/models/FormModel.php b/src/app/models/FormModel.php index 361cbac..a7b2022 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", "id"); + return $this->hasMany(FormInputModel::class, "form_id", "id")->orderBy("priority"); } } \ No newline at end of file diff --git a/src/exceptions/FormNotFoundException.php b/src/exceptions/FormNotFoundException.php new file mode 100644 index 0000000..c1649f3 --- /dev/null +++ b/src/exceptions/FormNotFoundException.php @@ -0,0 +1,11 @@ + message = "Форма $formId не найдена"; + } +} \ No newline at end of file diff --git a/src/inputs/RadioButton.php b/src/inputs/RadioButton.php index 139eaa7..5bf3abb 100755 --- a/src/inputs/RadioButton.php +++ b/src/inputs/RadioButton.php @@ -31,11 +31,11 @@ class RadioButton extends BaseInput $optionsString = $this->createOption($this->options, $this->value); $radioButton = "$optionsString"; $label = ""; - if($this->hasLabel == true) { - $label = ""; - } + + $this->createLabel(); + $this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate()); - $this->html = str_replace('{label}', $label, $this->html); + $this->html = str_replace('{label}', $this->labelString, $this->html); return $this; } diff --git a/src/migrations/FormsInputMigration.php b/src/migrations/FormsInputMigration.php index 1473d06..5cfadb9 100644 --- a/src/migrations/FormsInputMigration.php +++ b/src/migrations/FormsInputMigration.php @@ -14,7 +14,10 @@ class FormsInputMigration extends Illuminate\Database\Migrations\Migration $table->id("id"); $table->unsignedBigInteger('form_id'); $table->unsignedBigInteger('input_type_id'); + $table->string('label'); + $table->string('name'); $table->text('params'); + $table->integer(11)->default(1); $table->timestamps(); $table->index('form_id'); diff --git a/src/widgets/PaginationWidget.php b/src/widgets/PaginationWidget.php new file mode 100644 index 0000000..7480de2 --- /dev/null +++ b/src/widgets/PaginationWidget.php @@ -0,0 +1,69 @@ +countItem = $countItem; + $this->perPage = $perPage; + $this->currentPage = $currentPage; + $this->baseUrl = $baseUrl; + + $this->countPages = ceil($this->countItem / $perPage); + } + + public function create() + { + $prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null; + $next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null; + $btns = $prev ? "
  • $prev
  • " : ""; + $btns .= "
  • $this->currentPage
  • "; + $btns .= $next ? "
  • $next
  • " : ""; + + $this->html = str_replace('{btns}', $btns, $this->getTemplate()); + $this->html = str_replace('{previous_link}', $this->baseUrl . "/1", $this->html); + $this->html = str_replace('{next_link}', $this->baseUrl . "/" . $this->countPages, $this->html); + } + + public function render(): void + { + echo $this->html; + } + + public function fetch() + { + return $this->html; + } + + private function getTemplate() + { + return ''; + } +} \ No newline at end of file diff --git a/src/widgets/TableJsonWidget.php b/src/widgets/TableJsonWidget.php index 496fa66..6a72922 100644 --- a/src/widgets/TableJsonWidget.php +++ b/src/widgets/TableJsonWidget.php @@ -12,10 +12,14 @@ class TableJsonWidget extends BaseWidget private string $html = ''; private string $json; + private int $count = 0; + private \Closure|false $beforePrintCell; + private array $data; public function __construct(string $json) { + $this->beforePrintCell = false; $this->json = $json; $this->data = json_decode($this->json, true); } @@ -33,6 +37,7 @@ class TableJsonWidget extends BaseWidget } $this->html .= ""; + $this->html .= "" . "ID" . ""; foreach ($this->data['meta']['columns'] as $key => $column){ $this->html .= "" . $column . ""; } @@ -44,8 +49,15 @@ class TableJsonWidget extends BaseWidget if ($this->data['data']){ foreach ($this->data['data'] as $col){ $this->html .= ""; + $this->count += 1; + $this->html .= "" . $this->count . ""; foreach ($col as $key => $row){ if ($this->issetColumn($key)){ + if ($this->beforePrintCell){ + $hook = $this->beforePrintCell; + $row = $hook($key, $row); + } + $this->html .= "" . $row . ""; } } @@ -97,4 +109,9 @@ class TableJsonWidget extends BaseWidget { echo $this->html; } + + public function setBeforePrintCell(\Closure $closure): void + { + $this->beforePrintCell = $closure; + } } \ No newline at end of file diff --git a/views/form/form.html.twig b/views/form/form.html.twig index ef73269..3054e0e 100644 --- a/views/form/form.html.twig +++ b/views/form/form.html.twig @@ -5,4 +5,5 @@ {% block content %}

    {{ title }}

    {{ create_form() }} + {% endblock %} \ No newline at end of file diff --git a/views/form/result.html.twig b/views/form/result.html.twig index dfc2846..69b47a1 100644 --- a/views/form/result.html.twig +++ b/views/form/result.html.twig @@ -4,5 +4,7 @@ {% block content %}

    {{ title }}

    + {{ create_table() }} + {{ create_pagination() }} {% endblock %} \ No newline at end of file diff --git a/views/layouts/simple.html.twig b/views/layouts/simple.html.twig index ab4c187..1ebffb5 100755 --- a/views/layouts/simple.html.twig +++ b/views/layouts/simple.html.twig @@ -16,6 +16,8 @@ + +