Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
152 lines
7.3 KiB
PHP
152 lines
7.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\Twig;
|
|
|
|
use MauticPlugin\ProgramBundle\Entity\Audience;
|
|
use MauticPlugin\ProgramBundle\Entity\AudienceType;
|
|
use MauticPlugin\ProgramBundle\Entity\AudienceRepository;
|
|
use MauticPlugin\ProgramBundle\Service\CurrencyProvider;
|
|
use MauticPlugin\ProgramBundle\Service\ItemTypeProvider;
|
|
use MauticPlugin\ProgramBundle\Service\AudienceAttributes;
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFilter;
|
|
use Twig\TwigFunction;
|
|
|
|
/**
|
|
* Twig functions for the plan templates (kind: "description", "allocation", "objective" or "task"):
|
|
* program_item_type_name(kind, key) localized type name, or null for an unknown key
|
|
* program_item_type_description(kind, key) localized type description ('' when none)
|
|
* program_item_type_icon(kind, key) the type's pictogram (Remix Icon class), or null
|
|
* program_currency() the general currency code, e.g. EUR
|
|
* program_money(amount) an amount with 2 decimals, e.g. 12,500.00
|
|
* program_audience_channel_label(key) the label key of an audience channel, e.g. "Email"
|
|
* program_audience_decision_role_label(key) the localized name of a decision role value, or null
|
|
* program_audience_tone_label(key) the localized name of a tone value, or null
|
|
* program_audience_attributes() the togglable audience attribute keys, in form order
|
|
* program_audience_attribute_toggle(key) the local.php parameter name of an attribute's toggle
|
|
* program_audience_segment_counts(items) contact counts of the segments of the listed audiences,
|
|
* keyed audience id => (segment id => count)
|
|
* program_audience_segment_count(segmentId) the contact count of one segment
|
|
*
|
|
* Filters:
|
|
* program_linkify(text) plain text as HTML, with the URLs in it as links opening in a new tab
|
|
* program_audience_type_icon(value) the pictogram of an AudienceType case value ('persona'…)
|
|
*/
|
|
class ProgramExtension 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('program_linkify', fn (?string $text): string => self::linkify($text), ['is_safe' => ['html']]),
|
|
new TwigFilter('program_audience_type_icon', fn ($value): string => self::audienceTypeIcon($value)),
|
|
];
|
|
}
|
|
|
|
public function getFunctions(): array
|
|
{
|
|
return [
|
|
new TwigFunction('program_item_type_name', fn (string $kind, ?string $key): ?string => $this->typeProvider->getName($kind, $key)),
|
|
new TwigFunction('program_item_type_description', fn (string $kind, ?string $key): ?string => $this->typeProvider->getDescription($kind, $key)),
|
|
new TwigFunction('program_item_type_icon', fn (string $kind, ?string $key): ?string => $this->typeProvider->getIcon($kind, $key)),
|
|
new TwigFunction('program_currency', fn (): string => $this->currencyProvider->getCurrency()),
|
|
new TwigFunction('program_money', fn ($amount): string => number_format((float) $amount, 2)),
|
|
new TwigFunction('program_audience_channel_label', fn (string $key): string => Audience::CHANNELS[$key] ?? $key),
|
|
new TwigFunction('program_audience_decision_role_label', fn (?string $key): ?string => $this->typeProvider->getName(ItemTypeProvider::KIND_AUDIENCE_DECISION_ROLE, $key)),
|
|
new TwigFunction('program_audience_tone_label', fn (?string $key): ?string => $this->typeProvider->getName(ItemTypeProvider::KIND_AUDIENCE_TONE, $key)),
|
|
new TwigFunction('program_audience_attributes', fn (): array => array_keys(AudienceAttributes::ATTRIBUTES)),
|
|
new TwigFunction('program_audience_attribute_toggle', fn (string $attribute): string => AudienceAttributes::toggleParameter($attribute)),
|
|
new TwigFunction('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('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?://|(?<![\w.])www\.)[^\s<>"\']+)~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(
|
|
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>%s',
|
|
htmlspecialchars($href, ENT_QUOTES, 'UTF-8'),
|
|
htmlspecialchars($url, ENT_QUOTES, 'UTF-8'),
|
|
htmlspecialchars($after, ENT_QUOTES, 'UTF-8')
|
|
);
|
|
}
|
|
}
|