This commit is contained in:
kali 2024-05-21 15:53:19 +03:00
parent 8e13f4a425
commit eb361ff002
13 changed files with 80 additions and 54 deletions

View File

@ -1,44 +1,18 @@
function ajaxRequest() {
const request = new XMLHttpRequest();
request.open("GET", "/admin/form-add-fields");
request.addEventListener("load", () => {
const responseText = request.responseText;
const targetElement = document.getElementById("buttonAdd");
const divElement = document.createElement("div");
divElement.classList.add("form-control");
divElement.style.backgroundColor = "#bbbbbb";
divElement.innerHTML = responseText;
targetElement.insertAdjacentElement("beforebegin", divElement);
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", () => {
divElement.remove();
});
divElement.appendChild(deleteButton);
});
request.send();
}
function getValueSelect(){ function getValueSelect(){
var selectValue = document.getElementById("selectID").value let selectValue = document.getElementById("selectID").value
var request = new XMLHttpRequest(); let request = new XMLHttpRequest();
let button = document.getElementById("buttonAdd"); let button = document.getElementById("buttonAdd");
let count = parseInt(button.dataset.count) + 1; let count = parseInt(button.dataset.count) + 1;
button.dataset.count = count; button.dataset.count = count;
console.log(count); console.log(count);
request.open("GET", "/admin/set-value-select?value=" + selectValue + "&count=" + count, true) request.open("GET", "/admin/set-value-select?value=" + selectValue + "&count=" + count, true)
request.addEventListener("load", () => { request.addEventListener("load", () => {
var responseText = request.responseText; let responseText = request.responseText;
var targetElement = document.getElementById("buttonAdd"); let targetElement = document.getElementById("buttonAdd");
var divElement = document.createElement("div"); let divElement = document.createElement("div");
divElement.classList.add("form-control form-new-block");
divElement.innerHTML = responseText; divElement.innerHTML = responseText;
targetElement.insertAdjacentElement("beforebegin", divElement); targetElement.insertAdjacentElement("beforebegin", divElement);
const deleteButton = document.createElement("button");
deleteButton.textContent = "X";
deleteButton.classList = 'btn-danger delete-button';
deleteButton.addEventListener("click", () => { deleteButton.addEventListener("click", () => {
divElement.remove(); divElement.remove();
}); });

View File

@ -2,8 +2,9 @@
position: absolute; position: absolute;
top: 5px; top: 5px;
right: 5px; right: 5px;
} }
.form-new-block { .form-block {
background-color: #bbbbbb; background-color: #bbbbbb;
position: relative; position: relative;

View File

@ -39,7 +39,7 @@ class AdminController extends BaseController
{ {
$this->cgView->layout = "/layouts/admin_layout.php"; $this->cgView->layout = "/layouts/admin_layout.php";
$this->cgView->render("/admin/index.php", [ $this->cgView->render("/admin/index.php", [
'title' => "Редактировать форму", 'title' => "Создать форму",
'form' => new ActiveForm(), 'form' => new ActiveForm(),
'dto' => new FormDTO(), 'dto' => new FormDTO(),
]); ]);
@ -139,16 +139,7 @@ class AdminController extends BaseController
$countInput = $_GET["count"]; $countInput = $_GET["count"];
$form = new ActiveForm(); $form = new ActiveForm();
$fields = [ $viewName = InputTypeModel::getViewNameByTypeId($selectValue);
1 => '_checkBox',
2 => '_text_input',
3 => '_textarea',
4 => '_radioButton',
5 => '_button',
6 => '_select'
];
$viewName = $fields[$selectValue] ?? false;
if (!$viewName) { if (!$viewName) {
throw new FieldTypeNotFound($selectValue); throw new FieldTypeNotFound($selectValue);
@ -159,13 +150,17 @@ class AdminController extends BaseController
public function editFormAction($id) public function editFormAction($id)
{ {
$form = FormModel::find($id)->toArray(); $form = FormModel::find($id);
$dto = new FormDTO(); $dto = new FormDTO();
$formArr = json_decode($form['params'], true); $formFields = $form->fields;
$formDataArr = $form->toArray();
$formArr = json_decode($formDataArr['params'], true);
$fillableRes = []; $fillableRes = [];
$fillableRes['title'] = $form['title']; $fillableRes['title'] = $formDataArr['title'];
$fillableRes['id'] = $form['id']; $fillableRes['id'] = $formDataArr['id'];
foreach ($formArr['meta'] as $key => $value) { foreach ($formArr['meta'] as $key => $value) {
if ($key == 'perpage') { if ($key == 'perpage') {
$fillableRes['perPage'] = (string)$value; $fillableRes['perPage'] = (string)$value;
@ -186,6 +181,7 @@ class AdminController extends BaseController
'title' => "Редактировать форму", 'title' => "Редактировать форму",
'form' => new ActiveForm(), 'form' => new ActiveForm(),
'dto' => $dto, 'dto' => $dto,
'fields' => $formFields,
]); ]);
} }

View File

@ -22,4 +22,18 @@ class InputTypeModel extends Model
protected $hidden = [ protected $hidden = [
]; ];
public static function getViewNameByTypeId(int $id): ?string
{
$fields = [
1 => '_checkBox',
2 => '_text_input',
3 => '_textarea',
4 => '_radioButton',
5 => '_button',
6 => '_select'
];
return $fields[$id] ?? null;
}
} }

View File

@ -32,6 +32,9 @@ class CgView
{ {
ob_start(); ob_start();
$cgView = new self();
$cgView->viewPath = $this->viewPath;
foreach ($data as $key => $datum){ foreach ($data as $key => $datum){
${"$key"} = $datum; ${"$key"} = $datum;
} }

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Кнопка</p></b> <b><p>Кнопка</p></b>
<?php <?php
/** /**
@ -15,3 +16,7 @@ $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class'
'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render(); 'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '5'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '5'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Чекбокс</p></b> <b><p>Чекбокс</p></b>
<?php <?php
/** /**
@ -13,4 +14,7 @@ use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control", $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '1'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '1'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Радио кнопка</p></b> <b><p>Радио кнопка</p></b>
<?php <?php
/** /**
@ -16,4 +17,7 @@ $form->field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][rad
->setLabel("Введите радио кнопки, каждую на новой строке") ->setLabel("Введите радио кнопки, каждую на новой строке")
->render(); ->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '4'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '4'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Список</p></b> <b><p>Список</p></b>
<?php <?php
/** /**
@ -18,4 +19,7 @@ $form->field(TextArea::class, name: "InputForm[$count][options]", params: ['clas
->setLabel("Опции выпадающего списка, каждый с новой строки") ->setLabel("Опции выпадающего списка, каждый с новой строки")
->render(); ->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '6'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '6'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Текстовое поле</p></b> <b><p>Текстовое поле</p></b>
<?php <?php
/** /**
@ -15,4 +16,7 @@ $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class'
$form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control", $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control",
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render(); 'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '2'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '2'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Описание</p></b> <b><p>Описание</p></b>
<?php <?php
/** /**
@ -14,4 +15,7 @@ $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class'
$form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control", $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control",
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render(); 'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render();
$form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render();
$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '3'])->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '3'])->render();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -2,15 +2,17 @@
/** /**
* @var \itguild\forms\ActiveForm $form * @var \itguild\forms\ActiveForm $form
* @var \itguild\forms\app\DTO\FormDTO $dto * @var \itguild\forms\app\DTO\FormDTO $dto
* @var \itguild\forms\core\cg_view\CgView $cgView
*/ */
use itguild\forms\ActiveForm;
use itguild\forms\inputs\TextInput; use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\TextArea; use itguild\forms\inputs\TextArea;
use itguild\forms\app\controllers\AdminController; use itguild\forms\app\controllers\AdminController;
?> ?>
<link rel="stylesheet" href="../../public/style/main.css">
<?php echo $form->beginForm("/admin/save-form/"); ?> <?php echo $form->beginForm("/admin/save-form/"); ?>
<?php $form->field(TextInput::class, name: "title", params: [ <?php $form->field(TextInput::class, name: "title", params: [
'class' => "form-control", 'class' => "form-control",
@ -64,6 +66,15 @@ $form->field(\itguild\forms\inputs\Hidden::class, name: "formId", params: [
"value" => $dto->get('id') "value" => $dto->get('id')
])->render(); ])->render();
if (isset($fields)){
$i = 0;
foreach ($fields as $field){
$viewName = \itguild\forms\app\models\InputTypeModel::getViewNameByTypeId($field['input_type_id']);
$cgView->render("/admin/field_type/$viewName.php", ['form' => new ActiveForm(), 'count' => $i]);
$i++;
}
}
$form->field(\itguild\forms\inputs\Button::class, name: "", params: [ $form->field(\itguild\forms\inputs\Button::class, name: "", params: [
"id" => 'buttonAdd', "id" => 'buttonAdd',
"value" => 'Добавить поле', "value" => 'Добавить поле',
@ -80,4 +91,5 @@ $form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
]) ])
->render(); ->render();
$form->endForm(); $form->endForm();
?> ?>

View File

@ -13,6 +13,7 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" /> integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
<script src="/public/js/ajax.js"></script> <script src="/public/js/ajax.js"></script>
<link rel="stylesheet" href="/public/style/main.css">
</head> </head>
<body> <body>
<div class="container"> <div class="container">