From 8ba1e82645cbef528bb8990552216ed2a5dcaf30 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Tue, 12 Jan 2021 22:36:29 +0100 Subject: [PATCH] [publish system] Important changes to publish system allowing attachments + ProcessEmailBatch.php now takes a builder argument --- .../Controllers/Admin/PublishController.php | 20 ++++- app/Jobs/ProcessEmailBatch.php | 87 +++++++++++++++++-- app/Nova/Actions/PdfAttachmentSetting.php | 63 ++++++++++++++ app/Nova/Subscriber.php | 8 ++ app/PdfFile.php | 15 +++- 5 files changed, 182 insertions(+), 11 deletions(-) create mode 100644 app/Nova/Actions/PdfAttachmentSetting.php diff --git a/app/Http/Controllers/Admin/PublishController.php b/app/Http/Controllers/Admin/PublishController.php index 3f0f424..ec0f4dd 100644 --- a/app/Http/Controllers/Admin/PublishController.php +++ b/app/Http/Controllers/Admin/PublishController.php @@ -33,10 +33,17 @@ class PublishController extends Controller } + /** + * @param Request $request + * @return array + * Dispatches publish jobs + */ public function publish(Request $request) { $batch = new EmailBatch(); $data = $request->input('data'); + + //0: newsletter, 1: PDF $type = Arr::get($data, 'email.type', 0); $recipientGroup = Arr::get($data, 'recipient_group'); $recipientSlug = Arr::get($data, 'recipient_group.slug'); @@ -55,11 +62,22 @@ class PublishController extends Controller $batch->save(); - dispatch(new ProcessEmailBatch($batch, $type, $recipientGroup)); + $builder = $recipientGroup === null ? + User::receivesEmails() : + PublishController::sendGroups()[$recipientGroup['slug']]['builder']; + $processEmailBatch = new ProcessEmailBatch($batch, $builder); + + dispatch($processEmailBatch->withFile()); return ['data' => $batch->id]; } + + /** + * @param Request $request + * @param PdfFile $file + * @return \Illuminate\Http\JsonResponse + */ public function file(Request $request, PdfFile $file) { return response()->json([ diff --git a/app/Jobs/ProcessEmailBatch.php b/app/Jobs/ProcessEmailBatch.php index a146c59..4deefd0 100644 --- a/app/Jobs/ProcessEmailBatch.php +++ b/app/Jobs/ProcessEmailBatch.php @@ -7,9 +7,11 @@ use App\Events\ProcessBatch; use App\Facades\Mailgun; use App\Http\Controllers\Admin\PublishController; use App\Mail\BatchMail; +use App\PdfFile; use App\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; @@ -26,33 +28,96 @@ class ProcessEmailBatch implements ShouldQueue */ private $batch; + /** + * @var int + * Type of batch (html, text) + */ private int $type; + + /** + * @var Collection + * list of users to send the email to + */ private $users; + /** + * @var int + */ public $timeout = 600; + /** + * @var string|null + */ private $html = null; + /** + * @var array + * @see https://github.com/mailgun/mailgun-php/blob/master/doc/attachments.md + * Array of files to attach + */ + private array $attachments = []; + + + /** + * @var bool + * Attach EmailBatch linked file to mail + */ + private bool $attachFile = false; + + + /** * Create a new job instance. * * @param EmailBatch $batch - * @param int $type + * @param Builder $usersBuilder */ - public function __construct(EmailBatch $batch, $type, $recipientGroup = null) + public function __construct(EmailBatch $batch, $usersBuilder) { $this->batch = $batch; - $this->type = $type; - $this->users = $recipientGroup === null ? - User::receivesEmails()->get() : - PublishController::sendGroups()[$recipientGroup['slug']]['builder']->get(); + + $this->users = $usersBuilder->get(); $this->html = $this->batch->content['html']; $this->replaceVariables(); } + /** + * @param $content + * @param $name + * @return ProcessEmailBatch + */ + protected function attachFile($content, $name): ProcessEmailBatch + { + $this->attachments[] = [ + 'fileContent' => $content, + 'filename' => $name + ]; + return $this; + } + + + /** + * @return $this + */ + public function withFile(): ProcessEmailBatch + { + $this->attachFile = true; + return $this; + } + + /** + * @param PdfFile $file + * @return ProcessEmailBatch + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function attachPdf(PdfFile $file): ProcessEmailBatch + { + return $this->attachFile($file->getPdf(), "$file->title.pdf"); + } + /** * @param $data */ @@ -84,7 +149,10 @@ class ProcessEmailBatch implements ShouldQueue */ public function handle() { - /** @var Collection $users */ + + if($this->attachFile && $this->batch->file) { + $this->attachPdf($this->batch->file); + } $size = config('mail.mailgun.chunk_size'); $chunks = $this->users->chunk($size); @@ -119,7 +187,7 @@ class ProcessEmailBatch implements ShouldQueue - $view = ($this->type == 1) ? $this->batch->renderFromContent() : $this->html; + $view = $this->html ?? $this->batch->renderFromContent(); @@ -129,9 +197,10 @@ class ProcessEmailBatch implements ShouldQueue 'subject' => $this->batch->subject, 'recipient-variables' => $variables, 'html' => $view, - 'o:tag' => [$this->batch->getTag(), config('app.env').'_batch_'.BatchMail::getView($this->type)], + 'o:tag' => [$this->batch->getTag(), config('app.env').'_batch_'], 'o:testmode' => config('mail.mailgun.test_mode', 'yes'), 'h:Reply-To' => 'olivier.robichon@prescription-quotidien.com', + 'attachment' => $this->attachments, ]; Mailgun::messages()->send(config('mail.mailgun.domain'), $params); diff --git a/app/Nova/Actions/PdfAttachmentSetting.php b/app/Nova/Actions/PdfAttachmentSetting.php new file mode 100644 index 0000000..c11db38 --- /dev/null +++ b/app/Nova/Actions/PdfAttachmentSetting.php @@ -0,0 +1,63 @@ +enable ? "Activer réception PDF" : "Désactiver réception PDF"; + } + + + /** + * PdfAttachmentSetting constructor. + * @param bool $enable + */ + public function __construct(bool $enable) + { + $this->enable = $enable; + } + + /** + * Perform the action on the given models. + * + * @param \Laravel\Nova\Fields\ActionFields $fields + * @param \Illuminate\Support\Collection $models + * @return mixed + */ + public function handle(ActionFields $fields, Collection $models) + { + $models->map(fn(User $user) => $user->forceFill(['receives_pdf' => $this->enable])->save()); + + $message = $this->enable ? + "Ces utilisateurs reçevront à présent le fichier PDF" : + "Ces utilisateurs ne reçevront plus le fichier PDF"; + return Action::message($message); + } + + /** + * Get the fields available on the action. + * + * @return array + */ + public function fields() + { + return []; + } +} diff --git a/app/Nova/Subscriber.php b/app/Nova/Subscriber.php index 7f26224..67f01ae 100644 --- a/app/Nova/Subscriber.php +++ b/app/Nova/Subscriber.php @@ -4,6 +4,7 @@ namespace App\Nova; +use App\Nova\Actions\PdfAttachmentSetting; use App\Nova\Filters\AccountStates; use Illuminate\Http\Request; use Laravel\Nova\Http\Requests\NovaRequest; @@ -29,5 +30,12 @@ class Subscriber extends User } + public function actions(Request $request) + { + return array_merge([ + new PdfAttachmentSetting(true), + new PdfAttachmentSetting(false) + ],parent::actions($request)); + } } diff --git a/app/PdfFile.php b/app/PdfFile.php index 83a607d..df1f31d 100644 --- a/app/PdfFile.php +++ b/app/PdfFile.php @@ -361,11 +361,22 @@ class PdfFile extends TwillModel implements Sortable * @return string * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ - public function getPdf() + public function getLocalPdf(): string { return Storage::disk('local')->get($this->pdfPath); } + + /** + * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function getPdf(): string + { + return Storage::cloud()->get($this->binPath); + } + + /** * @return string */ @@ -430,6 +441,7 @@ class PdfFile extends TwillModel implements Sortable /** * @param false $download * @return \Symfony\Component\HttpFoundation\StreamedResponse + * @throws \League\Flysystem\FileNotFoundException */ public function downloadFile($download = false) { @@ -448,6 +460,7 @@ class PdfFile extends TwillModel implements Sortable } + /** * Processes pdf to json conversion for in-file search */ -- 2.39.5