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 14, 2023
1 parent 19c1295 commit ae6d756
Show file tree
Hide file tree
Showing 10 changed files with 413 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-acl",
"version": "1.0.0",
"version": "1.1.0",
"description": "Trip Teknologi's Laravel.php ACLs",

"readme": "README.md",
Expand Down
Empty file removed stubs/app/Exports/ACLs/.gitkeep
Empty file.
47 changes: 47 additions & 0 deletions stubs/app/Exports/ACLs/PermissionExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Exports\ACLs;

use Spatie\Permission\Contracts\Permission as PermissionModel;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;

class PermissionExport implements FromCollection, 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 [

"Name",
];
}

/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function collection()
{
return app(PermissionModel::class)->all([

"name",
]);
}
};
47 changes: 47 additions & 0 deletions stubs/app/Exports/ACLs/RoleExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Exports\ACLs;

use Spatie\Permission\Contracts\Role as RoleModel;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;

class RoleExport implements FromCollection, 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 [

"Name",
];
}

/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function collection()
{
return app(RoleModel::class)->all([

"name",
]);
}
};
104 changes: 104 additions & 0 deletions stubs/app/Http/Controllers/Admin/ACL/PermissionAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Maatwebsite\Excel\Facades\Excel;
use Tripteki\ACL\Contracts\Repository\Admin\IACLPermissionRepository;
use App\Imports\ACLs\PermissionImport;
use App\Exports\ACLs\PermissionExport;
use App\Http\Requests\Admin\ACLs\Permissions\PermissionShowValidation;
use App\Http\Requests\Admin\ACLs\Permissions\PermissionStoreValidation;
use App\Http\Requests\Admin\ACLs\Permissions\PermissionDestroyValidation;
use Tripteki\Helpers\Http\Requests\FileImportValidation;
use Tripteki\Helpers\Http\Requests\FileExportValidation;
use Tripteki\Helpers\Http\Controllers\Controller;

class PermissionAdminController extends Controller
Expand Down Expand Up @@ -201,4 +206,103 @@ public function destroy(PermissionDestroyValidation $request, $permission)

return iresponse($data, $statecode);
}

/**
* @OA\Post(
* path="/admin/acls/permissions-import",
* tags={"Admin ACL Permission"},
* summary="Import",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* property="file",
* type="file",
* description="Permission'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 PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::CSV);

} else if ($form["file"]->getClientOriginalExtension() == "xls") {

$data = Excel::import(new PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLS);

} else if ($form["file"]->getClientOriginalExtension() == "xlsx") {

$data = Excel::import(new PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLSX);
}

return iresponse($data, $statecode);
}

/**
* @OA\Get(
* path="/admin/acls/permissions-export",
* tags={"Admin ACL Permission"},
* summary="Export",
* @OA\Parameter(
* required=false,
* in="query",
* name="file",
* schema={"type": "string", "enum": {"csv", "xls", "xlsx"}},
* description="Permission'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 PermissionExport(), "Permission.csv", \Maatwebsite\Excel\Excel::CSV);

} else if ($form["file"] == "xls") {

$data = Excel::download(new PermissionExport(), "Permission.xls", \Maatwebsite\Excel\Excel::XLS);

} else if ($form["file"] == "xlsx") {

$data = Excel::download(new PermissionExport(), "Permission.xlsx", \Maatwebsite\Excel\Excel::XLSX);
}

return $data;
}
};
104 changes: 104 additions & 0 deletions stubs/app/Http/Controllers/Admin/ACL/RoleAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Maatwebsite\Excel\Facades\Excel;
use Tripteki\ACL\Contracts\Repository\Admin\IACLRoleRepository;
use App\Imports\ACLs\RoleImport;
use App\Exports\ACLs\RoleExport;
use App\Http\Requests\Admin\ACLs\Roles\RoleShowValidation;
use App\Http\Requests\Admin\ACLs\Roles\RoleStoreValidation;
use App\Http\Requests\Admin\ACLs\Roles\RoleDestroyValidation;
use Tripteki\Helpers\Http\Requests\FileImportValidation;
use Tripteki\Helpers\Http\Requests\FileExportValidation;
use Tripteki\Helpers\Http\Controllers\Controller;

class RoleAdminController extends Controller
Expand Down Expand Up @@ -201,4 +206,103 @@ public function destroy(RoleDestroyValidation $request, $role)

return iresponse($data, $statecode);
}

/**
* @OA\Post(
* path="/admin/acls/roles-import",
* tags={"Admin ACL Role"},
* summary="Import",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* property="file",
* type="file",
* description="Role'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 RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::CSV);

} else if ($form["file"]->getClientOriginalExtension() == "xls") {

$data = Excel::import(new RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLS);

} else if ($form["file"]->getClientOriginalExtension() == "xlsx") {

$data = Excel::import(new RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLSX);
}

return iresponse($data, $statecode);
}

/**
* @OA\Get(
* path="/admin/acls/roles-export",
* tags={"Admin ACL Role"},
* summary="Export",
* @OA\Parameter(
* required=false,
* in="query",
* name="file",
* schema={"type": "string", "enum": {"csv", "xls", "xlsx"}},
* description="Role'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 RoleExport(), "Role.csv", \Maatwebsite\Excel\Excel::CSV);

} else if ($form["file"] == "xls") {

$data = Excel::download(new RoleExport(), "Role.xls", \Maatwebsite\Excel\Excel::XLS);

} else if ($form["file"] == "xlsx") {

$data = Excel::download(new RoleExport(), "Role.xlsx", \Maatwebsite\Excel\Excel::XLSX);
}

return $data;
}
};
Empty file removed stubs/app/Imports/ACLs/.gitkeep
Empty file.
52 changes: 52 additions & 0 deletions stubs/app/Imports/ACLs/PermissionImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Imports\ACLs;

use Tripteki\ACL\Contracts\Repository\Admin\IACLPermissionRepository;
use App\Http\Requests\Admin\ACLs\Permissions\PermissionStoreValidation;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;

class PermissionImport 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 PermissionStoreValidation())->rules();

Validator::make($rows->toArray(), [

"*.0" => $validator["permission"],

])->validate();
}

/**
* @param \Illuminate\Support\Collection $rows
* @return void
*/
public function collection(Collection $rows)
{
$this->validate($rows);

$aclPermissionAdminRepository = app(IACLPermissionRepository::class);

foreach ($rows as $row) {

$aclPermissionAdminRepository->rule($row[0]);
}
}
};
Loading

0 comments on commit ae6d756

Please sign in to comment.