Files

181 lines
6.7 KiB
PHP

<?php
namespace App\Mail;
use App\Models\Mailing;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class TestNewsletterMail extends Mailable
{
use Queueable, SerializesModels;
protected $mailing;
protected $contentBlocks;
protected $recipientLocale;
/**
* Create a new message instance.
*/
public function __construct(Mailing $mailing, $locale)
{
$this->mailing = $mailing;
$this->recipientLocale = $locale;
// Set locale before generating content: locale-scoped relations (e.g. CityLocale::translations)
// are queried during content generation, not later in build().
App::setLocale($locale);
$this->contentBlocks = $this->generateContentBlocksForLocale($locale);
}
/**
* Build the message.
*/
public function build()
{
// Set locale for the email
App::setLocale($this->recipientLocale);
$fromAddress = timebank_config("mailing.from_address.{$this->mailing->type}");
$subject = $this->mailing->getSubjectForLocale($this->recipientLocale);
$emailSubject = "[TEST - {$this->recipientLocale}] " . $subject;
return $this
->from($fromAddress, config('app.name'))
->subject($emailSubject)
->view('emails.newsletter.wrapper')
->with([
'subject' => $subject,
'mailingTitle' => $this->mailing->title,
'mailingId' => $this->mailing->id,
'locale' => $this->recipientLocale,
'contentBlocks' => $this->contentBlocks,
'unsubscribeUrl' => '#test-unsubscribe-link',
'isTestMail' => true,
]);
}
/**
* Generate content blocks for a specific locale
*/
protected function generateContentBlocksForLocale($locale)
{
$blocks = [];
$contentBlocks = $this->mailing->getContentBlocksForLocale($locale);
foreach ($contentBlocks as $block) {
$post = \App\Models\Post::with(['translations', 'category', 'meeting.location.city.translations', 'poll'])->find($block['post_id']);
if (!$post) {
continue;
}
// Get translation for the specific locale
$translation = $this->mailing->getPostTranslationForLocale($post->id, $locale);
if (!$translation) {
continue; // Skip posts without translations in this locale
}
// Determine post type for template selection
$postType = $this->determinePostType($post);
// Prepare post data for template
$postData = $this->preparePostData($post, $translation);
$blocks[] = [
'type' => $postType,
'data' => $postData,
'template' => \App\Models\Mailing::blockTemplate($postType)
];
}
return $blocks;
}
/**
* Determine post type based on category or content
*/
protected function determinePostType($post)
{
return \App\Models\Mailing::determinePostType($post);
}
/**
* Prepare post data for email template
*/
protected function preparePostData($post, $translation)
{
// Generate fully localized URL with translated route path for recipient's language
$url = LaravelLocalization::getURLFromRouteNameTranslated(
$this->recipientLocale,
'routes.post.show_by_slug',
['slug' => $translation->slug]
);
$data = [
'title' => $translation->title,
'excerpt' => $translation->excerpt ?: Str::limit(strip_tags($translation->content ?? ''), 150),
'content' => $translation->content,
'url' => $url,
'date' => $post->updated_at->locale($this->recipientLocale)->translatedFormat('M j, Y'),
'author' => $post->author ? $post->author->name : null,
'show_button' => $this->mailing->type !== 'system_message',
];
// Poll posts link to the poll page and show whether the poll is open
$data = array_merge($data, \App\Models\Mailing::pollBlockData($post, $this->recipientLocale));
// Add category information
if ($post->category) {
$categoryTranslation = $post->category->translations()->where('locale', $this->recipientLocale)->first();
$data['category'] = $categoryTranslation ? $categoryTranslation->name : $post->category->translations()->first()->name;
}
// Add location prefix for news
if ($post->category && $post->category->categoryable && $post->category->categoryable->translations) {
$locationTranslation = $post->category->categoryable->translations->where('locale', $this->recipientLocale)->first();
if ($locationTranslation && $locationTranslation->name) {
$data['location_prefix'] = strtoupper($locationTranslation->name);
}
}
// Add event-specific data
if ($post->meeting) {
$data['venue'] = $post->meeting->venue;
$cityTranslations = $post->meeting->location?->city?->translations;
$data['city'] = $cityTranslations?->firstWhere('locale', $this->recipientLocale)?->name
?? $cityTranslations?->first()?->name
?? null;
}
// Add event date/time from meeting->from only; hide if not set
$eventFromDate = $post->meeting?->from;
if ($eventFromDate) {
$eventDate = \Carbon\Carbon::parse($eventFromDate);
$data['event_date'] = ucfirst($eventDate->locale($this->recipientLocale)->translatedFormat('j F'));
$data['event_time'] = $eventDate->locale($this->recipientLocale)->translatedFormat('H:i');
if ($post->meeting->till) {
$data['event_time_end'] = \Carbon\Carbon::parse($post->meeting->till)->locale($this->recipientLocale)->translatedFormat('H:i');
}
}
// Add image if available - use email conversion (resized without cropping)
if ($post->hasMedia('posts')) {
$data['image'] = $post->getFirstMediaUrl('posts', 'email');
// Add media caption and owner for image posts
$media = $post->getFirstMedia('posts');
if ($media) {
$captionKey = 'caption-' . $this->recipientLocale;
$data['media_caption'] = $media->getCustomProperty($captionKey, '');
$data['media_owner'] = $media->getCustomProperty('owner', '');
}
}
return $data;
}
}