| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /*
- * https://www.we-rc.com/blog/2015/07/19/nested-set-model-practical-examples-part-i
- */
- namespace App\Http\Controllers\Admin\Page;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use App\Http\Controllers\Controller;
- use App\Models\Menu;
- class MenuController extends Controller
- {
- private Menu $menuModel;
- public function __construct(Menu $menu)
- {
- $this->menuModel = $menu;
- }
- /**
- * 메뉴 관리
- * @method GET
- * @see /admin/page/menu
- */
- public function index()
- {
- $menuData = $this->menuModel->data();
- if ($menuData->rows > 0) {
- foreach ($menuData->list as $i => $row) {
- $row->upURL = route('admin.page.menu.up', $row->id);
- $row->downURL = route('admin.page.menu.down', $row->id);
- $menuData->list[$i] = $row;
- }
- }
- return view('admin.page.menu.index', [
- 'actionURL' => route('admin.page.menu.store'),
- 'menuData' => $menuData
- ]);
- }
- /**
- * 메뉴 관리 저장
- * @method POST
- * @see /admin/page/menu
- */
- public function store(Request $request)
- {
- $rules = [
- 'menu_id' => 'nullable|numeric',
- 'name' => 'string',
- 'link' => 'string',
- 'target' => 'string|nullable',
- 'desktop' => 'numeric|in:1,0',
- 'mobile' => 'numeric|in:1,0',
- 'custom' => 'string|nullable',
- 'icon' => 'string|nullable',
- 'depth' => 'numeric'
- ];
- $attributes = [
- 'menu_id' => '메뉴 PK',
- 'name' => '메뉴명',
- 'link' => 'URL',
- 'target' => '새창 여부',
- 'desktop' => 'PC',
- 'mobile' => '모바일',
- 'custom' => '속성',
- 'icon' => '아이콘',
- 'depth' => '분류 깊이'
- ];
- $posts = $this->validate($request, $rules, [], $attributes);
- $this->menuModel->register($posts);
- $message = '메뉴 정보가 저장되었습니다.';
- return redirect()->route('admin.page.menu.index')->with('message', $message);
- }
- /**
- * 메뉴 관리 수정
- * @method PUT
- * @see /admin/page/menu/{pk}
- */
- public function update(Request $request)
- {
- $posts = $request->all();
- if ($posts['chk']) {
- foreach ($posts['chk'] as $menuID) {
- $updateData = [
- 'menu_id' => $menuID,
- 'parent_id' => ($posts['parent_id'][$menuID] ?? ''),
- 'name' => ($posts['name'][$menuID] ?? ''),
- 'link' => ($posts['link'][$menuID] ?? ''),
- 'target' => ($posts['target'][$menuID] ?? ''),
- 'desktop' => ($posts['desktop'][$menuID] ?? ''),
- 'mobile' => ($posts['mobile'][$menuID] ?? ''),
- 'custom' => ($posts['custom'][$menuID] ?? ''),
- 'icon' => ($posts['icon'][$menuID] ?? ''),
- 'depth' => ($posts['depth'][$menuID] ?? '')
- ];
- $rules = [
- 'menu_id' => 'required|numeric|exists:tb_menu,id',
- 'parent_id' => 'required|numeric|exists:tb_menu,parent_id',
- 'name' => 'string',
- 'link' => 'string',
- 'target' => 'numeric|in:1,0',
- 'desktop' => 'numeric|in:1,0',
- 'mobile' => 'numeric|in:1,0',
- 'custom' => 'string|nullable',
- 'icon' => 'string|nullable',
- 'depth' => 'numeric'
- ];
- $attributes = [
- 'menu_id' => '메뉴 PK',
- 'parent_id' => '부모 메뉴 PK',
- 'name' => '메뉴명',
- 'link' => 'URL',
- 'target' => '새창 여부',
- 'desktop' => 'PC',
- 'mobile' => '모바일',
- 'custom' => '속성',
- 'icon' => '아이콘',
- 'depth' => '분류 깊이'
- ];
- $validator = Validator::make($updateData, $rules, [], $attributes);
- if ($validator->fails()) {
- return back()->withErrors($validator)->withInput();
- }
- $this->menuModel->updater($updateData);
- }
- }
- $message = '메뉴 정보가 변경되었습니다.';
- return redirect()->route('admin.page.menu.index')->with('message', $message);
- }
- /**
- * 메뉴 관리 삭제
- * @method DELETE
- * @see /admin/page/menu/destroy
- */
- public function destroy(Request $request)
- {
- $chk = $request->post('chk');
- if ($chk) {
- foreach ($chk as $menuID) {
- $this->menuModel->remove($menuID);
- }
- }
- $message = '메뉴 정보가 삭제되었습니다.';
- return redirect()->route('admin.page.menu.index')->with('message', $message);
- }
- /**
- * 메뉴 한 단계 위로
- * @method GET
- */
- public function up(int $menuID)
- {
- $this->menuModel->orderUp($menuID);
- return redirect()->route('admin.page.menu.index');
- }
- /**
- * 메뉴 한 단계 아래로
- * @method GET
- */
- public function down(int $menuID)
- {
- $this->menuModel->orderDown($menuID);
- return redirect()->route('admin.page.menu.index');
- }
- }
|