diff --git a/index.php b/index.php index 28ab764..14bd952 100755 --- a/index.php +++ b/index.php @@ -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-edit/{id}", [\itguild\forms\app\controllers\FormController::class, "editAction"]); $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/set-value-select", [\itguild\forms\app\controllers\AdminController::class, "getSelectValue"]); $router->get("/admin/edit-form/{id}", [\itguild\forms\app\controllers\AdminController::class, "editFormAction"]); diff --git a/public/js/ajax.js b/public/js/ajax.js index 035730a..196672c 100644 --- a/public/js/ajax.js +++ b/public/js/ajax.js @@ -6,13 +6,20 @@ function ajaxRequest() { const targetElement = document.getElementById("buttonAdd"); const divElement = document.createElement("div"); divElement.classList.add("form-control"); - divElement.style.backgroundColor = "#bbbbbb"; // Задайте здесь свой нужный стиль + 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(); @@ -25,11 +32,20 @@ function getValueSelect(){ var responseText = request.responseText; var targetElement = document.getElementById("buttonAdd"); var divElement = document.createElement("div"); - divElement.classList.add("form-control"); - divElement.style.backgroundColor = "#bbbbbb"; // Задайте здесь свой нужный стиль + divElement.classList.add("form-control form-new-block"); 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(); + }); + divElement.appendChild(deleteButton); }); request.send() } +function getIdBlock(){ + +} diff --git a/public/style/main.css b/public/style/main.css new file mode 100644 index 0000000..f7da086 --- /dev/null +++ b/public/style/main.css @@ -0,0 +1,10 @@ +.delete-button { + position: absolute; + top: 5px; + right: 5px; +} +.form-new-block { + background-color: #bbbbbb; + position: relative; + +} \ No newline at end of file diff --git a/src/app/DTO/FormDTO.php b/src/app/DTO/FormDTO.php index fb89d99..7504ce2 100644 --- a/src/app/DTO/FormDTO.php +++ b/src/app/DTO/FormDTO.php @@ -9,12 +9,14 @@ class FormDTO extends CGDTO { public string $title; public $perPage; + + public $id; public string $view; public string $edit; public string $delete; protected array $fillable = [ - 'title', 'perPage', 'view', 'edit', 'delete' + 'title', 'id', 'perPage', 'view', 'edit', 'delete' ]; diff --git a/src/app/controllers/AdminController.php b/src/app/controllers/AdminController.php index b7882da..4763790 100644 --- a/src/app/controllers/AdminController.php +++ b/src/app/controllers/AdminController.php @@ -52,14 +52,13 @@ class AdminController extends BaseController { $request = new CreateFormRequest(); if (!$request->validate()) { - throw new CreateFormException("Validation error"); - } $title = $request->post('title'); $perPage = $request->post('perPage', 10); $inputs = $request->post('InputForm', []); + $formId = $request->post('formId'); $actionView = $request->post("actionView"); $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']]; - - $form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]); + if ($formId) { + //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) { $name = TranslitorHelper::translit($input['name']); $params = [ "name" => $name, "class" => "form-control", + 'placeholder' => $input['placeholder'] ]; 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 { - $formInput = FormInputModel::Create([ 'form_id' => $form->id, 'input_type_id' => $input['type'], - 'label' => $name, + 'label' => $input['name'], 'name' => $name, '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); $fillableRes = []; $fillableRes['title'] = $form['title']; - foreach ($formArr['meta'] as $key => $value){ - if($key == 'perpage'){ + $fillableRes['id'] = $form['id']; + foreach ($formArr['meta'] as $key => $value) { + if ($key == 'perpage') { $fillableRes['perPage'] = (string)$value; - } - else if($key == "actions"){ - foreach ($value as $item){ + } else if ($key == "actions") { + foreach ($value as $item) { $fillableRes[$item] = $item; } } @@ -184,5 +190,4 @@ class AdminController extends BaseController } - } \ No newline at end of file diff --git a/src/app/controllers/FormController.php b/src/app/controllers/FormController.php index 89298b2..395229f 100644 --- a/src/app/controllers/FormController.php +++ b/src/app/controllers/FormController.php @@ -7,6 +7,7 @@ use itguild\forms\app\models\FormModel; use itguild\forms\app\models\FormResModel; use itguild\forms\app\models\InputTypeModel; use itguild\forms\app\models\InputValueModel; +use itguild\forms\builders\LabelBuilder; use itguild\forms\debug\Debug; use itguild\forms\exceptions\FormNotFoundException; use itguild\forms\exceptions\FormResultNotFoundException; @@ -297,8 +298,7 @@ class FormController extends BaseController // Обработка полей формы if ($form->fields) { - $fields = $form->fields; - foreach ($fields as $field) { + foreach ($form->fields as $field) { $options = []; $fieldArr = []; $params = []; @@ -329,6 +329,7 @@ class FormController extends BaseController } $fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput"; $fieldArr['options'] = $options; + $fieldArr['label'] = ['title' => $field->label, 'for' => $fieldArr['name'], 'class' => 'form-label']; $formArr['data'][] = $fieldArr; } } @@ -336,5 +337,4 @@ class FormController extends BaseController return $formArr; } - } \ No newline at end of file diff --git a/src/app/core/CGDTO.php b/src/app/core/CGDTO.php index bfc17e3..2894c97 100644 --- a/src/app/core/CGDTO.php +++ b/src/app/core/CGDTO.php @@ -22,12 +22,16 @@ class CGDTO public function get(string $name) { - if (!empty($this->$name)) - { + if (!empty($this->$name)) { return $this->$name; } return null; } + public function set(string $name, string $value): void + { + $this->$name = $value; + } + } \ No newline at end of file diff --git a/src/app/requests/CreateFormRequest.php b/src/app/requests/CreateFormRequest.php index 597e982..a78b53b 100644 --- a/src/app/requests/CreateFormRequest.php +++ b/src/app/requests/CreateFormRequest.php @@ -10,6 +10,7 @@ class CreateFormRequest extends CgRequest public function rules(): array { return [ + 'formId' => 'numeric', 'title' => 'required|min:5', 'perPage' => 'numeric', 'InputForm' => 'required|array', diff --git a/src/helpers/TranslitorHelper.php b/src/helpers/TranslitorHelper.php index dc495ad..8190e71 100644 --- a/src/helpers/TranslitorHelper.php +++ b/src/helpers/TranslitorHelper.php @@ -10,13 +10,13 @@ class TranslitorHelper 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', - 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я'); + 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', ' '); $translit = array('A', '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', 'Y', 'Y', 'Y', 'E', 'Yu', 'Ya', 'a', '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', - 'y', 'y', 'y', 'e', 'yu', 'ya'); + 'y', 'y', 'y', 'e', 'yu', 'ya', '_'); return str_replace($russian, $translit, $str); } } \ No newline at end of file diff --git a/src/inputs/Label.php b/src/inputs/Label.php index 5b197fc..1702ac6 100755 --- a/src/inputs/Label.php +++ b/src/inputs/Label.php @@ -4,6 +4,7 @@ namespace itguild\forms\inputs; use itguild\forms\traits\CreateParams; use itguild\forms\inputs\BaseInput; + class Label extends BaseInput { use CreateParams; @@ -36,6 +37,14 @@ class Label extends BaseInput } + /** + * @return string + */ + public function getHtml(): string + { + return $this->html; + } + /** * @param string $title * @param array $paramsArray @@ -45,9 +54,6 @@ class Label extends BaseInput { $label = new self($title, $paramsArray); $label->create()->render(); - } - - } \ No newline at end of file diff --git a/views/admin/index.php b/views/admin/index.php index f5a3add..a41190a 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -6,11 +6,12 @@ use itguild\forms\inputs\TextInput; use itguild\forms\inputs\TextArea; +use itguild\forms\app\controllers\AdminController; ?> - -beginForm("/admin/save-form"); ?> + +beginForm("/admin/save-form/"); ?> field(TextInput::class, name: "title", params: [ 'class' => "form-control", 'placeholder' => 'Название формы', @@ -58,13 +59,20 @@ $form->field(\itguild\forms\inputs\Select::class, name: "inputType", params: [ ]) ->setLabel("Добавить поле") ->render(); -?> - +$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(); -field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [ 'class' => "btn btn-primary ", 'value' => 'Отправить',