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

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