diff --git a/core/trigger.class.inc.php b/core/trigger.class.inc.php index 273b09794..01242bc2e 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(implode(",", array_map(fn($case) => $case->value, 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()))); + MetaModel::Init_AddAttribute(new AttributeEnum("subscription_policy", array("allowed_values" => new ValueSetEnum(implode(",", array_map(fn($case) => $case->value, Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::cases()))), "sql" => "subscription_policy", "default_value" => \Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::AllowNoChannel->value, "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/core/valuesetdef.class.inc.php b/core/valuesetdef.class.inc.php index c32766b0f..21a7fb654 100644 --- a/core/valuesetdef.class.inc.php +++ b/core/valuesetdef.class.inc.php @@ -471,6 +471,29 @@ class ValueSetEnum extends ValueSetDefinition */ protected bool $bSortByValues; + /** + * @param string $sBackedEnumFQCN FQCN of the backed enum to use + * @param bool $bSortValues {@see static::$bSortValues} + * + * @return \ValueSetEnum ValueSetEnum based on the values of the $sBackedEnumFQCN backed enum + * @throws \CoreException + * @since 3.2.0 + */ + public static function FromBackedEnum(string $sBackedEnumFQCN, bool $bSortValues = false): ValueSetEnum + { + // First, check that we pass an enum as there is no generic type hint for that yet + if (false === enum_exists($sBackedEnumFQCN)) { + throw new CoreException("Can't instantiate " . __CLASS__ . "::" . __METHOD__ . " from a non-enum argument", [ + "argument" => $sBackedEnumFQCN + ]); + } + + // Implode cases + $sJoinedValues = implode(",", array_map(fn($case) => $case->value, $sBackedEnumFQCN::cases())); + + return new ValueSetEnum($sJoinedValues, $bSortValues); + } + /** * @param array|string $Values * @param bool $bLocalizedSort diff --git a/sources/Controller/Notifications/NotificationsCenterController.php b/sources/Controller/Notifications/NotificationsCenterController.php index 8520879dc..325715ffd 100644 --- a/sources/Controller/Notifications/NotificationsCenterController.php +++ b/sources/Controller/Notifications/NotificationsCenterController.php @@ -83,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') === SubscriptionPolicy::ForceAllChannels; + $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels->value; } // Build table rows @@ -156,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 === SubscriptionPolicy::ForceAllChannels) { + if ($sTriggerSubscriptionPolicy === SubscriptionPolicy::ForceAllChannels->value) { $oChannelSet->SetIsDisabled(true); } // Add a CSRF Token @@ -192,7 +192,7 @@ $.ajax({ JS ); // Set the minimum number of channels to 1 if the subscription policy is {@see SubscriptionPolicy::ForceAtLeastOneChannel} - if($sTriggerSubscriptionPolicy === SubscriptionPolicy::ForceAtLeastOneChannel) + if($sTriggerSubscriptionPolicy === SubscriptionPolicy::ForceAtLeastOneChannel->value) { $oChannelSet->SetMinItems(1); } @@ -290,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') === SubscriptionPolicy::ForceAllChannels; + $aSubscribedActionsNotificationsByTrigger[$iTriggerId][$oSubscribedActionNotification->GetKey()] = $oLnkActionsNotifications->Get('subscribed') || $oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels->value; } $oPage->AddTabContainer('NotificationsCenter', '', $oNotificationsPanel); @@ -492,7 +492,7 @@ JS throw new \Exception('Invalid trigger'); } // Check the trigger subscription policy - if ($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels) { + if ($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels->value) { throw new \Exception('You are not allowed to unsubscribe from this channel'); } @@ -502,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') === SubscriptionPolicy::ForceAtLeastOneChannel) { + if($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAtLeastOneChannel->value) { $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/Service/Notification/NotificationsService.php b/sources/Service/Notification/NotificationsService.php index 8412a2dc6..4beecfc13 100644 --- a/sources/Service/Notification/NotificationsService.php +++ b/sources/Service/Notification/NotificationsService.php @@ -100,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') === SubscriptionPolicy::ForceAllChannels) { + if ($oTrigger->Get('subscription_policy') === SubscriptionPolicy::ForceAllChannels->value) { return true; } // Check if the user is already subscribed to the action notification