authors.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. use App\Models\Author;
  3. $authorModel = new Author();
  4. $action = $_GET['action'] ?? 'list';
  5. $id = $_GET['id'] ?? null;
  6. // Handle form submissions
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. $data = $_POST;
  9. // Handle image upload
  10. if (!empty($_FILES['image_upload']['name'])) {
  11. $uploadedImage = uploadImage($_FILES['image_upload']);
  12. if ($uploadedImage) {
  13. $data['image'] = $uploadedImage;
  14. }
  15. } else {
  16. // Keep current image if no new upload
  17. $data['image'] = $data['current_image'] ?? '';
  18. }
  19. // Handle social links
  20. if (isset($data['social_links']) && is_array($data['social_links'])) {
  21. // Filter out empty social links
  22. $socialLinks = array_filter($data['social_links'], function($link) {
  23. return !empty($link['platform']) && !empty($link['url']);
  24. });
  25. $data['social_links'] = array_values($socialLinks); // Reindex array
  26. }
  27. // Get action from form data
  28. $formAction = $data['action'] ?? $action;
  29. try {
  30. switch ($formAction) {
  31. case 'create':
  32. case 'edit':
  33. if ($formAction === 'create') {
  34. $authorId = $authorModel->create($data);
  35. $success = "Author created successfully!";
  36. } else {
  37. $authorModel->update($id, $data);
  38. $authorId = $id;
  39. $success = "Author updated successfully!";
  40. }
  41. header("Location: /admin/authors/?action=edit&id=" . $authorId);
  42. exit;
  43. break;
  44. case 'delete':
  45. if ($id) {
  46. $authorModel->delete($id);
  47. $success = "Author deleted successfully!";
  48. $action = 'list';
  49. }
  50. break;
  51. }
  52. } catch (Exception $e) {
  53. $error = "Error: " . $e->getMessage();
  54. }
  55. }
  56. // Get data for different actions
  57. switch ($action) {
  58. case 'create':
  59. case 'new':
  60. $author = [
  61. 'name' => '',
  62. 'image' => '',
  63. 'description' => '',
  64. 'social_links' => []
  65. ];
  66. $action = 'create';
  67. break;
  68. case 'edit':
  69. if (!$id) {
  70. $action = 'list';
  71. break;
  72. }
  73. $author = $authorModel->getById($id);
  74. if (!$author) {
  75. $error = "Author not found!";
  76. $action = 'list';
  77. break;
  78. }
  79. break;
  80. case 'list':
  81. default:
  82. $authors = $authorModel->getAll();
  83. $action = 'list';
  84. break;
  85. }
  86. // Determine which template to use
  87. $template = match($action) {
  88. 'list' => 'admin/authors/list.php',
  89. 'create', 'edit' => 'admin/authors/form.php',
  90. default => 'admin/authors/list.php'
  91. };
  92. return ViewRender($template, [
  93. 'action' => $action,
  94. 'authors' => $authors ?? [],
  95. 'author' => $author ?? null,
  96. 'success' => $success ?? null,
  97. 'error' => $error ?? null
  98. ]);