Files
timebank-cc-public/app/Mail/InactiveProfileWarning1Mail.php

130 lines
4.6 KiB
PHP

<?php
namespace App\Mail;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class InactiveProfileWarning1Mail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $profile;
public $profileType;
public $secondsRemaining;
public $daysRemaining;
public $accounts;
public $totalBalance;
public $daysSinceLogin;
public $locale;
public $loginUrl;
public function __construct($profile, $profileType, $timeRemaining, $daysRemaining, $accounts, $totalBalance, $daysSinceLogin)
{
$this->profile = $profile;
$this->profileType = $profileType;
$this->secondsRemaining = $daysRemaining * 86400;
$this->daysRemaining = $daysRemaining;
$this->accounts = $accounts;
$this->totalBalance = $totalBalance;
$this->daysSinceLogin = $daysSinceLogin;
// Set locale from profile preference
$this->locale = $profile->lang_preference ?? config('app.fallback_locale', 'en');
// Generate direct login URL
if ($profileType === 'User') {
$this->loginUrl = route('user.direct-login', [
'userId' => $profile->id,
'name' => $profile->name
]);
} elseif ($profileType === 'Organization') {
$this->loginUrl = route('organization.direct-login', [
'organizationId' => $profile->id
]);
}
}
public function envelope(): Envelope
{
return new Envelope(
subject: trans('Warning: Your profile will be deleted soon', [], $this->locale),
);
}
public function content(): Content
{
return new Content(
view: 'emails.inactive-profiles.warning-1',
);
}
public function build()
{
// Set application locale for this email before any translation calls
app()->setLocale($this->locale);
return $this
->from(
timebank_config('mail.system_admin.email'),
timebank_config('mail.system_admin.name')
)
->subject(trans('Warning: Your profile will be deleted soon', [], $this->locale))
->view('emails.inactive-profiles.warning-1')
->with([
'profile' => $this->profile,
'profileType' => $this->profileType,
'timeRemaining' => $this->formatTimeRemaining($this->secondsRemaining),
'daysRemaining' => $this->daysRemaining,
'accounts' => $this->accounts,
'totalBalance' => $this->totalBalance,
'daysSinceLogin' => $this->daysSinceLogin,
'periodNotLoggedIn' => $this->formatPeriod($this->profile->last_login_at),
'loginUrl' => $this->loginUrl,
'totalInactiveDays' => timebank_config('profile_inactive.days_not_logged_in') + timebank_config('delete_profile.days_after_inactive.run_delete'),
]);
}
protected function formatPeriod($lastLoginAt): string
{
if (!$lastLoginAt) {
return '';
}
$seconds = (int) (now()->diffInSeconds(Carbon::parse($lastLoginAt), true));
$days = $seconds / 86400;
if ($days >= 7) {
$weeks = round($days / 7);
return trans_choice('weeks_not_logged_in', $weeks, ['count' => $weeks]);
} elseif ($days >= 1) {
$daysRounded = round($days);
return trans_choice('days_not_logged_in', $daysRounded, ['count' => $daysRounded]);
} else {
$hours = max(1, round($seconds / 3600));
return trans_choice('hours_not_logged_in', $hours, ['count' => $hours]);
}
}
protected function formatTimeRemaining($seconds): string
{
$days = $seconds / 86400;
if ($days >= 7) {
$weeks = round($days / 7);
return trans_choice('weeks_remaining', $weeks, ['count' => $weeks]);
} elseif ($days >= 1) {
$daysRounded = round($days);
return trans_choice('days_remaining', $daysRounded, ['count' => $daysRounded]);
} elseif ($seconds >= 3600) {
$hours = round($seconds / 3600);
return trans_choice('hours_remaining', $hours, ['count' => $hours]);
} else {
$minutes = max(1, round($seconds / 60));
return trans_choice('minutes_remaining', $minutes, ['count' => $minutes]);
}
}
}