sdl3/examples/ServerManager/UI/SettingsModal.php
2025-11-12 11:56:45 +01:00

119 lines
4.8 KiB
PHP

<?php
namespace ServerManager\UI;
use PHPNative\Framework\Settings;
use PHPNative\Tailwind\Data\Icon as IconName;
use PHPNative\Ui\Widget\Button;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Icon;
use PHPNative\Ui\Widget\Label;
use PHPNative\Ui\Widget\MenuBar;
use PHPNative\Ui\Widget\Modal;
use PHPNative\Ui\Widget\TextInput;
class SettingsModal
{
private Modal $modal;
private TextInput $apiKeyInput;
private TextInput $privateKeyPathInput;
private TextInput $remoteStartDirInput;
public function __construct(Settings $settings, MenuBar $menuBar, string &$apiKey, string &$privateKeyPath, string &$remoteStartDir)
{
// Create input fields
$this->apiKeyInput = new TextInput('API Key', 'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black');
$this->privateKeyPathInput = new TextInput(
'Private Key Path',
'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black'
);
$this->remoteStartDirInput = new TextInput(
'Remote Start Directory',
'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black'
);
// Set initial values
$this->apiKeyInput->setValue($apiKey);
$this->privateKeyPathInput->setValue($privateKeyPath);
$this->remoteStartDirInput->setValue($remoteStartDir);
// Create modal dialog
$modalDialog = new Container('bg-white rounded-lg p-6 flex flex-col w-96 gap-3');
$modalDialog->addComponent(new Label('API Einstellungen', 'text-xl font-bold text-black'));
$modalDialog->addComponent(new Label(
'Bitte gib deinen API Key und den Pfad zum Private Key für SSH-Verbindungen ein.',
'text-sm text-gray-700'
));
// API Key field
$fieldContainer = new Container('flex flex-col gap-1');
$fieldContainer->addComponent(new Label('API Key', 'text-sm text-gray-600'));
$fieldContainer->addComponent($this->apiKeyInput);
$modalDialog->addComponent($fieldContainer);
// Private Key field
$privateKeyFieldContainer = new Container('flex flex-col gap-1');
$privateKeyFieldContainer->addComponent(new Label('Private Key Pfad', 'text-sm text-gray-600'));
$privateKeyFieldContainer->addComponent($this->privateKeyPathInput);
$modalDialog->addComponent($privateKeyFieldContainer);
// Remote Start Directory field
$remoteStartDirFieldContainer = new Container('flex flex-col gap-1');
$remoteStartDirFieldContainer->addComponent(new Label('Remote Start Verzeichnis (z.B. /var/www)', 'text-sm text-gray-600'));
$remoteStartDirFieldContainer->addComponent($this->remoteStartDirInput);
$modalDialog->addComponent($remoteStartDirFieldContainer);
// Buttons
$buttonRow = new Container('flex flex-row gap-2 justify-end');
$cancelButton = new Button('Abbrechen', 'px-4 py-2 bg-gray-200 text-black rounded hover:bg-gray-300');
$saveButton = new Button('Speichern', 'px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 flex items-center');
$saveIcon = new Icon(IconName::save, 18, 'text-white mr-2');
$saveButton->setIcon($saveIcon);
$buttonRow->addComponent($cancelButton);
$buttonRow->addComponent($saveButton);
$modalDialog->addComponent($buttonRow);
$this->modal = new Modal($modalDialog, 'flex items-center justify-center bg-black', 200);
// Setup button handlers - use references directly in closures
$settingsModal = $this;
$apiKeyInputRef = $this->apiKeyInput;
$privateKeyPathInputRef = $this->privateKeyPathInput;
$remoteStartDirInputRef = $this->remoteStartDirInput;
$cancelButton->setOnClick(function () use ($menuBar, $settingsModal) {
$menuBar->closeAllMenus();
$settingsModal->hide();
});
$saveButton->setOnClick(function () use ($menuBar, $settingsModal, &$apiKey, &$privateKeyPath, &$remoteStartDir, $settings, $apiKeyInputRef, $privateKeyPathInputRef, $remoteStartDirInputRef) {
$apiKey = trim($apiKeyInputRef->getValue());
$privateKeyPath = trim($privateKeyPathInputRef->getValue());
$remoteStartDir = trim($remoteStartDirInputRef->getValue());
$settings->set('api_key', $apiKey);
$settings->set('private_key_path', $privateKeyPath);
$settings->set('remote_start_dir', $remoteStartDir);
$settings->save();
$menuBar->closeAllMenus();
$settingsModal->hide();
});
}
public function getModal(): Modal
{
return $this->modal;
}
public function show(): void
{
$this->modal->setVisible(true);
}
public function hide(): void
{
$this->modal->setVisible(false);
}
}