This commit is contained in:
kali 2024-05-03 18:11:43 +03:00
parent 143fa9f324
commit 8e13f4a425
11 changed files with 93 additions and 41 deletions

View File

@ -31,7 +31,7 @@ $router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::
$router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteAction"]); $router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteAction"]);
$router->get("/form-edit/{id}", [\itguild\forms\app\controllers\FormController::class, "editAction"]); $router->get("/form-edit/{id}", [\itguild\forms\app\controllers\FormController::class, "editAction"]);
$router->get("/admin/create-form", [\itguild\forms\app\controllers\AdminController::class, "createAction"]); $router->get("/admin/create-form", [\itguild\forms\app\controllers\AdminController::class, "createAction"]);
$router->post("/admin/save-form", [\itguild\forms\app\controllers\AdminController::class, "saveFormAction"]); $router->post("/admin/save-form/", [\itguild\forms\app\controllers\AdminController::class, "saveFormAction"]);
$router->get("/admin/form-add-fields", [\itguild\forms\app\controllers\AdminController::class, "getFormAddFieldsAction"]); $router->get("/admin/form-add-fields", [\itguild\forms\app\controllers\AdminController::class, "getFormAddFieldsAction"]);
$router->get("/admin/set-value-select", [\itguild\forms\app\controllers\AdminController::class, "getSelectValue"]); $router->get("/admin/set-value-select", [\itguild\forms\app\controllers\AdminController::class, "getSelectValue"]);
$router->get("/admin/edit-form/{id}", [\itguild\forms\app\controllers\AdminController::class, "editFormAction"]); $router->get("/admin/edit-form/{id}", [\itguild\forms\app\controllers\AdminController::class, "editFormAction"]);

View File

@ -6,13 +6,20 @@ function ajaxRequest() {
const targetElement = document.getElementById("buttonAdd"); const targetElement = document.getElementById("buttonAdd");
const divElement = document.createElement("div"); const divElement = document.createElement("div");
divElement.classList.add("form-control"); divElement.classList.add("form-control");
divElement.style.backgroundColor = "#bbbbbb"; // Задайте здесь свой нужный стиль divElement.style.backgroundColor = "#bbbbbb";
divElement.innerHTML = responseText; divElement.innerHTML = responseText;
targetElement.insertAdjacentElement("beforebegin", divElement); targetElement.insertAdjacentElement("beforebegin", divElement);
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", () => {
divElement.remove();
});
divElement.appendChild(deleteButton);
}); });
request.send(); request.send();
} }
function getValueSelect(){ function getValueSelect(){
var selectValue = document.getElementById("selectID").value var selectValue = document.getElementById("selectID").value
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
@ -25,11 +32,20 @@ function getValueSelect(){
var responseText = request.responseText; var responseText = request.responseText;
var targetElement = document.getElementById("buttonAdd"); var targetElement = document.getElementById("buttonAdd");
var divElement = document.createElement("div"); var divElement = document.createElement("div");
divElement.classList.add("form-control"); divElement.classList.add("form-control form-new-block");
divElement.style.backgroundColor = "#bbbbbb"; // Задайте здесь свой нужный стиль
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", () => {
divElement.remove();
});
divElement.appendChild(deleteButton);
}); });
request.send() request.send()
} }
function getIdBlock(){
}

10
public/style/main.css Normal file
View File

@ -0,0 +1,10 @@
.delete-button {
position: absolute;
top: 5px;
right: 5px;
}
.form-new-block {
background-color: #bbbbbb;
position: relative;
}

View File

