CommentController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board\Board;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Board;
  7. use App\Models\BoardMeta;
  8. use Exception;
  9. class CommentController extends Controller
  10. {
  11. private Board $boardModel;
  12. private BoardMeta $boardMetaModel;
  13. public function __construct(Board $board, BoardMeta $boardMeta)
  14. {
  15. $this->boardModel = $board;
  16. $this->boardMetaModel = $boardMeta;
  17. }
  18. /**
  19. * 게시판 수정 - 댓글
  20. * @method GET
  21. * @see /admin/board/board/comment/{pk}
  22. */
  23. public function show(int $boardID)
  24. {
  25. return view('admin.board.board.comment', [
  26. 'boardID' => $boardID,
  27. 'actionURL' => route('admin.board.board.comment.store'),
  28. // 게시판 정보
  29. 'boardData' => $this->boardModel->find($boardID),
  30. // 게시판 메타 정보
  31. 'boardMetaData' => $this->boardMetaModel->getAllMeta($boardID),
  32. // 모든 게시판
  33. 'boardAllData' => $this->boardModel->all()
  34. ]);
  35. }
  36. /**
  37. * 게시판 수정 - 작성 저장
  38. * @method POST
  39. * @see /admin/board/board/comment/{pk}
  40. */
  41. public function store(Request $request)
  42. {
  43. $rules = [
  44. 'board_id' => 'required|numeric|exists:tb_board,id',
  45. 'use_comment' => 'required|numeric',
  46. 'comment_per_page' => 'required|numeric|min:0|max:20',
  47. 'comment_page_count' => 'required|numeric|min:0|max:10',
  48. 'use_comment_like' => 'required|numeric',
  49. 'use_comment_dislike' => 'required|numeric',
  50. 'show_user_thumb_in_comment' => 'required|numeric|in:0,1',
  51. 'show_user_icon_in_comment' => 'required|numeric|in:0,1',
  52. 'update_order_on_comment' => 'required|numeric',
  53. 'comment_default_content' => 'string|nullable|max:1000',
  54. 'comment_min_length' => 'required|numeric|min:0',
  55. 'comment_max_length' => 'required|numeric|min:0|max:99999999',
  56. 'use_comment_secret' => 'nullable|numeric|in:1,2',
  57. 'use_comment_secret_selected' => 'required|numeric|in:0,1',
  58. 'show_comment_ip' => 'nullable|numeric',
  59. 'use_comment_blame' => 'required|numeric|in:0,1',
  60. 'comment_blame_blind_count' => 'required|numeric|min:0',
  61. 'protect_delete_comment' => 'required|numeric|in:0,1',
  62. 'protect_update_comment' => 'required|numeric|in:0,1',
  63. 'grp' => 'array',
  64. 'all' => 'array'
  65. ];
  66. $attributes = [
  67. 'board_id' => '게시판 PK',
  68. 'use_comment' => '댓글 사용 여부',
  69. 'comment_per_page' => '댓글 목록 수',
  70. 'comment_page_count' => '댓글 페이지 수',
  71. 'use_comment_like' => '댓글 추천 기능',
  72. 'use_comment_dislike' => '댓글 비추천 기능',
  73. 'show_user_thumb_in_comment' => '회원 프로필 이미지',
  74. 'show_user_icon_in_comment' => '회원 아이콘 이미지',
  75. 'update_order_on_comment' => '댓글 작성시 글 수정 시각 갱신',
  76. 'comment_default_content' => '댓글 기본 내용',
  77. 'comment_min_length' => '최소 글수 제한',
  78. 'comment_max_length' => '최대 글수 제한',
  79. 'use_comment_secret' => '비밀글 사용',
  80. 'use_comment_secret_selected' => '비밀글 기본 선택',
  81. 'show_comment_ip' => 'IP 보이기',
  82. 'use_comment_blame' => '댓글 신고 기능',
  83. 'comment_blame_blind_count' => '댓글 신고 시 숨김',
  84. 'protect_delete_comment' => '댓글 보호 기능 (삭제 시)',
  85. 'protect_update_comment' => '댓글 보호 기능 (수정 시)',
  86. 'grp' => '그룹 적용',
  87. 'all' => '전체 적용'
  88. ];
  89. $posts = $this->validate($request, $rules, [], $attributes);
  90. $boardID = $posts['board_id'];
  91. $defaultData = [ // 그룹, 전체 적용 값
  92. 'use_comment', 'comment_per_page', 'comment_page_count', 'use_comment_like', 'use_comment_dislike',
  93. 'show_user_thumb_in_comment', 'show_user_icon_in_comment', 'update_order_on_comment',
  94. 'comment_default_content', 'comment_min_length', 'comment_max_length',
  95. 'use_comment_secret', 'use_comment_secret_selected', 'show_comment_ip',
  96. 'use_comment_blame', 'comment_blame_blind_count', 'protect_delete_comment', 'protect_update_comment'
  97. ];
  98. // 게시판 정보 조회
  99. $boardData = $this->boardModel->find($boardID);
  100. // 메타 정보 저장
  101. $metaData = [
  102. 'use_comment' => $posts['use_comment'],
  103. 'comment_per_page' => $posts['comment_per_page'],
  104. 'comment_page_count' => $posts['comment_page_count'],
  105. 'use_comment_like' => $posts['use_comment_like'],
  106. 'use_comment_dislike' => $posts['use_comment_dislike'],
  107. 'show_user_thumb_in_comment' => $posts['show_user_thumb_in_comment'],
  108. 'show_user_icon_in_comment' => $posts['show_user_icon_in_comment'],
  109. 'update_order_on_comment' => $posts['update_order_on_comment'],
  110. 'comment_default_content' => $posts['comment_default_content'],
  111. 'comment_min_length' => $posts['comment_min_length'],
  112. 'comment_max_length' => $posts['comment_max_length'],
  113. 'use_comment_secret' => $posts['use_comment_secret'],
  114. 'use_comment_secret_selected' => $posts['use_comment_secret_selected'],
  115. 'show_comment_ip' => $posts['show_comment_ip'],
  116. 'use_comment_blame' => $posts['use_comment_blame'],
  117. 'comment_blame_blind_count' => $posts['comment_blame_blind_count'],
  118. 'protect_delete_comment' => $posts['protect_delete_comment'],
  119. 'protect_update_comment' => $posts['protect_update_comment']
  120. ];
  121. DB::beginTransaction();
  122. try
  123. {
  124. $this->boardMetaModel->save($boardID, $metaData);
  125. /*
  126. * 그룹, 전체 적용
  127. */
  128. $groupData = []; // 그룹적용
  129. $allData = []; // 전체적용
  130. foreach ($defaultData as $field) {
  131. if (isset($posts['grp'][$field])) {
  132. $groupData[$field] = $posts[$field];
  133. }
  134. if (isset($posts['all'][$field])) {
  135. $allData[$field] = $posts[$field];
  136. }
  137. }
  138. if ($groupData) {
  139. $brdGroupData = $this->boardModel->findByGroup($boardData->board_group_id);
  140. foreach ($brdGroupData as $bKey => $bVal) {
  141. if ($bVal->id === $boardID) {
  142. continue;
  143. }
  144. $this->boardMetaModel->save($bVal->id, $groupData);
  145. }
  146. }
  147. if ($allData) {
  148. $brdAllData = $this->boardModel->all();
  149. foreach ($brdAllData as $bKey => $bVal) {
  150. if ($bVal->id === $boardID) {
  151. continue;
  152. }
  153. $this->boardMetaModel->save($bVal->id, $allData);
  154. }
  155. }
  156. $message = '게시판 - 댓글 정보가 저장되었습니다.';
  157. DB::commit();
  158. } catch (Exception $e) {
  159. $message = $e->getMessage();
  160. DB::rollBack();
  161. }
  162. return redirect()->route('admin.board.board.comment.show', $boardID)->with('message', $message);
  163. }
  164. }