mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 15:34:12 +01:00
* N°7157 - Allow users to unsubscribe from notification channels * Fix type hinting * Add missing dict entries * Allows to subscribe/unsubscribe from notifications individually * Refactor NotificationsService to singleton pattern * Refactor NotificationsRepository to singleton pattern and rename methods to a more functional naming * Add PHPDoc and type hints * Dump autoloaders * Replace modals with toasts * Add dict entry --------- Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
116 lines
3.8 KiB
PHP
116 lines
3.8 KiB
PHP
<?php
|
|
namespace Combodo\iTop\Service\Notification;
|
|
|
|
|
|
use ActionNotification;
|
|
use Contact;
|
|
use lnkActionNotificationToContact;
|
|
use Trigger;
|
|
|
|
/**
|
|
* Class NotificationsService
|
|
*
|
|
* @author Stephen Abello <stephen.abello@combodo.com>
|
|
* @package Combodo\iTop\Service\Notification
|
|
* @since 3.2.0
|
|
*/
|
|
class NotificationsService {
|
|
protected static ?NotificationsService $oSingleton = null;
|
|
|
|
/**
|
|
* @api
|
|
* @return static The singleton instance of the notifications service
|
|
*/
|
|
public static function GetInstance(): static
|
|
{
|
|
if (null === static::$oSingleton) {
|
|
static::$oSingleton = new static();
|
|
}
|
|
|
|
return static::$oSingleton;
|
|
}
|
|
|
|
/**********************/
|
|
/* Non-static methods */
|
|
/**********************/
|
|
|
|
/**
|
|
* Singleton pattern, can't use the constructor. Use {@see \Combodo\iTop\Service\Notification\NotificationsService::GetInstance()} instead.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function __construct() {
|
|
// Don't do anything, we don't want to be initialized
|
|
}
|
|
|
|
/**
|
|
* Register that $oRecipient was a recipient for the $oTrigger / $oActionNotification tuple at least one time
|
|
*
|
|
* @param \Trigger $oTrigger
|
|
* @param \ActionNotification $oActionNotification
|
|
* @param \Contact $oRecipient
|
|
*
|
|
* @return void
|
|
* @throws \ArchivedObjectException
|
|
* @throws \CoreCannotSaveObjectException
|
|
* @throws \CoreException
|
|
* @throws \CoreUnexpectedValue
|
|
* @throws \CoreWarning
|
|
* @throws \MissingQueryArgument
|
|
* @throws \MySQLException
|
|
* @throws \MySQLHasGoneAwayException
|
|
* @throws \OQLException
|
|
*/
|
|
public function RegisterSubscription(Trigger $oTrigger, ActionNotification $oActionNotification, Contact $oRecipient): void
|
|
{
|
|
// Check if the user is already subscribed to the action notification
|
|
$oSubscribedActionsNotificationsSet = NotificationsRepository::GetInstance()->SearchSubscriptionByTriggerContactAndAction($oTrigger->GetKey(), $oRecipient->GetKey(), $oActionNotification->GetKey());
|
|
if ($oSubscribedActionsNotificationsSet->Count() === 0) {
|
|
// Create a new subscription
|
|
$oSubscribedActionsNotifications = new lnkActionNotificationToContact();
|
|
$oSubscribedActionsNotifications->Set('action_id', $oActionNotification->GetKey());
|
|
$oSubscribedActionsNotifications->Set('contact_id', $oRecipient->GetKey());
|
|
$oSubscribedActionsNotifications->Set('trigger_id', $oTrigger->GetKey());
|
|
$oSubscribedActionsNotifications->Set('subscribed', true);
|
|
$oSubscribedActionsNotifications->DBInsertNoReload();
|
|
}
|
|
else {
|
|
while ($oSubscribedActionsNotifications = $oSubscribedActionsNotificationsSet->Fetch()) {
|
|
// Update the subscription
|
|
$oSubscribedActionsNotifications->Set('subscribed', true);
|
|
$oSubscribedActionsNotifications->DBUpdate();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param \Trigger $oTrigger
|
|
* @param \ActionNotification $oActionNotification
|
|
* @param \Contact $oRecipient
|
|
*
|
|
* @return bool
|
|
* @throws \ArchivedObjectException
|
|
* @throws \CoreException
|
|
* @throws \CoreUnexpectedValue
|
|
* @throws \MissingQueryArgument
|
|
* @throws \MySQLException
|
|
* @throws \MySQLHasGoneAwayException
|
|
*/
|
|
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') {
|
|
return true;
|
|
}
|
|
// Check if the user is already subscribed to the action notification
|
|
$oSubscribedActionsNotificationsSet = NotificationsRepository::GetInstance()->SearchSubscriptionByTriggerContactAndAction($oTrigger->GetKey(), $oRecipient->GetKey(), $oActionNotification->GetKey());
|
|
if ($oSubscribedActionsNotificationsSet->Count() === 0) {
|
|
return false;
|
|
}
|
|
|
|
// Return the subscribed status
|
|
$oSubscribedActionsNotifications = $oSubscribedActionsNotificationsSet->Fetch();
|
|
return $oSubscribedActionsNotifications->Get('subscribed');
|
|
}
|
|
|
|
} |