MenuController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /*
  3. * https://www.we-rc.com/blog/2015/07/19/nested-set-model-practical-examples-part-i
  4. */
  5. namespace App\Http\Controllers\Admin\Page;
  6. use App\Models\Board;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Validator;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Menu;
  11. class MenuController extends Controller
  12. {
  13. private Menu $menuModel;
  14. private Board $boardModel;
  15. public function __construct(Menu $menu, Board $board)
  16. {
  17. $this->menuModel = $menu;
  18. $this->boardModel = $board;
  19. }
  20. /**
  21. * 메뉴 관리
  22. * @method GET
  23. * @see /admin/page/menu
  24. */
  25. public function index()
  26. {
  27. $menuData = $this->menuModel->data();
  28. if ($menuData->rows > 0) {
  29. foreach ($menuData->list as $i => $row) {
  30. $row->upURL = route('admin.page.menu.up', $row->id);
  31. $row->downURL = route('admin.page.menu.down', $row->id);
  32. $menuData->list[$i] = $row;
  33. }
  34. }
  35. // 게시판 목록 조회
  36. $boardList = $this->boardModel->all();
  37. return view('admin.page.menu.index', [
  38. 'actionURL' => route('admin.page.menu.store'),
  39. 'menuData' => $menuData,
  40. 'boardList' => $boardList
  41. ]);
  42. }
  43. /**
  44. * 메뉴 관리 저장
  45. * @method POST
  46. * @see /admin/page/menu
  47. */
  48. public function store(Request $request)
  49. {
  50. $rules = [
  51. 'menu_id' => 'nullable|numeric',
  52. 'board_id' => 'nullable|numeric|exists:tb_board,id',
  53. 'name' => 'string',
  54. 'link' => 'string',
  55. 'target' => 'string|nullable',
  56. 'desktop' => 'numeric|in:1,0',
  57. 'mobile' => 'numeric|in:1,0',
  58. 'custom' => 'string|nullable',
  59. 'icon' => 'string|nullable',
  60. 'depth' => 'numeric'
  61. ];
  62. $attributes = [
  63. 'menu_id' => '메뉴 PK',
  64. 'board_id' => '게시판 PK',
  65. 'name' => '메뉴명',
  66. 'link' => 'URL',
  67. 'target' => '새창 여부',
  68. 'desktop' => 'PC',
  69. 'mobile' => '모바일',
  70. 'custom' => '속성',
  71. 'icon' => '아이콘',
  72. 'depth' => '분류 깊이'
  73. ];
  74. $posts = $this->validate($request, $rules, [], $attributes);
  75. $this->menuModel->register($posts);
  76. $message = '메뉴 정보가 저장되었습니다.';
  77. return redirect()->route('admin.page.menu.index')->with('message', $message);
  78. }
  79. /**
  80. * 메뉴 관리 수정
  81. * @method PUT
  82. * @see /admin/page/menu/{pk}
  83. */
  84. public function update(Request $request)
  85. {
  86. $posts = $request->all();
  87. if ($posts['chk']) {
  88. foreach ($posts['chk'] as $menuID) {
  89. $updateData = [
  90. 'menu_id' => $menuID,
  91. 'parent_id' => ($posts['parent_id'][$menuID] ?? ''),
  92. 'board_id' => ($posts['board_id'][$menuID] ?? ''),
  93. 'name' => ($posts['name'][$menuID] ?? ''),
  94. 'link' => ($posts['link'][$menuID] ?? ''),
  95. 'target' => ($posts['target'][$menuID] ?? ''),
  96. 'desktop' => ($posts['desktop'][$menuID] ?? ''),
  97. 'mobile' => ($posts['mobile'][$menuID] ?? ''),
  98. 'custom' => ($posts['custom'][$menuID] ?? ''),
  99. 'icon' => ($posts['icon'][$menuID] ?? ''),
  100. 'depth' => ($posts['depth'][$menuID] ?? '')
  101. ];
  102. $rules = [
  103. 'menu_id' => 'required|numeric|exists:tb_menu,id',
  104. 'parent_id' => 'required|numeric|exists:tb_menu,parent_id',
  105. 'board_id' => 'required|numeric|exists:tb_board,id',
  106. 'name' => 'string',
  107. 'link' => 'string',
  108. 'target' => 'numeric|in:1,0',
  109. 'desktop' => 'numeric|in:1,0',
  110. 'mobile' => 'numeric|in:1,0',
  111. 'custom' => 'string|nullable',
  112. 'icon' => 'string|nullable',
  113. 'depth' => 'numeric'
  114. ];
  115. $attributes = [
  116. 'menu_id' => '메뉴 PK',
  117. 'parent_id' => '부모 메뉴 PK',
  118. 'board_id' => '게시판 PK',
  119. 'name' => '메뉴명',
  120. 'link' => 'URL',
  121. 'target' => '새창 여부',
  122. 'desktop' => 'PC',
  123. 'mobile' => '모바일',
  124. 'custom' => '속성',
  125. 'icon' => '아이콘',
  126. 'depth' => '분류 깊이'
  127. ];
  128. $validator = Validator::make($updateData, $rules, [], $attributes);
  129. if ($validator->fails()) {
  130. return back()->withErrors($validator)->withInput();
  131. }
  132. $this->menuModel->updater($updateData);
  133. }
  134. }
  135. $message = '메뉴 정보가 변경되었습니다.';
  136. return redirect()->route('admin.page.menu.index')->with('message', $message);
  137. }
  138. /**
  139. * 메뉴 관리 삭제
  140. * @method DELETE
  141. * @see /admin/page/menu/destroy
  142. */
  143. public function destroy(Request $request)
  144. {
  145. $chk = $request->post('chk');
  146. if ($chk) {
  147. foreach ($chk as $menuID) {
  148. $this->menuModel->remove($menuID);
  149. }
  150. }
  151. $message = '메뉴 정보가 삭제되었습니다.';
  152. return redirect()->route('admin.page.menu.index')->with('message', $message);
  153. }
  154. /**
  155. * 메뉴 한 단계 위로
  156. * @method GET
  157. */
  158. public function up(int $menuID)
  159. {
  160. $this->menuModel->orderUp($menuID);
  161. return redirect()->route('admin.page.menu.index');
  162. }
  163. /**
  164. * 메뉴 한 단계 아래로
  165. * @method GET
  166. */
  167. public function down(int $menuID)
  168. {
  169. $this->menuModel->orderDown($menuID);
  170. return redirect()->route('admin.page.menu.index');
  171. }
  172. }