Session messages: Introduce severity constants

This commit is contained in:
Molkobain
2021-03-12 09:46:34 +01:00
parent 33d72890fa
commit bdc8fe9db3
2 changed files with 40 additions and 14 deletions

View File

@@ -232,26 +232,25 @@ EOF
* last plugin to set the message for a given message id) In practice, standard messages are recorded at the end
* but they will not overwrite existing messages
*
* @see SetSessionMessageFromInstance() to call from within an instance
*
* @param string $sClass The class of the object (must be the final class)
* @param int $iKey The identifier of the object
* @param string $sMessageId Your id or one of the well-known ids: 'create', 'update' and 'apply_stimulus'
* @param string $sMessage The HTML message (must be correctly escaped)
* @param string $sSeverity Any of the following: ok, info, error.
* @param string $sSeverity Any of the \WebPage::ENUM_SESSION_MESSAGE_SEVERITY_XXX constants
* @param float $fRank Ordering of the message: smallest displayed first (can be negative)
* @param bool $bMustNotExist Do not alter any existing message (considering the id)
*
* @see SetSessionMessageFromInstance() to call from within an instance
* @return void
*/
public static function SetSessionMessage(
$sClass, $iKey, $sMessageId, $sMessage, $sSeverity, $fRank, $bMustNotExist = false
) {
public static function SetSessionMessage($sClass, $iKey, $sMessageId, $sMessage, $sSeverity, $fRank, $bMustNotExist = false)
{
$sMessageKey = $sClass.'::'.$iKey;
if (!isset($_SESSION['obj_messages'][$sMessageKey]))
{
if (!isset($_SESSION['obj_messages'][$sMessageKey])) {
$_SESSION['obj_messages'][$sMessageKey] = array();
}
if (!$bMustNotExist || !array_key_exists($sMessageId, $_SESSION['obj_messages'][$sMessageKey]))
{
if (!$bMustNotExist || !array_key_exists($sMessageId, $_SESSION['obj_messages'][$sMessageKey])) {
$_SESSION['obj_messages'][$sMessageKey][$sMessageId] = array(
'rank' => $fRank,
'severity' => $sSeverity,

View File

@@ -50,6 +50,34 @@ class WebPage implements Page
* @since 2.7.0 N°2529
*/
const PAGES_CHARSET = 'utf-8';
/**
* @var string
* @since 3.0.0
*/
public const ENUM_SESSION_MESSAGE_SEVERITY_INFO = 'INFO';
/**
* @var string
* @since 3.0.0
*/
public const ENUM_SESSION_MESSAGE_SEVERITY_OK = 'ok';
/**
* @var string
* @since 3.0.0
*/
public const ENUM_SESSION_MESSAGE_SEVERITY_WARNING = 'warning';
/**
* @var string
* @since 3.0.0
*/
public const ENUM_SESSION_MESSAGE_SEVERITY_ERROR = 'error';
/**
* @var string
* @since 3.0.0
*/
public const DEFAULT_SESSION_MESSAGE_SEVERITY = self::ENUM_SESSION_MESSAGE_SEVERITY_INFO;
const DEFAULT_PAGE_TEMPLATE_REL_PATH = 'pages/backoffice/webpage/layout';
protected $s_title;
@@ -92,7 +120,6 @@ class WebPage implements Page
protected $sTemplateRelPath;
/**
* @var bool|string|string[]
*/
@@ -150,16 +177,16 @@ class WebPage implements Page
$aReadMessages[] = $aMessageData['message'];
$aRanks[] = $aMessageData['rank'];
switch ($aMessageData['severity']) {
case 'ok':
case static::ENUM_SESSION_MESSAGE_SEVERITY_OK:
$aMessages[] = AlertUIBlockFactory::MakeForSuccess('', $aMessageData['message']);
break;
case 'warning':
case static::ENUM_SESSION_MESSAGE_SEVERITY_WARNING:
$aMessages[] = AlertUIBlockFactory::MakeForWarning('', $aMessageData['message']);
break;
case 'error':
case static::ENUM_SESSION_MESSAGE_SEVERITY_ERROR:
$aMessages[] = AlertUIBlockFactory::MakeForDanger('', $aMessageData['message']);
break;
case 'info':
case static::ENUM_SESSION_MESSAGE_SEVERITY_INFO:
default:
$aMessages[] = AlertUIBlockFactory::MakeForInformation('', $aMessageData['message']);
break;