SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Notification.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16.  
  17. /**
  18. * Provides user event notification functions
  19. **/
  20. class Notification
  21. {
  22. const TYPE_CONFIRM = 1;
  23. const TYPE_ERROR = 2;
  24. const TYPE_POPUP = 3;
  25.  
  26.  
  27. // CSS classes for the different types
  28. static $classes = array(
  29. self::TYPE_CONFIRM => 'confirm',
  30. self::TYPE_ERROR => 'error'
  31. );
  32.  
  33.  
  34. /**
  35.   * Renders notification messages for display to the user
  36.   *
  37.   * @param string $scope If set the the messages are pulled from a different session key
  38.   * @return string HTML
  39.   **/
  40. public static function checkMessages($scope = 'default')
  41. {
  42. Session::Instance();
  43. $out = '';
  44.  
  45. if (empty($_SESSION['notify'][$scope])) {
  46. return;
  47. }
  48.  
  49. $has_type = array();
  50. foreach ($_SESSION['notify'][$scope] as $idx => $message) {
  51. if ($message[0] == self::TYPE_POPUP) continue;
  52. $has_type[$message[0]] = $message[0];
  53. }
  54.  
  55. // If all messages are of a certain type, add a class for that type
  56. $class = 'messages';
  57. if (count($has_type) == 1) {
  58. reset($has_type);
  59. $class .= ' all-type-' . self::$classes[key($has_type)];
  60. } else {
  61. $class .= ' mixed-type';
  62. }
  63.  
  64. $out .= "<ul class=\"{$class}\">\n";
  65.  
  66. foreach ($_SESSION['notify'][$scope] as $idx => $message) {
  67. // Ignore popups at this point and render out regular notifications
  68. if ($message[0] == self::TYPE_POPUP) continue;
  69.  
  70. $out .= "<li class=\"" . self::$classes[$message[0]] . "\">";
  71. $out .= "<span class=\"notification--text\">{$message[1]}</span>";
  72.  
  73. // render action buttons
  74. foreach ($message[2] as $action => $anchor) {
  75. $anchor = Enc::html($anchor);
  76. $out .= "<span class=\"notification--action\"><a href=\"{$action}\">{$anchor}</a></span>";
  77. }
  78. $out .= "</li>";
  79. unset ($_SESSION['notify'][$scope][$idx]);
  80. }
  81.  
  82. $out .= "</ul>\n";
  83.  
  84. if (empty($_SESSION['notify'][$scope])) {
  85. unset($_SESSION['notify'][$scope]);
  86. return $out;
  87. }
  88.  
  89. // If we still have notifications they must be pop-ups - render them
  90. foreach ($_SESSION['notify'][$scope] as $idx => $message) {
  91. $popup = '<div id="notification-box">';
  92. $popup .= '<div id="notification-box-content">';
  93. $popup .= '<h2 class="notification-box-msg">' . Enc::html($message[1]) . '</h2>';
  94. $popup .= '<div class="info">Additional actions are available:</div>';
  95. $popup .= '<div id="notification-box-links">';
  96. foreach ($message[2] as $action => $anchor) {
  97. $anchor = Enc::html($anchor);
  98. $popup .= "<span class=\"action-link\"><a href=\"{$action}\">{$anchor}</a></span>";
  99. }
  100.  
  101. $popup .= '</div>';
  102. $popup .= '</div>';
  103. $popup .= '</div>';
  104.  
  105. $out .= '<script type="text/javascript">';
  106. $out .= '$(document).ready(function() {';
  107. $out .= ' $.facebox("' . Enc::js($popup) . '");';
  108. $out .= '})';
  109. $out .= '</script>';
  110. }
  111.  
  112. unset($_SESSION['notify'][$scope]);
  113.  
  114. return $out;
  115. }
  116.  
  117.  
  118. /**
  119.   * Checks to see if a notification of a particular type has been set
  120.   * @param int $type One of the Notification::TYPE_* constants
  121.   * @param string $scope System to allow for multiple notification areas
  122.   * @return bool True if a notification has been set
  123.   */
  124. public static function has($type, $scope = 'default')
  125. {
  126. Session::instance();
  127. if (empty($_SESSION['notify'][$scope])) return false;
  128. foreach ($_SESSION['notify'][$scope] as $record) {
  129. list($rec_type, $message, $actions) = $record;
  130. if ($type == $rec_type) return true;
  131. }
  132. return false;
  133. }
  134.  
  135.  
  136. /**
  137.   * Adds a confirmation message to the list of messages that are shown to the user
  138.   *
  139.   * @param string $message The message to show
  140.   * @param string $format Either 'plain' for plain-text or 'html' for HTML which is limited to a safe subset
  141.   * @param string $scope System to allow for multiple notification areas
  142.   **/
  143. public static function confirm($message, $format = 'plain', $scope = 'default')
  144. {
  145. Session::Instance();
  146. if ($format === 'html') {
  147. $_SESSION['notify'][$scope][] = array(self::TYPE_CONFIRM, Text::limitedSubsetHtml($message), []);
  148. } else {
  149. $_SESSION['notify'][$scope][] = array(self::TYPE_CONFIRM, Enc::html($message), []);
  150. }
  151. }
  152.  
  153.  
  154. /**
  155.   * Adds an error message to the list of messages that are shown to the user
  156.   *
  157.   * @param string $message The message to show
  158.   * @param string $message_format Either 'plain' for plain-text or 'html' for HTML which is limited to a safe subset
  159.   * @param string $scope System to allow for multiple notification areas
  160.   **/
  161. public static function error($message, $format = 'plain', $scope = 'default')
  162. {
  163. Session::Instance();
  164. if ($format === 'html') {
  165. $_SESSION['notify'][$scope][] = array(self::TYPE_ERROR, Text::limitedSubsetHtml($message), []);
  166. } else {
  167. $_SESSION['notify'][$scope][] = array(self::TYPE_ERROR, Enc::html($message), []);
  168. }
  169. }
  170.  
  171.  
  172. /**
  173.   * Adds a popup message to the list of messages that are shown to the user
  174.   *
  175.   * @param string $message The message to show
  176.   * @param array $actions Additional buttons to show with the message. In the format [ url => label ]
  177.   * @param string $scope System to allow for multiple notification areas
  178.   **/
  179. public static function popup($message, array $actions = [], $scope = 'default') {
  180. Session::Instance();
  181. $_SESSION['notify'][$scope][] = array(self::TYPE_POPUP, Enc::html($message), $actions);
  182. }
  183.  
  184. }
  185.  
  186.  
  187.