vendor/contao/core-bundle/src/Resources/contao/library/Contao/Message.php line 114

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. /**
  11.  * Stores and outputs messages
  12.  *
  13.  * The class handles system messages which are shown to the user. You can add
  14.  * messages from anywhere in the application.
  15.  *
  16.  * Usage:
  17.  *
  18.  *     Message::addError('Please enter your name');
  19.  *     Message::addConfirmation('The data has been stored');
  20.  *     Message::addNew('There are two new messages');
  21.  *     Message::addInfo('You can upload only two files');
  22.  */
  23. class Message
  24. {
  25.     /**
  26.      * Add an error message
  27.      *
  28.      * @param string $strMessage The error message
  29.      * @param string $strScope   An optional message scope
  30.      */
  31.     public static function addError($strMessage$strScope=TL_MODE)
  32.     {
  33.         static::add($strMessage'TL_ERROR'$strScope);
  34.     }
  35.     /**
  36.      * Add a confirmation message
  37.      *
  38.      * @param string $strMessage The confirmation message
  39.      * @param string $strScope   An optional message scope
  40.      */
  41.     public static function addConfirmation($strMessage$strScope=TL_MODE)
  42.     {
  43.         static::add($strMessage'TL_CONFIRM'$strScope);
  44.     }
  45.     /**
  46.      * Add a new message
  47.      *
  48.      * @param string $strMessage The new message
  49.      * @param string $strScope   An optional message scope
  50.      */
  51.     public static function addNew($strMessage$strScope=TL_MODE)
  52.     {
  53.         static::add($strMessage'TL_NEW'$strScope);
  54.     }
  55.     /**
  56.      * Add an info message
  57.      *
  58.      * @param string $strMessage The info message
  59.      * @param string $strScope   An optional message scope
  60.      */
  61.     public static function addInfo($strMessage$strScope=TL_MODE)
  62.     {
  63.         static::add($strMessage'TL_INFO'$strScope);
  64.     }
  65.     /**
  66.      * Add a preformatted message
  67.      *
  68.      * @param string $strMessage The preformatted message
  69.      * @param string $strScope   An optional message scope
  70.      */
  71.     public static function addRaw($strMessage$strScope=TL_MODE)
  72.     {
  73.         static::add($strMessage'TL_RAW'$strScope);
  74.     }
  75.     /**
  76.      * Add a message
  77.      *
  78.      * @param string $strMessage The message text
  79.      * @param string $strType    The message type
  80.      * @param string $strScope   An optional message scope
  81.      *
  82.      * @throws \Exception If $strType is not a valid message type
  83.      */
  84.     public static function add($strMessage$strType$strScope=TL_MODE)
  85.     {
  86.         if (!$strMessage)
  87.         {
  88.             return;
  89.         }
  90.         if (!\in_array($strType, static::getTypes()))
  91.         {
  92.             throw new \Exception("Invalid message type $strType");
  93.         }
  94.         System::getContainer()->get('session')->getFlashBag()->add(static::getFlashBagKey($strType$strScope), $strMessage);
  95.     }
  96.     /**
  97.      * Return the messages with a wrapping container as HTML
  98.      *
  99.      * @param string $strScope An optional message scope
  100.      *
  101.      * @return string The messages HTML markup
  102.      */
  103.     public static function generate($strScope=TL_MODE)
  104.     {
  105.         $strMessages = static::generateUnwrapped($strScope);
  106.         if ($strMessages)
  107.         {
  108.             $strMessages '<div class="tl_message">' $strMessages '</div>';
  109.         }
  110.         return $strMessages;
  111.     }
  112.     /**
  113.      * Return the messages as HTML
  114.      *
  115.      * @param string  $strScope An optional message scope
  116.      * @param boolean $blnRaw   Optionally return the raw messages
  117.      *
  118.      * @return string The messages HTML markup
  119.      */
  120.     public static function generateUnwrapped($strScope=TL_MODE$blnRaw=false)
  121.     {
  122.         $session System::getContainer()->get('session');
  123.         if (!$session->isStarted())
  124.         {
  125.             return '';
  126.         }
  127.         $strMessages '';
  128.         $flashBag $session->getFlashBag();
  129.         foreach (static::getTypes() as $strType)
  130.         {
  131.             $strClass strtolower($strType);
  132.             $arrMessages $flashBag->get(static::getFlashBagKey($strType$strScope));
  133.             foreach (array_unique($arrMessages) as $strMessage)
  134.             {
  135.                 if ($strType == 'TL_RAW' || $blnRaw)
  136.                 {
  137.                     $strMessages .= $strMessage;
  138.                 }
  139.                 else
  140.                 {
  141.                     $strMessages .= '<p class="' $strClass '">' $strMessage '</p>';
  142.                 }
  143.             }
  144.         }
  145.         return trim($strMessages);
  146.     }
  147.     /**
  148.      * Reset the message system
  149.      */
  150.     public static function reset()
  151.     {
  152.         $session System::getContainer()->get('session');
  153.         if (!$session->isStarted())
  154.         {
  155.             return;
  156.         }
  157.         $flashBag $session->getFlashBag();
  158.         // Find all contao. keys (see #3393)
  159.         $keys preg_grep('(^contao\.)'$flashBag->keys());
  160.         foreach ($keys as $key)
  161.         {
  162.             $flashBag->get($key); // clears the message
  163.         }
  164.     }
  165.     /**
  166.      * Return all available message types
  167.      *
  168.      * @return array An array of message types
  169.      */
  170.     public static function getTypes()
  171.     {
  172.         return array('TL_ERROR''TL_CONFIRM''TL_NEW''TL_INFO''TL_RAW');
  173.     }
  174.     /**
  175.      * Check if there are error messages
  176.      *
  177.      * @param string $strScope An optional message scope
  178.      *
  179.      * @return boolean True if there are error messages
  180.      */
  181.     public static function hasError($strScope=TL_MODE)
  182.     {
  183.         $session System::getContainer()->get('session');
  184.         if (!$session->isStarted())
  185.         {
  186.             return false;
  187.         }
  188.         return $session->getFlashBag()->has(static::getFlashBagKey('error'$strScope));
  189.     }
  190.     /**
  191.      * Check if there are confirmation messages
  192.      *
  193.      * @param string $strScope An optional message scope
  194.      *
  195.      * @return boolean True if there are confirmation messages
  196.      */
  197.     public static function hasConfirmation($strScope=TL_MODE)
  198.     {
  199.         $session System::getContainer()->get('session');
  200.         if (!$session->isStarted())
  201.         {
  202.             return false;
  203.         }
  204.         return $session->getFlashBag()->has(static::getFlashBagKey('confirm'$strScope));
  205.     }
  206.     /**
  207.      * Check if there are new messages
  208.      *
  209.      * @param string $strScope An optional message scope
  210.      *
  211.      * @return boolean True if there are new messages
  212.      */
  213.     public static function hasNew($strScope=TL_MODE)
  214.     {
  215.         $session System::getContainer()->get('session');
  216.         if (!$session->isStarted())
  217.         {
  218.             return false;
  219.         }
  220.         return $session->getFlashBag()->has(static::getFlashBagKey('new'$strScope));
  221.     }
  222.     /**
  223.      * Check if there are info messages
  224.      *
  225.      * @param string $strScope An optional message scope
  226.      *
  227.      * @return boolean True if there are info messages
  228.      */
  229.     public static function hasInfo($strScope=TL_MODE)
  230.     {
  231.         $session System::getContainer()->get('session');
  232.         if (!$session->isStarted())
  233.         {
  234.             return false;
  235.         }
  236.         return $session->getFlashBag()->has(static::getFlashBagKey('info'$strScope));
  237.     }
  238.     /**
  239.      * Check if there are raw messages
  240.      *
  241.      * @param string $strScope An optional message scope
  242.      *
  243.      * @return boolean True if there are raw messages
  244.      */
  245.     public static function hasRaw($strScope=TL_MODE)
  246.     {
  247.         $session System::getContainer()->get('session');
  248.         if (!$session->isStarted())
  249.         {
  250.             return false;
  251.         }
  252.         return $session->getFlashBag()->has(static::getFlashBagKey('raw'$strScope));
  253.     }
  254.     /**
  255.      * Check if there are any messages
  256.      *
  257.      * @param string $strScope An optional message scope
  258.      *
  259.      * @return boolean True if there are messages
  260.      */
  261.     public static function hasMessages($strScope=TL_MODE)
  262.     {
  263.         return static::hasError($strScope) || static::hasConfirmation($strScope) || static::hasNew($strScope) || static::hasInfo($strScope) || static::hasRaw($strScope);
  264.     }
  265.     /**
  266.      * Return the flash bag key
  267.      *
  268.      * @param string      $strType  The message type
  269.      * @param string|null $strScope The message scope
  270.      *
  271.      * @return string The flash bag key
  272.      */
  273.     protected static function getFlashBagKey($strType$strScope=TL_MODE)
  274.     {
  275.         return 'contao.' $strScope '.' strtolower(str_replace('TL_'''$strType));
  276.     }
  277. }
  278. class_alias(Message::class, 'Message');