From c6b4721d61ec7e6a9916346f03f1c8d888192321 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 15 Mar 2022 09:30:19 +1100 Subject: [PATCH] Bulk download documents --- app/Http/Controllers/DocumentController.php | 8 +- app/Jobs/Document/ZipDocuments.php | 118 ++++++++++++++++++ app/Mail/DownloadDocuments.php | 56 +++++++++ app/Models/Company.php | 5 + app/Models/Document.php | 5 + resources/lang/en/texts.php | 1 + .../email/admin/download_documents.blade.php | 10 ++ .../admin/download_documents_text.blade.php | 5 + .../admin/download_invoices_text.blade.php | 2 +- 9 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 app/Jobs/Document/ZipDocuments.php create mode 100644 app/Mail/DownloadDocuments.php create mode 100644 resources/views/email/admin/download_documents.blade.php create mode 100644 resources/views/email/admin/download_documents_text.blade.php diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 4d0e81101eb1..4db5a77d9c51 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -173,10 +173,16 @@ class DocumentController extends BaseController $documents = Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get(); - if (! $invoices) { + if (! $documents) { return response()->json(['message' => ctrans('texts.no_documents_found')]); } + if($action == 'download'){ + + ZipDocuments::dispatch($documents->pluck('id'), auth()->user()->company(), auth()->user()); + + return response()->json(['message' => ctrans('texts.sent_message')], 200); + } /* * Send the other actions to the switch */ diff --git a/app/Jobs/Document/ZipDocuments.php b/app/Jobs/Document/ZipDocuments.php new file mode 100644 index 000000000000..12312acb207e --- /dev/null +++ b/app/Jobs/Document/ZipDocuments.php @@ -0,0 +1,118 @@ +document_ids = $document_ids; + + $this->company = $company; + + $this->user = $user; + + $this->settings = $company->settings; + } + + /** + * Execute the job. + * + * @return void + * @throws \ZipStream\Exception\FileNotFoundException + * @throws \ZipStream\Exception\FileNotReadableException + * @throws \ZipStream\Exception\OverflowException + */ + + public function handle() + { + MultiDB::setDb($this->company->db); + + # create new zip object + $zipFile = new \PhpZip\ZipFile(); + $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.documents')).'.zip'; + $path = $this->company->file_path(); + + try{ + + $documents = Document::whereIn('id', $this->document_ids)->get(); + + foreach ($documents as $document) { + + $zipFile->addFromString($document->name, $document->getFile()); + + } + + Storage::put($path.$file_name, $zipFile->outputAsString()); + + $nmo = new NinjaMailerObject; + $nmo->mailable = new DownloadDocuments(Storage::url($path.$file_name), $this->company); + $nmo->to_user = $this->user; + $nmo->settings = $this->settings; + $nmo->company = $this->company; + + NinjaMailerJob::dispatch($nmo); + + UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); + + } + catch(\PhpZip\Exception\ZipException $e){ + nlog("could not make zip => ". $e->getMessage()); + } + finally{ + $zipFile->close(); + } + + + } + +} diff --git a/app/Mail/DownloadDocuments.php b/app/Mail/DownloadDocuments.php new file mode 100644 index 000000000000..bc40f45971cd --- /dev/null +++ b/app/Mail/DownloadDocuments.php @@ -0,0 +1,56 @@ +file_path = $file_path; + + $this->company = $company; + } + + /** + * Build the message. + */ + public function build() + { + + App::setLocale($this->company->getLocale()); + + return $this->from(config('mail.from.address'), config('mail.from.name')) + ->subject(ctrans('texts.download_files')) + ->text('email.admin.download_documents_text', [ + 'url' => $this->file_path, + ]) + ->view('email.admin.download_documents', [ + 'url' => $this->file_path, + 'logo' => $this->company->present()->logo, + 'whitelabel' => $this->company->account->isPaid() ? true : false, + 'settings' => $this->company->settings, + 'greeting' => $this->company->present()->name(), + ]); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 0bfa5e1431cd..3caf9d00e612 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -508,6 +508,11 @@ class Company extends BaseModel return $this->slack_webhook_url; } + public function file_path() + { + return $this->company_key.'/'; + } + public function rBits() { $user = $this->owner(); diff --git a/app/Models/Document.php b/app/Models/Document.php index cddd583321ea..2428c301c57a 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -135,4 +135,9 @@ class Document extends BaseModel { return Storage::disk($this->disk)->path($this->url); } + + public function getFile() + { + return Storage::get($this->filePath()); + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index dd23ac320e87..b591ae905d23 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4568,6 +4568,7 @@ $LANG = array( 'small' => 'Small', 'quotes_backup_subject' => 'Your quotes are ready for download', 'credits_backup_subject' => 'Your credits are ready for download', + 'document_download_subject' => 'Your documents are ready for download', ); diff --git a/resources/views/email/admin/download_documents.blade.php b/resources/views/email/admin/download_documents.blade.php new file mode 100644 index 000000000000..a428452a1953 --- /dev/null +++ b/resources/views/email/admin/download_documents.blade.php @@ -0,0 +1,10 @@ +@component('email.template.admin', ['logo' => $logo, 'settings' => $settings]) +
+

{{ ctrans('texts.document_download_subject') }}

+

{{ ctrans('texts.download_timeframe') }}

+ + + {{ ctrans('texts.download') }} + +
+@endcomponent diff --git a/resources/views/email/admin/download_documents_text.blade.php b/resources/views/email/admin/download_documents_text.blade.php new file mode 100644 index 000000000000..c39d09549ae8 --- /dev/null +++ b/resources/views/email/admin/download_documents_text.blade.php @@ -0,0 +1,5 @@ +{!! ctrans('texts.invoices_backup_subject') !!} + +{!! ctrans('texts.download_timeframe') !!} + +{!! $url !!} \ No newline at end of file diff --git a/resources/views/email/admin/download_invoices_text.blade.php b/resources/views/email/admin/download_invoices_text.blade.php index c39d09549ae8..13c4cd4c4dc2 100644 --- a/resources/views/email/admin/download_invoices_text.blade.php +++ b/resources/views/email/admin/download_invoices_text.blade.php @@ -1,4 +1,4 @@ -{!! ctrans('texts.invoices_backup_subject') !!} +{!! ctrans('texts.document_download_subject') !!} {!! ctrans('texts.download_timeframe') !!}