]> _ Git - psq.git/commitdiff
[publish system] Important changes to publish system allowing attachments + ProcessEm...
authorLouis Jeckel <louis.jeckel@outlook.com>
Tue, 12 Jan 2021 21:36:29 +0000 (22:36 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Tue, 12 Jan 2021 21:36:29 +0000 (22:36 +0100)
app/Http/Controllers/Admin/PublishController.php
app/Jobs/ProcessEmailBatch.php
app/Nova/Actions/PdfAttachmentSetting.php [new file with mode: 0644]
app/Nova/Subscriber.php
app/PdfFile.php

index 3f0f424f443afcd8aeb2e1dd390efb348a6adaca..ec0f4dd39a61257dcb7df5b772aa35cd44e33252 100644 (file)
@@ -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([
index a146c598501fcb33665fc67200dd7f8459b4b6d2..4deefd0cd0a5b7b27bde9c8232ecdf461ef2f174 100644 (file)
@@ -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 (file)
index 0000000..c11db38
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Nova\Actions;
+
+use App\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Collection;
+use Laravel\Nova\Actions\Action;
+use Laravel\Nova\Fields\ActionFields;
+
+class PdfAttachmentSetting extends Action
+{
+    use InteractsWithQueue, Queueable;
+
+    private bool $enable;
+
+    public $withoutConfirmation = true;
+    public $showOnTableRow = true;
+
+    public function name()
+    {
+        return $this->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 [];
+    }
+}
index 7f2622462fa11ad61c15c281b8edf32d11d1f726..67f01aea95ebed6cc9ec074a8ecae6878b30d518 100644 (file)
@@ -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));
+    }
 
 }
index 83a607dd5aaf7465859c97b77156aa9e41cc5fc0..df1f31d71fb8c721b44b40fa041533d302887723 100644 (file)
@@ -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
      */