(segment id => count) * mt3s_marketing_program_audience_segment_count(segmentId) the contact count of one segment * * Filters: * mt3s_marketing_program_linkify(text) plain text as HTML, with the URLs in it as links opening in a new tab * mt3s_marketing_program_audience_type_icon(value) the pictogram of an AudienceType case value ('persona'…) */ class MT3SMarketingProgramExtension extends AbstractExtension { public function __construct( private ItemTypeProvider $typeProvider, private CurrencyProvider $currencyProvider, private AudienceAttributes $audienceAttributes, private AudienceRepository $audienceRepository, ) { } public function getFilters(): array { return [ // The filter escapes everything itself, so its output is safe for templates. new TwigFilter('mt3s_marketing_program_linkify', fn (?string $text): string => self::linkify($text), ['is_safe' => ['html']]), new TwigFilter('mt3s_marketing_program_audience_type_icon', fn ($value): string => self::audienceTypeIcon($value)), ]; } public function getFunctions(): array { return [ new TwigFunction('mt3s_marketing_program_item_type_name', fn (string $kind, ?string $key): ?string => $this->typeProvider->getName($kind, $key)), new TwigFunction('mt3s_marketing_program_item_type_description', fn (string $kind, ?string $key): ?string => $this->typeProvider->getDescription($kind, $key)), new TwigFunction('mt3s_marketing_program_item_type_icon', fn (string $kind, ?string $key): ?string => $this->typeProvider->getIcon($kind, $key)), new TwigFunction('mt3s_marketing_program_currency', fn (): string => $this->currencyProvider->getCurrency()), new TwigFunction('mt3s_marketing_program_money', fn ($amount): string => number_format((float) $amount, 2)), new TwigFunction('mt3s_marketing_program_audience_channel_label', fn (string $key): string => Audience::CHANNELS[$key] ?? $key), new TwigFunction('mt3s_marketing_program_audience_decision_role_label', fn (?string $key): ?string => $this->typeProvider->getName(ItemTypeProvider::KIND_AUDIENCE_DECISION_ROLE, $key)), new TwigFunction('mt3s_marketing_program_audience_tone_label', fn (?string $key): ?string => $this->typeProvider->getName(ItemTypeProvider::KIND_AUDIENCE_TONE, $key)), new TwigFunction('mt3s_marketing_program_audience_attributes', fn (): array => array_keys(AudienceAttributes::ATTRIBUTES)), new TwigFunction('mt3s_marketing_program_audience_attribute_toggle', fn (string $attribute): string => AudienceAttributes::toggleParameter($attribute)), new TwigFunction('mt3s_marketing_program_audience_segment_counts', fn (iterable $items): array => $this->audienceRepository->getSegmentContactCountsForAudiences( array_map( static fn (Audience $audience): int => $audience->getId(), is_array($items) ? $items : iterator_to_array($items) ) )), new TwigFunction('mt3s_marketing_program_audience_segment_count', fn (int $leadlistId): int => $this->audienceRepository->getSegmentContactCount($leadlistId)), ]; } /** * The pictogram of an audience type. Accepts the enum itself (what the form's and entity's value * holds) or its backing value ('persona'); null defaults to Persona, the form's default. */ private static function audienceTypeIcon($value): string { if ($value instanceof AudienceType) { return $value->getIcon(); } try { return AudienceType::from((string) ($value ?? 'persona'))->getIcon(); } catch (\ValueError) { return ''; } } /** * Plain text as HTML, with the URLs in it (https://… and www.…) turned into links that open in a * new tab. For the description fields of plans and tasks, which stay plain text: typing or pasting * a URL is enough, no editor needed. Everything is escaped here, so no HTML can be injected. */ public static function linkify(?string $text): string { if (null === $text || '' === $text) { return ''; } // One capture group: preg_split then alternates plain text (even) and URLs (odd). $parts = preg_split('~((?:https?://|(?"\']+)~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE); $html = ''; foreach ($parts as $i => $part) { $html .= $i % 2 ? self::urlAnchor($part) : htmlspecialchars($part, ENT_QUOTES, 'UTF-8'); } return $html; } /** * One URL as a link. Sentence punctuation that follows the URL (".", ")", "!", …) stays outside * the link, and a bare "www." address gets the https:// prefix. */ private static function urlAnchor(string $url): string { $after = ''; while ('' !== $url && str_contains('.,;:!?', substr($url, -1))) { $after = substr($url, -1).$after; $url = substr($url, 0, -1); } // An unbalanced closing parenthesis belongs to the sentence, not the URL. if (str_ends_with($url, ')') && !str_contains($url, '(')) { $after = ')'.$after; $url = substr($url, 0, -1); } // Only a prefix is left ("www.", "https://"): not a link after all. if ('' === $url || 'www.' === $url || 'http://' === $url || 'https://' === $url) { return htmlspecialchars($url.$after, ENT_QUOTES, 'UTF-8'); } $href = preg_match('~^https?://~i', $url) ? $url : 'https://'.$url; return sprintf( '%s%s', htmlspecialchars($href, ENT_QUOTES, 'UTF-8'), htmlspecialchars($url, ENT_QUOTES, 'UTF-8'), htmlspecialchars($after, ENT_QUOTES, 'UTF-8') ); } }