MenuController.php 5.5 KB

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