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

97 lines
3.7 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 ProfileConvertedByAdminMail extends Mailable implements ShouldQueue
{
use Queueable;
use SerializesModels;
protected $profile;
protected $convertedTo;
protected $manager; // User model of the organization manager (for 'organization' conversions)
protected $buttonUrl;
public $locale;
/**
* @param mixed $profile The profile after conversion (new model type)
* @param string $convertedTo 'organization' or 'user'
* @param mixed|null $manager The User model who became organization manager
*/
public function __construct($profile, string $convertedTo, $manager = null)
{
$this->profile = $profile;
$this->convertedTo = $convertedTo;
$this->manager = $manager;
$this->locale = $profile->lang_preference ?? config('app.fallback_locale');
if (empty($this->locale)) {
$this->locale = config('app.fallback_locale');
}
$profileClass = get_class($profile);
$mainPath = LaravelLocalization::getURLFromRouteNameTranslated($this->locale, 'routes.main');
$mainUrl = url($mainPath);
if ($profileClass === 'App\\Models\\User') {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('user.direct-login', ['userId' => $profile->id, 'intended' => $mainUrl, 'name' => $profile->name]),
$this->locale
);
} elseif ($profileClass === 'App\\Models\\Organization') {
// Button goes to the organization via the manager's direct-login,
// pre-filling the manager's username at the login page
if ($manager) {
$orgUrl = LaravelLocalization::localizeURL(
route('organization.direct-login', ['organizationId' => $profile->id]),
$this->locale
);
$this->buttonUrl = LaravelLocalization::localizeURL(
route('user.direct-login', ['userId' => $manager->id, 'intended' => $orgUrl, 'name' => $manager->name]),
$this->locale
);
} else {
$this->buttonUrl = LaravelLocalization::localizeURL(
route('organization.direct-login', ['organizationId' => $profile->id, 'intended' => $mainUrl]),
$this->locale
);
}
} else {
$this->buttonUrl = LaravelLocalization::localizeURL(route('login'), $this->locale);
}
Log::info('ProfileConvertedByAdminMail: Constructor called', [
'profile_id' => $profile->id,
'profile_type' => $profileClass,
'converted_to' => $convertedTo,
'manager_id' => $manager?->id,
'button_url' => $this->buttonUrl,
]);
}
public function build()
{
app()->setLocale($this->locale);
$subject = trans('messages.profile_converted_by_admin_subject', [], $this->locale);
return $this
->from(timebank_config('mail.support.email'), timebank_config('mail.support.name'))
->subject($subject)
->view('emails.profile-converted.profile-converted')
->with([
'profile' => $this->profile,
'convertedTo' => $this->convertedTo,
'manager' => $this->manager,
'buttonUrl' => $this->buttonUrl,
]);
}
}