This commit is contained in:
kali 2024-05-24 18:03:12 +03:00
parent eb361ff002
commit 7c951565bd
15 changed files with 198 additions and 39 deletions

View File

@ -13,6 +13,7 @@ function getValueSelect(){
let divElement = document.createElement("div");
divElement.innerHTML = responseText;
targetElement.insertAdjacentElement("beforebegin", divElement);
let deleteButton = document.getElementById("deleteButton");
deleteButton.addEventListener("click", () => {
divElement.remove();
});
@ -20,6 +21,16 @@ function getValueSelect(){
});
request.send()
}
function getIdBlock(){
}
document.addEventListener("DOMContentLoaded", function() {
let divElement = document.getElementsByClassName("form-block");
let deleteButton = document.getElementsByClassName("deleteButton");
console.log(deleteButton);
for(let i = 0; i < deleteButton.length; i++){
deleteButton[i].addEventListener("click", () => {
deleteButton[i].parentElement.remove();
});
}
});

View File

@ -2,7 +2,11 @@
position: absolute;
top: 5px;
right: 5px;
text-decoration: none;
width: 20px;
height: 20px;
text-align: center;
vertical-align: baseline;
}
.form-block {
background-color: #bbbbbb;

View File

@ -83,7 +83,7 @@ class AdminController extends BaseController
$params = [
"name" => $name,
"class" => "form-control",
'placeholder' => $input['placeholder']
'placeholder' => $input['placeholder'] ?? null
];
if ($input['type'] == InputTypeModel::RADIO_TYPE) {
@ -108,6 +108,17 @@ class AdminController extends BaseController
} elseif ($input['type'] == InputTypeModel::SELECT_TYPE) {
$options = explode("\n", $input['options']);
$paramsRadio = [
"name" => $name,
'label' => ['title' => $input["name"], 'for' => $name, "class" => 'form-label']
];
$formInput = FormInputModel::Create([
'form_id' => $form->id,
'input_type_id' => $input['type'],
'label' => $input["name"],
'name' => $name,
'params' => json_encode($paramsRadio)
]);
foreach ($options as $option) {
InputValueModel::Create(['form_input_id' => $formInput->id, 'value' => $option]);
}

View File

@ -0,0 +1,14 @@
<?php
namespace itguild\forms\app\services;
class InputValueService
{
/**
* @return mixed
*/
public static function getInputValuesById(int $inputId): array
{
return \itguild\forms\app\models\InputValueModel::where("form_input_id", $inputId)->pluck('value', 'id')->toArray();
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace itguild\forms\widgets;
use itguild\forms\core\cg_view\CgView;
abstract class BaseWidget
{
protected CgView $cgView;
public function __construct()
{
$this->cgView = new CgView();
$this->cgView->viewPath = VIEW_PATH;
}
/**
* @return self
*/
public static function create(): BaseWidget
{
return new static();
}
/**
* @param array $data
* @return mixed
*/
abstract public function run(array $data = []);
}

View File

@ -0,0 +1,63 @@
<?php
/**
* @var \itguild\forms\ActiveForm $form
* @var \itguild\forms\app\DTO\FormDTO $dto
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
namespace itguild\forms\widgets;
use Illuminate\Database\Eloquent\Collection;
use itguild\forms\app\models\InputTypeModel;
use itguild\forms\ActiveForm;
use itguild\forms\app\services\InputValueService;
use itguild\forms\debug\Debug;
class FormFieldBlockWidget extends BaseWidget
{
public function run(array $data = []): void
{
foreach ($this->prepare($data['fields']) as $field) {
$this->cgView->render('/admin/field_type/' . $field['view_name'] . ".php", $field);
}
}
private function prepare(Collection $rawFields): array
{
$fields = [];
$i = 0;
foreach ($rawFields as $field) {
$params = json_decode($field['params'], true);
$viewName = \itguild\forms\app\models\InputTypeModel::getViewNameByTypeId($field['input_type_id']);
if (!isset($fields[$field['name']])) {
$fields[$field['name']] = [
'name' => $field['name'],
'form' => new ActiveForm(),
'view_name' => $viewName,
'count' => $i,
];
}
if ($field['input_type_id'] == InputTypeModel::RADIO_TYPE) {
if (isset($fields[$field['name']]['value'])) {
$fields[$field['name']]['value'] .= "\n" . $params['value'];
} else {
$fields[$field['name']]['value'] = $params['value'];
}
} elseif ($field['input_type_id'] == InputTypeModel::SELECT_TYPE) {
$fields[$field['name']]['value'] = implode("\n", InputValueService::getInputValuesById($field['id']));
} else {
$fields[$field['name']]['value'] = $params['value'] ?? null;
$fields[$field['name']]['placeholder'] = $params['placeholder'] ?? null;
}
$i++;
}
return $fields;
}
}

View File

@ -0,0 +1 @@
<a href="#" class="delete-button btn-danger deleteButton">X</a>

View File

@ -5,7 +5,8 @@
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
*
* @var integer $name
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
use itguild\forms\inputs\Checkbox;
@ -13,10 +14,11 @@ use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render();
'placeholder' => "Наименование поля", 'value' => $name ?? null])->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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -5,16 +5,18 @@
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
*
* @var integer $name
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
'placeholder' => "Наименование", 'value' => $name ?? null])->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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -5,19 +5,22 @@
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
*
* @var integer $name
* @var string $value
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][radio]", params: ['class' => "form-control"])
'placeholder' => "Наименование", 'value' => $name ?? null])->setLabel("Наименование (например \"Телефон\")")->render();
$form->field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][radio]", params: ['class' => "form-control", 'value' => $value])
->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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -1,25 +1,34 @@
<div class="form-block form-control">
<b><p>Список</p></b>
<?php
/**
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
*
* @var integer $name
* @var string $value
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\TextInput;
?>
<div class="form-block form-control">
<b><p>Список</p></b>
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование"])->setLabel("Наименование (например \"Список городов\")")->render();
$form->field(TextArea::class, name: "InputForm[$count][options]", params: ['class' => "form-control"])
<?php
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование", 'value' => $name ?? null])->setLabel("Наименование (например \"Список городов\")")->render();
$form->field(TextArea::class, name: "InputForm[$count][options]", params: [
'class' => "form-control",
'value' => $value,
])
->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();
?>
<button class="delete-button btn-danger">X</button>
$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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
</div>

View File

@ -5,6 +5,9 @@
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
* @var integer $name
* @var integer $placeholder
* @var \itguild\forms\core\cg_view\CgView $cgView
*
*/
@ -12,11 +15,12 @@ use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render();
'placeholder' => "Наименование поля", 'value' => $name ?? null])->setLabel("Наименование поля (например \"Телефон\")")->render();
$form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control",
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render();
'placeholder' => 'Placeholder', "value" => $placeholder ?? null])->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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -5,17 +5,21 @@
*
* @var \itguild\forms\ActiveForm $form
* @var integer $count
*
* @var integer $name
* @var integer $placeholder
* @var \itguild\forms\core\cg_view\CgView $cgView
*/
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\TextInput;
$form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control",
'placeholder' => "Наименование поля"])->setLabel("Наименование поля (например \"Телефон\")")->render();
'placeholder' => "Наименование поля", 'value' => $name ?? null])->setLabel("Наименование поля (например \"Телефон\")")->render();
$form->field(TextInput::class, name: "InputForm[$count][placeholder]", params: ['class' => "form-control",
'placeholder' => 'Placeholder'])->setLabel("Placeholder")->render();
'placeholder' => 'Placeholder', "value" => $placeholder ?? null])->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();
$cgView->render("/admin/additional_elements/_delete_field_box_button.php", []);
?>
<button class="delete-button btn-danger">X</button>
</div>

View File

@ -6,6 +6,7 @@
*/
use itguild\forms\ActiveForm;
use itguild\forms\app\models\InputTypeModel;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\TextArea;
use itguild\forms\app\controllers\AdminController;
@ -67,12 +68,7 @@ $form->field(\itguild\forms\inputs\Hidden::class, name: "formId", params: [
])->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++;
}
\itguild\forms\widgets\FormFieldBlockWidget::create()->run(['fields' => $fields]);
}
$form->field(\itguild\forms\inputs\Button::class, name: "", params: [

View File

@ -0,0 +1,3 @@
<?php
echo 123;