nativegallery/views/pages/Admin/EntityCreate.php

119 lines
5.3 KiB
PHP
Raw Normal View History

2024-09-14 23:12:21 +03:00
<?php
2024-09-15 21:12:45 +03:00
if (isset($_POST['create'])) {
$postData = $_POST;
2024-09-14 23:12:21 +03:00
2024-09-15 21:12:45 +03:00
$result = [];
foreach ($postData as $key => $value) {
if (strpos($key, 'variable') === 0) {
preg_match('/_(\d+)$/', $key, $matches);
if (isset($matches[1])) {
$index = $matches[1];
if (!isset($result[$index])) {
$result[$index] = [];
}
$newKey = preg_replace('/^variable/', '', $key);
$newKey = preg_replace('/_\d+$/', '', $newKey);
$result[$index][$newKey] = $value;
}
}
}
$jsonResult = json_encode($result, JSON_PRETTY_PRINT);
header('Content-Type: application/json');
echo $jsonResult;
}
2024-09-14 23:12:21 +03:00
?>
<h1><b>Создание сущности</b></h1>
2024-09-15 21:12:45 +03:00
<form action="/admin?type=EntityCreate" method="post" name="form" id="form" enctype="multipart/form-data" style="display:inline-block; min-width:500px;">
2024-09-14 23:12:21 +03:00
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Название</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Вагон метро">
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Цвет</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="#FFFFFF">
</div>
<p>Вводимые переменные</p>
<div class="alert alert-dark" role="alert">
Добавляйте и регулируйте поля ввода, которые будут являться шаблонной формой для создания моделей к сущности.
</div>
2024-09-14 23:55:42 +03:00
<div class="row" id="entityform">
2024-09-14 23:12:21 +03:00
<div class="col-md-3">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Название переменной</label>
2024-09-14 23:55:42 +03:00
<input name="variablename_1" type="text" class="form-control" id="exampleFormControlInput1" placeholder="#FFFFFF">
2024-09-14 23:12:21 +03:00
</div>
</div>
<div class="col-md-3">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">ID</label>
2024-09-14 23:55:42 +03:00
<input name="variableid_1" type="text" class="form-control" id="exampleFormControlInput1" placeholder="blablabla">
2024-09-14 23:12:21 +03:00
</div>
</div>
<div class="col-md-3">
<label for="exampleFormControlInput1" class="form-label">Тип</label>
2024-09-14 23:55:42 +03:00
<select name="variabletype_1" class="form-select" aria-label="Default select example">
2024-09-14 23:12:21 +03:00
<option value="1">Строка</option>
<option value="2">Число</option>
</select>
</div>
<div class="col-md-3">
<label for="exampleFormControlInput1" class="form-label">Обязателен?</label>
2024-09-14 23:55:42 +03:00
<select name="variableimportant_1" class="form-select" aria-label="Default select example">
2024-09-14 23:12:21 +03:00
<option value="1">Да</option>
<option value="2">Нет</option>
</select>
</div>
</div>
2024-09-14 23:55:42 +03:00
<button id="addButton" type="button" class="btn btn-outline-primary">Добавить ещё</button>
2024-09-15 21:12:45 +03:00
<button id="addButton" type="submit" name="create" class="btn btn-primary">Создать сущность</button>
2024-09-14 23:12:21 +03:00
</div>
2024-09-15 21:12:45 +03:00
2024-09-14 23:55:42 +03:00
</form>
<script>
let count = 1; // Начальное значение для номера переменной
document.getElementById('addButton').addEventListener('click', function() {
count++; // Увеличиваем номер переменной
// Создаем новый элемент
const newElement =
`<div class="col-md-3">
<div class="mb-3">
<label for="exampleFormControlInput${count}" class="form-label">Название переменной</label>
<input name="variablename_${count}" type="text" class="form-control" id="exampleFormControlInput${count}" placeholder="#FFFFFF">
</div>
</div>
<div class="col-md-3">
<div class="mb-3">
<label for="exampleFormControlInput${count}" class="form-label">ID</label>
<input name="variableid_${count}" type="text" class="form-control" id="exampleFormControlInput${count}" placeholder="blablabla">
</div>
</div>
<div class="col-md-3">
<label for="exampleFormControlInput${count}" class="form-label">Тип</label>
<select name="variabletype_${count}" class="form-select" aria-label="Default select example">
<option value="1">Строка</option>
<option value="2">Число</option>
</select>
</div>
<div class="col-md-3">
<label for="exampleFormControlInput${count}" class="form-label">Обязателен?</label>
<select name="variableimportant_${count}" class="form-select" aria-label="Default select example">
<option value="1">Да</option>
<option value="2">Нет</option>
</select>
</div>`
;
// Добавляем новый элемент в #entityform
document.getElementById('entityform').insertAdjacentHTML('beforeend', newElement);
});
</script>