getFilteredUsers()->count(); if ($count === 0) { $this->dispatch('notify', type: 'info', message: __('export.no_users_match')); return null; } $locale = auth()->user()->preferred_language ?? 'ar'; return response()->streamDownload(function () use ($locale) { // UTF-8 BOM for Excel Arabic support echo "\xEF\xBB\xBF"; $csv = Writer::createFromString(); // Headers based on admin language $csv->insertOne([ __('export.name', [], $locale), __('export.email', [], $locale), __('export.phone', [], $locale), __('export.user_type', [], $locale), __('export.id_number', [], $locale), __('export.status', [], $locale), __('export.created_at', [], $locale), ]); $this->getFilteredUsers()->cursor()->each(function ($user) use ($csv, $locale) { $csv->insertOne([ $user->user_type === UserType::Company ? $user->company_name : $user->full_name, $user->email, $user->phone, __('export.type_'.$user->user_type->value, [], $locale), $user->user_type === UserType::Company ? $user->company_cert_number : $user->national_id, __('export.status_'.$user->status->value, [], $locale), $user->created_at->format($locale === 'ar' ? 'd/m/Y' : 'm/d/Y'), ]); }); echo $csv->toString(); }, 'users-export-'.now()->format('Y-m-d').'.csv', [ 'Content-Type' => 'text/csv; charset=UTF-8', ]); } public function exportPdf(): ?StreamedResponse { $users = $this->getFilteredUsers()->get(); if ($users->isEmpty()) { $this->dispatch('notify', type: 'info', message: __('export.no_users_match')); return null; } if ($users->count() > 500) { $this->dispatch('notify', type: 'warning', message: __('export.large_export_warning')); } $locale = auth()->user()->preferred_language ?? 'ar'; $pdf = Pdf::loadView('pdf.users-export', [ 'users' => $users, 'locale' => $locale, 'generatedAt' => now(), 'filters' => $this->getActiveFilters(), 'totalCount' => $users->count(), ]); $pdf->setOption('isHtml5ParserEnabled', true); $pdf->setOption('defaultFont', 'DejaVu Sans'); return response()->streamDownload( fn () => print($pdf->output()), 'users-export-'.now()->format('Y-m-d').'.pdf' ); } public function clearFilters(): void { $this->userType = 'all'; $this->status = 'all'; $this->dateFrom = ''; $this->dateTo = ''; } public function with(): array { return [ 'userTypes' => [ 'all' => __('export.all_types'), 'individual' => __('export.type_individual'), 'company' => __('export.type_company'), ], 'statuses' => [ 'all' => __('export.all_statuses'), 'active' => __('export.status_active'), 'deactivated' => __('export.status_deactivated'), ], 'previewCount' => $this->getFilteredUsers()->count(), ]; } private function getFilteredUsers() { return User::query() ->when($this->userType !== 'all', fn ($q) => $q->where('user_type', $this->userType)) ->when($this->status !== 'all', fn ($q) => $q->where('status', $this->status)) ->when($this->dateFrom, fn ($q) => $q->whereDate('created_at', '>=', $this->dateFrom)) ->when($this->dateTo, fn ($q) => $q->whereDate('created_at', '<=', $this->dateTo)) ->whereIn('user_type', [UserType::Individual, UserType::Company]) ->orderBy('created_at', 'desc'); } private function getActiveFilters(): array { $filters = []; if ($this->userType !== 'all') { $filters['user_type'] = __('export.type_'.$this->userType); } if ($this->status !== 'all') { $filters['status'] = __('export.status_'.$this->status); } if ($this->dateFrom) { $filters['date_from'] = $this->dateFrom; } if ($this->dateTo) { $filters['date_to'] = $this->dateTo; } return $filters; } }; ?>
{{ __('export.export_users') }} {{ __('export.export_users_description') }}
{{ __('export.filters_applied') }}
@foreach ($userTypes as $value => $label) {{ $label }} @endforeach
@foreach ($statuses as $value => $label) {{ $label }} @endforeach
@if ($userType !== 'all' || $status !== 'all' || $dateFrom || $dateTo)
{{ __('export.clear_filters') }}
@endif
{{ __('export.total_records') }}: {{ $previewCount }}
{{ __('export.export_csv') }} {{ __('export.exporting') }} {{ __('export.export_pdf') }} {{ __('export.exporting') }}
@if ($previewCount === 0)
{{ __('export.no_users_match') }}
@endif