diff --git a/composer.json b/composer.json index 8a39991..a4c75de 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tripteki/laravelphp-setting-menu", - "version": "1.0.0", + "version": "1.1.0", "description": "Trip Teknologi's Laravel.php Setting Menu", "readme": "README.md", diff --git a/src/Http/Resources/MenudetailResource.php b/src/Http/Resources/MenudetailResource.php new file mode 100644 index 0000000..91830c7 --- /dev/null +++ b/src/Http/Resources/MenudetailResource.php @@ -0,0 +1,27 @@ + $this->bar, + "menu" => $this->menu, + "category" => $this->category, + "icon" => $this->icon, + "title" => $this->title, + "description" => $this->description, + ]; + } +}; diff --git a/stubs/app/Exports/Settings/Menus/.gitkeep b/stubs/app/Exports/Settings/Menus/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/stubs/app/Exports/Settings/Menus/MenuExport.php b/stubs/app/Exports/Settings/Menus/MenuExport.php new file mode 100644 index 0000000..d867dab --- /dev/null +++ b/stubs/app/Exports/Settings/Menus/MenuExport.php @@ -0,0 +1,50 @@ + [ "font" => [ "bold" => true, ], ], + ]; + } + + /** + * @return array + */ + public function headings(): array + { + return [ + + "Bar", + "Menu", + "Category", + "Icon", + "Title", + "Description", + ]; + } + + /** + * @return array + */ + public function array(): array + { + return MenudetailResource::collection(Detailmenu::all())->resolve(); + } +}; diff --git a/stubs/app/Http/Controllers/Admin/Setting/Menu/MenuAdminController.php b/stubs/app/Http/Controllers/Admin/Setting/Menu/MenuAdminController.php index 6930a55..d44e5c1 100644 --- a/stubs/app/Http/Controllers/Admin/Setting/Menu/MenuAdminController.php +++ b/stubs/app/Http/Controllers/Admin/Setting/Menu/MenuAdminController.php @@ -5,12 +5,17 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Maatwebsite\Excel\Facades\Excel; use Tripteki\SettingMenu\Contracts\Repository\Admin\ISettingMenuDetailRepository; +use App\Imports\Settings\Menus\MenuImport; +use App\Exports\Settings\Menus\MenuExport; use App\Http\Requests\Admin\Settings\Menus\MenuIndexValidation; use App\Http\Requests\Admin\Settings\Menus\MenuShowValidation; use App\Http\Requests\Admin\Settings\Menus\MenuStoreValidation; use App\Http\Requests\Admin\Settings\Menus\MenuUpdateValidation; use App\Http\Requests\Admin\Settings\Menus\MenuDestroyValidation; +use Tripteki\Helpers\Http\Requests\FileImportValidation; +use Tripteki\Helpers\Http\Requests\FileExportValidation; use Tripteki\Helpers\Http\Controllers\Controller; class MenuAdminController extends Controller @@ -336,4 +341,103 @@ public function destroy(MenuDestroyValidation $request, $bar, $menu) return iresponse($data, $statecode); } + + /** + * @OA\Post( + * path="/admin/menus-import", + * tags={"Admin Menu"}, + * summary="Import", + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="multipart/form-data", + * @OA\Schema( + * @OA\Property( + * property="file", + * type="file", + * description="Menu's File." + * ) + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileImportValidation $request + * @return \Illuminate\Http\JsonResponse + */ + public function import(FileImportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"]->getClientOriginalExtension() == "csv" || $form["file"]->getClientOriginalExtension() == "txt") { + + $data = Excel::import(new MenuImport(), $form["file"], null, \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"]->getClientOriginalExtension() == "xls") { + + $data = Excel::import(new MenuImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"]->getClientOriginalExtension() == "xlsx") { + + $data = Excel::import(new MenuImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLSX); + } + + return iresponse($data, $statecode); + } + + /** + * @OA\Get( + * path="/admin/menus-export", + * tags={"Admin Menu"}, + * summary="Export", + * @OA\Parameter( + * required=false, + * in="query", + * name="file", + * schema={"type": "string", "enum": {"csv", "xls", "xlsx"}}, + * description="Menu's File." + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileExportValidation $request + * @return mixed + */ + public function export(FileExportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"] == "csv") { + + $data = Excel::download(new MenuExport(), "Menu.csv", \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"] == "xls") { + + $data = Excel::download(new MenuExport(), "Menu.xls", \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"] == "xlsx") { + + $data = Excel::download(new MenuExport(), "Menu.xlsx", \Maatwebsite\Excel\Excel::XLSX); + } + + return $data; + } }; diff --git a/stubs/app/Imports/Settings/Menus/.gitkeep b/stubs/app/Imports/Settings/Menus/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/stubs/app/Imports/Settings/Menus/MenuImport.php b/stubs/app/Imports/Settings/Menus/MenuImport.php new file mode 100644 index 0000000..0f473c1 --- /dev/null +++ b/stubs/app/Imports/Settings/Menus/MenuImport.php @@ -0,0 +1,63 @@ +rules(); + + Validator::make($rows->toArray(), [ + + "*.0" => $validator["bar"], + "*.1" => $validator["menu"], + "*.2" => $validator["category"], + "*.3" => $validator["icon"], + "*.4" => $validator["title"], + "*.5" => $validator["description"], + + ])->validate(); + } + + /** + * @param \Illuminate\Support\Collection $rows + * @return void + */ + public function collection(Collection $rows) + { + $this->validate($rows); + + $menuAdminRepository = app(ISettingMenuDetailRepository::class); + + foreach ($rows as $row) { + + $menuAdminRepository->create($row[0], $row[1], [ + + "category" => $row[2], + "icon" => $row[3], + "title" => $row[4], + "description" => $row[5], + ]); + } + } +}; diff --git a/stubs/routes/admin/setting/menu.php b/stubs/routes/admin/setting/menu.php index 996f22e..93c13bb 100644 --- a/stubs/routes/admin/setting/menu.php +++ b/stubs/routes/admin/setting/menu.php @@ -9,4 +9,6 @@ * Settings Menus. */ Route::apiResource("bars.menus", MenuAdminController::class)->parameters([ "bars" => "bar", "menus" => "menu", ]); + Route::post("menus-import", [ MenuAdminController::class, "import", ]); + Route::get("menus-export", [ MenuAdminController::class, "export", ]); });