diff --git a/laravel_app/app/Livewire/Forms/MailingForm.php b/laravel_app/app/Livewire/Forms/MailingForm.php index 5ad428e..8f5c4d1 100644 --- a/laravel_app/app/Livewire/Forms/MailingForm.php +++ b/laravel_app/app/Livewire/Forms/MailingForm.php @@ -44,6 +44,7 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien $mailing = $report->project->mailings()->create([ 'subject' => $subject, 'message' => $message, + 'report_id' => $report->id, ]); $mailing->attachments()->create([ @@ -53,7 +54,7 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien $mailing->recipients()->createMany($recipients); Mail::to($mailing->recipients()->pluck('email')->toArray()) - ->send(new \App\Mail\ReportMailer($mailing)); + ->send(new \App\Mail\ReportMailer($mailing, $report)); } } diff --git a/laravel_app/app/Mail/ReportMailer.php b/laravel_app/app/Mail/ReportMailer.php index 129fa48..c7aa3da 100644 --- a/laravel_app/app/Mail/ReportMailer.php +++ b/laravel_app/app/Mail/ReportMailer.php @@ -5,6 +5,7 @@ use App\Models\ProjectMailing; use App\Models\ProjectMailingAttachment; use App\Models\ProjectMosaic; +use App\Models\ProjectReport; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; @@ -52,40 +53,44 @@ public function content(): Content 'logoPath'=> resource_path('images/smartcane.png'), 'subject' => $this->mailing->subject, 'mailing' => $this->mailing, + 'reportUrl' => route( + 'project.report.download', + $this->mailing->report->token + ), ], //htmlString: $this->mailing->message ); } - /** - * Get the attachments for the message. - * - * @return array - */ - public function attachments(): array - { - return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) { - $attachment_path = Storage::path($attachment->mailing->project->download_path."/".$attachment->path); - - if (!File::exists($attachment_path)) { - logger('Could not find attachment: ' . $attachment_path); - return null; - } - logger('Attachment found: ' . $attachment_path); - - $mime = 'application/pdf'; // default MIME type - $extension = pathinfo($attachment->path, PATHINFO_EXTENSION); - if ($extension === 'docx') { - $mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; - } - - return Attachment::fromPath( - $attachment_path - ) - ->as($attachment->path) - ->withMime($mime); - }) - ->filter() - ->toArray(); - } +// /** +// * Get the attachments for the message. +// * +// * @return array +// */ +// public function attachments(): array +// { +// return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) { +// $attachment_path = Storage::path($attachment->mailing->project->download_path."/".$attachment->path); +// +// if (!File::exists($attachment_path)) { +// logger('Could not find attachment: ' . $attachment_path); +// return null; +// } +// logger('Attachment found: ' . $attachment_path); +// +// $mime = 'application/pdf'; // default MIME type +// $extension = pathinfo($attachment->path, PATHINFO_EXTENSION); +// if ($extension === 'docx') { +// $mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; +// } +// +// return Attachment::fromPath( +// $attachment_path +// ) +// ->as($attachment->path) +// ->withMime($mime); +// }) +// ->filter() +// ->toArray(); +// } } diff --git a/laravel_app/app/Models/ProjectMailing.php b/laravel_app/app/Models/ProjectMailing.php index 149f58a..fe99a98 100644 --- a/laravel_app/app/Models/ProjectMailing.php +++ b/laravel_app/app/Models/ProjectMailing.php @@ -19,10 +19,10 @@ class ProjectMailing extends Model protected $fillable = [ 'subject', 'message', - 'status' + 'status', + 'report_id', ]; - public function addAttachment($name, UploadedFile $file) { $prefix = Str::random(10); @@ -39,7 +39,10 @@ public function addAttachment($name, UploadedFile $file) 'path' => $path ])); } - + public function report() + { + return $this->belongsTo(ProjectReport::class, 'report_id', 'id'); + } public function project() { diff --git a/laravel_app/app/Models/ProjectReport.php b/laravel_app/app/Models/ProjectReport.php index 1292268..eabe254 100644 --- a/laravel_app/app/Models/ProjectReport.php +++ b/laravel_app/app/Models/ProjectReport.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; class ProjectReport extends Model { @@ -18,6 +19,17 @@ public function project() return $this->belongsTo(Project::class); } + protected static function boot() + { + parent::boot(); + + static::creating(function ($model) { + if (empty($model->token)) { + $model->token = Str::random(32); // Generates a random string of 60 characters + } + }); + } + public function getFileNameAttribute(): string { diff --git a/laravel_app/database/migrations/2024_09_09_064355_add_token_column_to_project_reports_table.php b/laravel_app/database/migrations/2024_09_09_064355_add_token_column_to_project_reports_table.php new file mode 100644 index 0000000..3f5a376 --- /dev/null +++ b/laravel_app/database/migrations/2024_09_09_064355_add_token_column_to_project_reports_table.php @@ -0,0 +1,28 @@ +string('token')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('project_reports', function (Blueprint $table) { + $table->dropColumn('token'); + }); + } +}; diff --git a/laravel_app/database/migrations/2024_09_09_080730_add_report_id_column_to_project_mailings_table.php b/laravel_app/database/migrations/2024_09_09_080730_add_report_id_column_to_project_mailings_table.php new file mode 100644 index 0000000..a350888 --- /dev/null +++ b/laravel_app/database/migrations/2024_09_09_080730_add_report_id_column_to_project_mailings_table.php @@ -0,0 +1,28 @@ +bigInteger('report_id')->unsigned()->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('project_mailings', function (Blueprint $table) { + $table->dropColumn('report_id'); + }); + } +}; diff --git a/laravel_app/resources/views/emails/scheduled-report.blade.php b/laravel_app/resources/views/emails/scheduled-report.blade.php index 01b7945..4398184 100644 --- a/laravel_app/resources/views/emails/scheduled-report.blade.php +++ b/laravel_app/resources/views/emails/scheduled-report.blade.php @@ -16,7 +16,10 @@ {{$mailingContent}} -Thanks. +
View report
+ +## Thanks. + ## Contact diff --git a/laravel_app/routes/web.php b/laravel_app/routes/web.php index 082a2ce..950211e 100644 --- a/laravel_app/routes/web.php +++ b/laravel_app/routes/web.php @@ -33,3 +33,12 @@ Route::get('/projects/{projectReport}/download', [\App\Http\Controllers\ProjectReportController::class, 'download'])->name('project.report.download'); Route::get('/projects', [\App\Http\Controllers\ProjectController::class, 'index'])->name('project'); }); + +Route::get('download/{token}', function ($token) { + $report = App\Models\ProjectReport::where('token', $token)->first(); + if ($report) { + $path = $report->project->download_path . '/' . $report->path; + return response()->download(Storage::path($path)); + } + return abort(404); +})->name('project.report.download');