This commit is contained in:
Kavalar 2025-03-14 17:42:58 +03:00
commit a283d2cf37
13 changed files with 395 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
vendor
composer.lock

View File

@ -0,0 +1,18 @@
<?php
namespace Itguild\App\active_fields;
use Itguild\Form2\ActiveField;
class TextActiveField extends ActiveField
{
public function fetchField(): string
{
$html = '';
$html .= "</br><label for='" . $this->data['id'] . ">" . $this->data['label'] . "</label>";
$html .= "<input type='" . $this->data['type'] . "' name='" . $this->data['id'] . "' id='" . $this->data['id'] . "' class='" . $this->data['class'] . "' >";
return $html;
}
}

15
app/forms/UserForm.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Itguild\App\forms;
use Itguild\Form2\Form;
class UserForm extends Form
{
public function load(array $arr): void
{
parent::load($arr);
}
}

22
composer.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "itguild/form2",
"description": "Form library",
"type": "library",
"require-dev": {},
"autoload": {
"psr-4": {
"Itguild\\Form2\\": "src/",
"Itguild\\App\\": "app/"
}
},
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"minimum-stability": "dev",
"require": {
"itguild/debug": "0.1"
}
}

26
form.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": {
"label": "Имя",
"type": "text",
"id": "user_name",
"class": "form-control form-control-dark"
},
"user_email": {
"label": "Email",
"type": "email",
"id": "user_email",
"class": "form-control form-control-dark"
},
"tg": {
"label": "Telegram",
"type": "text",
"id": "user_tg",
"class": "form-control form-control-dark"
},
"phone": {
"label": "Телефон",
"type": "number",
"id": "user_phone",
"class": "form-control form-control-dark"
}
}

63
form.php Normal file
View File

@ -0,0 +1,63 @@
<?php
require_once "form_helper.php";
require_once "Form.php";
require_once "UserForm.php";
include "header.php";
require_once "vendor/autoload.php";
$inputs = [
'name' => [
'label' => 'Имя',
'type' => 'text',
'id' => 'user_name',
'class' => 'inputForm',
],
'email' => [
'label' => 'Email',
'type' => 'email',
'id' => 'user_email',
'class' => 'inputForm',
],
'tg' => [
'label' => 'Telegram',
'type' => 'text',
'id' => 'user_tg',
'class' => 'inputForm',
],
'phone' => [
'label' => 'Телефон',
'type' => 'number',
'id' => 'user_phone',
'class' => 'inputForm',
],
];
$form = new UserForm([
'action' => 'save_user.php',
'method' => 'POST'
]);
$form->load($inputs);
$form->field(type: "text", name: "additional_email", params: [
'class' => 'additional_email inputForm',
'label' => 'Дполнительный email',
'id' => 'additional_email',
]);
$form->render();
$form2 = new UserForm([
'action' => "save_client.php",
'method' => 'POST'
]);
$form2->field(type: "text", name: "user_id", params: [
'class' => 'inputForm',
'label' => 'Пользователь',
'id' => 'user_id',
]);
$form2->render();

11
form_helper.php Normal file
View File

@ -0,0 +1,11 @@
<?php
function printForm(array $inputsArr): void
{
if ($_GET['show_form']) {
foreach ($inputsArr as $key => $input) {
echo "</br><label for='$key'>" . $input['label'] . "</label>";
echo "<input type='" . $input['type'] . "' name='$key' id='" . $input['id'] . "' class='" . $input['class'] . "' >";
}
}
}

18
header.php Normal file
View File

@ -0,0 +1,18 @@
<?php
echo "
<style>
.inputForm {
color: RED;
margin: 10px;
}
.additional_email {
color: #2c35cc;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
console.log('form ready');
});
</script>
";

23
index.php Normal file
View File

