From eeaa33fcc4441cda454042075f3cd41130ae10a7 Mon Sep 17 00:00:00 2001 From: farhan048 Date: Mon, 13 Nov 2023 18:45:01 +0700 Subject: [PATCH] menu ospro General --- app/Http/Controllers/Controller.php | 48 +++++ .../Controllers/MenuCompanyController.php | 195 ++++++++++++++++++ app/Models/Menu.php | 2 +- app/Models/MenuCompany.php | 27 +++ app/Models/Project.php | 17 +- routes/web.php | 8 + 6 files changed, 287 insertions(+), 10 deletions(-) create mode 100644 app/Http/Controllers/MenuCompanyController.php create mode 100644 app/Models/MenuCompany.php diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 31f6ef2..02049ab 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -7,6 +7,8 @@ use Illuminate\Support\Facades\Http; use App\Models\UserToActivity; use App\Models\Activity; use App\Models\AssignMaterial; +use App\Models\User; +use App\Models\Divisi; use Laravel\Lumen\Routing\Controller as BaseController; use App\Models\ReportK3Detail; @@ -321,4 +323,50 @@ class Controller extends BaseController ReportK3Detail::create($dataNew); } } + + protected function getDataHierarchy($divisi, $paramUserId) + { + $parentDivisi = Divisi::where('id', $divisi)->get(); + $divisions = []; + $tempUser = []; + $userId = [$paramUserId]; + // if ($parentDivisi->parent == null){ + + // }else{ + foreach ($parentDivisi as $key) { + $data = $this->getAllChildrenDivisi($key->id); + $divisions[] = $data; + } + if (count($divisions) > 0) { + for ($i = 0; $i < count($divisions[0]); $i++) { + if (User::select('id', 'name', 'divisi_id')->where('divisi_id', $divisions[0][$i]->id)->exists()) { + $tempUser[] = User::select('id', 'name', 'divisi_id')->where('divisi_id', $divisions[0][$i]->id)->get(); + } + } + + for ($x = 0; $x < count($tempUser); $x++) { + for ($y = 0; $y < count($tempUser[$x]); $y++) { + $userId[] = $tempUser[$x][$y]->id; + } + } + } + + return $userId; + // } + // return $userId; + } + + private function getAllChildrenDivisi($divisi) + { + $finalData = []; + $divisi = Divisi::where('parent', $divisi)->get(); + if (count($divisi) > 0) { + foreach ($divisi as $key) { + $childDivisi = $this->getAllChildrenDivisi($key->id); + $finalData[] = $key; + $finalData = array_merge($finalData, $childDivisi); + } + } + return $finalData; + } } diff --git a/app/Http/Controllers/MenuCompanyController.php b/app/Http/Controllers/MenuCompanyController.php new file mode 100644 index 0000000..2b79748 --- /dev/null +++ b/app/Http/Controllers/MenuCompanyController.php @@ -0,0 +1,195 @@ +validate($request, [ + // 'icon' => 'required', + // 'name' => 'string|required', + // 'url' => 'required|unique:m_company_menu,url', + // 'sequence' => 'required|integer', + // ]); + + // $data = $request->all(); + + // $data['created_by'] = $this->currentName; + + // $result = MenuCompany::create($data); + + // if ($result) { + // return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200], 200); + // } else { + // return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400); + // } + // } + + public function addMultiple(Request $request) + { + $menus = $request->all(); + + if (is_array($menus) && count($menus) > 0) { + $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' => 400]); + } + } else { + return response()->json(['status' => 'failed', 'message' => 'add data menu failed created!', 'code' => 400]); + } + } + + + 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); + } + } +} diff --git a/app/Models/Menu.php b/app/Models/Menu.php index 951f6da..a507545 100644 --- a/app/Models/Menu.php +++ b/app/Models/Menu.php @@ -13,6 +13,6 @@ class Menu extends Model protected $fillable = [ 'parent_id', 'icon', 'name', 'alias_name', 'url', 'sequence', - 'created_at', 'created_by', 'updated_at', 'updated_by', 'company_id' + 'created_at', 'created_by', 'updated_at', 'updated_by' ]; } diff --git a/app/Models/MenuCompany.php b/app/Models/MenuCompany.php new file mode 100644 index 0000000..6038bda --- /dev/null +++ b/app/Models/MenuCompany.php @@ -0,0 +1,27 @@ +group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro $router->post('/company-management/search', 'CompanyController@search'); $router->delete('/company-management/delete/{id}', 'CompanyController@delete'); $router->get('/company-management/list', 'CompanyController@list'); + + $router->post('/menu-company/add-multiple', 'MenuCompanyController@addMultiple'); + $router->get('/menu-company/edit/{id}', 'MenuCompanyController@edit'); + $router->put('/menu-company/update/{id}', 'MenuCompanyController@update'); + $router->delete('/menu-company/delete/{id}', 'MenuCompanyController@delete'); + $router->post('/menu-company/search', 'MenuCompanyController@search'); + $router->get('/menu-company/management/{id}', 'MenuCompanyController@listMenu'); + $router->get('/menu-company/list', 'MenuCompanyController@list'); }); });