Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

213 lines
6.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\Board;
  5. use App\Repositories\BoardColumnRepository;
  6. use App\Repositories\BoardRepository;
  7. use App\Repositories\CardMovementRepository;
  8. use App\Repositories\CardRepository;
  9. use App\Repositories\SwimLaneRepository;
  10. use App\Services\AuthService;
  11. use Core\Controller;
  12. use Core\Request;
  13. class BoardsController extends Controller
  14. {
  15. private function boards(): BoardRepository
  16. {
  17. return new BoardRepository(database());
  18. }
  19. private function columns(): BoardColumnRepository
  20. {
  21. return new BoardColumnRepository(database());
  22. }
  23. private function lanes(): SwimLaneRepository
  24. {
  25. return new SwimLaneRepository(database());
  26. }
  27. private function cards(): CardRepository
  28. {
  29. return new CardRepository(database());
  30. }
  31. private function cardMovements(): CardMovementRepository
  32. {
  33. return new CardMovementRepository(database());
  34. }
  35. public function index(): mixed
  36. {
  37. if ($guard = AuthService::requireLogin()) {
  38. return $guard;
  39. }
  40. $boards = $this->boards()->getAll();
  41. return $this->view('boards.index', [
  42. 'pageTitle' => 'Boards',
  43. 'boards' => $boards,
  44. ]);
  45. }
  46. public function create(): mixed
  47. {
  48. if ($guard = AuthService::requireLogin()) {
  49. return $guard;
  50. }
  51. return $this->view('boards.create', ['pageTitle' => 'New Board']);
  52. }
  53. public function store(Request $request): mixed
  54. {
  55. if ($guard = AuthService::requireLogin()) {
  56. return $guard;
  57. }
  58. $name = trim((string) $request->input('name', ''));
  59. if ($name === '') {
  60. return $this->view('boards.create', [
  61. 'pageTitle' => 'New Board',
  62. 'error' => 'Board name is required.',
  63. 'old' => $request->all(),
  64. ]);
  65. }
  66. $username = AuthService::getCurrentUsername();
  67. $now = date('Y-m-d H:i:s');
  68. $board = new Board();
  69. $board->name = $name;
  70. $board->slug = $this->boards()->uniqueSlug($this->generateSlug($name));
  71. $board->importFromPrintstream = $request->input('import_from_printstream') === 'on';
  72. $board->printstreamJobName = trim((string) $request->input('printstream_job_name', ''));
  73. $board->createdAt = $now;
  74. $board->createdBy = $username;
  75. $board->updatedAt = $now;
  76. $board->updatedBy = $username;
  77. $this->boards()->insert($board);
  78. return $this->redirect('/board/' . $board->slug);
  79. }
  80. public function show(string $slug): mixed
  81. {
  82. if ($guard = AuthService::requireLogin()) {
  83. return $guard;
  84. }
  85. $board = $this->boards()->findBySlug($slug);
  86. if ($board === null) {
  87. return \Core\Response::notFound('Board not found.');
  88. }
  89. $columns = $this->columns()->findByBoardId($board->id);
  90. $lanes = $this->lanes()->findByBoardId($board->id);
  91. $allCards = $this->cards()->findByBoardId($board->id);
  92. $cardsJson = json_encode(
  93. array_map(fn($c) => $c->toJsonArray(), $allCards),
  94. JSON_THROW_ON_ERROR
  95. );
  96. return $this->fragment('boards.show', [
  97. 'board' => $board,
  98. 'columns' => $columns,
  99. 'lanes' => $lanes,
  100. 'cardCount' => count($allCards),
  101. 'cardsJson' => $cardsJson,
  102. ]);
  103. }
  104. public function edit(string $slug): mixed
  105. {
  106. if ($guard = AuthService::requireLogin()) {
  107. return $guard;
  108. }
  109. $board = $this->boards()->findBySlug($slug);
  110. if ($board === null) {
  111. return \Core\Response::notFound('Board not found.');
  112. }
  113. return $this->view('boards.edit', [
  114. 'pageTitle' => 'Edit Board',
  115. 'board' => $board,
  116. ]);
  117. }
  118. public function update(Request $request, string $slug): mixed
  119. {
  120. if ($guard = AuthService::requireLogin()) {
  121. return $guard;
  122. }
  123. $board = $this->boards()->findBySlug($slug);
  124. if ($board === null) {
  125. return \Core\Response::notFound('Board not found.');
  126. }
  127. $newName = trim((string) $request->input('name', ''));
  128. if ($newName === '') {
  129. return $this->view('boards.edit', [
  130. 'pageTitle' => 'Edit Board',
  131. 'board' => $board,
  132. 'error' => 'Board name is required.',
  133. ]);
  134. }
  135. $board->name = $newName;
  136. $board->slug = $this->boards()->uniqueSlug($this->generateSlug($newName), $board->id);
  137. $board->importFromPrintstream = $request->input('import_from_printstream') === 'on';
  138. $board->printstreamJobName = trim((string) $request->input('printstream_job_name', ''));
  139. $board->updatedAt = date('Y-m-d H:i:s');
  140. $board->updatedBy = AuthService::getCurrentUsername();
  141. $this->boards()->update($board);
  142. return $this->redirect('/board/' . $board->slug);
  143. }
  144. public function destroy(string $slug): mixed
  145. {
  146. if ($guard = AuthService::requireLogin()) {
  147. return $guard;
  148. }
  149. $board = $this->boards()->findBySlug($slug);
  150. if ($board === null) {
  151. return $this->redirect('/boards');
  152. }
  153. $this->cardMovements()->deleteByBoardId($board->id);
  154. $this->cards()->deleteByBoardId($board->id);
  155. $this->columns()->deleteByBoardId($board->id);
  156. $this->lanes()->deleteByBoardId($board->id);
  157. $this->boards()->delete($board->id);
  158. return $this->redirect('/boards');
  159. }
  160. private function generateSlug(string $text): string
  161. {
  162. $slug = strtolower($text);
  163. $slug = preg_replace('/[^a-z0-9\s-]/', '', $slug) ?? $slug;
  164. $slug = preg_replace('/[\s-]+/', '-', trim($slug)) ?? $slug;
  165. return trim($slug, '-');
  166. }
  167. }

Powered by TurnKey Linux.