This commit is contained in:
kali 2024-04-03 18:25:38 +03:00
parent 09b5167a1a
commit cfe5b3cca5
17 changed files with 227 additions and 147 deletions

View File

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

@ -60,6 +60,8 @@ class __TwigTemplate_e48551b2ca758109aa7de7556ee07a2e extends Template
echo " </div>
</div>
</div>
</body>
@ -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()

View File

@ -58,10 +58,15 @@ class __TwigTemplate_e631c8331e0b3314f0fca4478f143546 extends Template
echo " <h1 class=\"display-4\">";
echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true);
echo "</h1>
";
// 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()

View File

@ -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();

View File

@ -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"
}
},
{

View File

@ -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));

View File

@ -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,18 +30,27 @@ 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;
// Обработка полей формы
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'];
@ -46,10 +60,12 @@ class FormController extends BaseController
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"];
@ -75,36 +91,73 @@ 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 = [];
$columns = [];
$columnsToMod = [];
$table = FormResModel::where("form_id", $id)->get();
foreach ($table as $key => $item){
$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['columns'] = $columns;
$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) {
$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"]]);
}

View File

@ -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 = [

View File

@ -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");
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace itguild\forms\exceptions;
class FormNotFoundException extends \Exception
{
function __construct($formId)
{
$this -> message = "Форма $formId не найдена";
}
}

View File

@ -31,11 +31,11 @@ class RadioButton extends BaseInput
$optionsString = $this->createOption($this->options, $this->value);
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
$label = "";
if($this->hasLabel == true) {
$label = "<label >$this->labelTitle</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;
}

View File

@ -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');

View File

@ -0,0 +1,69 @@
<?php
namespace itguild\forms\widgets;
use itguild\forms\debug\Debug;
class PaginationWidget extends BaseWidget
{
private string $html = "";
private int $perPage = 10;
private int $countItem;
private int $currentPage = 1;
private int $countPages;
private string $baseUrl;
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
{
$this->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 ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$prev'>$prev</a></li>" : "";
$btns .= "<li class='page-item'><a class='page-link active' href='#'>$this->currentPage</a></li>";
$btns .= $next ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$next'>$next</a></li>" : "";
$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 '<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="{previous_link}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{btns}
<li class="page-item">
<a class="page-link" href="{next_link}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>';
}
}

View File

@ -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 .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column){
$this->html .= "<th>" . $column . "</th>";
}
@ -44,8 +49,15 @@ class TableJsonWidget extends BaseWidget
if ($this->data['data']){
foreach ($this->data['data'] as $col){
$this->html .= "<tr>";
$this->count += 1;
$this->html .= "<td>" . $this->count . "</td>";
foreach ($col as $key => $row){
if ($this->issetColumn($key)){
if ($this->beforePrintCell){
$hook = $this->beforePrintCell;
$row = $hook($key, $row);
}
$this->html .= "<td>" . $row . "</td>";
}
}
@ -97,4 +109,9 @@ class TableJsonWidget extends BaseWidget
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
}

View File

@ -5,4 +5,5 @@
{% block content %}
<h1 class="display-4">{{ title }}</h1>
{{ create_form() }}
{% endblock %}

View File

@ -4,5 +4,7 @@
{% block content %}
<h1 class="display-4">{{ title }}</h1>
{{ create_table() }}
{{ create_pagination() }}
{% endblock %}

View File

@ -16,6 +16,8 @@
</div>
</div>
</div>
</body>