render('layout/base.php', [ 'pageTitle' => 'Kunden', 'content' => $this->renderView('customers/list', [ 'customers' => $customers, 'filter' => $filter, ]), 'topbarActions' => '' . '' . '' . '' . '', ]); } public function create(array $params): void { $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => 'Kunde anlegen', 'content' => $this->renderView('customers/form', [ 'customer' => [], 'errors' => [], 'action' => '/customers', ]), ]); } public function store(array $params): void { $data = $_POST; $errors = $this->validate($data); if ($errors) { $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => 'Kunde anlegen', 'content' => $this->renderView('customers/form', [ 'customer' => $data, 'errors' => $errors, 'action' => '/customers', ]), ]); return; } Customer::create($data); View::redirect('/customers', 'Kunde erfolgreich angelegt.'); } public function show(array $params): void { $customer = Customer::find((int)$params['id']); if (!$customer) { http_response_code(404); return; } $invoices = \Steuerkram\Database::fetchAll( 'SELECT * FROM invoices WHERE customer_id=? ORDER BY rechnungs_datum DESC LIMIT 20', [$customer['id']] ); $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => Customer::displayName($customer), 'content' => $this->renderView('customers/show', [ 'customer' => $customer, 'invoices' => $invoices, ]), ]); } public function edit(array $params): void { $customer = Customer::find((int)$params['id']); if (!$customer) { http_response_code(404); return; } $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => 'Kunde bearbeiten', 'content' => $this->renderView('customers/form', [ 'customer' => $customer, 'errors' => [], 'action' => '/customers/' . $customer['id'], ]), ]); } public function update(array $params): void { $id = (int)$params['id']; $customer = Customer::find($id); if (!$customer) { http_response_code(404); return; } $data = $_POST; $errors = $this->validate($data); if ($errors) { $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => 'Kunde bearbeiten', 'content' => $this->renderView('customers/form', [ 'customer' => array_merge($customer, $data), 'errors' => $errors, 'action' => '/customers/' . $id, ]), ]); return; } Customer::update($id, $data); View::redirect('/customers/' . $id, 'Kundendaten aktualisiert.'); } public function archive(array $params): void { Customer::archive((int)$params['id']); View::redirect('/customers', 'Kunde archiviert.'); } private function validate(array $data): array { $errors = []; if (empty($data['nachname'])) $errors['nachname'] = 'Pflichtfeld'; if (empty($data['strasse'])) $errors['strasse'] = 'Pflichtfeld'; if (empty($data['plz'])) $errors['plz'] = 'Pflichtfeld'; if (empty($data['ort'])) $errors['ort'] = 'Pflichtfeld'; return $errors; } }