125 lines
4 KiB
PHP
125 lines
4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Steuerkram\Controllers;
|
|
|
|
use Steuerkram\Models\Setting;
|
|
use Steuerkram\Services\EmailService;
|
|
use Steuerkram\View;
|
|
|
|
class SettingsController
|
|
{
|
|
public function show(array $params): void
|
|
{
|
|
$settings = Setting::get();
|
|
$version = trim(@file_get_contents(dirname(__DIR__) . '/version.txt') ?: '');
|
|
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Einstellungen',
|
|
'version' => $version,
|
|
'topbarActions' => '<button type="submit" form="settings-form" class="btn btn-primary">Einstellungen speichern</button>',
|
|
'content' => $this->renderView('settings/form', [
|
|
'settings' => $settings,
|
|
'errors' => [],
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function update(array $params): void
|
|
{
|
|
$data = $_POST;
|
|
$errors = [];
|
|
|
|
if (empty($data['firma'])) $errors['firma'] = 'Pflichtfeld';
|
|
if (empty($data['strasse'])) $errors['strasse'] = 'Pflichtfeld';
|
|
if (empty($data['plz'])) $errors['plz'] = 'Pflichtfeld';
|
|
if (empty($data['ort'])) $errors['ort'] = 'Pflichtfeld';
|
|
|
|
if ($errors) {
|
|
$version = trim(@file_get_contents(dirname(__DIR__) . '/version.txt') ?: '');
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Einstellungen',
|
|
'version' => $version,
|
|
'content' => $this->renderView('settings/form', [
|
|
'settings' => $data,
|
|
'errors' => $errors,
|
|
]),
|
|
]);
|
|
return;
|
|
}
|
|
|
|
Setting::update($data);
|
|
View::redirect('/settings', 'Einstellungen gespeichert.');
|
|
}
|
|
|
|
public function uploadLogo(array $params): void
|
|
{
|
|
if (empty($_FILES['logo']) || $_FILES['logo']['error'] !== UPLOAD_ERR_OK) {
|
|
View::redirect('/settings', 'Kein gültiges Bild übermittelt.', 'error');
|
|
return;
|
|
}
|
|
|
|
$file = $_FILES['logo'];
|
|
$mime = mime_content_type($file['tmp_name']);
|
|
|
|
if (!in_array($mime, ['image/png', 'image/jpeg'])) {
|
|
View::redirect('/settings', 'Logo muss PNG oder JPEG sein.', 'error');
|
|
return;
|
|
}
|
|
|
|
$ext = ['image/png' => 'png', 'image/jpeg' => 'jpg'][$mime];
|
|
$relPath = 'static/uploads/logo/logo.' . $ext;
|
|
$absDir = dirname(__DIR__, 2) . '/public/static/uploads/logo';
|
|
|
|
if (!is_dir($absDir) && !mkdir($absDir, 0755, true)) {
|
|
View::redirect('/settings', 'Upload-Verzeichnis konnte nicht erstellt werden.', 'error');
|
|
return;
|
|
}
|
|
|
|
$dest = dirname(__DIR__, 2) . '/public/' . $relPath;
|
|
if (!move_uploaded_file($file['tmp_name'], $dest)) {
|
|
View::redirect('/settings', 'Datei konnte nicht gespeichert werden. Bitte Schreibrechte prüfen.', 'error');
|
|
return;
|
|
}
|
|
|
|
Setting::setLogo($relPath);
|
|
|
|
View::redirect('/settings', 'Logo hochgeladen.');
|
|
}
|
|
|
|
public function mailTest(array $params): void
|
|
{
|
|
header('Content-Type: application/json');
|
|
$settings = Setting::get();
|
|
$to = $settings['email'] ?? '';
|
|
|
|
if (empty($to)) {
|
|
echo json_encode(['error' => 'Keine Firmen-E-Mail hinterlegt (Einstellungen → Firmendaten).']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
(new EmailService())->sendTestMail($settings, $to);
|
|
echo json_encode(['ok' => true]);
|
|
} catch (\RuntimeException $e) {
|
|
echo json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function deleteLogo(array $params): void
|
|
{
|
|
Setting::deleteLogo();
|
|
View::redirect('/settings', 'Logo gelöscht.');
|
|
}
|
|
|
|
private function renderView(string $template, array $data): string
|
|
{
|
|
extract($data, EXTR_SKIP);
|
|
ob_start();
|
|
include dirname(__DIR__) . '/Views/' . $template . '.php';
|
|
return ob_get_clean();
|
|
}
|
|
}
|