HomeController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Classe HomeController
  4. *
  5. * Gère les actions liées à la page d'accueil.
  6. */
  7. if (!class_exists('HomeController')) {
  8. class HomeController
  9. {
  10. /**
  11. * Affiche la page d'accueil.
  12. */
  13. public static function index()
  14. {
  15. $data = [
  16. 'title' => 'Bienvenue dans l\'application',
  17. 'content' => 'Ceci est le contenu principal de la page d\'accueil.',
  18. 'loaded_map' => null,
  19. 'map_data' => null
  20. ];
  21. // Vérifier si une carte doit être chargée
  22. if (isset($_GET['load']) && is_numeric($_GET['load'])) {
  23. $mapId = (int) $_GET['load'];
  24. $map = MapModel::findMap($mapId);
  25. if ($map) {
  26. $data['loaded_map'] = $map;
  27. $data['map_data'] = [
  28. 'id' => $mapId,
  29. 'name' => $map->getName(),
  30. 'width' => $map->getWidth(),
  31. 'height' => $map->getHeight(),
  32. 'created_at' => $map->getCreatedAt(),
  33. 'updated_at' => $map->getUpdatedAt(),
  34. 'statistics' => $map->getStatistics()
  35. ];
  36. $data['title'] = $map->getName() . ' - Map Generator';
  37. } else {
  38. $data['load_error'] = 'Carte non trouvée (ID: ' . $mapId . '). Vérifiez que l\'ID est correct.';
  39. }
  40. }
  41. Renderer::render('home', $data);
  42. }
  43. /**
  44. * Affiche le formulaire de création d'un nouveau projet.
  45. */
  46. public static function newProject()
  47. {
  48. $data = [
  49. 'title' => 'Nouveau projet - Map Generator',
  50. 'page' => 'new_project'
  51. ];
  52. Renderer::render('projects/new', $data);
  53. }
  54. /**
  55. * Crée un nouveau projet (endpoint POST)
  56. * Accepte form POST classique ou JSON
  57. */
  58. public static function createProject()
  59. {
  60. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  61. http_response_code(405);
  62. echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
  63. return;
  64. }
  65. $contentType = $_SERVER['CONTENT_TYPE'] ?? '';
  66. if (strpos($contentType, 'application/json') !== false) {
  67. $raw = file_get_contents('php://input');
  68. $input = json_decode($raw, true) ?: [];
  69. } else {
  70. $input = $_POST;
  71. }
  72. $name = isset($input['name']) ? trim($input['name']) : '';
  73. $description = $input['description'] ?? null;
  74. $template = $input['template'] ?? null;
  75. if (!$name) {
  76. http_response_code(400);
  77. echo json_encode(['success' => false, 'message' => 'Le nom est requis']);
  78. return;
  79. }
  80. // Si un radius est envoyé, créer la map via radius
  81. $radius = isset($input['radius']) ? (int) $input['radius'] : null;
  82. $map = null;
  83. if ($radius && $radius > 0) {
  84. $map = Map::fromRadius($name, $radius);
  85. } else {
  86. // Fallback: utiliser width/height
  87. $width = isset($input['width']) ? (int) $input['width'] : 10;
  88. $height = isset($input['height']) ? (int) $input['height'] : 10;
  89. $map = new Map($name, $width, $height);
  90. }
  91. $created = MapModel::create($map, $description);
  92. // Détecter AJAX
  93. $isAjax = false;
  94. $xrw = $_SERVER['HTTP_X_REQUESTED_WITH'] ?? '';
  95. if (strtolower($xrw) === 'xmlhttprequest' || strpos($contentType, 'application/json') !== false) {
  96. $isAjax = true;
  97. }
  98. if ($created) {
  99. if ($isAjax) {
  100. echo json_encode(['success' => true, 'id' => $created]);
  101. } else {
  102. header('Location: /projects');
  103. }
  104. } else {
  105. if ($isAjax) {
  106. http_response_code(500);
  107. echo json_encode(['success' => false, 'message' => 'Erreur lors de la création']);
  108. } else {
  109. header('Location: /projects');
  110. }
  111. }
  112. }
  113. /**
  114. * Affiche la liste des projets (cartes) disponibles.
  115. */
  116. public static function projects()
  117. {
  118. // Récupérer la liste des cartes depuis la base de données
  119. $maps = MapModel::paginate(1, 20)['data']; // Récupérer les 20 premières cartes
  120. // Récupérer les statistiques globales
  121. $stats = MapModel::getGlobalStats();
  122. $data = [
  123. 'title' => 'Mes projets - Map Generator',
  124. 'page' => 'projects',
  125. 'maps' => $maps,
  126. 'stats' => $stats
  127. ];
  128. Renderer::render('projects/index', $data);
  129. }
  130. /**
  131. * Supprime une carte (endpoint POST)
  132. * Accepte JSON (AJAX) ou form POST standard.
  133. */
  134. public static function deleteProject()
  135. {
  136. // Méthode attendue: POST
  137. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  138. http_response_code(405);
  139. echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
  140. return;
  141. }
  142. // Lire input JSON si présent
  143. $input = [];
  144. $contentType = $_SERVER['CONTENT_TYPE'] ?? '';
  145. if (strpos($contentType, 'application/json') !== false) {
  146. $raw = file_get_contents('php://input');
  147. $input = json_decode($raw, true) ?: [];
  148. } else {
  149. $input = $_POST;
  150. }
  151. $id = isset($input['id']) ? (int) $input['id'] : null;
  152. if (!$id) {
  153. http_response_code(400);
  154. echo json_encode(['success' => false, 'message' => 'ID manquant']);
  155. return;
  156. }
  157. // Vérifier existence
  158. if (!MapModel::exists($id)) {
  159. http_response_code(404);
  160. echo json_encode(['success' => false, 'message' => 'Carte non trouvée']);
  161. return;
  162. }
  163. $deleted = MapModel::delete($id);
  164. // Détecter si la requête est AJAX
  165. $isAjax = false;
  166. $xrw = $_SERVER['HTTP_X_REQUESTED_WITH'] ?? '';
  167. if (strtolower($xrw) === 'xmlhttprequest' || strpos($contentType, 'application/json') !== false) {
  168. $isAjax = true;
  169. }
  170. if ($deleted) {
  171. if ($isAjax) {
  172. echo json_encode(['success' => true]);
  173. } else {
  174. // Redirection pour les formulaires classiques
  175. header('Location: /projects');
  176. }
  177. } else {
  178. if ($isAjax) {
  179. http_response_code(500);
  180. echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression']);
  181. } else {
  182. // Redirection avec message d'erreur (simple)
  183. header('Location: /projects');
  184. }
  185. }
  186. }
  187. }
  188. }