Skip to content

Commit

Permalink
feat(core): import-export
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbmaulana committed Nov 10, 2023
1 parent ebcc680 commit cab0c30
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 27 additions & 0 deletions src/Http/Resources/MenudetailResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tripteki\SettingMenu\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\Resources\Json\JsonResource;

class MenudetailResource extends JsonResource
{
/**
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [

"bar" => $this->bar,
"menu" => $this->menu,
"category" => $this->category,
"icon" => $this->icon,
"title" => $this->title,
"description" => $this->description,
];
}
};
Empty file.
50 changes: 50 additions & 0 deletions stubs/app/Exports/Settings/Menus/MenuExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Exports\Settings\Menus;

use Tripteki\SettingMenu\Models\Admin\Detailmenu;
use Tripteki\SettingMenu\Http\Resources\MenudetailResource;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromArray;

class MenuExport implements FromArray, ShouldAutoSize, WithStyles, WithHeadings
{
/**
* @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet
* @return array
*/
public function styles(Worksheet $sheet)
{
return [

1 => [ "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();
}
};
104 changes: 104 additions & 0 deletions stubs/app/Http/Controllers/Admin/Setting/Menu/MenuAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
};
Empty file.
63 changes: 63 additions & 0 deletions stubs/app/Imports/Settings/Menus/MenuImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Imports\Settings\Menus;

use Tripteki\SettingMenu\Contracts\Repository\Admin\ISettingMenuDetailRepository;
use App\Http\Requests\Admin\Settings\Menus\MenuStoreValidation;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;

class MenuImport implements ToCollection, WithStartRow
{
/**
* @return int
*/
public function startRow(): int
{
return 2;
}

/**
* @param \Illuminate\Support\Collection $rows
* @return void
*/
protected function validate(Collection $rows)
{
$validator = (new MenuStoreValidation())->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],
]);
}
}
};
2 changes: 2 additions & 0 deletions stubs/routes/admin/setting/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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", ]);
});

0 comments on commit cab0c30

Please sign in to comment.