diff --git a/in1.php b/in1.php deleted file mode 100644 index e69de29..0000000 diff --git a/index.php b/index.php index 4f92388..f9b9458 100755 --- a/index.php +++ b/index.php @@ -31,8 +31,9 @@ $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, "saveForm"]); -$router->post("/admin/handler", [\itguild\forms\app\controllers\AdminController::class, "ajax"]); +$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"]); $dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); diff --git a/public/js/ajax.js b/public/js/ajax.js index 9c6449f..3a0b523 100644 --- a/public/js/ajax.js +++ b/public/js/ajax.js @@ -1,6 +1,6 @@ function ajaxRequest() { const request = new XMLHttpRequest(); - request.open("POST", "handler"); + request.open("GET", "/admin/form-add-fields"); request.addEventListener("load", () => { const responseText = request.responseText; const targetElement = document.getElementById("buttonAdd"); @@ -12,4 +12,22 @@ function ajaxRequest() { }); request.send(); -} \ No newline at end of file +} +function getValueSelect(){ + var selectValue = document.getElementById("selectID").value + var inputTags = document.getElementsByTagName('input'); + var inputCount = inputTags.length - 3; + var request = new XMLHttpRequest() + request.open("GET", "/admin/set-value-select?value=" + selectValue + "&count=" + inputCount, true) + request.addEventListener("load", () => { + var responseText = request.responseText; + var targetElement = document.getElementById("buttonAdd"); + var divElement = document.createElement("div"); + divElement.classList.add("form-control"); + divElement.style.backgroundColor = "#bbbbbb"; // Задайте здесь свой нужный стиль + divElement.innerHTML = responseText; + targetElement.insertAdjacentElement("beforebegin", divElement); + + }); + request.send() +} diff --git a/src/app/controllers/AdminController.php b/src/app/controllers/AdminController.php index b345096..31fd953 100644 --- a/src/app/controllers/AdminController.php +++ b/src/app/controllers/AdminController.php @@ -2,6 +2,7 @@ namespace itguild\forms\app\controllers; +use Illuminate\Database\Events\DatabaseBusy; use itguild\forms\ActiveForm; use itguild\forms\app\core\BaseController; use itguild\forms\app\models\FormModel; @@ -11,6 +12,8 @@ use itguild\forms\core\cg_view\CgView; * @var \itguild\forms\ActiveForm $form ; */ +use itguild\forms\debug\Debug; +use itguild\forms\exceptions\FieldTypeNotFound; use itguild\forms\Form; use itguild\forms\inputs\Select; use itguild\forms\inputs\TextInput; @@ -22,15 +25,11 @@ class AdminController extends BaseController public function createAction(): void { - $view = new CgView(); - - $view->viewPath = VIEW_PATH; - $view->layout = "/layouts/admin_layout.php"; - - $view->render("/admin/index.php", ['title' => "Создать форму", 'form' => new ActiveForm()]); + $this->cgView->layout = "/layouts/admin_layout.php"; + $this->cgView->render("/admin/index.php", ['title' => "Создать форму", 'form' => new ActiveForm()]); } - public function saveForm() + public function saveFormAction(): void { $title = $_POST["title"]; $params = $_POST["params"]; @@ -39,33 +38,33 @@ class AdminController extends BaseController echo "Форма сохранена!"; } } - public function ajax() + public function getFormAddFieldsAction(): void { + $this->cgView->render("/admin/_form_add_fields_ajax.php", ['form' => new ActiveForm()]); + } + public function getSelectValue(): void + { + $selectValue = $_GET["value"]; + $countInput = $_GET["count"]; + Debug::prn($countInput); $form = new ActiveForm(); - $form->field(Select::class, name: "selectItem", params: [ - 'class' => "form-control", "options" => ["textInput", "textArea", "checkBox", "button", "radioButton", "select", "label"] - ]) - ->setLabel("Выберите тип инпута") - ->render(); - $form->field(TextInput::class, name: "name", params: [ - 'class' => "form-control", - 'placeholder' => 'Name' - ]) - ->setLabel("Имя поля") - ->render(); - $form->field(TextInput::class, name: "value", params: [ - 'class' => "form-control", - 'placeholder' => 'Значение value' - ]) - ->setLabel("Значение") - ->render(); - $form->field(TextArea::class, name: "value", params: [ - 'class' => "form-control", - 'placeholder' => 'Параметры поля' - ]) - ->setLabel("Параметры") - ->render(); + $fields = [ + 1 => '_checkBox', + 2 => '_text_input', + 3 => '_textarea', + 4 => '_radioButton', + 5 => '_button', + 6 => '_select' + ]; + + $viewName = $fields[$selectValue] ?? false; + + if (!$viewName){ + throw new FieldTypeNotFound($selectValue); + } + + $this->cgView->render("/admin/field_type/$viewName.php", ['form' => new ActiveForm()]); } diff --git a/src/app/core/BaseController.php b/src/app/core/BaseController.php index 8833fd2..fbd4f89 100755 --- a/src/app/core/BaseController.php +++ b/src/app/core/BaseController.php @@ -2,9 +2,13 @@ namespace itguild\forms\app\core; +use itguild\forms\core\cg_view\CgView; + class BaseController { - protected $view; + protected \Twig\Environment $view; + + protected CgView $cgView; public function __construct() { @@ -12,6 +16,9 @@ class BaseController $this->view = new \Twig\Environment($loader, [ 'cache' => VIEW_CACHE_PATH, ]); + + $this->cgView = new CgView(); + $this->cgView->viewPath = VIEW_PATH; } } \ No newline at end of file diff --git a/src/exceptions/FieldTypeNotFound.php b/src/exceptions/FieldTypeNotFound.php new file mode 100644 index 0000000..7004684 --- /dev/null +++ b/src/exceptions/FieldTypeNotFound.php @@ -0,0 +1,13 @@ +message = "Тип поля $type не найден"; + } + +} \ No newline at end of file diff --git a/views/admin/_form_add_fields_ajax.php b/views/admin/_form_add_fields_ajax.php new file mode 100644 index 0000000..a2da56c --- /dev/null +++ b/views/admin/_form_add_fields_ajax.php @@ -0,0 +1,29 @@ +field(Select::class, name: "selectItem", params: [ + 'class' => "form-control", "options" => ["textInput", "textArea", "checkBox", "button", "radioButton", "select", "label"] +]) + ->setLabel("Выберите тип инпута") + ->render(); +$form->field(TextInput::class, name: "name", params: [ + 'class' => "form-control", + 'placeholder' => 'Name' +]) + ->setLabel("Имя поля") + ->render(); +$form->field(TextInput::class, name: "value", params: [ + 'class' => "form-control", + 'placeholder' => 'Значение value' +]) + ->setLabel("Значение") + ->render(); diff --git a/views/admin/field_type/_button.php b/views/admin/field_type/_button.php new file mode 100644 index 0000000..98d513f --- /dev/null +++ b/views/admin/field_type/_button.php @@ -0,0 +1,14 @@ +
Кнопка
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render(); diff --git a/views/admin/field_type/_checkBox.php b/views/admin/field_type/_checkBox.php new file mode 100644 index 0000000..9abd729 --- /dev/null +++ b/views/admin/field_type/_checkBox.php @@ -0,0 +1,14 @@ +Чекбокс
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render(); \ No newline at end of file diff --git a/views/admin/field_type/_radioButton.php b/views/admin/field_type/_radioButton.php new file mode 100644 index 0000000..40787de --- /dev/null +++ b/views/admin/field_type/_radioButton.php @@ -0,0 +1,14 @@ +Радио кнопка
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render(); \ No newline at end of file diff --git a/views/admin/field_type/_select.php b/views/admin/field_type/_select.php new file mode 100644 index 0000000..68c43fa --- /dev/null +++ b/views/admin/field_type/_select.php @@ -0,0 +1,22 @@ +Список
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование поля"]) + ->setLabel("Наименование поля (например \"Телефон\")") + ->render(); +$form->field(TextArea::class, name: "OptionsInput", params: ['class' => "form-control"]) + ->setLabel("Опции выпадающего списка") + ->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[]) + ->setLabel("Обязательное поле") + ->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 new file mode 100644 index 0000000..8f327c4 --- /dev/null +++ b/views/admin/field_type/_text_input.php @@ -0,0 +1,16 @@ +Текстовое поле
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render(); +$form->field(TextInput::class, name: "PlaceholderInput", params: ['class' => "form-control", + 'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное поле")->render(); \ No newline at end of file diff --git a/views/admin/field_type/_textarea.php b/views/admin/field_type/_textarea.php new file mode 100644 index 0000000..a72cd78 --- /dev/null +++ b/views/admin/field_type/_textarea.php @@ -0,0 +1,16 @@ +Описание
+field(TextInput::class, name: "NameInput", params: ['class' => "form-control", + 'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render(); +$form->field(TextInput::class, name: "PlaceholderInput", params: ['class' => "form-control", + 'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render(); +$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное поле")->render(); \ No newline at end of file diff --git a/views/admin/index.php b/views/admin/index.php index da3c34a..3f038df 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -6,6 +6,8 @@ use itguild\forms\inputs\TextInput; use itguild\forms\inputs\TextArea; +$types = \itguild\forms\app\models\InputTypeModel::where("status", 1)->pluck('name', 'id')->toArray(); + ?> beginForm("/admin/save-form"); ?> @@ -16,20 +18,28 @@ use itguild\forms\inputs\TextArea; ->setLabel("Название формы") ->render(); -$form->field(TextInput::class, name: "title", params: [ +$form->field(TextInput::class, name: "perPage", params: [ 'class' => "form-control", 'placeholder' => 'Perpage' ]) ->setLabel("Количество выводимых записей на страницу") ->render(); -$form->field(TextArea::class, name: "title", params: [ +$form->field(TextArea::class, name: "paramsForm", params: [ 'class' => "form-control", 'placeholder' => 'Параметры' ]) ->setLabel("Параметры") ->render(); +$form->field(\itguild\forms\inputs\Select::class, name: "inputType", params: [ + 'class' => "form-control-sm", + 'id' => "selectID", + 'placeholder' => 'Параметры', + 'options' => $types +]) + ->setLabel("Добавить поле") + ->render(); ?> - +