From e2679852cea3a6d21ba6dbe690c5992ca2470273 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 10 Apr 2024 11:31:30 -0700 Subject: [PATCH] added export button, half the logic for export method --- .../Licenses/LicensesController.php | 98 +++++++++++++++++++ resources/views/licenses/index.blade.php | 3 + routes/web/licenses.php | 7 ++ 3 files changed, 108 insertions(+) diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index c55181c518..0570e0ce70 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -11,6 +11,7 @@ use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; +use Symfony\Component\HttpFoundation\StreamedResponse; /** * This controller handles all actions related to Licenses for @@ -289,4 +290,101 @@ class LicensesController extends Controller ->with('item', $license) ->with('maintained_list', $maintained_list); } + + /** + * Exports users to CSV + * + * @author [A. Gianotto] [] + * @since [v3.5] + * @return StreamedResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function getExportLicensesCsv() + { + $this->authorize('view', License::class); + \Debugbar::disable(); + + $response = new StreamedResponse(function () { + // Open output stream + $handle = fopen('php://output', 'w'); + + License::all() + ->orderBy('created_at', 'DESC')->get() + ->chunk(500, function ($licenses) use ($handle) { + $headers = [ + // strtolower to prevent Excel from trying to open it as a SYLK file + strtolower(trans('general.id')), + trans('admin/companies/table.title'), + trans('admin/users/table.title'), + trans('admin/users/table.name'), + trans('admin/users/table.username'), + trans('admin/users/table.email'), + trans('admin/users/table.manager'), + trans('admin/users/table.location'), + trans('general.department'), + trans('general.assets'), + trans('general.licenses'), + trans('general.accessories'), + trans('general.consumables'), + trans('admin/users/table.groups'), + trans('general.notes'), + trans('admin/users/table.activated'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + trans('general.created_at'), + ]; + + fputcsv($handle, $headers); + + foreach ($licenses as $license) { + // Add a new row with data + $values = [ + $license->id, + $license->company->name, + $license->name, + $license->serial, + $license->purchase_date, + $license->purchase_cost, + $license->order_number, + $license->seats, + $license->notes, + $license->user->id, + $license->depreciation->name, + $license->updated_at, + $license->deleted_at, + $license->license_name, + $license->email, + $license->depreciate, + $license->supplier->name, + $license->expiration_date, + $license->purchase_order, + $license->termination_date, + $license->maintained, + $license->reassignable, + $license->manufacturer->name, + $license->category->name, + $license->min_amt, + + ( $license->reassignable == '1') ? trans('general.yes') : trans('general.no'), + $license->created_at, + ]; + + fputcsv($handle, $values); + } + }); + + // Close the output stream + fclose($handle); + }, 200, [ + 'Content-Type' => 'text/csv; charset=UTF-8', + 'Content-Disposition' => 'attachment; filename="users-'.date('Y-m-d-his').'.csv"', + ]); + + return $response; + } } diff --git a/resources/views/licenses/index.blade.php b/resources/views/licenses/index.blade.php index 8fa52fd1fc..fe13df3c70 100755 --- a/resources/views/licenses/index.blade.php +++ b/resources/views/licenses/index.blade.php @@ -13,6 +13,9 @@ {{ trans('general.create') }} @endcan +@can('view', \App\Models\User::class) + {{ trans('general.export') }} +@endcan @stop {{-- Page content --}} diff --git a/routes/web/licenses.php b/routes/web/licenses.php index b70347793c..7212a47648 100644 --- a/routes/web/licenses.php +++ b/routes/web/licenses.php @@ -48,6 +48,13 @@ Route::group(['prefix' => 'licenses', 'middleware' => ['auth']], function () { '{licenseId}/showfile/{fileId}/{download?}', [Licenses\LicenseFilesController::class, 'show'] )->name('show.licensefile'); + Route::get( + 'export', + [ + Licenses\LicensesController::class, + 'getExportLicensesCsv' + ] + )->name('licenses.export'); }); Route::resource('licenses', Licenses\LicensesController::class, [