feat(06-admin-base): Admin\ base infrastructure — Form Edit System + Support layer (Phase 6)
Phase 6 zamknięta po 2 planach. Pełny fundament dla Phase 7-13 (migracja 17 admin controllers do Admin\ namespace). 06-01 (Forms infrastructure): - Admin\ViewModels\Forms\* — 5 ViewModeli (687 L) - Admin\Validation\FormValidator (196 L) - composer.json: php >=7.4, PSR-4 paths cross-platform safe (Admin\ → autoload/admin/, Frontend\ → autoload/front/) 06-02 (Support layer): - Admin\Support\TableListRequestFactory (99 L) — parser list z $_GET - Admin\Support\Forms\FormRequestHandler (159 L) — POST + CSRF + walidacja + persist - Admin\Support\Forms\FormFieldRenderer (494 L) — renderer HTML pól Decyzje: - Brak BaseController — Phase 7+ kontrolery jako POJOs z DI (jak shopPRO) - PSR-4 filename fix: TableListRequestFactory.php (bez shopPRO 'class.' prefix) - PascalCase namespace (Admin\Support) na lowercase folder admin/ ze względu na Windows fs case-insensitivity vs legacy admin/controls/ Pliki: 8 nowych klas, 1635 L kodu PHP 7.4-kompatybilnego, zero regresji. Smoke test: walidacja e-maila zwraca PL komunikat, factory parsuje ?page=&per_page=&sort=&filter=, Domain/Shared nadal ładują się. PHPUnit: 37/37 OK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
178
autoload/admin/ViewModels/Forms/FormEditViewModel.php
Normal file
178
autoload/admin/ViewModels/Forms/FormEditViewModel.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
namespace Admin\ViewModels\Forms;
|
||||
|
||||
/**
|
||||
* Główny model widoku formularza edycji
|
||||
*/
|
||||
class FormEditViewModel
|
||||
{
|
||||
public string $formId;
|
||||
public string $title;
|
||||
public string $method;
|
||||
public string $action;
|
||||
public ?string $backUrl;
|
||||
public array $tabs;
|
||||
public array $fields;
|
||||
public array $hiddenFields;
|
||||
public array $actions;
|
||||
public bool $persist;
|
||||
public array $data;
|
||||
public ?array $validationErrors;
|
||||
public ?array $languages;
|
||||
|
||||
/**
|
||||
* @param string $formId Unikalny identyfikator formularza
|
||||
* @param string $title Tytuł formularza
|
||||
* @param array $data Dane obiektu (np. banner)
|
||||
* @param array $fields Pola formularza
|
||||
* @param array $tabs Zakładki formularza
|
||||
* @param array $actions Akcje (przyciski)
|
||||
* @param string $method Metoda HTTP (POST, GET)
|
||||
* @param string $action URL akcji formularza
|
||||
* @param string|null $backUrl URL powrotu
|
||||
* @param bool $persist Czy zapamiętywać dane w sesji
|
||||
* @param array $hiddenFields Dodatkowe ukryte pola
|
||||
* @param array|null $languages Dostępne języki (dla sekcji językowych)
|
||||
* @param array|null $validationErrors Błędy walidacji
|
||||
*/
|
||||
public function __construct(
|
||||
string $formId,
|
||||
string $title,
|
||||
array $data = [],
|
||||
array $fields = [],
|
||||
array $tabs = [],
|
||||
array $actions = [],
|
||||
string $method = 'POST',
|
||||
string $action = '',
|
||||
?string $backUrl = null,
|
||||
bool $persist = true,
|
||||
array $hiddenFields = [],
|
||||
?array $languages = null,
|
||||
?array $validationErrors = null
|
||||
) {
|
||||
$this->formId = $formId;
|
||||
$this->title = $title;
|
||||
$this->data = $data;
|
||||
$this->fields = $fields;
|
||||
$this->tabs = $tabs;
|
||||
$this->actions = $actions;
|
||||
$this->method = $method;
|
||||
$this->action = $action;
|
||||
$this->backUrl = $backUrl;
|
||||
$this->persist = $persist;
|
||||
$this->hiddenFields = $hiddenFields;
|
||||
$this->languages = $languages;
|
||||
$this->validationErrors = $validationErrors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprawdza czy formularz ma zakładki
|
||||
*/
|
||||
public function hasTabs(): bool
|
||||
{
|
||||
return count($this->tabs) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprawdza czy formularz ma sekcje językowe
|
||||
*/
|
||||
public function hasLangSections(): bool
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
if ($field->type === FormFieldType::LANG_SECTION) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca pola dla konkretnej zakładki
|
||||
*/
|
||||
public function getFieldsForTab(string $tabId): array
|
||||
{
|
||||
return array_filter($this->fields, function (FormField $field) use ($tabId) {
|
||||
return $field->tabId === $tabId && $field->type !== FormFieldType::LANG_SECTION;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca sekcje językowe dla konkretnej zakładki
|
||||
*/
|
||||
public function getLangSectionsForTab(string $tabId): array
|
||||
{
|
||||
return array_filter($this->fields, function (FormField $field) use ($tabId) {
|
||||
return $field->type === FormFieldType::LANG_SECTION &&
|
||||
$field->langSectionParentTab === $tabId;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera wartość pola z danych lub sesji (persist)
|
||||
*/
|
||||
public function getFieldValue(FormField $field, $languageId = null, ?string $langFieldName = null)
|
||||
{
|
||||
$fieldName = $field->name;
|
||||
|
||||
// Dla sekcji językowych - pobierz wartość z data[lang_id][field_name]
|
||||
if ($languageId !== null && $langFieldName !== null) {
|
||||
$fieldName = $langFieldName;
|
||||
return $this->data['languages'][$languageId][$fieldName] ?? null;
|
||||
}
|
||||
|
||||
// Zwykłe pole - najpierw sprawdź sesję (persist), potem dane
|
||||
if ($this->persist && isset($_SESSION['form_persist'][$this->formId][$fieldName])) {
|
||||
return $_SESSION['form_persist'][$this->formId][$fieldName];
|
||||
}
|
||||
|
||||
return $this->data[$fieldName] ?? $field->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprawdza czy pole ma błąd walidacji
|
||||
*/
|
||||
public function hasError(string $fieldName, $languageId = null): bool
|
||||
{
|
||||
if ($this->validationErrors === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($languageId !== null) {
|
||||
return isset($this->validationErrors[$fieldName][$languageId]);
|
||||
}
|
||||
|
||||
return isset($this->validationErrors[$fieldName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera komunikat błędu dla pola
|
||||
*/
|
||||
public function getError(string $fieldName, $languageId = null): ?string
|
||||
{
|
||||
if ($languageId !== null) {
|
||||
return $this->validationErrors[$fieldName][$languageId] ?? null;
|
||||
}
|
||||
return $this->validationErrors[$fieldName] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Czyści dane persist z sesji
|
||||
*/
|
||||
public function clearPersist(): void
|
||||
{
|
||||
if (isset($_SESSION['form_persist'][$this->formId])) {
|
||||
unset($_SESSION['form_persist'][$this->formId]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapisuje dane do sesji (persist)
|
||||
*/
|
||||
public function saveToPersist(array $data): void
|
||||
{
|
||||
if (!isset($_SESSION['form_persist'])) {
|
||||
$_SESSION['form_persist'] = [];
|
||||
}
|
||||
$_SESSION['form_persist'][$this->formId] = $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user