diff --git a/API.php b/API.php index 64565e3..5f482f3 100644 --- a/API.php +++ b/API.php @@ -19,21 +19,35 @@ public function isEnabled() return false; } + + + public function getTitle() + { + $settings = new SystemSettings(); + return $settings->title->getValue(); + } + + public function getMessage() + { + $settings = new SystemSettings(); + return $settings->message->getValue(); + } + public function getContext() { $settings = new SystemSettings(); return $settings->context->getValue(); } - public function getTitle() + public function getType() { $settings = new SystemSettings(); - return $settings->title->getValue(); + return $settings->type->getValue(); } - public function getMessage() + public function getPriority() { $settings = new SystemSettings(); - return $settings->message->getValue(); + return $settings->priority->getValue(); } } diff --git a/AdminNotification.php b/AdminNotification.php index 1880478..a0b4b10 100644 --- a/AdminNotification.php +++ b/AdminNotification.php @@ -3,10 +3,9 @@ namespace Piwik\Plugins\AdminNotification; use Piwik\Piwik; +use Piwik\Common; use Piwik\Notification; -/** - */ class AdminNotification extends \Piwik\Plugin { private static $hooks = array( @@ -22,37 +21,94 @@ public function registerEvents() public function settingsChangedV3($settings) { if ($settings->getPluginName() === 'AdminNotification') { + self::cancel_notification(); $this->setNotificationV3(); } } public function setNotificationV3() { - // Known issue. The alert notification is not updated until login/logout on v3.x. + // Known issue. The alert notification is not updated until login/logout on v3.x. - // 2.X Compatibility. This method appears to be getting called in v2.X which I didn't - // believe would trigger the newer hooks. + // 2.X Compatibility. This method appears to be getting called in v2.X which I didn't + // believe would trigger the newer hooks. if (!class_exists('\Piwik\Settings\Plugin\SystemSettings')) { //If class doesn't exist just get out. return; } - $settings = new SystemSettings(); - //print_r($settings->enabled->getValue()); + $settings = new SystemSettings(); + //print_r($settings->enabled->getValue()); if ($settings->enabled->getValue()) { - $notification = new Notification($settings->message->getValue()); + + //Transform newlines to avoid removal in sanitization + $message = str_replace(["\r\n","\r","\n"],"!nl",$settings->message->getValue()); + + $sanitized_message = Common::sanitizeInputValue($message); + + //Reverse newline transform + $sanitized_message = str_replace("!nl","\n",$sanitized_message); + + //Process markdown + $markdowned_message = self::minimal_markdown($sanitized_message); + + $notification = new Notification($markdowned_message); $notification->title = $settings->messageTitle->getValue(); $notification->context = $settings->context->getValue(); - $notification->type = Notification::TYPE_PERSISTENT; - //$notification->priority = Notification::PRIORITY_MAX; + $notification->type = $settings->type->getValue(); + $notification->priority = $settings->priority->getValue(); + $notification->raw = true; //echo "NOTIFY"; //print_r($notification); Notification\Manager::notify('AdminNotification_notice', $notification); + Piwik::postEvent('AdminNotification.notice', [&$notification]); } else { - //echo "NOTIFY CANCEL"; - Notification\Manager::cancel('AdminNotification_notice'); + self::cancel_notification(); } } + + public function deactivate() + { + self::cancel_notification(); + } + + private static function cancel_notification(){ + Notification\Manager::cancel('AdminNotification_notice'); + } + + private static function minimal_markdown($escaped_input){ + + //Replace with common Markdown markup + $markdown_processed = preg_replace( + [ + "/\*{3}([\w\s]*)\*{3}/im", + "/\*{2}([\w\s]*)\*{2}/im", + "/\*([\w\s]*)\*/im", + "/^#\s(.*)$/im", + "/^##\s(.*)$/im", + "/^###\s(.*)$/im", + "/^####\s(.*)$/im", + "/\[(.*)\]\(((?:https?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))\)/im", + "/\n/m" + ], + [ + "$1", + "$1", + "$1", + "

$1

", + "

$1

", + "

$1

", + "

$1

", + "$1", + "
\n" + ], + $escaped_input + ); + + //Remove break from headers + return preg_replace("/<\/h(\d)>
/m","",$markdown_processed); + + } } diff --git a/README.md b/README.md index efad69e..8e4f2e3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Adds the ability for Matomo administrators to include an informative message on The easiest way to install is to find the plugin in the [Matomo Marketplace](http://plugins.matomo.org/). ## Changelog - +* 5.1.0 Added message transience and priority options. Added minimal markdown support (see [./docs/index.md](./docs/index.md)) * 5.0.0 Matomo v5 compatible, not backwards compatible. * 3.0.0 Piwik v3 compatible. Effort was made to maintain backwards compatibility. This should work all the way back to 2.12.x * 0.1.2 Tested with Piwik v2.15 and included new registerEvents() hook for compatibility with Piwik 3.0 diff --git a/SystemSettings.php b/SystemSettings.php index 295ce5c..a3b60c3 100644 --- a/SystemSettings.php +++ b/SystemSettings.php @@ -36,25 +36,80 @@ class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings /** @var Setting */ public $message; + /** @var Setting */ + public $type; + + /** @var Setting */ + public $priority; + protected function init() { - $this->enabled = $this->createEnabledSetting(); - $this->context = $this->createContextSetting(); - $this->messageTitle = $this->createTitleSetting(); $this->message = $this->createMessageSetting(); + + $this->context = $this->createContextSetting(); + + $this->type = $this->createTypeSetting(); + + $this->priority = $this->createPrioritySetting(); } private function createEnabledSetting() { - return $this->makeSetting('enabled', $default = false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) { - $field->title = $this->t('EnabledSettingTitle'); - $field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX; - $field->description = $this->t('EnabledSettingDescription'); - $field->readableByCurrentUser = true; + return $this->makeSetting( + 'enabled', + $default = false, + FieldConfig::TYPE_BOOL, + function (FieldConfig $field) { + $field->title = $this->t('EnabledSettingTitle'); + $field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX; + $field->description = $this->t('EnabledSettingDescription'); + } + ); + } + + private function createTitleSetting() + { + return $this->makeSetting( + 'title', + $default = "Message from Matomo Administrator", + FieldConfig::TYPE_STRING, + function (FieldConfig $field) { + $field->title = $this->t('TitleSettingTitle'); + $field->condition = 'enabled'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXT; + $field->description = $this->t('TitleSettingDescription'); + $field->validate = function ($value) { + $value = trim($value); + if (strlen($value) == 0) { + throw new \Exception($this->t('TitleMissing')); + } + }; + } + ); + } + + private function createMessageSetting() + { + return $this->makeSetting( + 'message', + $default = "", + FieldConfig::TYPE_STRING, + function (FieldConfig $field) { + $field->title = $this->t('MessageSettingTitle'); + $field->condition = 'enabled'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA; + $field->description = $this->t('MessageSettingDescription'); + $field->inlineHelp = $this->t('MessageSettingHelp'); + $field->validate = function ($value) { + $value = trim($value); + if (strlen($value) == 0) { + throw new \Exception($this->t('MessageMissing')); + } + }; }); } @@ -62,7 +117,7 @@ private function createContextSetting() { return $this->makeSetting( 'context', - $default = "info", + $default = Notification::CONTEXT_INFO, FieldConfig::TYPE_STRING, function (FieldConfig $field) { $field->title = $this->t('ContextSettingTitle'); @@ -77,30 +132,41 @@ function (FieldConfig $field) { ); } - private function createTitleSetting() + private function createTypeSetting() { return $this->makeSetting( - 'title', - $default = "Message from Piwik Administrator", + 'type', + $default = Notification::TYPE_PERSISTENT, FieldConfig::TYPE_STRING, function (FieldConfig $field) { - $field->title = $this->t('TitleSettingTitle'); + $field->title = $this->t('TypeSettingTitle'); $field->condition = 'enabled'; - $field->uiControl = FieldConfig::UI_CONTROL_TEXT; - //$field->uiControlAttributes = array("size"=> 65); - $field->description = $this->t('TitleSettingDescription'); + $field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT; + $field->description = $this->t('TypeSettingDescription'); + $field->availableValues = array(Notification::TYPE_PERSISTENT => Notification::TYPE_PERSISTENT, + Notification::TYPE_TRANSIENT => Notification::TYPE_TRANSIENT, + Notification::TYPE_TOAST => Notification::TYPE_TOAST); } ); } - private function createMessageSetting() + private function createPrioritySetting() { - return $this->makeSetting('message', $default = "", FieldConfig::TYPE_STRING, function (FieldConfig $field) { - $field->title = $this->t('MessageSettingTitle'); - $field->condition = 'enabled'; - $field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA; - $field->description = $this->t('MessageSettingDescription'); - }); + return $this->makeSetting( + 'priority', + $default = Notification::PRIORITY_MAX, + FieldConfig::TYPE_FLOAT, + function (FieldConfig $field) { + $field->title = $this->t('PrioritySettingTitle'); + $field->condition = 'enabled'; + $field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT; + $field->description = $this->t('PrioritySettingDescription'); + $field->availableValues = array(Notification::PRIORITY_MIN => Notification::PRIORITY_MIN, + Notification::PRIORITY_LOW => Notification::PRIORITY_LOW, + Notification::PRIORITY_HIGH => Notification::PRIORITY_HIGH, + Notification::PRIORITY_MAX => Notification::PRIORITY_MAX); + } + ); } private function t($translate_token) diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..a66d4f1 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,37 @@ +# Documentation + +## Message Markdown +A minimal set of markdown support was added to the message field with 5.1 release of the plugin. + +The following markdown variants are supported* + +### Headings +```markdown +# Header 1 +Message + +## Header 2 +Message + +### Header 3 +Message + +### Header 4 +Message +``` +> *Need to be at beginning of line + +### Bolding and Italics +```markdown +*This is italicized* + +**This is bolded** + +***This is italicized and bolded*** +``` + +### Links +Links will open in a new tab -> target="_blank". This behavior cannot be changed. +```markdown +[Link Name](https://github.com/jbrule) +``` \ No newline at end of file diff --git a/lang/en.json b/lang/en.json index b904251..855bc00 100644 --- a/lang/en.json +++ b/lang/en.json @@ -2,12 +2,19 @@ "AdminNotification": { "PluginDescription": "Places a message on all user's dashboards.", "EnabledSettingTitle": "Enabled", - "EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required in v3.x)", + "EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required for ≥ v3.x)", "MessageSettingTitle": "Message", - "MessageSettingDescription": "Message to display to users", + "MessageSettingDescription": "Message to display to users. Supports a minimal set of markdown.", + "MessageSettingHelp": "See plugin documentation for supported markdown.", + "MessageMissing": "You must provide a message.", "ContextSettingTitle": "Context", "ContextSettingDescription": "Sets what type of message should be displayed (affects color).", "TitleSettingTitle": "Title", - "TitleSettingDescription": "Sets the title of the message. This will be dislayed in bold with the message." + "TitleSettingDescription": "Sets the title of the message. This will be displayed in bold with the message.", + "TitleMissing": "You must provide a title.", + "TypeSettingTitle": "Type", + "TypeSettingDescription": "Persistent (display until user closes the notification), Transient (displays once, disappear on page reload) or Toast (displayed for a few seconds, and the fades out)", + "PrioritySettingTitle": "Priority", + "PrioritySettingDescription": "Higher priority, higher order" } } \ No newline at end of file diff --git a/lang/ja.json b/lang/ja.json index 9576334..1543a39 100644 --- a/lang/ja.json +++ b/lang/ja.json @@ -2,13 +2,20 @@ "AdminNotification": { "PluginDescription": "Places a message on all user's dashboards.", "EnabledSettingTitle": "使用可能", - "EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required in v3.x)", + "EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required for ≥ v3.x)", "MessageSettingTitle": "メッセージ", - "MessageSettingDescription": "Message to display to users", + "MessageSettingDescription": "Message to display to users. Supports a minimal set of markdown.", + "MessageSettingHelp": "See plugin documentation for supported markdown.", + "MessageMissing": "You must provide a message.", "ContextSettingTitle": "コンテキスト", "ContextSettingDescription": "Sets what type of message should be displayed (affects color).", "TitleSettingTitle": "タイトル", - "TitleSettingDescription": "Sets the title of the message. This will be dislayed in bold with the message." + "TitleSettingDescription": "Sets the title of the message. This will be displayed in bold with the message.", + "TitleMissing": "You must provide a title.", + "TypeSettingTitle": "型", + "TypeSettingDescription": "Persistent (display until user closes the notification), Transient (displays once, disappear on page reload) or Toast (displayed for a few seconds, and the fades out)", + "PrioritySettingTitle": "優先", + "PrioritySettingDescription": "Higher priority, higher order" } } diff --git a/plugin.json b/plugin.json index daab9e9..40d56e7 100644 --- a/plugin.json +++ b/plugin.json @@ -1,6 +1,6 @@ { "name": "AdminNotification", - "version": "5.0.0", + "version": "5.1.0", "description": "Adds the ability for Piwik administrators to include an informative message to all user's dashboards. This uses the built in Notification function.", "license": "GPL v3+", "keywords": [ diff --git a/screenshots/Dashboard_with_Notification.png b/screenshots/Dashboard_with_Notification.png index 2e98005..89bb579 100644 Binary files a/screenshots/Dashboard_with_Notification.png and b/screenshots/Dashboard_with_Notification.png differ diff --git a/screenshots/Notification_Settings.png b/screenshots/Notification_Settings.png index 8cf91ef..581e72a 100644 Binary files a/screenshots/Notification_Settings.png and b/screenshots/Notification_Settings.png differ