CategoryController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board\Board;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Board;
  6. use App\Models\BoardMeta;
  7. use App\Models\BoardCategory;
  8. class CategoryController extends Controller
  9. {
  10. private Board $boardModel;
  11. private BoardMeta $boardMetaModel;
  12. private BoardCategory $boardCategoryModel;
  13. public function __construct(Board $board, BoardMeta $boardMeta, BoardCategory $boardCategory)
  14. {
  15. $this->boardModel = $board;
  16. $this->boardMetaModel = $boardMeta;
  17. $this->boardCategoryModel = $boardCategory;
  18. }
  19. /**
  20. * 게시판 수정 - 분류
  21. * @method GET
  22. * @see /admin/board/board/category/{pk}
  23. */
  24. public function show(int $boardID)
  25. {
  26. return view('admin.board.board.category', [
  27. 'boardID' => $boardID,
  28. // 게시판 분류
  29. 'boardCategoryData' => $this->boardCategoryModel->getCategoryList($boardID),
  30. // 게시판 정보
  31. 'boardData' => $this->boardModel->find($boardID),
  32. // 게시판 메타 정보
  33. 'boardMetaData' => $this->boardMetaModel->getAllMeta($boardID),
  34. // 모든 게시판
  35. 'boardAllData' => $this->boardModel->all()
  36. ]);
  37. }
  38. /**
  39. * 게시판 그룹 분류 등록 저장
  40. * @method POST
  41. * @see /admin/board/board/category/{pk}
  42. */
  43. public function store(Request $request)
  44. {
  45. $rules = [
  46. 'board_id' => 'required|numeric|exists:tb_board,id',
  47. 'name' => 'required|string',
  48. 'depth' => 'required|numeric|min:0',
  49. ];
  50. $attributes = [
  51. 'board_id' => '게시판 수정 - 분류',
  52. 'category_id' => '분류 PK',
  53. 'parent_id' => '분류 부모 PK',
  54. 'name' => '분류 명',
  55. 'depth' => '분류 깊이',
  56. ];
  57. $type = $request->post('type');
  58. if($type == 'modify') { // 수정
  59. $rules['board_category_id'] = 'required|numeric|exists:tb_board_category,id';
  60. $rules['parent_id'] = 'numeric|nullable|exists:tb_board_category,parent_id';
  61. }else{ // 입력
  62. $rules['parent_id'] = 'required|numeric|min:0';
  63. }
  64. $posts = $this->validate($request, $rules, [], $attributes);
  65. $boardID = $posts['board_id'];
  66. $parentID = ($posts['parent_id'] ?? null);
  67. $today = now();
  68. $saveData = [
  69. 'board_id' => $boardID,
  70. 'parent_id' => $parentID,
  71. 'name' => $posts['name'],
  72. 'depth' => $posts['depth']
  73. ];
  74. if($type == 'add') {
  75. $code = $this->boardCategoryModel->nextKey($parentID, $boardID);
  76. $saveData['code'] = $code;
  77. $saveData['created_at'] = $today;
  78. $this->boardCategoryModel->register($saveData);
  79. $message = '게시판 분류가 새로 등록되었습니다.';
  80. }else{
  81. $saveData['id'] = $posts['board_category_id'];
  82. $saveData['updated_at'] = $today;
  83. $this->boardCategoryModel->updater($saveData);
  84. $message = '게시판 분류가 수정 되었습니다.';
  85. }
  86. return redirect()->route('admin.board.board.category.show', $boardID)->with('message', $message);
  87. }
  88. /**
  89. * 게시판 그룹 분류 삭제
  90. * @method DELETE
  91. * @see /admin/board/board/category/{pk}/destroy
  92. */
  93. public function destroy(Request $request)
  94. {
  95. $boardID = $request->post('board_id');
  96. $chk = $request->post('chk');
  97. if ($chk) {
  98. foreach ($chk as $boardCategoryID) {
  99. $this->boardCategoryModel->remove($boardCategoryID);
  100. }
  101. }
  102. $message = '게시판 분류 정보가 삭제되었습니다.';
  103. return redirect()->route('admin.board.board.category.show', $boardID)->with('message', $message);
  104. }
  105. /**
  106. * 분류 한 단계 위로
  107. */
  108. public function up(int $boardCategoryID)
  109. {
  110. $boardID = $this->boardCategoryModel->find($boardCategoryID)->getOriginal('board_id');
  111. if($boardID) {
  112. $this->boardCategoryModel->orderUp($boardCategoryID);
  113. return redirect()->route('admin.board.board.category.show', $boardID);
  114. }else{
  115. return back();
  116. }
  117. }
  118. /**
  119. * 분류 한 단계 아래로
  120. */
  121. public function down(int $boardCategoryID)
  122. {
  123. $boardID = $this->boardCategoryModel->find($boardCategoryID)->getOriginal('board_id');
  124. if($boardID) {
  125. $this->boardCategoryModel->orderDown($boardCategoryID);
  126. return redirect()->route('admin.board.board.category.show', $boardID);
  127. }else{
  128. return back();
  129. }
  130. }
  131. }