You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

164 lines
5.7 KiB

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\{Menu, RoleMenu};
use Illuminate\Support\Facades\Validator;
class MenuController extends Controller
{
public function add(Request $request)
{
$validator = Validator::make($request->all(), [
'parent_id' => 'nullable',
'icon' => 'required',
'name' => 'required|string',
'alias_name' => 'nullable',
'url' => 'required',
'sequence' => 'required',
'created_by' => 'nullable|string|max:80',
'updated_by' => 'nullable|string|max:80',
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
}
$validated = $validator->validated();
$validated['created_by'] = $this->currentName;
Menu::create($validated);
return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200], 200);
}
public function edit($id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$result = Menu::find($id);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get data menu, please try again later!', 'code' => 400], 400);
}
}
public function update(Request $request, $id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$data = Menu::find($id);
$validator = Validator::make($request->all(), [
'parent_id' => '',
'icon' => 'required',
'name' => 'required|string',
'alias_name' => '',
'url' => 'required',
'sequence' => 'required',
'created_by' => '',
'updated_by' => '',
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'data menu failed updated!', 'code' => 400], 400);
}
$validated = $validator->validated();
if ($data) {
$result = $data->update($validated);
if ($result) :
return response()->json(['status' => 'success', 'message' => 'data menu successfully updated!', 'code' => 200], 200);
else :
return response()->json(['status' => 'failed', 'message' => 'data menu failed updated!', 'code' => 400], 400);
endif;
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
}
}
public function delete($id)
{
$data = Menu::find($id);
if ($data) {
$delete = $data->delete();
if ($delete) :
return response()->json(['status' => 'success', 'message' => 'data menu successfully deleted!', 'code' => 200], 200);
else :
return response()->json(['status' => 'failed', 'message' => 'data menu failed deleted!', 'code' => 400], 400);
endif;
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_menu');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
public function listMenu($id)
{
$dataParent = RoleMenu::query()
->select("m_menu.*")
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->whereNull('m_menu.parent_id')
->where('m_roles.id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$finalData = [];
foreach ($dataParent as $data) {
$data->children = $this->getChildrenMenu($data->id, $id);
$finalData[] = $data;
}
return response()->json(['status' => 'success', 'code' => 200, 'data' => $finalData], 200);
}
protected function getChildrenMenu($id, $role_id)
{
$dataChildren = RoleMenu::query()
->select("m_menu.*")
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->where('m_roles.id', $role_id)
->where('m_menu.parent_id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$children = [];
if ($dataChildren) {
foreach ($dataChildren as $data) {
$data->children = $this->getChildrenMenu($data->id, $role_id);
$children[] = $data;
}
}
return $children;
}
public function list()
{
$data = Menu::get();
$countData = $data->count();
if ($data) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get list menu, please try again later!', 'code' => 400], 400);
}
}
}