ver 0.250: migrate settings to form-edit and cleanup legacy settings classes
This commit is contained in:
@@ -2,87 +2,375 @@
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Settings\SettingsRepository;
|
||||
use admin\ViewModels\Forms\FormEditViewModel;
|
||||
use admin\ViewModels\Forms\FormField;
|
||||
use admin\ViewModels\Forms\FormTab;
|
||||
use admin\ViewModels\Forms\FormAction;
|
||||
use admin\Support\Forms\FormRequestHandler;
|
||||
|
||||
/**
|
||||
* Kontroler ustawień w panelu administratora (nowa architektura)
|
||||
*
|
||||
* Używa Dependency Injection zamiast static methods
|
||||
* Deleguje logikę do Domain\Settings\SettingsRepository
|
||||
* Kontroler ustawien w panelu administratora.
|
||||
*/
|
||||
class SettingsController
|
||||
{
|
||||
private SettingsRepository $settingsRepository;
|
||||
private FormRequestHandler $formHandler;
|
||||
|
||||
public function __construct(SettingsRepository $settingsRepository)
|
||||
{
|
||||
$this->settingsRepository = $settingsRepository;
|
||||
$this->formHandler = new FormRequestHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Czyszczenie cache
|
||||
* Czyszczenie cache.
|
||||
*/
|
||||
public function clearCache(): void
|
||||
{
|
||||
\S::delete_dir( '../temp/' );
|
||||
\S::delete_dir( '../thumbs/' );
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../thumbs/');
|
||||
|
||||
$redis = \RedisConnection::getInstance()->getConnection();
|
||||
if ( $redis )
|
||||
if ($redis) {
|
||||
$redis->flushAll();
|
||||
}
|
||||
|
||||
\S::alert( 'Cache został wyczyszczony.' );
|
||||
\S::alert('Cache został wyczyszczony.');
|
||||
\S::htacces();
|
||||
|
||||
header( 'Location: /admin/dashboard/main_view/' );
|
||||
header('Location: /admin/dashboard/main_view/');
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Czyszczenie cache (AJAX)
|
||||
* Czyszczenie cache (AJAX).
|
||||
*/
|
||||
public function clearCacheAjax(): void
|
||||
{
|
||||
try {
|
||||
\S::delete_dir( '../temp/' );
|
||||
\S::delete_dir( '../thumbs/' );
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../thumbs/');
|
||||
|
||||
$redis = \RedisConnection::getInstance()->getConnection();
|
||||
if ( $redis )
|
||||
if ($redis) {
|
||||
$redis->flushAll();
|
||||
}
|
||||
|
||||
\S::htacces();
|
||||
|
||||
echo json_encode( [ 'status' => 'success', 'message' => 'Cache został wyczyszczony.' ] );
|
||||
} catch ( \Exception $e ) {
|
||||
echo json_encode( [ 'status' => 'error', 'message' => 'Błąd podczas czyszczenia cache: ' . $e->getMessage() ] );
|
||||
echo json_encode(['status' => 'success', 'message' => 'Cache został wyczyszczony.']);
|
||||
} catch (\Exception $e) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Błąd podczas czyszczenia cache: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapis ustawień (AJAX)
|
||||
* Zapis ustawien (AJAX).
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
// Kompatybilnosc wsteczna dla legacy gridEdit (values jako JSON).
|
||||
$legacyValues = \S::get('values');
|
||||
if ($legacyValues) {
|
||||
$values = json_decode($legacyValues, true);
|
||||
$result = $this->settingsRepository->saveSettings(is_array($values) ? $values : []);
|
||||
|
||||
$response = $this->settingsRepository->saveSettings( $values );
|
||||
\S::delete_dir('../temp/');
|
||||
\S::htacces();
|
||||
|
||||
\S::delete_dir( '../temp/' );
|
||||
echo json_encode($result);
|
||||
exit;
|
||||
}
|
||||
|
||||
$languages = \admin\factory\Languages::languages_list();
|
||||
$settings = $this->settingsRepository->getSettings();
|
||||
$viewModel = $this->buildFormViewModel($settings, $languages);
|
||||
|
||||
$result = $this->formHandler->handleSubmit($viewModel, $_POST);
|
||||
if (!$result['success']) {
|
||||
$_SESSION['form_errors'][$this->getFormId()] = $result['errors'];
|
||||
echo json_encode(['success' => false, 'errors' => $result['errors']]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$values = $this->transformFormDataToSettings($result['data']);
|
||||
$saveResult = $this->settingsRepository->saveSettings($values);
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
\S::htacces();
|
||||
|
||||
echo json_encode( $response );
|
||||
echo json_encode([
|
||||
'success' => ($saveResult['status'] ?? '') === 'ok',
|
||||
'message' => $saveResult['msg'] ?? 'Ustawienia zostały zapisane.',
|
||||
'errors' => (($saveResult['status'] ?? '') === 'ok') ? [] : ['general' => ($saveResult['msg'] ?? 'Błąd zapisu.')],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Widok ustawień
|
||||
* Widok ustawien.
|
||||
*/
|
||||
public function view(): string
|
||||
{
|
||||
return \Tpl::view( 'settings/settings', [
|
||||
'languages' => \admin\factory\Languages::languages_list(),
|
||||
'settings' => $this->settingsRepository->getSettings()
|
||||
] );
|
||||
$languages = \admin\factory\Languages::languages_list();
|
||||
$settings = $this->settingsRepository->getSettings();
|
||||
|
||||
$validationErrors = $_SESSION['form_errors'][$this->getFormId()] ?? null;
|
||||
if ($validationErrors) {
|
||||
unset($_SESSION['form_errors'][$this->getFormId()]);
|
||||
}
|
||||
|
||||
$viewModel = $this->buildFormViewModel($settings, $languages, $validationErrors);
|
||||
|
||||
return \Tpl::view('components/form-edit', ['form' => $viewModel]);
|
||||
}
|
||||
|
||||
private function buildFormViewModel(array $settings, array $languages, ?array $errors = null): FormEditViewModel
|
||||
{
|
||||
$data = $this->transformSettingsToFormData($settings, $languages);
|
||||
|
||||
$tabs = [
|
||||
new FormTab('contact', 'Dane kontaktowe', 'fa-paper-plane'),
|
||||
new FormTab('shop', 'Sklep', 'fa-dollar'),
|
||||
new FormTab('products', 'Produkty', 'fa-shopping-cart'),
|
||||
new FormTab('mail', 'Poczta', 'fa-envelope'),
|
||||
new FormTab('other', 'Pozostałe', 'fa-bars'),
|
||||
new FormTab('system', 'System', 'fa-cog'),
|
||||
new FormTab('conversions', 'Konwersje', 'fa-line-chart'),
|
||||
];
|
||||
|
||||
$fields = [
|
||||
FormField::text('firm_name', [
|
||||
'label' => 'Nazwa firmy',
|
||||
'tab' => 'contact',
|
||||
]),
|
||||
FormField::editor('additional_info', [
|
||||
'label' => 'Dodatkowe informacje',
|
||||
'tab' => 'contact',
|
||||
'height' => 150,
|
||||
]),
|
||||
FormField::switch('google_maps', [
|
||||
'label' => 'Mapa',
|
||||
'tab' => 'contact',
|
||||
]),
|
||||
FormField::textarea('firm_adress', [
|
||||
'label' => 'Mapa - adres',
|
||||
'tab' => 'contact',
|
||||
]),
|
||||
|
||||
FormField::editor('shop_bank_account_info', [
|
||||
'label' => 'Dane do przelewu',
|
||||
'tab' => 'shop',
|
||||
'height' => 200,
|
||||
]),
|
||||
FormField::text('hotpay_api', [
|
||||
'label' => 'Klucz API HotPay',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::switch('tpay_sandbox', [
|
||||
'label' => 'Tpay.com - tryb sandbox',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::text('tpay_id', [
|
||||
'label' => 'Tpay.com ID',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::text('tpay_security_code', [
|
||||
'label' => 'Tpay.com - kod bezpieczeństwa',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::switch('przelewy24_sandbox', [
|
||||
'label' => 'Przelewy24.pl - tryb sandbox',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::text('przelewy24_merchant_id', [
|
||||
'label' => 'Przelewy24.pl - merchant ID',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::text('przelewy24_crc_key', [
|
||||
'label' => 'Przelewy24.pl - klucz CRC',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
FormField::text('free_delivery', [
|
||||
'label' => 'Darmowa dostawa od',
|
||||
'tab' => 'shop',
|
||||
'attributes' => ['class' => 'number-format'],
|
||||
]),
|
||||
FormField::text('orlen_paczka_map_token', [
|
||||
'label' => 'Orlen Paczka map token',
|
||||
'tab' => 'shop',
|
||||
]),
|
||||
|
||||
FormField::langSection('warehouse_messages', 'products', [
|
||||
FormField::text('warehouse_message_zero', [
|
||||
'label' => 'Komunikat gdy stan magazynowy równy 0',
|
||||
]),
|
||||
FormField::text('warehouse_message_nonzero', [
|
||||
'label' => 'Komunikat gdy stan magazynowy większy niż 0',
|
||||
]),
|
||||
]),
|
||||
|
||||
FormField::switch('contact_form', [
|
||||
'label' => 'Formularz kontaktowy',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
FormField::text('contact_email', [
|
||||
'label' => 'Email kontaktowy',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
FormField::text('email_host', [
|
||||
'label' => 'Email - host',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
FormField::text('email_port', [
|
||||
'label' => 'Email - port',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
FormField::text('email_login', [
|
||||
'label' => 'Email - login',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
FormField::text('email_password', [
|
||||
'label' => 'Email - hasło',
|
||||
'tab' => 'mail',
|
||||
]),
|
||||
|
||||
FormField::text('facebook_link', [
|
||||
'label' => 'Facebook link',
|
||||
'tab' => 'other',
|
||||
]),
|
||||
FormField::text('piksel', [
|
||||
'label' => 'Piksel Facebook',
|
||||
'tab' => 'other',
|
||||
]),
|
||||
FormField::textarea('statistic_code', [
|
||||
'label' => 'Kod statystyk',
|
||||
'tab' => 'other',
|
||||
'rows' => 10,
|
||||
]),
|
||||
FormField::textarea('htaccess', [
|
||||
'label' => 'Własne reguły htacess',
|
||||
'tab' => 'other',
|
||||
'rows' => 10,
|
||||
]),
|
||||
FormField::textarea('robots', [
|
||||
'label' => 'Własne reguły robots.txt',
|
||||
'tab' => 'other',
|
||||
'rows' => 10,
|
||||
]),
|
||||
|
||||
FormField::switch('update', [
|
||||
'label' => 'Aktualizacja',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::text('update_key', [
|
||||
'label' => 'Numer licencji',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::switch('devel', [
|
||||
'label' => 'Strona konstrukcyjna',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::switch('lazy_loading', [
|
||||
'label' => 'Lazy loading obrazów',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::switch('generate_webp', [
|
||||
'label' => 'Generowanie obrazków WEBP',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::switch('infinitescroll', [
|
||||
'label' => 'Infinitescroll',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
FormField::switch('htaccess_cache', [
|
||||
'label' => 'Htaccess cache',
|
||||
'tab' => 'system',
|
||||
]),
|
||||
|
||||
FormField::text('google_tag_manager_id', [
|
||||
'label' => 'Google Tag Manager - ID',
|
||||
'tab' => 'conversions',
|
||||
]),
|
||||
FormField::textarea('own_gtm_js', [
|
||||
'label' => 'Własny kod GTM JS (bez tagu script)',
|
||||
'tab' => 'conversions',
|
||||
'rows' => 10,
|
||||
]),
|
||||
FormField::textarea('own_gtm_html', [
|
||||
'label' => 'Własny kod GTM HTML',
|
||||
'tab' => 'conversions',
|
||||
'rows' => 10,
|
||||
]),
|
||||
];
|
||||
|
||||
$actions = [
|
||||
FormAction::save('/admin/settings/settings_save/', ''),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
$this->getFormId(),
|
||||
'Edycja ustawień',
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
'/admin/settings/settings_save/',
|
||||
null,
|
||||
false,
|
||||
[],
|
||||
$languages,
|
||||
$errors
|
||||
);
|
||||
}
|
||||
|
||||
private function getFormId(): string
|
||||
{
|
||||
return 'settings-edit';
|
||||
}
|
||||
|
||||
private function transformSettingsToFormData(array $settings, array $languages): array
|
||||
{
|
||||
$data = $settings;
|
||||
$data['languages'] = [];
|
||||
|
||||
foreach ($languages as $lang) {
|
||||
if (!($lang['status'] ?? false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$langId = (string)$lang['id'];
|
||||
$data['languages'][$langId] = [
|
||||
'warehouse_message_zero' => $settings['warehouse_message_zero_' . $langId] ?? '',
|
||||
'warehouse_message_nonzero' => $settings['warehouse_message_nonzero_' . $langId] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function transformFormDataToSettings(array $data): array
|
||||
{
|
||||
if (!isset($data['warehouse_messages']) || !is_array($data['warehouse_messages'])) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$data['warehouse_message_zero'] = [];
|
||||
$data['warehouse_message_nonzero'] = [];
|
||||
|
||||
foreach ($data['warehouse_messages'] as $langId => $langValues) {
|
||||
if (!is_array($langValues)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['warehouse_message_zero'][$langId] = $langValues['warehouse_message_zero'] ?? '';
|
||||
$data['warehouse_message_nonzero'][$langId] = $langValues['warehouse_message_nonzero'] ?? '';
|
||||
}
|
||||
|
||||
unset($data['warehouse_messages']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user