create($data); $success = "Author created successfully!"; } else { $authorModel->update($id, $data); $authorId = $id; $success = "Author updated successfully!"; } header("Location: /admin/authors/?action=edit&id=" . $authorId); exit; break; case 'delete': if ($id) { $authorModel->delete($id); $success = "Author deleted successfully!"; $action = 'list'; } break; } } catch (Exception $e) { $error = "Error: " . $e->getMessage(); } } // Get data for different actions switch ($action) { case 'create': case 'new': $author = [ 'name' => '', 'image' => '', 'description' => '', 'social_links' => [] ]; $action = 'create'; break; case 'edit': if (!$id) { $action = 'list'; break; } $author = $authorModel->getById($id); if (!$author) { $error = "Author not found!"; $action = 'list'; break; } break; case 'list': default: $authors = $authorModel->getAll(); $action = 'list'; break; } // Determine which template to use $template = match($action) { 'list' => 'admin/authors/list.php', 'create', 'edit' => 'admin/authors/form.php', default => 'admin/authors/list.php' }; return ViewRender($template, [ 'action' => $action, 'authors' => $authors ?? [], 'author' => $author ?? null, 'success' => $success ?? null, 'error' => $error ?? null ]);