From 17bab06ff5eacf97e2ffaaf2e744ee336ed3f79c Mon Sep 17 00:00:00 2001 From: Molkobain Date: Wed, 6 Mar 2024 18:40:47 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B07157=20-=20Migrate=20enum=20values=20fro?= =?UTF-8?q?m=20strings=20to=20PHP=20Enum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/trigger.class.inc.php | 2 +- lib/composer/autoload_classmap.php | 1 + lib/composer/autoload_static.php | 1 + .../NotificationsCenterController.php | 15 ++++++++------- sources/Core/Trigger/Enum/SubscriptionPolicy.php | 16 ++++++++++++++++ .../Notification/NotificationsService.php | 3 ++- 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 sources/Core/Trigger/Enum/SubscriptionPolicy.php diff --git a/core/trigger.class.inc.php b/core/trigger.class.inc.php index 24a8ecb85..701333917 100644 --- a/core/trigger.class.inc.php +++ b/core/trigger.class.inc.php @@ -53,7 +53,7 @@ abstract class Trigger extends cmdbAbstractObject MetaModel::Init_AddAttribute(new AttributeEnumSet("context", array("allowed_values" => null, "possible_values" => new ValueSetEnumPadded($aTags, true), "sql" => "context", "depends_on" => array(), "is_null_allowed" => true, "max_items" => 12))); // "complement" is a computed field, fed by Trigger sub-classes, in general in ComputeValues method, for eg. the TriggerOnObject fed it with target_class info MetaModel::Init_AddAttribute(new AttributeString("complement", array("allowed_values" => null, "sql" => "complement", "default_value" => null, "is_null_allowed" => true, "depends_on" => array()))); - MetaModel::Init_AddAttribute(new AttributeEnum("subscription_policy", array("allowed_values" => new ValueSetEnum('allow_no_channel,force_at_least_one_channel,force_all_channels'), "sql" => "subscription_policy", "default_value" => 'allow_no_channel', "is_null_allowed" => false, "depends_on" => array()))); + MetaModel::Init_AddAttribute(new AttributeEnum("subscription_policy", array("allowed_values" => new ValueSetEnum(implode(",", Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::cases())), "sql" => "subscription_policy", "default_value" => \Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::AllowNoChannel, "is_null_allowed" => false, "depends_on" => array()))); // Display lists MetaModel::Init_SetZListItems('details', array('finalclass', 'description', 'context', 'subscription_policy', 'action_list', 'complement')); // Attributes to be displayed for the complete details diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index b7dc1b2dc..75334c1ce 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -417,6 +417,7 @@ return array( 'Combodo\\iTop\\Core\\Kpi\\KpiLogData' => $baseDir . '/sources/Core/Kpi/KpiLogData.php', 'Combodo\\iTop\\Core\\MetaModel\\FriendlyNameType' => $baseDir . '/sources/Core/MetaModel/FriendlyNameType.php', 'Combodo\\iTop\\Core\\MetaModel\\HierarchicalKey' => $baseDir . '/sources/Core/MetaModel/HierarchicalKey.php', + 'Combodo\\iTop\\Core\\Trigger\\Enum\\SubscriptionPolicy' => $baseDir . '/sources/Core/Trigger/Enum/SubscriptionPolicy.php', 'Combodo\\iTop\\DesignDocument' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => $baseDir . '/sources/Form/Field/AbstractSimpleField.php', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 5006a1900..6c8d92a98 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -792,6 +792,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f 'Combodo\\iTop\\Core\\Kpi\\KpiLogData' => __DIR__ . '/../..' . '/sources/Core/Kpi/KpiLogData.php', 'Combodo\\iTop\\Core\\MetaModel\\FriendlyNameType' => __DIR__ . '/../..' . '/sources/Core/MetaModel/FriendlyNameType.php', 'Combodo\\iTop\\Core\\MetaModel\\HierarchicalKey' => __DIR__ . '/../..' . '/sources/Core/MetaModel/HierarchicalKey.php', + 'Combodo\\iTop\\Core\\Trigger\\Enum\\SubscriptionPolicy' => __DIR__ . '/../..' . '/sources/Core/Trigger/Enum/SubscriptionPolicy.php', 'Combodo\\iTop\\DesignDocument' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => __DIR__ . '/../..' . '/sources/Form/Field/AbstractSimpleField.php', diff --git a/sources/Controller/Notifications/NotificationsCenterController.php b/sources/Controller/Notifications/NotificationsCenterController.php index d121af6bf..8520879dc 100644 --- a/sources/Controller/Notifications/NotificationsCenterController.php +++ b/sources/Controller/Notifications/NotificationsCenterController.php @@ -11,6 +11,7 @@ use Combodo\iTop\Application\UI\Base\Component\Input\Set\SetUIBlockFactory; use Combodo\iTop\Application\UI\Base\Component\Panel\Panel; use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock; use Combodo\iTop\Application\WebPage\iTopWebPage; +use Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy; use Combodo\iTop\Renderer\BlockRenderer; use Combodo\iTop\Service\Notification\NotificationsRepository; use Combodo\iTop\Service\Router\Router; @@ -82,7 +83,7 @@ class NotificationsCenterController extends Controller // Add the action notification to the list of actions notifications for the trigger $oActionsNotificationsByTrigger[$iTriggerId][] = $oSubscribedActionNotification; // Add the subscribed status to the list of subscribed actions notifications for the trigger - $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === 'force_all_channels'; + $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels; } // Build table rows @@ -155,7 +156,7 @@ class NotificationsCenterController extends Controller $oChannelSet->SetOptionsTemplate('application/object/set/option_renderer.html.twig'); $oChannelSet->SetItemsTemplate('application/preferences/notification-center/item_renderer.html.twig'); // Disable the input set if the subscription policy is 'force_all_channels' - if($sTriggerSubscriptionPolicy === 'force_all_channels'){ + if ($sTriggerSubscriptionPolicy === SubscriptionPolicy::ForceAllChannels) { $oChannelSet->SetIsDisabled(true); } // Add a CSRF Token @@ -190,8 +191,8 @@ $.ajax({ }); JS ); - // Set the minimum number of channels to 1 if the subscription policy is 'force_at_least_one_channel' - if($sTriggerSubscriptionPolicy === 'force_at_least_one_channel') + // Set the minimum number of channels to 1 if the subscription policy is {@see SubscriptionPolicy::ForceAtLeastOneChannel} + if($sTriggerSubscriptionPolicy === SubscriptionPolicy::ForceAtLeastOneChannel) { $oChannelSet->SetMinItems(1); } @@ -289,7 +290,7 @@ JS // Add the action notification to the list of actions notifications for the trigger $oActionsNotificationsByTrigger[$iTriggerId][] = $oSubscribedActionNotification; // Add the subscribed status to the list of subscribed actions notifications for the trigger - $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === 'force_all_channels'; + $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels; } $oPage->AddTabContainer('NotificationsCenter', '', $oNotificationsPanel); @@ -491,7 +492,7 @@ JS throw new \Exception('Invalid trigger'); } // Check the trigger subscription policy - if($oTrigger->Get('subscription_policy') === 'force_all_channels'){ + if ($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels) { throw new \Exception('You are not allowed to unsubscribe from this channel'); } @@ -501,7 +502,7 @@ JS throw new \Exception('You are not subscribed to any channel'); } // Check the trigger subscription policy and if we are subscribed to at least 1 channel if necessary - if($oTrigger->Get('subscription_policy') === 'force_at_least_one_channel') { + if($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAtLeastOneChannel) { $oTotalSubscribedActionsNotificationsSet = NotificationsRepository::GetInstance()->SearchSubscriptionByTriggerContactAndSubscription($iTriggerId, \UserRights::GetContactId(), '1'); if (($oTotalSubscribedActionsNotificationsSet->Count() - $oSubscribedActionsNotificationsSet->Count()) === 0) { throw new \Exception('You can\'t unsubscribe from this channel, you must be subscribed to at least one channel'); diff --git a/sources/Core/Trigger/Enum/SubscriptionPolicy.php b/sources/Core/Trigger/Enum/SubscriptionPolicy.php new file mode 100644 index 000000000..67e46879b --- /dev/null +++ b/sources/Core/Trigger/Enum/SubscriptionPolicy.php @@ -0,0 +1,16 @@ + + * @package Combodo\iTop\Core\Trigger\Enum + * @since 3.2.0 + */ +enum SubscriptionPolicy: string { + case AllowNoChannel = "allow_no_channel"; + case ForceAtLeastOneChannel = "force_at_least_one_channel"; + case ForceAllChannels = "force_all_channels"; +} diff --git a/sources/Service/Notification/NotificationsService.php b/sources/Service/Notification/NotificationsService.php index 810e27b1c..8412a2dc6 100644 --- a/sources/Service/Notification/NotificationsService.php +++ b/sources/Service/Notification/NotificationsService.php @@ -3,6 +3,7 @@ namespace Combodo\iTop\Service\Notification; use ActionNotification; +use Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy; use Contact; use lnkActionNotificationToContact; use Trigger; @@ -99,7 +100,7 @@ class NotificationsService { public function IsSubscribed(Trigger $oTrigger, ActionNotification $oActionNotification, Contact $oRecipient): bool { // Check if the trigger subscription policy is 'force_all_channels' - if ($oTrigger->Get('subscription_policy') === 'force_all_channels') { + if ($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels) { return true; } // Check if the user is already subscribed to the action notification