From eb361ff002f188b00699604a2c9e204abbcd399f Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 21 May 2024 15:53:19 +0300 Subject: [PATCH] bd 12.0 --- public/js/ajax.js | 36 ++++--------------------- public/style/main.css | 3 ++- src/app/controllers/AdminController.php | 26 ++++++++---------- src/app/models/InputTypeModel.php | 14 ++++++++++ src/core/cg_view/CgView.php | 3 +++ views/admin/field_type/_button.php | 5 ++++ views/admin/field_type/_checkBox.php | 6 ++++- views/admin/field_type/_radioButton.php | 6 ++++- views/admin/field_type/_select.php | 6 ++++- views/admin/field_type/_text_input.php | 6 ++++- views/admin/field_type/_textarea.php | 6 ++++- views/admin/index.php | 16 +++++++++-- views/layouts/admin_layout.php | 1 + 13 files changed, 80 insertions(+), 54 deletions(-) diff --git a/public/js/ajax.js b/public/js/ajax.js index 196672c..155fe07 100644 --- a/public/js/ajax.js +++ b/public/js/ajax.js @@ -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(); }); diff --git a/public/style/main.css b/public/style/main.css index f7da086..823302a 100644 --- a/public/style/main.css +++ b/public/style/main.css @@ -2,8 +2,9 @@ position: absolute; top: 5px; right: 5px; + } -.form-new-block { +.form-block { background-color: #bbbbbb; position: relative; diff --git a/src/app/controllers/AdminController.php b/src/app/controllers/AdminController.php index 4763790..dfd9952 100644 --- a/src/app/controllers/AdminController.php +++ b/src/app/controllers/AdminController.php @@ -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, ]); } diff --git a/src/app/models/InputTypeModel.php b/src/app/models/InputTypeModel.php index b08c786..b27a983 100644 --- a/src/app/models/InputTypeModel.php +++ b/src/app/models/InputTypeModel.php @@ -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; + } } \ No newline at end of file diff --git a/src/core/cg_view/CgView.php b/src/core/cg_view/CgView.php index 78f0282..ef24fc1 100644 --- a/src/core/cg_view/CgView.php +++ b/src/core/cg_view/CgView.php @@ -32,6 +32,9 @@ class CgView { ob_start(); + $cgView = new self(); + $cgView->viewPath = $this->viewPath; + foreach ($data as $key => $datum){ ${"$key"} = $datum; } diff --git a/views/admin/field_type/_button.php b/views/admin/field_type/_button.php index 67d86a1..93b1c5f 100644 --- a/views/admin/field_type/_button.php +++ b/views/admin/field_type/_button.php @@ -1,3 +1,4 @@ +

Кнопка

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(); +?> + +
+ diff --git a/views/admin/field_type/_checkBox.php b/views/admin/field_type/_checkBox.php index 51d4f8c..458757f 100644 --- a/views/admin/field_type/_checkBox.php +++ b/views/admin/field_type/_checkBox.php @@ -1,3 +1,4 @@ +

Чекбокс

field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control", '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(); \ No newline at end of file +$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '1'])->render(); +?> + +
diff --git a/views/admin/field_type/_radioButton.php b/views/admin/field_type/_radioButton.php index d0007bd..8ef00a3 100644 --- a/views/admin/field_type/_radioButton.php +++ b/views/admin/field_type/_radioButton.php @@ -1,3 +1,4 @@ +

Радио кнопка

field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][rad ->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(); \ No newline at end of file +$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '4'])->render(); +?> + +
\ No newline at end of file diff --git a/views/admin/field_type/_select.php b/views/admin/field_type/_select.php index 9ee4533..f5ed08c 100644 --- a/views/admin/field_type/_select.php +++ b/views/admin/field_type/_select.php @@ -1,3 +1,4 @@ +

Список

field(TextArea::class, name: "InputForm[$count][options]", params: ['clas ->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(); \ No newline at end of file +$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '6'])->render(); +?> + +
\ No newline at end of file diff --git a/views/admin/field_type/_text_input.php b/views/admin/field_type/_text_input.php index 4895371..143ed74 100644 --- a/views/admin/field_type/_text_input.php +++ b/views/admin/field_type/_text_input.php @@ -1,3 +1,4 @@ +

Текстовое поле

field(TextInput::class, name: "InputForm[$count][name]", params: ['class' $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control", '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(); \ No newline at end of file +$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '2'])->render(); +?> + +
\ No newline at end of file diff --git a/views/admin/field_type/_textarea.php b/views/admin/field_type/_textarea.php index bd21cc6..1543870 100644 --- a/views/admin/field_type/_textarea.php +++ b/views/admin/field_type/_textarea.php @@ -1,3 +1,4 @@ +

Описание

field(TextInput::class, name: "InputForm[$count][name]", params: ['class' $form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control", '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(); \ No newline at end of file +$form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '3'])->render(); +?> + +
\ No newline at end of file diff --git a/views/admin/index.php b/views/admin/index.php index a41190a..7f715b1 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -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; - ?> - + + beginForm("/admin/save-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(); + ?> diff --git a/views/layouts/admin_layout.php b/views/layouts/admin_layout.php index fed82de..bcb7de9 100644 --- a/views/layouts/admin_layout.php +++ b/views/layouts/admin_layout.php @@ -13,6 +13,7 @@ +