wahyuun
1 year ago
1 changed files with 159 additions and 151 deletions
@ -1,151 +1,159 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Http\Request; |
use Illuminate\Http\Request; |
||||||
use App\Models\Menu; |
use App\Models\Menu; |
||||||
use App\Models\RoleMenu; |
use App\Models\RoleMenu; |
||||||
|
|
||||||
class MenuController extends Controller |
class MenuController extends Controller |
||||||
{ |
{ |
||||||
public function add(Request $request) |
public function add(Request $request) |
||||||
{ |
{ |
||||||
$this->validate($request, [ |
$this->validate($request, [ |
||||||
'icon' => 'required', |
'icon' => 'required', |
||||||
'name' => 'required', |
'name' => 'string|required|unique:m_menu,name', |
||||||
'url' => 'required', |
'url' => 'required|unique:m_menu,url', |
||||||
'sequence' => 'required', |
'sequence' => 'required|integer', |
||||||
]); |
]); |
||||||
|
|
||||||
$data = $request->all(); |
$data = $request->all(); |
||||||
|
|
||||||
$data['created_by'] = $this->currentName; |
$data['created_by'] = $this->currentName; |
||||||
|
|
||||||
$result = Menu::create($data); |
$result = Menu::create($data); |
||||||
|
|
||||||
if($result){ |
if($result){ |
||||||
return response()->json(['status'=>'success','message'=>'add data menu successfully!','code'=>200], 200); |
return response()->json(['status'=>'success','message'=>'add data menu successfully!','code'=>200], 200); |
||||||
}else{ |
}else{ |
||||||
return response()->json(['status'=>'failed','message'=>'add data menu failed!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'add data menu failed!','code'=>400], 400); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
public function edit($id){ |
public function edit($id){ |
||||||
if(!$id || (int) $id < 0 || $id==""){ |
if(!$id || (int) $id < 0 || $id==""){ |
||||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||||
die(); |
die(); |
||||||
} |
} |
||||||
|
|
||||||
$result = Menu::find($id); |
$result = Menu::find($id); |
||||||
|
|
||||||
if($result){ |
if($result){ |
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
||||||
}else{ |
}else{ |
||||||
return response()->json(['status'=>'failed','message'=>'failed get data menu, please try again later!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'failed get data menu, please try again later!','code'=>400], 400); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
public function update(Request $request, $id) |
public function update(Request $request, $id) |
||||||
{ |
{ |
||||||
if(!$id || (int) $id < 0 || $id==""){ |
if(!$id || (int) $id < 0 || $id==""){ |
||||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||||
} |
} |
||||||
|
$this->validate($request, [ |
||||||
$data = Menu::find($id); |
'icon' => 'required', |
||||||
|
'name' => 'string|required', |
||||||
if($data){ |
'url' => 'required', |
||||||
$result = $data->update($request->all()); |
'sequence' => 'required|integer', |
||||||
}else{ |
]); |
||||||
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); |
$data = Menu::find($id); |
||||||
die(); |
|
||||||
} |
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_menu,name']) : ''; |
||||||
|
$request->url !== $data['url'] ? $this->validate($request,['url'=>'unique:m_menu,url']) : ''; |
||||||
|
|
||||||
if($result){ |
if($data){ |
||||||
return response()->json(['status'=>'success','message'=>'data menu successfully updated!','code'=>200], 200); |
$result = $data->update($request->all()); |
||||||
}else{ |
}else{ |
||||||
return response()->json(['status'=>'failed','message'=>'data menu failed updated!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); |
||||||
} |
die(); |
||||||
} |
} |
||||||
|
|
||||||
public function delete($id) |
|
||||||
{ |
if($result){ |
||||||
$data = Menu::find($id); |
return response()->json(['status'=>'success','message'=>'data menu successfully updated!','code'=>200], 200); |
||||||
|
}else{ |
||||||
if($data){ |
return response()->json(['status'=>'failed','message'=>'data menu failed updated!','code'=>400], 400); |
||||||
$delete = $data->delete(); |
} |
||||||
}else{ |
} |
||||||
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); |
|
||||||
die(); |
public function delete($id) |
||||||
} |
{ |
||||||
|
$data = Menu::find($id); |
||||||
|
|
||||||
if($delete){ |
if($data){ |
||||||
return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200); |
$delete = $data->delete(); |
||||||
}else{ |
}else{ |
||||||
return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400); |
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); |
||||||
} |
die(); |
||||||
} |
} |
||||||
|
|
||||||
public function search(Request $request) |
|
||||||
{ |
if($delete){ |
||||||
$payload = $request->all(); |
return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200); |
||||||
$dataBuilder = $this->setUpPayload($payload, 'm_menu'); |
}else{ |
||||||
$builder = $dataBuilder['builder']; |
return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400); |
||||||
$countBuilder = $dataBuilder['count']; |
} |
||||||
$dataGet = $builder->get(); |
} |
||||||
$totalRecord = $countBuilder->count(); |
|
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
public function search(Request $request) |
||||||
} |
{ |
||||||
|
$payload = $request->all(); |
||||||
public function listMenu($id) |
$dataBuilder = $this->setUpPayload($payload, 'm_menu'); |
||||||
{ |
$builder = $dataBuilder['builder']; |
||||||
$dataParent = RoleMenu::select("m_menu.*") |
$countBuilder = $dataBuilder['count']; |
||||||
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id') |
$dataGet = $builder->get(); |
||||||
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') |
$totalRecord = $countBuilder->count(); |
||||||
->whereNull('m_menu.parent_id') |
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
||||||
->where('m_roles.id', $id) |
} |
||||||
->orderBy('m_menu.sequence', 'ASC') |
|
||||||
->get(); |
public function listMenu($id) |
||||||
|
{ |
||||||
$finalData = []; |
$dataParent = RoleMenu::select("m_menu.*") |
||||||
foreach($dataParent as $data){ |
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id') |
||||||
$data->children = $this->getChildrenMenu($data->id, $id); |
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') |
||||||
$finalData[] = $data; |
->whereNull('m_menu.parent_id') |
||||||
} |
->where('m_roles.id', $id) |
||||||
|
->orderBy('m_menu.sequence', 'ASC') |
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$finalData], 200); |
->get(); |
||||||
} |
|
||||||
|
$finalData = []; |
||||||
protected function getChildrenMenu($id, $role_id) |
foreach($dataParent as $data){ |
||||||
{ |
$data->children = $this->getChildrenMenu($data->id, $id); |
||||||
$dataChildren = RoleMenu::select("m_menu.*") |
$finalData[] = $data; |
||||||
->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) |
return response()->json(['status'=>'success','code'=>200,'data'=>$finalData], 200); |
||||||
->where('m_menu.parent_id', $id) |
} |
||||||
->orderBy('m_menu.sequence', 'ASC') |
|
||||||
->get(); |
protected function getChildrenMenu($id, $role_id) |
||||||
$children = []; |
{ |
||||||
if($dataChildren){ |
$dataChildren = RoleMenu::select("m_menu.*") |
||||||
foreach($dataChildren as $data) |
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id') |
||||||
{ |
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') |
||||||
$data->children = $this->getChildrenMenu($data->id, $role_id); |
->where('m_roles.id', $role_id) |
||||||
$children[] = $data; |
->where('m_menu.parent_id', $id) |
||||||
} |
->orderBy('m_menu.sequence', 'ASC') |
||||||
} |
->get(); |
||||||
return $children; |
$children = []; |
||||||
} |
if($dataChildren){ |
||||||
|
foreach($dataChildren as $data) |
||||||
public function list() |
{ |
||||||
{ |
$data->children = $this->getChildrenMenu($data->id, $role_id); |
||||||
$data = Menu::all(); |
$children[] = $data; |
||||||
$countData = $data->count(); |
} |
||||||
|
} |
||||||
if($data){ |
return $children; |
||||||
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); |
public function list() |
||||||
} |
{ |
||||||
} |
$data = Menu::all(); |
||||||
} |
$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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue