bd 3.0
This commit is contained in:
@ -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"]]);
|
||||
}
|
||||
|
||||
}
|
@ -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 = [
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
11
src/exceptions/FormNotFoundException.php
Normal file
11
src/exceptions/FormNotFoundException.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\exceptions;
|
||||
|
||||
class FormNotFoundException extends \Exception
|
||||
{
|
||||
function __construct($formId)
|
||||
{
|
||||
$this -> message = "Форма $formId не найдена";
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
|
69
src/widgets/PaginationWidget.php
Normal file
69
src/widgets/PaginationWidget.php
Normal 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">«</span>
|
||||
</a>
|
||||
</li>
|
||||
{btns}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{next_link}" aria-label="Next">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>';
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user