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

102 lines
3.8 KiB
PHP

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class AccountsEditedByAdminMail extends Mailable implements ShouldQueue
{
use Queueable;
use SerializesModels;
protected $profile;
protected $changedFields;
protected $buttonUrl;
public $locale;
public function __construct($profile, $changedFields = [])
{
$this->profile = $profile;
$this->changedFields = $changedFields;
Log::info('AccountsEditedByAdminMail: Constructor called', [
'profile_id' => $profile->id,
'profile_type' => get_class($profile),
'profile_lang_preference' => $profile->lang_preference ?? 'not set',
'changed_fields' => $changedFields,
]);
$this->locale = $profile->lang_preference ?? config('app.fallback_locale');
if (empty($this->locale)) {
$this->locale = config('app.fallback_locale');
}
// Generate login URL redirecting to the transactions view
$profileClass = get_class($profile);
$transactionsPath = LaravelLocalization::getURLFromRouteNameTranslated($this->locale, 'routes.transactions');
$transactionsUrl = url($transactionsPath);
if ($profileClass === 'App\\Models\\User') {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('user.direct-login', ['userId' => $profile->id, 'intended' => $transactionsUrl, 'name' => $profile->name]),
$this->locale
);
} elseif ($profileClass === 'App\\Models\\Organization') {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('organization.direct-login', ['organizationId' => $profile->id, 'intended' => $transactionsUrl]),
$this->locale
);
} elseif ($profileClass === 'App\\Models\\Bank') {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('bank.direct-login', ['bankId' => $profile->id, 'intended' => $transactionsUrl]),
$this->locale
);
} elseif ($profileClass === 'App\\Models\\Admin') {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('admin.direct-login', ['adminId' => $profile->id, 'intended' => $transactionsUrl]),
$this->locale
);
} else {
$this->buttonUrl = LaravelLocalization::localizeURL(route('login'), $this->locale);
}
Log::info('AccountsEditedByAdminMail: Generated button URL', [
'button_url' => $this->buttonUrl,
'profile_class' => $profileClass,
]);
}
public function build()
{
app()->setLocale($this->locale);
$subject = trans('messages.accounts_edited_by_admin_subject', [], $this->locale);
Log::info('AccountsEditedByAdminMail: Building email', [
'subject' => $subject,
'locale' => $this->locale,
'changed_fields' => $this->changedFields,
]);
$translatedFields = array_map(
fn($field) => __($field['key'], $field['params'] ?? []),
$this->changedFields
);
return $this
->from(timebank_config('mail.support.email'), timebank_config('mail.support.name'))
->subject($subject)
->view('emails.accounts-edited.accounts-edited')
->with([
'profile' => $this->profile,
'changedFields' => $translatedFields,
'buttonUrl' => $this->buttonUrl,
]);
}
}