69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Forms;
|
|
|
|
use App\Models\ProjectMailing;
|
|
use App\Models\ProjectReport;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Livewire\Attributes\Rule;
|
|
use Livewire\Form;
|
|
|
|
class MailingForm extends Form
|
|
{
|
|
#[Rule('required|min:3')]
|
|
public string $subject = '';
|
|
|
|
#[Rule('required')]
|
|
public string $message = '';
|
|
|
|
#[Rule('required')]
|
|
public array $recipients = [];
|
|
|
|
#[Rule('required')]
|
|
public ProjectReport $report;
|
|
|
|
public function setReport(ProjectReport $report)
|
|
{
|
|
$this->report = $report;
|
|
$this->subject = $report->project->mail_subject;
|
|
$this->message = $report->project->mail_template;
|
|
$this->recipients = $this->report->project->emailRecipients()->get(['email', 'name'])->toArray();
|
|
|
|
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
self::saveAndSendMailing($this->report, $this->subject, $this->message, $this->recipients);
|
|
|
|
$this->setReport($this->report);
|
|
}
|
|
|
|
public static function saveAndSendMailing($report, $subject, $message, $recipients) {
|
|
if ($report->documentExists()) {
|
|
$mailing = $report->project->mailings()->create([
|
|
'subject' => $subject,
|
|
'message' => $message,
|
|
'report_id' => $report->id,
|
|
]);
|
|
|
|
$mailing->attachments()->create([
|
|
'name' => $report->name,
|
|
'path' => $report->path,
|
|
]);
|
|
|
|
$mailing->recipients()->createMany($recipients);
|
|
Mail::to($mailing->recipients()->pluck('email')->toArray())
|
|
->send(new \App\Mail\ReportMailer($mailing, $report));
|
|
} else {
|
|
self::sendReportNotFoundNotificationToAdmin($report);
|
|
}
|
|
}
|
|
|
|
private static function sendReportNotFoundNotificationToAdmin($report) {
|
|
Mail::to(config('mail.from.address'))
|
|
->send(new \App\Mail\ReportNotFound($report));
|
|
}
|
|
}
|