This commit is contained in:
kali 2024-04-17 17:30:00 +03:00
parent 05cbb7ef43
commit 13dbef5b4f
14 changed files with 212 additions and 39 deletions

View File

View File

@ -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));

View File

@ -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");
@ -13,3 +13,21 @@ function ajaxRequest() {
});
request.send();
}
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()
}

View File

@ -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()]);
}

View File

@ -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;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace itguild\forms\exceptions;
class FieldTypeNotFound extends \Exception
{
function __construct(string $type)
{
$this->message = "Тип поля $type не найден";
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
*
* @var ActiveForm $form
*
*/
use itguild\forms\ActiveForm;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
$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();

View File

@ -0,0 +1,14 @@
<b><p>Кнопка</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "NameInput", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render();

View File

@ -0,0 +1,14 @@
<b><p>Чекбокс</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "NameInput", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render();

View File

@ -0,0 +1,14 @@
<b><p>Радио кнопка</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "NameInput", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(Checkbox::class, name: "RequiredInput", params:[])->setLabel("Обязательное")->render();

View File

@ -0,0 +1,22 @@
<b><p>Список</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
$form->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();

View File

@ -0,0 +1,16 @@
<b><p>Текстовое поле</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->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();

View File

@ -0,0 +1,16 @@
<b><p>Описание</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
*
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->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();

View File

@ -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();
?>
<?php echo $form->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();
?>
<button style="margin-top:15px" type="button" id="buttonAdd" class="btn btn-primary form-control" onclick="ajaxRequest()">Добавить поле</button>
<button style="margin-top:15px" type="button" id="buttonAdd" class="btn btn-success " onclick="getValueSelect()">Добавить поле</button>