# Menu Module

## 1. Pengenalan

Module menu terbahagi kepada **tiga sub-sistem**:
- **Backend Menu** — Sidebar navigation untuk admin panel
- **Frontend Menu** — Navigation untuk public site
- **Menu Category** — Kategori untuk frontend menu grouping

## 2. Database

### Backend Menu

**Table:** `backend_menu`
| Column | Type | Description |
|--------|------|-------------|
| menu_id | int (PK) | Primary key |
| menu_name | string | Nama menu |
| menu_link | string | Route name link |
| menu_active_route | text | Multiple route names (JSON array) — untuk active state |
| menu_class | string (nullable) | CSS class / FontAwesome icon |
| menu_param | string (nullable) | Route parameters |
| menu_parent_id | int | Parent menu ID (default 0) |
| menu_status | boolean (nullable) | Active/inactive |

**Table:** `backend_role_mapping`
| Column | Type | Description |
|--------|------|-------------|
| id | int (PK) | Primary key |
| role_code | string | Role name |
| menu_id | int | Menu ID |
| parent_id | int | Parent mapping ID (default 0) |
| sort | int | Urutan |
| created_by | string | Creator username/ID |
| created_at | datetime | Tarikh cipta |
| updated_by | string (nullable) | Updater username/ID |
| updated_at | timestamp | Tarikh kemaskini |

### Frontend Menu

**Table:** `frontend_menu`
| Column | Type | Description |
|--------|------|-------------|
| menu_id | int (PK) | Primary key |
| menu_name | string | Nama menu |
| menu_link | string (nullable) | Route name |
| menu_icon | string (nullable) | Icon |
| menu_class | string (nullable) | CSS class |
| menu_descr | string (nullable) | Description |
| menu_img | string (nullable) | Image |
| menu_field_id | string (nullable) | Foreign key to content |
| menu_parent_id | int | Parent menu (default 0) |
| menu_status | boolean | Active/inactive (default 1) |
| timestamps | - | created_at, updated_at |

**Table:** `frontend_role_mapping`
| Column | Type | Description |
|--------|------|-------------|
| id | int (PK) | Primary key |
| role_code | string | Role name |
| category_id | int (FK) | Foreign key to `frontend_menu_category` |
| menu_group | string | Menu group |
| menu_id | int | Menu ID |
| parent_id | int (nullable) | Parent mapping |
| sort | int | Urutan |
| page_id | int (nullable) | Page reference |
| site_id | int (nullable) | Site reference |
| status | string (nullable) | Status filter |

**Table:** `frontend_menu_category`
| Column | Type | Description |
|--------|------|-------------|
| category_id | int (PK) | Primary key |
| category_name | string | Nama kategori |
| category_description | text (nullable) | Description |
| category_status | tinyint | Active/inactive |

## 3. Routes

### Backend Menu

| Method | URI | Permission | Name |
|--------|-----|------------|------|
| GET/POST | `/admin/backend-menu` | `backend-menu.view` | `backend-menu.index` |
| GET | `/admin/backend-menu/create` | `backend-menu.create` | `backend-menu.create` |
| POST | `/admin/backend-menu` | `backend-menu.create` | `backend-menu.store` |
| GET | `/admin/backend-menu/{id}/edit` | `backend-menu.update` | `backend-menu.edit` |
| PUT | `/admin/backend-menu/{id}` | `backend-menu.update` | `backend-menu.update` |
| DELETE | `/admin/backend-menu/{id}` | `backend-menu.delete` | `backend-menu.destroy` |
| POST | `/admin/backend-menu/delete` | `backend-menu.delete` | `backend-menu.delete` |
| GET/POST | `/admin/backend-menu/assign-menu` | `backend-menu.update` | `backend-menu.assign` |
| GET | `/admin/backend-menu/level/{id}/{role}/{pos}/{sort}` | `backend-menu.update` | `backend-menu.level` |
| GET | `/admin/backend-menu/stub` | `backend-menu.view` | `backend-menu.stub` |
| POST | `/admin/backend-menu/disabled/{id}` | `backend-menu.update` | `backend-menu.disabled` |

### Frontend Menu

| Method | URI | Permission | Name |
|--------|-----|------------|------|
| GET | `/admin/frontend-menu` | `frontend-menu.view` | `frontend-menu.index` |
| GET | `/admin/frontend-menu/create` | `frontend-menu.create` | `frontend-menu.create` |
| POST | `/admin/frontend-menu` | `frontend-menu.create` | `frontend-menu.store` |
| GET | `/admin/frontend-menu/{id}/edit` | `frontend-menu.update` | `frontend-menu.edit` |
| PUT | `/admin/frontend-menu/{id}` | `frontend-menu.update` | `frontend-menu.update` |
| DELETE | `/admin/frontend-menu/{id}` | `frontend-menu.delete` | `frontend-menu.destroy` |
| POST | `/admin/frontend-menu/delete` | `frontend-menu.delete` | `frontend-menu.delete` |
| GET/POST | `/admin/frontend-menu/assign-menu` | `frontend-menu.update` | `frontend-menu.assign` |
| GET | `/admin/frontend-menu/level/{id}/{role}/{pos}/{sort}` | `frontend-menu.update` | `frontend-menu.level` |
| GET | `/admin/frontend-menu/disabled/{id}` | `frontend-menu.update` | `frontend-menu.disabled` |
| GET | `/admin/frontend-menu/article-item` | `frontend-menu.view` | `frontend-menu.article-item` |
| GET | `/admin/frontend-menu/slider-item` | `frontend-menu.view` | `frontend-menu.slider-item` |

