Integration Baniere dans le backend
This commit is contained in:
@@ -18,6 +18,7 @@ final class SignatureBuilder
|
||||
public function __construct(
|
||||
private readonly TemplateRenderer $renderer,
|
||||
private readonly TemplateSelector $selector,
|
||||
private readonly CampaignSelector $campaigns,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -68,7 +69,8 @@ final class SignatureBuilder
|
||||
public function variables(DirectoryUser $user, string $variant = 'full'): array
|
||||
{
|
||||
$vars = VariableMap::build($user->getAttributes(), $this->allowedOverrides($user));
|
||||
$vars['banniere'] = $variant === 'full' ? $this->bannerHtml($user) : '';
|
||||
$campaign = $variant === 'full' ? $this->campaignFor($user) : null;
|
||||
$vars['banniere'] = $campaign ? $this->bannerHtml($campaign) : '';
|
||||
$vars['site_url'] = config('signature.public_url');
|
||||
|
||||
return $vars;
|
||||
@@ -129,27 +131,38 @@ final class SignatureBuilder
|
||||
return SharedMailbox::with('template')->where('tenant_id', $user->tenant_id)->where('address', $mailbox)->first();
|
||||
}
|
||||
|
||||
private function bannerHtml(DirectoryUser $user): string
|
||||
/** Campagne dont la bannière s'affiche pour cet agent en ce moment (une seule par signature). */
|
||||
public function campaignFor(DirectoryUser $user): ?Campaign
|
||||
{
|
||||
$groupIds = $user->groups()->pluck('directory_groups.id')->all();
|
||||
|
||||
$campaign = Campaign::running()
|
||||
->where('tenant_id', $user->tenant_id)
|
||||
->where(fn ($q) => $q->whereDoesntHave('groups')
|
||||
->orWhereHas('groups', fn ($g) => $g->whereIn('directory_groups.id', $groupIds)))
|
||||
->orderByDesc('priority')->orderBy('id')
|
||||
->first();
|
||||
|
||||
if (! $campaign) {
|
||||
return '';
|
||||
$running = Campaign::running()->where('tenant_id', $user->tenant_id)->with('groups:id')->get();
|
||||
if ($running->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$src = htmlspecialchars(config('signature.public_url').'/img/'.ltrim($campaign->image_path, '/'), ENT_QUOTES);
|
||||
$alt = htmlspecialchars($campaign->alt_text, ENT_QUOTES);
|
||||
$img = "<img src=\"{$src}\" alt=\"{$alt}\" width=\"600\" style=\"display:block;max-width:100%;height:auto;border:0\">";
|
||||
$id = $this->campaigns->select(
|
||||
$running->map(fn (Campaign $c) => [
|
||||
'id' => $c->id,
|
||||
'priority' => $c->priority,
|
||||
'group_ids' => $c->groups->pluck('id')->all(),
|
||||
])->all(),
|
||||
$user->groups()->pluck('directory_groups.id')->all(),
|
||||
);
|
||||
|
||||
return $campaign->link_url
|
||||
? '<a href="'.htmlspecialchars($campaign->link_url, ENT_QUOTES).'">'.$img.'</a>'
|
||||
return $id ? $running->firstWhere('id', $id) : null;
|
||||
}
|
||||
|
||||
/** HTML de la bannière d'une campagne : seul bloc injecté sans échappement, donc tout y est échappé ici. */
|
||||
public function bannerHtml(Campaign $campaign): string
|
||||
{
|
||||
$path = implode('/', array_map('rawurlencode', explode('/', ltrim($campaign->image_path, '/'))));
|
||||
$src = htmlspecialchars(config('signature.public_url').'/img/'.$path, ENT_QUOTES);
|
||||
$alt = htmlspecialchars($campaign->alt_text, ENT_QUOTES);
|
||||
$width = $campaign->displayWidth();
|
||||
$img = "<img src=\"{$src}\" alt=\"{$alt}\" width=\"{$width}\" style=\"display:block;width:{$width}px;max-width:100%;height:auto;border:0\">";
|
||||
|
||||
// Lien : http(s) uniquement, même si la valeur a été écrite en base hors du back-office.
|
||||
return preg_match('#^https?://#i', (string) $campaign->link_url)
|
||||
? '<a href="'.htmlspecialchars($campaign->link_url, ENT_QUOTES).'" target="_blank" rel="noopener">'.$img.'</a>'
|
||||
: $img;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user