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.
182 lines
6.3 KiB
182 lines
6.3 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use Illuminate\Http\Request; |
|
use App\Models\MenuCompany; |
|
use App\Models\RoleMenu; |
|
use Illuminate\Support\Facades\Log; |
|
|
|
class MenuCompanyController extends Controller |
|
{ |
|
public function addMultiple(Request $request) |
|
{ |
|
$menus = $request->all(); |
|
|
|
$data = MenuCompany::where('company_id',$menus[0]['company_id']); |
|
if($data){ |
|
$data->delete(); |
|
} |
|
if (is_array($menus) && count($menus) > 0 && isset($menus)) { |
|
$countRes = 0; |
|
foreach ($menus as $menu) { |
|
|
|
$dataInsert = array( |
|
"menu_id" => $menu['menu_id'], |
|
"parent_menu_id" => $menu['parent_id'], |
|
"company_id" => $menu['company_id'], |
|
"icon" => $menu['icon'], |
|
"alias_name" => $menu['alias_name'], |
|
"url" => $menu['url'], |
|
"sequence" => $menu['sequence'], |
|
"created_by" => $this->currentName |
|
); |
|
|
|
$result = MenuCompany::create($dataInsert); |
|
|
|
if ($result) { |
|
$countRes++; |
|
} else { |
|
$countRes--; |
|
} |
|
} |
|
|
|
if ($countRes > 0) { |
|
return response()->json(['status' => 'success', 'message' => 'Add data menu successfully!', 'code' => 200]); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'Add data menu failed created!', 'code' => 500]); |
|
} |
|
} else { |
|
if(isset($data)) |
|
{ |
|
return response()->json(['status' => 'success', 'message' => 'Add data menu successfully!', 'code' => 200]); |
|
}else{ |
|
return response()->json(['status' => 'failed', 'message' => 'Add data menu failed created!', 'code' => 500]); |
|
} |
|
} |
|
} |
|
|
|
|
|
public function edit($id) |
|
{ |
|
if (!$id || (int) $id < 0 || $id == "") { |
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
$result = MenuCompany::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 (!$id || (int) $id < 0 || $id == "") { |
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
|
} |
|
$this->validate($request, [ |
|
'icon' => 'required', |
|
'sequence' => 'required|integer', |
|
]); |
|
$data = MenuCompany::find($id); |
|
|
|
|
|
if ($data) { |
|
$result = $data->update($request->all()); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
|
|
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); |
|
} |
|
} |
|
|
|
public function delete($id) |
|
{ |
|
$data = MenuCompany::find($id); |
|
|
|
if ($data) { |
|
$delete = $data->delete(); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'Data menu not found!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
|
|
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); |
|
} |
|
} |
|
|
|
public function search(Request $request) |
|
{ |
|
$payload = $request->all(); |
|
$dataBuilder = $this->setUpPayload($payload, 'm_company_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::select("m_company_menu.*") |
|
->join('m_company_menu', 'm_company_menu.id', '=', 't_roles_menu.menu_id') |
|
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') |
|
->whereNull('m_company_menu.parent_id') |
|
->where('m_roles.id', $id) |
|
->orderBy('m_company_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::select("m_company_menu.*") |
|
->join('m_company_menu', 'm_company_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_company_menu.parent_id', $id) |
|
->orderBy('m_company_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 = MenuCompany::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); |
|
} |
|
} |
|
}
|
|
|