diff --git a/public/js/ajax.js b/public/js/ajax.js index 155fe07..44b6d3c 100644 --- a/public/js/ajax.js +++ b/public/js/ajax.js @@ -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(); + }); + } + +}); diff --git a/public/style/main.css b/public/style/main.css index 823302a..5a0523f 100644 --- a/public/style/main.css +++ b/public/style/main.css @@ -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; diff --git a/src/app/controllers/AdminController.php b/src/app/controllers/AdminController.php index dfd9952..5294c8d 100644 --- a/src/app/controllers/AdminController.php +++ b/src/app/controllers/AdminController.php @@ -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]); } diff --git a/src/app/services/InputValueService.php b/src/app/services/InputValueService.php new file mode 100644 index 0000000..902ca13 --- /dev/null +++ b/src/app/services/InputValueService.php @@ -0,0 +1,14 @@ +pluck('value', 'id')->toArray(); + } +} \ No newline at end of file diff --git a/src/widgets/BaseWidget.php b/src/widgets/BaseWidget.php new file mode 100644 index 0000000..101404e --- /dev/null +++ b/src/widgets/BaseWidget.php @@ -0,0 +1,32 @@ +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 = []); +} \ No newline at end of file diff --git a/src/widgets/FormFieldBlockWidget.php b/src/widgets/FormFieldBlockWidget.php new file mode 100644 index 0000000..55a7f5f --- /dev/null +++ b/src/widgets/FormFieldBlockWidget.php @@ -0,0 +1,63 @@ +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; + } + +} \ No newline at end of file diff --git a/views/admin/additional_elements/_delete_field_box_button.php b/views/admin/additional_elements/_delete_field_box_button.php new file mode 100644 index 0000000..bf47afd --- /dev/null +++ b/views/admin/additional_elements/_delete_field_box_button.php @@ -0,0 +1 @@ +X \ No newline at end of file diff --git a/views/admin/field_type/_button.php b/views/admin/field_type/_button.php index 93b1c5f..414899f 100644 --- a/views/admin/field_type/_button.php +++ b/views/admin/field_type/_button.php @@ -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", []); ?> - diff --git a/views/admin/field_type/_checkBox.php b/views/admin/field_type/_checkBox.php index 458757f..224897f 100644 --- a/views/admin/field_type/_checkBox.php +++ b/views/admin/field_type/_checkBox.php @@ -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", []); ?> - diff --git a/views/admin/field_type/_radioButton.php b/views/admin/field_type/_radioButton.php index 8ef00a3..e7f5c5c 100644 --- a/views/admin/field_type/_radioButton.php +++ b/views/admin/field_type/_radioButton.php @@ -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", []); ?> - \ No newline at end of file diff --git a/views/admin/field_type/_select.php b/views/admin/field_type/_select.php index f5ed08c..6894cf6 100644 --- a/views/admin/field_type/_select.php +++ b/views/admin/field_type/_select.php @@ -1,25 +1,34 @@ -
-

Список

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"]) - ->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(); ?> - +
+

Список

+ + 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(); + + $cgView->render("/admin/additional_elements/_delete_field_box_button.php", []); + ?>
\ 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 143ed74..3ec3c0c 100644 --- a/views/admin/field_type/_text_input.php +++ b/views/admin/field_type/_text_input.php @@ -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", []); ?> -
\ No newline at end of file diff --git a/views/admin/field_type/_textarea.php b/views/admin/field_type/_textarea.php index 1543870..5c78dc4 100644 --- a/views/admin/field_type/_textarea.php +++ b/views/admin/field_type/_textarea.php @@ -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", []); ?> - \ No newline at end of file diff --git a/views/admin/index.php b/views/admin/index.php index 7f715b1..4ec6dba 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -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: [ diff --git a/views/widgets/form_field_block/index.php b/views/widgets/form_field_block/index.php new file mode 100644 index 0000000..e5f3f1d --- /dev/null +++ b/views/widgets/form_field_block/index.php @@ -0,0 +1,3 @@ +