@ -0,0 +1,23 @@
<?php
require_once "vendor/autoload.php";
error_reporting(-1);
ini_set("display_errors", true);
$inputs = json_decode(file_get_contents(__DIR__ . "/form.json"), true);
// включаем буфер
ob_start();
// выводим информацию
include __DIR__ . "/views/index.php";
// сохраняем всё что есть в буфере в переменную $content
$content = ob_get_contents();
// отключаем и очищаем буфер
ob_end_clean();
include "views/layouts/main.php";

19
src/ActiveField.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Itguild\Form2;
class ActiveField
{
protected array $data = [];
public function __construct(array $data)
{
$this->data = $data;
}
public function fetchField()
{
}
}

86
src/Form.php Normal file
View File

@ -0,0 +1,86 @@
<?php
namespace Itguild\Form2;
class Form
{
protected array $data = [];
protected array $formParams = [];
protected string $formParamsStr = '';
protected string $html = '';
public function __construct(array $formParams = [])
{
$this->formParams = $formParams;
$this->createParams();
}
protected function _start(): void
{
$this->html .= "<form $this->formParamsStr>";
}
protected function _end(): void
{
$this->html .= "</form>";
}
public function render(): void
{
$this->_start();
$this->createFields();
$this->_end();
echo $this->html;
}
public function fetch(): string
{
$this->_start();
$this->createFields();
$this->_end();
return $this->html;
}
public function field(string $type, string $name, array $params = []): void
{
$fieldArr = [];
$fieldArr['type'] = $type;
$fieldArr = array_merge($fieldArr, $params);
$this->data[$name] = $fieldArr;
}
public function activeField()
{
}
/**
* @param array $arr
* @return void
*/
public function load(array $arr): void
{
$this->data = $arr;
}
public function createFields(): void
{
foreach ($this->data as $key => $input) {
$this->html .= "</br><label for='$key'>" . $input['label'] . "</label>";
$this->html .= "<input type='" . $input['type'] . "' name='$key' id='" . $input['id'] . "' class='" . $input['class'] . "' >";
}
}
protected function createParams(): void
{
foreach ($this->formParams as $key => $param){
$this->formParamsStr .= " $key='$param'";
}
}
private function setSome()
{
}
}

31
views/index.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/**
* @var array $inputs
*/
use Itguild\App\forms\UserForm;
$form = new UserForm([
'action' => 'save_user.php',
'method' => 'POST'
]);
$form->load($inputs);
$form->field(type: "text", name: "additional_email", params: [
'class' => 'additional_email form-control form-control-dark',
'label' => 'Дполнительный email',
'id' => 'additional_email',
]);
$form->render();
$form2 = new UserForm([
'action' => "save_client.php",
'method' => 'POST'
]);
$form2->field(type: "text", name: "user_id", params: [
'class' => 'form-control form-control-dark',
'label' => 'Пользователь',
'id' => 'user_id',
]);
$form2->render();

60
views/layouts/main.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/**
* @var string $content
*/
?>
<html>
<head>
<title>Form2</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<style>
.inputForm {
color: RED;
margin: 10px;
}
.additional_email {
color: #2c35cc;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
console.log('form ready');
});
</script>
</head>
<body>
<header class="p-3 bg-dark text-white">
<div class="container">
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
<a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none">
<svg class="bi me-2" width="40" height="32" role="img" aria-label="Bootstrap">
<use xlink:href="#bootstrap"></use>
</svg>
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="#" class="nav-link px-2 text-secondary">Home</a></li>
<li><a href="#" class="nav-link px-2 text-white">Features</a></li>
<li><a href="#" class="nav-link px-2 text-white">Pricing</a></li>
<li><a href="#" class="nav-link px-2 text-white">FAQs</a></li>
<li><a href="#" class="nav-link px-2 text-white">About</a></li>
</ul>
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
<input type="search" class="form-control form-control-dark" placeholder="Search..." aria-label="Search">
</form>
<div class="text-end">
<button type="button" class="btn btn-outline-light me-2">Login</button>
<button type="button" class="btn btn-warning">Sign-up</button>
</div>
</div>
</div>
</header>
<div class="container">
<?= $content ?>
</div>
</body>
</html>