Steuerkram/src/Controllers/SupplierController.php

144 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Steuerkram\Controllers;
use Steuerkram\Models\Supplier;
use Steuerkram\View;
class SupplierController
{
public function index(array $params): void
{
$suppliers = Supplier::all();
$view = new View();
$view->render('layout/base.php', [
'pageTitle' => 'Lieferanten',
'content' => $this->renderView('suppliers/list', [
'suppliers' => $suppliers,
]),
'topbarActions' => '<a href="/suppliers/new" class="btn btn-primary">+ Lieferant anlegen</a>',
]);
}
public function create(array $params): void
{
$view = new View();
$view->render('layout/base.php', [
'pageTitle' => 'Lieferant anlegen',
'content' => $this->renderView('suppliers/form', [
'supplier' => [],
'errors' => [],
'action' => '/suppliers',
]),
]);
}
public function store(array $params): void
{
$name = trim($_POST['name'] ?? '');
$errors = [];
if ($name === '') {
$errors['name'] = 'Pflichtfeld';
} elseif (Supplier::findByName($name)) {
$errors['name'] = 'Dieser Lieferantenname existiert bereits.';
}
if ($errors) {
$view = new View();
$view->render('layout/base.php', [
'pageTitle' => 'Lieferant anlegen',
'content' => $this->renderView('suppliers/form', [
'supplier' => ['name' => $name],
'errors' => $errors,
'action' => '/suppliers',
]),
]);
return;
}
Supplier::create($name);
View::redirect('/suppliers', 'Lieferant erfolgreich angelegt.');
}
public function edit(array $params): void
{
$supplier = Supplier::find((int)$params['id']);
if (!$supplier) { http_response_code(404); return; }
$view = new View();
$view->render('layout/base.php', [
'pageTitle' => 'Lieferant bearbeiten',
'content' => $this->renderView('suppliers/form', [
'supplier' => $supplier,
'errors' => [],
'action' => '/suppliers/' . $supplier['id'],
]),
]);
}
public function update(array $params): void
{
$id = (int)$params['id'];
$supplier = Supplier::find($id);
if (!$supplier) { http_response_code(404); return; }
$name = trim($_POST['name'] ?? '');
$errors = [];
if ($name === '') {
$errors['name'] = 'Pflichtfeld';
} else {
$existing = Supplier::findByName($name);
if ($existing && (int)$existing['id'] !== $id) {
$errors['name'] = 'Dieser Lieferantenname existiert bereits.';
}
}
if ($errors) {
$view = new View();
$view->render('layout/base.php', [
'pageTitle' => 'Lieferant bearbeiten',
'content' => $this->renderView('suppliers/form', [
'supplier' => array_merge($supplier, ['name' => $name]),
'errors' => $errors,
'action' => '/suppliers/' . $id,
]),
]);
return;
}
Supplier::update($id, $name);
View::redirect('/suppliers', 'Lieferant aktualisiert.');
}
public function destroy(array $params): void
{
if (!Supplier::delete((int)$params['id'])) {
View::redirect('/suppliers', 'Lieferant kann nicht gelöscht werden (noch in Belegen verwendet).', 'error');
return;
}
View::redirect('/suppliers', 'Lieferant gelöscht.');
}
/**
* AJAX: Autocomplete-Liste aller Lieferanten als JSON.
*/
public function search(array $params): void
{
$suppliers = Supplier::all();
header('Content-Type: application/json');
echo json_encode(array_map(fn($s) => ['id' => $s['id'], 'name' => $s['name']], $suppliers));
}
private function renderView(string $template, array $data): string
{
extract($data, EXTR_SKIP);
ob_start();
include dirname(__DIR__) . '/Views/' . $template . '.php';
return ob_get_clean();
}
}