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(){
var selectValue = document.getElementById("selectID").value
var request = new XMLHttpRequest();
let selectValue = document.getElementById("selectID").value
let request = new XMLHttpRequest();
let button = document.getElementById("buttonAdd");
let count = parseInt(button.dataset.count) + 1;
button.dataset.count = count;
console.log(count);
request.open("GET", "/admin/set-value-select?value=" + selectValue + "&count=" + count, true)
request.addEventListener("load", () => {
var responseText = request.responseText;
var targetElement = document.getElementById("buttonAdd");
var divElement = document.createElement("div");
divElement.classList.add("form-control form-new-block");
let responseText = request.responseText;
let targetElement = document.getElementById("buttonAdd");
let divElement = document.createElement("div");
divElement.innerHTML = responseText;
targetElement.insertAdjacentElement("beforebegin", divElement);
const deleteButton = document.createElement("button");
deleteButton.textContent = "X";
deleteButton.classList = 'btn-danger delete-button';
deleteButton.addEventListener("click", () => {
divElement.remove();
});

View File

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

View File

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

View File

@ -22,4 +22,18 @@ class InputTypeModel extends Model
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();
$cgView = new self();
$cgView->viewPath = $this->viewPath;
foreach ($data as $key => $datum){
${"$key"} = $datum;
}

View File

@ -1,3 +1,4 @@
<div class="form-block form-control">
<b><p>Кнопка</p></b>
<?php
/**
@ -15,3 +16,7 @@ $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class'
'placeholder' => "Наименование поля"])->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();
?>
<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>
<?php
/**
@ -14,3 +15,6 @@ $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class'
'placeholder' => "Наименование"])->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();
?>
<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>
<?php
/**
@ -17,3 +18,6 @@ $form->field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][rad
->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();
?>
<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>
<?php
/**
@ -19,3 +20,6 @@ $form->field(TextArea::class, name: "InputForm[$count][options]", params: ['clas
->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();
?>
<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>
<?php
/**
@ -16,3 +17,6 @@ $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: [
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->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();
?>
<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>
<?php
/**
@ -15,3 +16,6 @@ $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: [
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->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();
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -2,15 +2,17 @@
/**
* @var \itguild\forms\ActiveForm $form
* @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\TextArea;
use itguild\forms\app\controllers\AdminController;
?>
<link rel="stylesheet" href="../../public/style/main.css">
<?php echo $form->beginForm("/admin/save-form/"); ?>
<?php $form->field(TextInput::class, name: "title", params: [
'class' => "form-control",
@ -64,6 +66,15 @@ $form->field(\itguild\forms\inputs\Hidden::class, name: "formId", params: [
"value" => $dto->get('id')
])->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: [
"id" => 'buttonAdd',
"value" => 'Добавить поле',
@ -80,4 +91,5 @@ $form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
])
->render();
$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"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
<script src="/public/js/ajax.js"></script>
<link rel="stylesheet" href="/public/style/main.css">
</head>
<body>
<div class="container">