bd 1.0
This commit is contained in:
@ -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 "<form $paramsString >";
|
||||
}
|
||||
|
||||
public function endForm(): string
|
||||
{
|
||||
return "</form>";
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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' => "",]);
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\app\controllers;
|
||||
namespace itguild\forms\app\controllers;
|
||||
|
||||
use itguild\forms\app\core\BaseController;
|
||||
use itguild\forms\app\models\FormResModel;
|
||||
use itguild\forms\debug\Debug;
|
||||
use Illuminate\Http\Request;
|
||||
class FormResController extends BaseController
|
||||
{
|
||||
public function indexAction($id)
|
||||
public function formSetRes($id, $data)
|
||||
{
|
||||
Debug::prn(FormResModel::find($id)->first());
|
||||
|
||||
FormResModel::Create([
|
||||
'form_id' => $id,
|
||||
'data' => json_encode($data),
|
||||
]);
|
||||
|
||||
return "Данные успешно сохранены в базе данных.";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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 = "<input type='button' value='$this->value' name='$this->name' $paramsString>";
|
||||
$button = "<input type='$this->typeInput' value='$this->value' name='$this->name' $paramsString>";
|
||||
|
||||
$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();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user