### Menu Category

| Method | URI | Permission | Name |
|--------|-----|------------|------|
| GET | `/admin/menu-category` | `menu-category.view` | `menu-category.index` |
| GET | `/admin/menu-category/create` | `menu-category.create` | `menu-category.create` |
| POST | `/admin/menu-category` | `menu-category.create` | `menu-category.store` |
| GET | `/admin/menu-category/{id}/edit` | `menu-category.update` | `menu-category.edit` |
| PUT | `/admin/menu-category/{id}` | `menu-category.update` | `menu-category.update` |
| DELETE | `/admin/menu-category/{id}` | `menu-category.delete` | `menu-category.destroy` |

## 4. Controllers

### `MenuController` (Backend)
- CRUD backend menu items
- Assign menu to roles — tree view with role selector, add submenu, reorder, toggle
- Level reordering (move up/down)
- Menu helper cache busting

### `FrontendMenuController` (Frontend)
- CRUD frontend menu items
- Assign menu to roles with params (category_id, menu_group, page_id, site_id, status)
- Article/Slider items endpoints for dynamic menu content

### `MenuCategoryController`
- Simple CRUD for frontend menu categories

## 5. Models

### Backend
- `Menu` — Table: `backend_menu`. Methods: dropdown(), listofmenu(), getRoute(), getRole(), roleMappings()
- `RoleMapping` — Table: `backend_role_mapping`. Method: nestedmenu() (recursive tree)

### Frontend
- `Menu` — Table: `frontend_menu`. Appends: page_id, menu_type, param, param_item. Methods: dropdown(), listofmenu(), getPage(), getPageList(), getSiteList(), getArticleList(), getImageSliderList(), getRoute(), getRole(), typeahead(), paramProvider(), roleMappings()
- `RoleMapping` — Table: `frontend_role_mapping`. Methods: nestedmenu(), getParam(), setBaseParam()
- `MenuCategory` — Table: `frontend_menu_category`

## 6. Views

### Backend
- `index.blade.php` — List menu items with tree structure
- `create.blade.php` / `edit.blade.php` — shared `form.blade.php` partial
- `form.blade.php` — Fields: menu_name, parent (select), menu_link (Select2 with routes), menu_active_route (multi-select), menu_class/icon, menu_param, status
- `assign.blade.php` — Tree view with role selector, add submenu modal, move up/down, delete, toggle
- `_tree_node.blade.php` — Recursive tree node partial

### Frontend
- `index.blade.php` / `create.blade.php` / `edit.blade.php`
- `form.blade.php` — Fields: menu_name, menu_parent_id, menu_link, status, menu_icon, menu_class
- `assign.blade.php` — Advanced assignment with category_id, menu_group, page_id, site_id, status
- `_tree_node.blade.php` — Recursive tree node partial

### Menu Category
- `index.blade.php` / `create.blade.php` / `edit.blade.php` / `show.blade.php`
- `form.blade.php` — Fields: category_name, category_description, category_status

## 7. Seeder — `BackendMenuSeeder`

Create full menu hierarchy:
- **Dashboard** — Home, Activity Log, Visitor
- **Backend** — User, Parameters, Roles & Permissions (Roles, Permissions, Auto Permissions, Routes), Menu (Assign Menu, Stub), Dashboard (Data Dashboard)
- **Frontend** — User, File Manager, Menu (Assign Menu)
- **Content** — Articles, Links, Documents, Photo Galleries, Slider Images, Images, Videos

Assign all menus to super-admin role with role mapping.

## 8. Permissions

| Permission | Description |
|------------|-------------|
| `backend-menu.view` | View backend menu |
| `backend-menu.create` | Create backend menu |
| `backend-menu.update` | Edit/assign/reorder backend menu |
| `backend-menu.delete` | Delete backend menu |
| `frontend-menu.view` | View frontend menu |
| `frontend-menu.create` | Create frontend menu |
| `frontend-menu.update` | Edit/assign/reorder frontend menu |
| `frontend-menu.delete` | Delete frontend menu |
| `menu-category.view` | View menu categories |
| `menu-category.create` | Create menu category |
| `menu-category.update` | Edit menu category |
| `menu-category.delete` | Delete menu category |
