N°7157 - Fix usage of Enum case value

This commit is contained in:
Molkobain
2024-03-06 23:05:16 +01:00
parent 02efea4e55
commit e05b4e772c
4 changed files with 31 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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');

View File

@@ -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