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 0bc6572 commit e794fc8
Show file tree
Hide file tree
Showing 5 changed files with 116 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-log",
"version": "1.0.1",
"version": "1.1.0",
"description": "Trip Teknologi's Laravel.php Logs",

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

namespace App\Exports\Logs;

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 LogExport 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 [

"ID",
"Causer Type",
"Causer ID",
"Subject Type",
"Subject ID",
"Log Name",
"Properties",
"Event",
"Created At",
"Updated At",
];
}

/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function collection()
{
return \Spatie\Activitylog\ActivitylogServiceProvider::getActivityModelInstance()->all([

"id",
"causer_type",
"causer_id",
"subject_type",
"subject_id",
"log_name",
"properties",
"event",
"created_at",
"updated_at",
]);
}
};
50 changes: 50 additions & 0 deletions stubs/app/Http/Controllers/Admin/Log/LogAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Maatwebsite\Excel\Facades\Excel;
use Tripteki\Log\Contracts\Repository\Admin\ILogRepository;
use App\Exports\Log\LogExport;
use App\Http\Requests\Admin\Logs\LogShowValidation;
use Tripteki\Helpers\Http\Requests\FileExportValidation;
use Tripteki\Helpers\Http\Controllers\Controller;

class LogAdminController extends Controller
Expand Down Expand Up @@ -108,4 +111,51 @@ public function show(LogShowValidation $request, $log)

return iresponse($data, $statecode);
}

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

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

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

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

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

return $data;
}
};
1 change: 1 addition & 0 deletions stubs/routes/admin/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
* Logs.
*/
Route::apiResource("logs", LogAdminController::class)->only([ "index", "show", ])->parameters([ "logs" => "log", ]);
Route::get("logs-export", [ LogAdminController::class, "export", ]);
});

0 comments on commit e794fc8

Please sign in to comment.