N°6167 - Introduce API for the content of the Welcome Popup (#505)

* API for the content of the Welcome Popup

* Apply suggestions from code review

* N°7410 - Refactor code to match conventions

* N°7410 - Refactor to new design

* N°7410 - Review adjustments

* N°7410 - Review adjustments

* N°7410 - Update translations

* N°7410 - Update setup complied file

* Update sources/Application/WelcomePopup/Provider/DefaultProvider.php

---------

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
This commit is contained in:
Denis
2024-04-18 16:57:07 +02:00
committed by GitHub
parent 62f5f2c942
commit 3a8ad59e6c
59 changed files with 1665 additions and 649 deletions

View File

@@ -0,0 +1,194 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\WelcomePopup;
use iWelcomePopupExtension;
use utils;
/**
* Class Message
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\WelcomePopup
*/
class Message {
/** @var string Default TWIG template */
protected const DEFAULT_TWIG_TEMPLATE_REL_PATH = 'templates/application/welcome_popup/templates/left-title-description-right-illustration.html.twig';
/** @var string Unique ID of the message within its provider */
protected readonly string $sId;
/** @var int Importance of the message {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_HIGH} and {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_CRITICAL} */
protected int $iImportance;
/** @var string Title of the message in plain text */
protected string $sTitle;
/** @var string Description of the message, can contain HTML */
protected string $sDescription;
/** @var string|null Optional illustration to display with the description, should be an absolute URI (illustration can be on another server) */
protected null|string $sIllustrationAbsURI;
/** @var array Additional parameters to pass to {@see \Combodo\iTop\Application\WelcomePopup\Message::$sTWIGTemplateRelPath} */
protected array $aAdditionalParameters;
/** @var string|null TWIG template to use for the rendering of the message content (title, description, illustration) */
protected readonly null|string $sTWIGTemplateRelPath;
/**
* @param string $sId {@see \Combodo\iTop\Application\WelcomePopup\Message::$sId}
* @param string $sTitle {@see \Combodo\iTop\Application\WelcomePopup\Message::$sTitle}
* @param string $sDescription {@see \Combodo\iTop\Application\WelcomePopup\Message::$sDescription}
* @param string|null $sIllustrationAbsURI {@see \Combodo\iTop\Application\WelcomePopup\Message::$sIllustrationAbsURI}
* @param array $aAdditionalParameters {@see \Combodo\iTop\Application\WelcomePopup\Message::$aAdditionalParameters}
* @param int $iImportance {@see \Combodo\iTop\Application\WelcomePopup\Message::$iImportance}
* @param string|null $sTWIGTemplateRelPath {@see \Combodo\iTop\Application\WelcomePopup\Message::$sTWIGTemplateRelPath}
*/
public function __construct(string $sId, string $sTitle, string $sDescription, null|string $sIllustrationAbsURI = null, array $aAdditionalParameters = [], int $iImportance = iWelcomePopupExtension::DEFAULT_IMPORTANCE, null|string $sTWIGTemplateRelPath = null)
{
$this->sId = $sId;
$this->sTitle = $sTitle;
$this->sDescription = $sDescription;
$this->sIllustrationAbsURI = $sIllustrationAbsURI;
$this->aAdditionalParameters = $aAdditionalParameters;
$this->iImportance = $iImportance;
$this->sTWIGTemplateRelPath = $sTWIGTemplateRelPath ?? static::DEFAULT_TWIG_TEMPLATE_REL_PATH;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sId
* @return string
*/
public function GetID(): string
{
return $this->sId;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sDescription
* @return string
*/
public function GetTitle(): string
{
return $this->sTitle;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sTitle
* @param string $sTitle
*
* @return static
*/
public function SetTitle(string $sTitle): static
{
$this->sTitle = $sTitle;
return $this;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sDescription
* @return string
*/
public function GetDescription(): string
{
return $this->sDescription;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sDescription
* @param string $sDescription
*
* @return static
*/
public function SetDescription(string $sDescription): static
{
$this->sDescription = $sDescription;
return $this;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sIllustrationAbsURI
* @return string|null
*/
public function GetIllustrationAbsURI(): ?string
{
return $this->sIllustrationAbsURI;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sIllustrationAbsURI
*
* @param string|null $sIllustrationAbsURI
*
* @return static
*/
public function SetIllustrationAbsURI(?string $sIllustrationAbsURI): static
{
$this->sIllustrationAbsURI = $sIllustrationAbsURI;
return $this;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::HasIllustration()
* @return bool
*/
public function HasIllustration(): bool
{
return utils::IsNotNullOrEmptyString($this->sIllustrationAbsURI);
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$aAdditionalParameters
* @return array
*/
public function GetAdditionalParameters(): array
{
return $this->aAdditionalParameters;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$aAdditionalParameters
*
* @param array $aAdditionalParameters
*
* @return static
*/
public function SetAdditionalParameters(array $aAdditionalParameters): static
{
$this->aAdditionalParameters = $aAdditionalParameters;
return $this;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$iImportance
* @return int
*/
public function GetImportance(): int
{
return $this->iImportance;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$iImportance
*
* @param int $iImportance
*
* @return static
*/
public function SetImportance(int $iImportance): static
{
$this->iImportance = $iImportance;
return $this;
}
/**
* @see \Combodo\iTop\Application\WelcomePopup\Message::$sTWIGTemplateRelPath
* @return string|null
*/
public function GetTWIGTemplateRelPath(): ?string
{
return $this->sTWIGTemplateRelPath;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\WelcomePopup;
use iWelcomePopupExtension;
/**
* Class MessageFactory
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\WelcomePopup
* @api
*/
class MessageFactory {
/**
* @param string $sId Unique ID of the message within its provider
* @param string $sTitle Title of the message in plain text
* @param string $sDescription Description of the message, can contain HTML
* @param string|null $sIllustrationAbsURI Optional illustration to display with the description, should be an absolute URI (illustration can be on another server)
* @param int $iImportance Importance of the message {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_HIGH} and {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_CRITICAL}
*
* @api
* @return \Combodo\iTop\Application\WelcomePopup\Message Message with title / description on the left side, and an optional illustration on the right
*/
public static function MakeForLeftTextsRightIllustration(string $sId, string $sTitle, string $sDescription, null|string $sIllustrationAbsURI = null, int $iImportance = iWelcomePopupExtension::DEFAULT_IMPORTANCE): Message
{
return new Message(
$sId,
$sTitle,
$sDescription,
$sIllustrationAbsURI,
[],
$iImportance,
"templates/application/welcome_popup/templates/left-title-description-right-illustration.html.twig"
);
}
/**
* @param string $sId Unique ID of the message within its provider
* @param string $sTitle Title of the message in plain text
* @param string $sDescription Description of the message, can contain HTML
* @param string|null $sIllustrationAbsURI Optional illustration to display with the description, should be an absolute URI (illustration can be on another server)
* @param int $iImportance Importance of the message {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_HIGH} and {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_CRITICAL}
*
* @api
* @return \Combodo\iTop\Application\WelcomePopup\Message Message with title / description on the right side, and an optional illustration on the left
*/
public static function MakeForLeftIllustrationRightTexts(string $sId, string $sTitle, string $sDescription, null|string $sIllustrationAbsURI = null, int $iImportance = iWelcomePopupExtension::DEFAULT_IMPORTANCE): Message
{
return new Message(
$sId,
$sTitle,
$sDescription,
$sIllustrationAbsURI,
[],
$iImportance,
"templates/application/welcome_popup/templates/left-illustration-right-title-description.html.twig"
);
}
/**
* @param string $sId Unique ID of the message within its provider
* @param string $sTitle Title of the message in plain text
* @param string $sDescription Description of the message, can contain HTML
* @param string $sTWIGTemplateRelPath Rel. path (from app. root) to the TWIG template to use for the rendering of the message content
* @param string|null $sIllustrationAbsURI Optional illustration to display with the description, should be an absolute URI (illustration can be on another server)
* @param array $aAdditionalParameters Additional parameters to pass to the TWIG
* @param int $iImportance Importance of the message {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_HIGH} and {@see \iWelcomePopupExtension::ENUM_IMPORTANCE_CRITICAL}
*
* @api
* @return \Combodo\iTop\Application\WelcomePopup\Message Message with title / description on the right side, and an optional illustration on the left
*/
public static function MakeForCustomTemplate(string $sId, string $sTitle, string $sDescription, string $sTWIGTemplateRelPath, null|string $sIllustrationAbsURI = null, array $aAdditionalParameters = [], int $iImportance = iWelcomePopupExtension::DEFAULT_IMPORTANCE): Message
{
return new Message(
$sId,
$sTitle,
$sDescription,
$sIllustrationAbsURI,
$aAdditionalParameters,
$iImportance,
$sTWIGTemplateRelPath
);
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\WelcomePopup\Provider;
use Dict;
use AbstractWelcomePopupExtension;
use utils;
use Combodo\iTop\Application\WelcomePopup\MessageFactory;
/**
* Implementation of the "default" Welcome Popup message
* @since 3.2.0
*/
class DefaultProvider extends AbstractWelcomePopupExtension
{
/**
* @inheritDoc
*/
public function GetMessages(): array
{
return [
MessageFactory::MakeForLeftTextsRightIllustration(
"320_01",
Dict::S("UI:WelcomePopup:Message:320_01:Title"),
Dict::S("UI:WelcomePopup:Message:320_01:Description"),
utils::GetAbsoluteUrlAppRoot() . "images/illustrations/undraw_relaunch_day.svg"
),
];
}
}

View File

@@ -0,0 +1,255 @@
<?php
namespace Combodo\iTop\Application\WelcomePopup;
use AttributeDateTime;
use DBObjectSearch;
use DBObjectSet;
use Exception;
use IssueLog;
use LogChannels;
use MetaModel;
use UserRights;
use WelcomePopupAcknowledge;
use iWelcomePopupExtension;
use utils;
/**
* Handling of the messages displayed in the "Welcome Popup"
* @since 3.1.0
*
*/
class WelcomePopupService
{
private const PROVIDER_KEY_LENGTH = 128;
/** @var \Combodo\iTop\Application\WelcomePopup\WelcomePopupService|null Singleton instance */
protected static ?WelcomePopupService $oSingleton = null;
/** @var \Combodo\iTop\Application\WelcomePopup\Message[]|null Acknowledged messages for the current user */
protected static $aAcknowledgedMessages = null;
/** @var \iWelcomePopupExtension[]|null "Providers" of welcome popup messages */
protected $aMessagesProviders = null;
/**
* @internal
* @return $this The singleton instance of the service
*/
public static function GetInstance(): WelcomePopupService
{
if (null === static::$oSingleton) {
static::$oSingleton = new static();
}
return static::$oSingleton;
}
/**
* Helper function for usort to compare two items based on their 'importance' field
*
* @param array $aProviderMessageData1
* @param array $aProviderMessageData2
*
* @return int
*/
public static function SortOnImportance(array $aProviderMessageData1, array $aProviderMessageData2): int
{
if ($aProviderMessageData1['message']->GetImportance() === $aProviderMessageData2['message']->GetImportance()) {
return strcmp($aProviderMessageData1['message']->GetID(), $aProviderMessageData2['message']->GetID());
}
return ($aProviderMessageData1['message']->GetImportance() < $aProviderMessageData2['message']->GetImportance()) ? -1 : 1;
}
/**********************/
/* Non-static methods */
/**********************/
/**
* Singleton pattern, can't use the constructor. Use {@see \Combodo\iTop\Application\WelcomePopup\WelcomePopupService::GetInstance()} instead.
*
* @return void
*/
protected function __construct()
{
// Don't do anything, we don't want to be initialized
}
/**
* Get the list of {@see \Combodo\iTop\Application\WelcomePopup\Message} to display in the Welcome popup dialog
* @return \Combodo\iTop\Application\WelcomePopup\Message[]
*/
public function GetMessages(): array
{
$this->LoadProviders();
return $this->ProcessMessages();
}
/**
* Get the {@see \Combodo\iTop\Application\WelcomePopup\Message} to display from a list of {@see \iWelcomePopupExtension} instances
* The messages are ordered by importance ({@see \iWelcomePopupExtension::ENUM_IMPORTANCE_CRITICAL} first) then by ID
* Invalid messages or acknowledged messages are removed from the list
*
* @return array
*/
protected function ProcessMessages(): array
{
$this->LoadProviders();
/** @var array $aAllProvidersMessagesData */
$aAllProvidersMessagesData = [];
foreach($this->aMessagesProviders as $oProvider) {
$aProviderMessages = $oProvider->GetMessages();
if (count($aProviderMessages) === 0) {
IssueLog::Debug('Empty list of messages for '.$oProvider::class, LogChannels::CONSOLE);
continue;
}
$sProviderIconRelPath = $oProvider->GetIconRelPath();
foreach($aProviderMessages as $oMessage) {
if (false === ($oMessage instanceof Message)) {
IssueLog::Error('Invalid message returned by iWelcomePopupExtension::GetMessages(), must be of class ' . Message::class, LogChannels::CONSOLE, [
'provider_class' => $oProvider::class,
'message' => $oMessage,
]);
continue; // Fail silently
}
$aAllProvidersMessagesData[] = [
'uuid' => $this->MakeStringFitIn($oProvider::class, static::PROVIDER_KEY_LENGTH).'::'.$oMessage->GetID(),
'message' => $oMessage,
'provider_icon_rel_path' => $sProviderIconRelPath,
];
}
}
// Filter the acknowledged messages AFTER getting all messages
// This allows for "replacing" a message (from another provider for example)
// by automatically acknowledging it when called in GetMessages()
foreach($aAllProvidersMessagesData as $key => $aProviderMessageData) {
if ($this->IsMessageAcknowledged($aProviderMessageData['uuid'])) {
IssueLog::Debug('Ignoring already acknowledged message '.$aProviderMessageData['uuid'], LogChannels::CONSOLE);
unset($aAllProvidersMessagesData[$key]);
}
}
usort($aAllProvidersMessagesData, [static::class, 'SortOnImportance']);
return $aAllProvidersMessagesData;
}
/**
* Acknowledge a message (from a specific provider) for the current user, then notifies the provider (in case it wants to do some extra processing)
*
* @param string $sMessageUUID Format <PROVIDER_FQCN>::<MESSAGE_ID>
*
* @return void
* @throws \CoreException
*/
public function AcknowledgeMessage(string $sMessageUUID): void
{
$this->LoadProviders();
$oAcknowledge = MetaModel::NewObject(WelcomePopupAcknowledge::class, [
'message_uuid' => $sMessageUUID,
'acknowledge_date' => date(AttributeDateTime::GetSQLFormat()),
'user_id' => UserRights::GetConnectedUserId(),
]);
try {
$oAcknowledge->DBInsert();
$oProvider = $this->GetProviderByUUID($sMessageUUID);
if (static::$aAcknowledgedMessages !== null) {
static::$aAcknowledgedMessages[] = $sMessageUUID; // Update the cache
}
// Notify the provider of the message
$sMessageId = substr($sMessageUUID, strpos($sMessageUUID, '::') + 2);
if ($oProvider !== null) {
$oProvider->AcknowledgeMessage($sMessageId);
}
} catch(Exception $e) {
IssueLog::Error("Failed to acknowledge the message $sMessageUUID for user ".UserRights::GetConnectedUserId().". Reason: ".$e->getMessage(), LogChannels::CONSOLE);
}
}
/**
* Load the provider of messages, decoupled from the constructor for testability
*/
protected function LoadProviders(): void
{
if ($this->aMessagesProviders !== null) return;
$aProviders = [];
$aProviderClasses = utils::GetClassesForInterface(iWelcomePopupExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
foreach($aProviderClasses as $sProviderClass) {
$aProviders[] = new $sProviderClass();
}
$this->SetMessagesProviders($aProviders);
}
/**
* Check if a given message was acknowledged by the current user
* @param string $sMessageId
* @return bool
*/
protected function IsMessageAcknowledged(string $sMessageUUID): bool
{
$iUserId = UserRights::GetConnectedUserId();
if (static::$aAcknowledgedMessages === null) {
$oSearch = new DBObjectSearch(WelcomePopupAcknowledge::class);
$oSearch->AddCondition('user_id', $iUserId);
$oSet = new DBObjectSet($oSearch);
$aAcknowledgedMessages = $oSet->GetColumnAsArray('message_uuid');
$this->SetAcknowledgedMessagesCache($aAcknowledgedMessages);
}
return in_array($sMessageUUID, static::$aAcknowledgedMessages);
}
/**
* Set the cache of acknowledged messages (useful for testing)
* @param array $aAcknowledgedMessages
*/
protected function SetAcknowledgedMessagesCache(array $aAcknowledgedMessages): void
{
static::$aAcknowledgedMessages = $aAcknowledgedMessages;
}
/**
* Set the cache of welcome popup message providers (useful for testing)
* @param iWelcomePopupExtension[] $aMessagesProviders
*/
protected function SetMessagesProviders(array $aMessagesProviders): void
{
$this->aMessagesProviders = $aMessagesProviders;
}
/**
* Retrieve the provider associated with a message
* @param string $sMessageUUID
* @return iWelcomePopupExtension|NULL
*/
protected function GetProviderByUUID(string $sMessageUUID): ?iWelcomePopupExtension
{
$this->LoadProviders();
$sProviderKey = substr($sMessageUUID, 0, strpos($sMessageUUID, '::'));
foreach($this->aMessagesProviders as $oProvider) {
if ($this->MakeStringFitIn($oProvider::class, static::PROVIDER_KEY_LENGTH) === $sProviderKey) {
return $oProvider;
}
}
return null;
}
/**
* Shorten the given string (if needed) but preserving its uniqueness
* @param string $sProviderClass
* @param int $iLengthLimit
* @return string
*/
protected function MakeStringFitIn(string $sProviderClass, int $iLengthLimit): string
{
if(mb_strlen($sProviderClass) <= $iLengthLimit) {
return $sProviderClass;
}
// Truncate the string to $iLimitLength and replace the first carahcters with the MD5 of the complete string
$sMD5 = md5($sProviderClass, false);
return $sMD5.'-'.mb_substr($sProviderClass, -($iLengthLimit - strlen($sMD5) - 1)); // strlen is OK on the MD5 string, and '-' is not allowed in a class name
}
}