@ -9,12 +9,14 @@ class FormDTO extends CGDTO
{ {
public string $title; public string $title;
public $perPage; public $perPage;
public $id;
public string $view; public string $view;
public string $edit; public string $edit;
public string $delete; public string $delete;
protected array $fillable = [ protected array $fillable = [
'title', 'perPage', 'view', 'edit', 'delete' 'title', 'id', 'perPage', 'view', 'edit', 'delete'
]; ];

View File

@ -52,14 +52,13 @@ class AdminController extends BaseController
{ {
$request = new CreateFormRequest(); $request = new CreateFormRequest();
if (!$request->validate()) { if (!$request->validate()) {
throw new CreateFormException("Validation error"); throw new CreateFormException("Validation error");
} }
$title = $request->post('title'); $title = $request->post('title');
$perPage = $request->post('perPage', 10); $perPage = $request->post('perPage', 10);
$inputs = $request->post('InputForm', []); $inputs = $request->post('InputForm', []);
$formId = $request->post('formId');
$actionView = $request->post("actionView"); $actionView = $request->post("actionView");
$actionEdit = $request->post("actionEdit"); $actionEdit = $request->post("actionEdit");
@ -68,14 +67,23 @@ class AdminController extends BaseController
$resultArray = ['meta' => ['perpage' => $perPage, 'status' => "1", 'actions' => $actions], 'params' => ['class' => "form-control", 'id' => 'form']]; $resultArray = ['meta' => ['perpage' => $perPage, 'status' => "1", 'actions' => $actions], 'params' => ['class' => "form-control", 'id' => 'form']];
if ($formId) {
$form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]); //FormModel::where('id', $formId)->update(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]);
$form = FormModel::find($formId);
$form->title = $title;
$form->params = json_encode($resultArray, true);
$form->save();
FormInputModel::where('form_id', $form->id)->delete();
} else {
$form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]);
}
foreach ($inputs as $input) { foreach ($inputs as $input) {
$name = TranslitorHelper::translit($input['name']); $name = TranslitorHelper::translit($input['name']);
$params = [ $params = [
"name" => $name, "name" => $name,
"class" => "form-control", "class" => "form-control",
'placeholder' => $input['placeholder']
]; ];
if ($input['type'] == InputTypeModel::RADIO_TYPE) { if ($input['type'] == InputTypeModel::RADIO_TYPE) {
@ -98,27 +106,25 @@ class AdminController extends BaseController
} }
} elseif ($input['type'] == InputTypeModel::SELECT_TYPE) {
$options = explode("\n", $input['options']);
foreach ($options as $option) {
InputValueModel::Create(['form_input_id' => $formInput->id, 'value' => $option]);
}
} else { } else {
$formInput = FormInputModel::Create([ $formInput = FormInputModel::Create([
'form_id' => $form->id, 'form_id' => $form->id,
'input_type_id' => $input['type'], 'input_type_id' => $input['type'],
'label' => $name, 'label' => $input['name'],
'name' => $name, 'name' => $name,
'params' => json_encode($params) 'params' => json_encode($params)
]); ]);
} }
if ($input['type'] == InputTypeModel::SELECT_TYPE) {
$options = explode("\n", $input['options']);
foreach ($options as $option) {
InputValueModel::Create(['form_input_id' => $formInput->id, 'value' => $option]);
}
}
} }
$this->redirect("/form-result/$form->id"); $this->redirect("/form-result/" . $form->id);
} }
@ -159,12 +165,12 @@ class AdminController extends BaseController
$formArr = json_decode($form['params'], true); $formArr = json_decode($form['params'], true);
$fillableRes = []; $fillableRes = [];
$fillableRes['title'] = $form['title']; $fillableRes['title'] = $form['title'];
foreach ($formArr['meta'] as $key => $value){ $fillableRes['id'] = $form['id'];
if($key == 'perpage'){ foreach ($formArr['meta'] as $key => $value) {
if ($key == 'perpage') {
$fillableRes['perPage'] = (string)$value; $fillableRes['perPage'] = (string)$value;
} } else if ($key == "actions") {
else if($key == "actions"){ foreach ($value as $item) {
foreach ($value as $item){
$fillableRes[$item] = $item; $fillableRes[$item] = $item;
} }
} }
@ -184,5 +190,4 @@ class AdminController extends BaseController
} }
} }

View File

@ -7,6 +7,7 @@ use itguild\forms\app\models\FormModel;
use itguild\forms\app\models\FormResModel; use itguild\forms\app\models\FormResModel;
use itguild\forms\app\models\InputTypeModel; use itguild\forms\app\models\InputTypeModel;
use itguild\forms\app\models\InputValueModel; use itguild\forms\app\models\InputValueModel;
use itguild\forms\builders\LabelBuilder;
use itguild\forms\debug\Debug; use itguild\forms\debug\Debug;
use itguild\forms\exceptions\FormNotFoundException; use itguild\forms\exceptions\FormNotFoundException;
use itguild\forms\exceptions\FormResultNotFoundException; use itguild\forms\exceptions\FormResultNotFoundException;
@ -297,8 +298,7 @@ class FormController extends BaseController
// Обработка полей формы // Обработка полей формы
if ($form->fields) { if ($form->fields) {
$fields = $form->fields; foreach ($form->fields as $field) {
foreach ($fields as $field) {
$options = []; $options = [];
$fieldArr = []; $fieldArr = [];
$params = []; $params = [];
@ -329,6 +329,7 @@ class FormController extends BaseController
} }
$fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput"; $fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput";
$fieldArr['options'] = $options; $fieldArr['options'] = $options;
$fieldArr['label'] = ['title' => $field->label, 'for' => $fieldArr['name'], 'class' => 'form-label'];
$formArr['data'][] = $fieldArr; $formArr['data'][] = $fieldArr;
} }
} }
@ -336,5 +337,4 @@ class FormController extends BaseController
return $formArr; return $formArr;
} }
} }

