first delivery

This commit is contained in:
Alexandre Gain committed 2026-11-07 19:31:49 +01:00
1 parent 793fbc0c91
commit f6684f3b2e
65 files changed
+5828

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Service;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Symfony\Component\Intl\Currencies;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* The currency of all amounts in the plugin (plan budgets, allocation costs and totals).
*
* It is a general setting, chosen by an administrator in Settings > Configuration > Plan Settings and
* stored in the local.php parameter plan_currency; users don't pick a currency. Amounts aren't converted
* when it changes: they are simply shown with the new currency.
*/
class CurrencyProvider
{
public const CONFIG_PARAMETER = 'plan_currency';
public const DEFAULT_CURRENCY = 'EUR';
/**
* The main world currencies offered in the settings (ISO 4217), most traded first.
*/
public const MAIN_CURRENCIES = [
'USD', 'EUR', 'JPY', 'GBP', 'CNY', 'AUD', 'CAD', 'CHF', 'HKD', 'SGD',
'SEK', 'NOK', 'DKK', 'NZD', 'KRW', 'INR', 'BRL', 'MXN', 'ZAR', 'PLN',
'CZK', 'HUF', 'RON', 'TRY', 'AED', 'SAR', 'ILS', 'THB', 'MYR', 'IDR',
'PHP', 'TWD', 'RUB', 'MAD', 'EGP', 'NGN', 'CLP', 'COP', 'ARS', 'PEN',
];
public function __construct(
private CoreParametersHelper $coreParametersHelper,
private TranslatorInterface $translator,
) {
}
/**
* The general currency code, e.g. "EUR".
*/
public function getCurrency(): string
{
$currency = $this->coreParametersHelper->get(self::CONFIG_PARAMETER);
return is_string($currency) && in_array($currency, self::MAIN_CURRENCIES, true) ? $currency : self::DEFAULT_CURRENCY;
}
/**
* Dropdown choices: "EUR - Euro" => "EUR", names in the user's language.
*
* @return array<string, string>
*/
public function getChoices(): array
{
$locale = $this->translator->getLocale();
$choices = [];
foreach (self::MAIN_CURRENCIES as $code) {
try {
$name = Currencies::getName($code, $locale);
} catch (\Throwable) {
$name = Currencies::exists($code) ? Currencies::getName($code) : $code;
}
$choices[$code.' - '.$name] = $code;
}
return $choices;
}
}
+214
View File
@@ -0,0 +1,214 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Service;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\LanguageHelper;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Reads the item types set up in Settings > Configuration > Plan. There is one list of types per kind of
* plan item:
* - KIND_DESCRIPTION: types of description items (Description tab), parameter plan_description_item_types;
* - KIND_ALLOCATION: types of allocation items (Allocation tab), parameter plan_allocation_item_types.
*
* Each list is stored in local.php, in display order:
*
* [
* ['key' => 'type_1a2b3c4d', 'names' => ['en_US' => 'Context', 'fr' => 'Contexte'],
* 'descriptions' => ['en_US' => 'Why this plan exists', 'fr' => '…'], 'icon' => 'ri-lightbulb-line'],
* …
* ]
*
* Names and descriptions fall back from the user's language to the default language (the "locale"
* parameter), then to the first one filled in. The key is what items store; it never changes once the
* type is created.
*/
class ItemTypeProvider
{
public const KIND_DESCRIPTION = 'description';
public const KIND_ALLOCATION = 'allocation';
/**
* Kind => local.php parameter.
*/
public const PARAMETERS = [
self::KIND_DESCRIPTION => 'plan_description_item_types',
self::KIND_ALLOCATION => 'plan_allocation_item_types',
];
public function __construct(
private CoreParametersHelper $coreParametersHelper,
private LanguageHelper $languageHelper,
private TranslatorInterface $translator,
) {
}
public static function getParameter(string $kind): string
{
return self::PARAMETERS[$kind] ?? throw new \InvalidArgumentException('Unknown item kind: '.$kind);
}
/**
* The types as stored, cleaned up: only entries with a key, names/descriptions as locale => text.
*
* @return array<int, array{key: string, names: array<string, string>, descriptions: array<string, string>, icon?: string}>
*/
public function getTypes(string $kind): array
{
$types = $this->coreParametersHelper->get(self::getParameter($kind));
return self::normalize(is_array($types) ? $types : []);
}
/**
* Dropdown choices for an item form: localized name => key, in configured order.
*
* @return array<string, string>
*/
public function getChoices(string $kind, ?string $locale = null): array
{
$choices = [];
foreach ($this->getTypes($kind) as $type) {
$name = $this->pick($type['names'], $locale) ?: $type['key'];
// Two types with the same name would collide as choice labels; tell them apart.
$label = isset($choices[$name]) ? $name.' ('.$type['key'].')' : $name;
$choices[$label] = $type['key'];
}
return $choices;
}
/**
* Localized name of a type, or null when the key isn't a configured type of this kind.
*/
public function getName(string $kind, ?string $key, ?string $locale = null): ?string
{
$type = $this->find($kind, $key);
return null === $type ? null : ($this->pick($type['names'], $locale) ?: $type['key']);
}
/**
* Localized description of a type ('' when none), or null when the key isn't a configured type.
*/
public function getDescription(string $kind, ?string $key, ?string $locale = null): ?string
{
$type = $this->find($kind, $key);
return null === $type ? null : $this->pick($type['descriptions'], $locale, '');
}
/**
* Pictogram of a type (a Remix Icon class such as "ri-lightbulb-line"), or null when it has none or
* the key isn't a configured type. Only description item types have pictograms.
*/
public function getIcon(string $kind, ?string $key): ?string
{
return $this->find($kind, $key)['icon'] ?? null;
}
/**
* Languages the admin can fill in: the languages installed in Mautic, default language first.
*
* @return array<string, string> locale => language name
*/
public function getLanguages(): array
{
$languages = $this->languageHelper->getSupportedLanguages();
$default = $this->getDefaultLocale();
if (isset($languages[$default])) {
$languages = [$default => $languages[$default]] + $languages;
}
return $languages;
}
public function getDefaultLocale(): string
{
$locale = $this->coreParametersHelper->get('locale');
return is_string($locale) && '' !== $locale ? $locale : 'en_US';
}
/**
* @param array<mixed> $types
*
* @return array<int, array{key: string, names: array<string, string>, descriptions: array<string, string>, icon?: string}>
*/
public static function normalize(array $types): array
{
$clean = [];
foreach ($types as $type) {
if (!is_array($type) || !isset($type['key']) || !is_string($type['key']) || '' === trim($type['key'])) {
continue;
}
$entry = [
'key' => trim($type['key']),
'names' => self::texts($type['names'] ?? []),
'descriptions' => self::texts($type['descriptions'] ?? []),
];
// Pictogram (description item types only); kept only when it's one of the offered icons.
if (isset($type['icon']) && is_string($type['icon']) && Pictograms::isValid($type['icon'])) {
$entry['icon'] = $type['icon'];
}
$clean[] = $entry;
}
return $clean;
}
/**
* @param mixed $texts
*
* @return array<string, string>
*/
private static function texts($texts): array
{
if (!is_array($texts)) {
return [];
}
$clean = [];
foreach ($texts as $locale => $text) {
if (is_string($locale) && is_string($text) && '' !== trim($text)) {
$clean[$locale] = trim($text);
}
}
return $clean;
}
/**
* @return array{key: string, names: array<string, string>, descriptions: array<string, string>, icon?: string}|null
*/
private function find(string $kind, ?string $key): ?array
{
foreach ($this->getTypes($kind) as $type) {
if ($type['key'] === $key) {
return $type;
}
}
return null;
}
/**
* @param array<string, string> $texts
*/
private function pick(array $texts, ?string $locale, ?string $empty = null): string
{
$locale ??= $this->translator->getLocale();
return $texts[$locale]
?? $texts[$this->getDefaultLocale()]
?? (reset($texts) ?: ($empty ?? ''));
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Service;
/**
* Pictograms an administrator can give to a description item type (Settings > Configuration > Plan
* Settings). They are icons of the Remix Icon set that Mautic ships ("ri-*" classes); each one was
* checked against Mautic 7's icon stylesheet.
*/
final class Pictograms
{
public const ICONS = [
// Ideas, goals, guidance
'ri-lightbulb-line', 'ri-focus-3-line', 'ri-compass-3-line', 'ri-route-line', 'ri-flag-line',
'ri-rocket-line', 'ri-trophy-line', 'ri-award-line', 'ri-medal-line', 'ri-star-line',
'ri-sparkling-line', 'ri-magic-line', 'ri-key-2-line',
// Creative, style, tone
'ri-palette-line', 'ri-brush-line', 'ri-quill-pen-line', 'ri-pencil-line', 'ri-emotion-line',
'ri-emotion-happy-line', 'ri-heart-line', 'ri-thumb-up-line', 'ri-image-line', 'ri-camera-line',
'ri-video-line', 'ri-mic-line', 'ri-music-line', 'ri-layout-line', 'ri-pages-line',
'ri-presentation-line', 'ri-slideshow-line',
// Messages, content
'ri-chat-quote-line', 'ri-chat-3-line', 'ri-volume-up-line', 'ri-megaphone-line', 'ri-mail-line',
'ri-newspaper-line', 'ri-article-line', 'ri-file-text-line', 'ri-file-list-3-line',
'ri-book-open-line', 'ri-bookmark-line', 'ri-links-line',
// People, market
'ri-team-line', 'ri-group-line', 'ri-user-line', 'ri-user-heart-line', 'ri-customer-service-2-line',
'ri-hand-heart-line', 'ri-building-line', 'ri-briefcase-line', 'ri-store-2-line',
'ri-shopping-cart-line', 'ri-price-tag-3-line', 'ri-gift-line', 'ri-truck-line',
'ri-global-line', 'ri-earth-line', 'ri-map-pin-line',
// Figures, time
'ri-money-euro-circle-line', 'ri-wallet-3-line', 'ri-bar-chart-line', 'ri-pie-chart-line',
'ri-line-chart-line', 'ri-calendar-line', 'ri-time-line', 'ri-timer-line',
// Rules, status, structure
'ri-information-line', 'ri-question-line', 'ri-error-warning-line', 'ri-shield-check-line',
'ri-scales-3-line', 'ri-lock-line', 'ri-checkbox-circle-line', 'ri-list-check', 'ri-search-line',
'ri-eye-line', 'ri-tools-line', 'ri-settings-3-line', 'ri-stack-line', 'ri-node-tree', 'ri-leaf-line',
];
public static function isValid(?string $icon): bool
{
return null !== $icon && in_array($icon, self::ICONS, true);
}
/**
* Dropdown choices: readable name => icon class, e.g. "Lightbulb" => "ri-lightbulb-line".
*
* @return array<string, string>
*/
public static function getChoices(): array
{
$choices = [];
foreach (self::ICONS as $icon) {
$name = preg_replace(['/^ri-/', '/-line$/'], '', $icon);
$name = ucfirst(str_replace('-', ' ', (string) $name));
$choices[$name] = $icon;
}
return $choices;
}
}