API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Notification.php
215 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | /** |
| 12 | * Describes a UI notification. |
| 13 | * |
| 14 | * UI notifications are messages displayed to the user near the top of the screen. |
| 15 | * Notifications consist of a message, a context (the message type), a priority |
| 16 | * and a display type. |
| 17 | * |
| 18 | * **The context** affects the way the message looks, but not how it is displayed. |
| 19 | * |
| 20 | * **The display type** determines how the message is displayed. |
| 21 | * |
| 22 | * **The priority** determines where it is shown in the list of all displayed notifications. |
| 23 | * |
| 24 | * ### Examples |
| 25 | * |
| 26 | * **Display an error message** |
| 27 | * |
| 28 | * $notification = new Notification('My Error Message'); |
| 29 | * $notification->context = Notification::CONTEXT_ERROR; |
| 30 | * Notification\Manager::notify('myUniqueNotificationId', $notification); |
| 31 | * |
| 32 | * **Display a temporary success message** |
| 33 | * |
| 34 | * $notification = new Notificiation('Success'); |
| 35 | * $notification->context = Notification::CONTEXT_SUCCESS; |
| 36 | * $notification->type = Notification::TYPE_TOAST; |
| 37 | * Notification\Manager::notify('myUniqueNotificationId', $notification); |
| 38 | * |
| 39 | * **Display a message near the top of the screen** |
| 40 | * |
| 41 | * $notification = new Notification('Urgent: Your password has expired!'); |
| 42 | * $notification->context = Notification::CONTEXT_INFO; |
| 43 | * $notification->type = Notification::TYPE_PERSISTENT; |
| 44 | * $notification->priority = Notification::PRIORITY_MAX; |
| 45 | * |
| 46 | * @api |
| 47 | */ |
| 48 | class Notification |
| 49 | { |
| 50 | const CONTEXT_SUCCESS = 'success'; |
| 51 | const CONTEXT_ERROR = 'error'; |
| 52 | const CONTEXT_INFO = 'info'; |
| 53 | const CONTEXT_WARNING = 'warning'; |
| 54 | |
| 55 | /** |
| 56 | * Lowest priority value. |
| 57 | */ |
| 58 | const PRIORITY_MIN = 1; |
| 59 | |
| 60 | /** |
| 61 | * Lower priority value. |
| 62 | */ |
| 63 | const PRIORITY_LOW = 25; |
| 64 | |
| 65 | /** |
| 66 | * Higher priority value. |
| 67 | */ |
| 68 | const PRIORITY_HIGH = 50; |
| 69 | |
| 70 | /** |
| 71 | * Highest priority value. |
| 72 | */ |
| 73 | const PRIORITY_MAX = 100; |
| 74 | |
| 75 | /** |
| 76 | * If this flag is applied, no close icon will be displayed. _Note: persistent notifications always have a close |
| 77 | * icon._ |
| 78 | * |
| 79 | * See {@link $flags}. |
| 80 | */ |
| 81 | const FLAG_NO_CLEAR = 1; |
| 82 | |
| 83 | /** |
| 84 | * If this flag is applied, a close icon will be displayed. |
| 85 | * |
| 86 | * See {@link $flags}. |
| 87 | */ |
| 88 | const FLAG_CLEAR = 0; |
| 89 | |
| 90 | /** |
| 91 | * Notifications of this type will be displayed for a few seconds and then faded out. |
| 92 | */ |
| 93 | const TYPE_TOAST = 'toast'; |
| 94 | |
| 95 | /** |
| 96 | * Notifications of this type will be displayed until the new user explicitly closes the notification. |
| 97 | * The notifications will display even if the user reloads the page. |
| 98 | */ |
| 99 | const TYPE_PERSISTENT = 'persistent'; |
| 100 | |
| 101 | /** |
| 102 | * Notifications of this type will be displayed only once. They will disappear after a page reload or |
| 103 | * change. |
| 104 | */ |
| 105 | const TYPE_TRANSIENT = 'transient'; |
| 106 | |
| 107 | /** |
| 108 | * The notification title. The title is optional and is displayed directly before the message content. |
| 109 | * |
| 110 | * @var string |
| 111 | */ |
| 112 | public $title; |
| 113 | |
| 114 | /** |
| 115 | * The notification message. Must be set. |
| 116 | * |
| 117 | * @var string |
| 118 | */ |
| 119 | public $message; |
| 120 | |
| 121 | /** |
| 122 | * Contains extra display options. |
| 123 | * |
| 124 | * Usage: `$notification->flags = Notification::FLAG_BAR | Notification::FLAG_FOO`. |
| 125 | * |
| 126 | * @var int |
| 127 | */ |
| 128 | public $flags = self::FLAG_NO_CLEAR; |
| 129 | |
| 130 | /** |
| 131 | * The notification's display type. See `TYPE_*` constants in {@link Notification}. |
| 132 | * |
| 133 | * @var string |
| 134 | */ |
| 135 | public $type = self::TYPE_TRANSIENT; |
| 136 | |
| 137 | /** |
| 138 | * The notification's context (message type). See `CONTEXT_*` constants in {@link Notification}. |
| 139 | * |
| 140 | * A notification's context determines how it will be styled. |
| 141 | * |
| 142 | * @var string |
| 143 | */ |
| 144 | public $context = self::CONTEXT_INFO; |
| 145 | |
| 146 | /** |
| 147 | * The notification's priority. The higher the priority, the higher the order. See `PRIORITY_*` |
| 148 | * constants in {@link Notification} to see possible priority values. |
| 149 | * |
| 150 | * @var int |
| 151 | */ |
| 152 | public $priority; |
| 153 | |
| 154 | /** |
| 155 | * If true, the message will not be escaped before being outputted as HTML. If you set this to |
| 156 | * `true`, make sure you escape text yourself in order to avoid XSS vulnerabilities. |
| 157 | * |
| 158 | * @var bool |
| 159 | */ |
| 160 | public $raw = false; |
| 161 | |
| 162 | /** |
| 163 | * Constructor. |
| 164 | * |
| 165 | * @param string $message The notification message. |
| 166 | * @throws \Exception If the message is empty. |
| 167 | */ |
| 168 | public function __construct($message) |
| 169 | { |
| 170 | if (empty($message)) { |
| 171 | throw new \Exception('No notification message given'); |
| 172 | } |
| 173 | |
| 174 | $this->message = $message; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns `1` if the notification will be displayed without a close button, `0` if otherwise. |
| 179 | * |
| 180 | * @return int `1` or `0`. |
| 181 | */ |
| 182 | public function hasNoClear() |
| 183 | { |
| 184 | if ($this->flags & self::FLAG_NO_CLEAR) { |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the notification's priority. If no priority has been set, a priority will be set based |
| 193 | * on the notification's context. |
| 194 | * |
| 195 | * @return int |
| 196 | */ |
| 197 | public function getPriority() |
| 198 | { |
| 199 | if (!isset($this->priority)) { |
| 200 | $typeToPriority = array(static::CONTEXT_ERROR => static::PRIORITY_MAX, |
| 201 | static::CONTEXT_WARNING => static::PRIORITY_HIGH, |
| 202 | static::CONTEXT_SUCCESS => static::PRIORITY_MIN, |
| 203 | static::CONTEXT_INFO => static::PRIORITY_LOW); |
| 204 | |
| 205 | if (array_key_exists($this->context, $typeToPriority)) { |
| 206 | return $typeToPriority[$this->context]; |
| 207 | } |
| 208 | |
| 209 | return static::PRIORITY_LOW; |
| 210 | } |
| 211 | |
| 212 | return $this->priority; |
| 213 | } |
| 214 | } |
| 215 |