View File

@ -22,12 +22,16 @@ class CGDTO
public function get(string $name) public function get(string $name)
{ {
if (!empty($this->$name)) if (!empty($this->$name)) {
{
return $this->$name; return $this->$name;
} }
return null; return null;
} }
public function set(string $name, string $value): void
{
$this->$name = $value;
}
} }

View File

@ -10,6 +10,7 @@ class CreateFormRequest extends CgRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'formId' => 'numeric',
'title' => 'required|min:5', 'title' => 'required|min:5',
'perPage' => 'numeric', 'perPage' => 'numeric',
'InputForm' => 'required|array', 'InputForm' => 'required|array',

View File

@ -10,13 +10,13 @@ class TranslitorHelper
'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я',
'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц',
'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я'); 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', ' ');
$translit = array('A', 'B', 'V', 'G', 'D', 'E', 'E', 'Gh', 'Z', $translit = array('A', 'B', 'V', 'G', 'D', 'E', 'E', 'Gh', 'Z',
'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F',
'H', 'C', 'Ch', 'Sh', 'Sch', 'Y', 'Y', 'Y', 'E', 'Yu', 'Ya', 'a', 'H', 'C', 'Ch', 'Sh', 'Sch', 'Y', 'Y', 'Y', 'E', 'Yu', 'Ya', 'a',
'b', 'v', 'g', 'd', 'e', 'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm', 'b', 'v', 'g', 'd', 'e', 'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm',
'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch',
'y', 'y', 'y', 'e', 'yu', 'ya'); 'y', 'y', 'y', 'e', 'yu', 'ya', '_');
return str_replace($russian, $translit, $str); return str_replace($russian, $translit, $str);
} }
} }

View File

@ -4,6 +4,7 @@ namespace itguild\forms\inputs;
use itguild\forms\traits\CreateParams; use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput; use itguild\forms\inputs\BaseInput;
class Label extends BaseInput class Label extends BaseInput
{ {
use CreateParams; use CreateParams;
@ -36,6 +37,14 @@ class Label extends BaseInput
} }
/**
* @return string
*/
public function getHtml(): string
{
return $this->html;
}
/** /**
* @param string $title * @param string $title
* @param array $paramsArray * @param array $paramsArray
@ -45,9 +54,6 @@ class Label extends BaseInput
{ {
$label = new self($title, $paramsArray); $label = new self($title, $paramsArray);
$label->create()->render(); $label->create()->render();
} }
} }

View File

@ -6,11 +6,12 @@
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;
?> ?>
<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",
'placeholder' => 'Название формы', 'placeholder' => 'Название формы',
@ -58,13 +59,20 @@ $form->field(\itguild\forms\inputs\Select::class, name: "inputType", params: [
]) ])
->setLabel("Добавить поле") ->setLabel("Добавить поле")
->render(); ->render();
?>
<button style="margin-top:15px" type="button" id="buttonAdd" class="btn btn-success " onclick="getValueSelect()"
data-count=0>Добавить поле
</button>
$form->field(\itguild\forms\inputs\Hidden::class, name: "formId", params: [
"value" => $dto->get('id')
])->render();
$form->field(\itguild\forms\inputs\Button::class, name: "", params: [
"id" => 'buttonAdd',
"value" => 'Добавить поле',
'style' => "margin-top: 10px",
"class" => "btn btn-success",
"onclick" => "getValueSelect()",
"data-count" => "0"
])->render();
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [ $form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ", 'class' => "btn btn-primary ",
'value' => 'Отправить', 'value' => 'Отправить',