Skip to content

Commit

Permalink
5.1 rc (#22)
Browse files Browse the repository at this point in the history
* isWritableByCurrentUser is not part of systemsettings, remove it

* post event for message

* add the posibility to use persistent or transistent notification

* add toast type also

* Changes for Jan 3, 2025.

* Fixed static method reference. Grammer fixes

* Version 5.1 rc changes

---------

Co-authored-by: Mikke Schirén <[email protected]>
Co-authored-by: Josh <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2025
1 parent 69499ae commit 4402665
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 47 deletions.
22 changes: 18 additions & 4 deletions API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
80 changes: 68 additions & 12 deletions AdminNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
],
[
"<em><strong>$1</strong></em>",
"<strong>$1</strong>",
"<em>$1</em>",
"<h1>$1</h1>",
"<h2>$1</h2>",
"<h3>$1</h3>",
"<h4>$1</h4>",
"<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"$2\">$1</a>",
"<br>\n"
],
$escaped_input
);

//Remove break from headers
return preg_replace("/<\/h(\d)><br>/m","</h$1>",$markdown_processed);

}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 89 additions & 23 deletions SystemSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,88 @@ 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'));
}
};
});
}

private function createContextSetting()
{
return $this->makeSetting(
'context',
$default = "info",
$default = Notification::CONTEXT_INFO,
FieldConfig::TYPE_STRING,
function (FieldConfig $field) {
$field->title = $this->t('ContextSettingTitle');
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -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)
```
13 changes: 10 additions & 3 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https://plugins.matomo.org/AdminNotification#documentation\">plugin documentation</a> 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"
}
}
Loading

0 comments on commit 4402665

Please sign in to comment.