bd 2.0
This commit is contained in:
@ -6,9 +6,11 @@ use itguild\forms\app\core\BaseController;
|
||||
|
||||
use itguild\forms\app\models\FormModel;
|
||||
use itguild\forms\app\models\FormResModel;
|
||||
use itguild\forms\app\models\User;
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\Form;
|
||||
use itguild\forms\JsonForm;
|
||||
use itguild\forms\widgets\TableJsonWidget;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
|
||||
@ -35,8 +37,8 @@ class FormController extends BaseController
|
||||
foreach ($fields as $field) {
|
||||
$options = [];
|
||||
$fieldArr = [];
|
||||
if ($field->inputValue){
|
||||
foreach ($field->inputValue as $value){
|
||||
if ($field->inputValue) {
|
||||
foreach ($field->inputValue as $value) {
|
||||
$options[$value['id']] = $value['value'];
|
||||
}
|
||||
}
|
||||
@ -49,7 +51,7 @@ class FormController extends BaseController
|
||||
$formArr['data'][] = $fieldArr;
|
||||
}
|
||||
|
||||
$formArr['data'][] = ['type' => "button", 'typeInput' => "submit","value" => "Отправить", "name" => "btn", "class" => "btn btn-primary"];
|
||||
$formArr['data'][] = ['type' => "button", 'typeInput' => "submit", "value" => "Отправить", "name" => "", "class" => "btn btn-primary"];
|
||||
|
||||
//Debug::dd($fields);
|
||||
|
||||
@ -73,4 +75,37 @@ class FormController extends BaseController
|
||||
$form = Form::Create(['title' => "dsds", 'status' => 1, 'params' => "",]);
|
||||
}
|
||||
|
||||
public function result($id)
|
||||
{
|
||||
$meta = [];
|
||||
$data = [];
|
||||
$tableArr = [];
|
||||
|
||||
$table = FormResModel::where("form_id", $id)->get();
|
||||
foreach ($table as $key => $item){
|
||||
|
||||
$data[] = json_decode($item->data, true);
|
||||
|
||||
}
|
||||
|
||||
$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) {
|
||||
|
||||
$table = new TableJsonWidget($tableRes);
|
||||
$table->create();
|
||||
$table->render();
|
||||
|
||||
}));
|
||||
|
||||
echo $this->view->render('form/result.html.twig', ['title' => $tableArr['meta']["title"]]);
|
||||
}
|
||||
|
||||
}
|
8
src/widgets/BaseWidget.php
Normal file
8
src/widgets/BaseWidget.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\widgets;
|
||||
|
||||
class BaseWidget
|
||||
{
|
||||
|
||||
}
|
100
src/widgets/TableJsonWidget.php
Normal file
100
src/widgets/TableJsonWidget.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\widgets;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
|
||||
class TableJsonWidget extends BaseWidget
|
||||
{
|
||||
use CreateParams;
|
||||
|
||||
private string $html = '';
|
||||
private string $json;
|
||||
|
||||
private array $data;
|
||||
|
||||
public function __construct(string $json)
|
||||
{
|
||||
$this->json = $json;
|
||||
$this->data = json_decode($this->json, true);
|
||||
}
|
||||
|
||||
public function beginTable(): void
|
||||
{
|
||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||
$this->html = "<table $paramsStr>";
|
||||
}
|
||||
|
||||
public function createThead(): void
|
||||
{
|
||||
if (!isset($this->data['meta']['columns'])){
|
||||
return;
|
||||
}
|
||||
|
||||
$this->html .= "<thead><tr>";
|
||||
foreach ($this->data['meta']['columns'] as $key => $column){
|
||||
$this->html .= "<th>" . $column . "</th>";
|
||||
}
|
||||
$this->html .= "</tr></thead>";
|
||||
}
|
||||
|
||||
public function createTbody(): void
|
||||
{
|
||||
if ($this->data['data']){
|
||||
foreach ($this->data['data'] as $col){
|
||||
$this->html .= "<tr>";
|
||||
foreach ($col as $key => $row){
|
||||
if ($this->issetColumn($key)){
|
||||
$this->html .= "<td>" . $row . "</td>";
|
||||
}
|
||||
}
|
||||
$this->html .= "</tr>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function issetColumn($column)
|
||||
{
|
||||
if (isset($this->data['meta']['columns'])){
|
||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
|
||||
if ($key === $column){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->beginTable();
|
||||
$this->createThead();
|
||||
$this->createTbody();
|
||||
$this->endTable();
|
||||
}
|
||||
|
||||
public function tableAction($json): void
|
||||
{
|
||||
|
||||
$tableJson = json_decode($json, true);
|
||||
|
||||
foreach ($tableJson as $key => $value) {
|
||||
echo "<tr>";
|
||||
echo "<td>" . $key . "</td>";
|
||||
echo "<td>" . $value . "</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
}
|
||||
|
||||
public function endTable(): void
|
||||
{
|
||||
$this->html .= "</table>";
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
echo $this->html;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user