Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

212 wiersze
6.0KB

  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. 'cardsJson' => $cardsJson,
  101. ]);
  102. }
  103. public function edit(string $slug): mixed
  104. {
  105. if ($guard = AuthService::requireLogin()) {
  106. return $guard;
  107. }
  108. $board = $this->boards()->findBySlug($slug);
  109. if ($board === null) {
  110. return \Core\Response::notFound('Board not found.');
  111. }
  112. return $this->view('boards.edit', [
  113. 'pageTitle' => 'Edit Board',
  114. 'board' => $board,
  115. ]);
  116. }
  117. public function update(Request $request, string $slug): mixed
  118. {
  119. if ($guard = AuthService::requireLogin()) {
  120. return $guard;
  121. }
  122. $board = $this->boards()->findBySlug($slug);
  123. if ($board === null) {
  124. return \Core\Response::notFound('Board not found.');
  125. }
  126. $newName = trim((string) $request->input('name', ''));
  127. if ($newName === '') {
  128. return $this->view('boards.edit', [
  129. 'pageTitle' => 'Edit Board',
  130. 'board' => $board,
  131. 'error' => 'Board name is required.',
  132. ]);
  133. }
  134. $board->name = $newName;
  135. $board->slug = $this->boards()->uniqueSlug($this->generateSlug($newName), $board->id);
  136. $board->importFromPrintstream = $request->input('import_from_printstream') === 'on';
  137. $board->printstreamJobName = trim((string) $request->input('printstream_job_name', ''));
  138. $board->updatedAt = date('Y-m-d H:i:s');
  139. $board->updatedBy = AuthService::getCurrentUsername();
  140. $this->boards()->update($board);
  141. return $this->redirect('/board/' . $board->slug);
  142. }
  143. public function destroy(string $slug): mixed
  144. {
  145. if ($guard = AuthService::requireLogin()) {
  146. return $guard;
  147. }
  148. $board = $this->boards()->findBySlug($slug);
  149. if ($board === null) {
  150. return $this->redirect('/boards');
  151. }
  152. $this->cardMovements()->deleteByBoardId($board->id);
  153. $this->cards()->deleteByBoardId($board->id);
  154. $this->columns()->deleteByBoardId($board->id);
  155. $this->lanes()->deleteByBoardId($board->id);
  156. $this->boards()->delete($board->id);
  157. return $this->redirect('/boards');
  158. }
  159. private function generateSlug(string $text): string
  160. {
  161. $slug = strtolower($text);
  162. $slug = preg_replace('/[^a-z0-9\s-]/', '', $slug) ?? $slug;
  163. $slug = preg_replace('/[\s-]+/', '-', trim($slug)) ?? $slug;
  164. return trim($slug, '-');
  165. }
  166. }

Powered by TurnKey Linux.