}
+ /**
+ * @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');
$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([
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;
*/
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
*/
*/
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);
- $view = ($this->type == 1) ? $this->batch->renderFromContent() : $this->html;
+ $view = $this->html ?? $this->batch->renderFromContent();
'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);
--- /dev/null
+<?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 [];
+ }
+}
namespace App\Nova;
+use App\Nova\Actions\PdfAttachmentSetting;
use App\Nova\Filters\AccountStates;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
}
+ public function actions(Request $request)
+ {
+ return array_merge([
+ new PdfAttachmentSetting(true),
+ new PdfAttachmentSetting(false)
+ ],parent::actions($request));
+ }
}
* @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
*/
/**
* @param false $download
* @return \Symfony\Component\HttpFoundation\StreamedResponse
+ * @throws \League\Flysystem\FileNotFoundException
*/
public function downloadFile($download = false)
{
}
+
/**
* Processes pdf to json conversion for in-file search
*/