Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated for Matomo 5 #9

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
28 changes: 15 additions & 13 deletions API.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

class API extends \Piwik\Plugin\API
{

private static $plugin_name = 'AdminNotification';

public function isEnabled()
{
$settings = new Settings(self::$plugin_name);
$settings = new SystemSettings();
$enabled = $settings->enabled->getValue();
$message = $this->getMessage();

Expand All @@ -19,19 +18,22 @@ public function isEnabled()

return false;
}

public function getContext(){
$settings = new Settings(self::$plugin_name);

public function getContext()
{
$settings = new SystemSettings();
return $settings->context->getValue();
}

public function getTitle(){
$settings = new Settings(self::$plugin_name);

public function getTitle()
{
$settings = new SystemSettings();
return $settings->title->getValue();
}

public function getMessage(){
$settings = new Settings(self::$plugin_name);

public function getMessage()
{
$settings = new SystemSettings();
return $settings->message->getValue();
}
}
}
65 changes: 33 additions & 32 deletions AdminNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,53 @@
use Piwik\Notification;

/**
*/
*/
class AdminNotification extends \Piwik\Plugin
{
private static $hooks = array(
'Login.authenticate.successful' => 'setNotificationV3', //Version 3.X Post login handler
'SystemSettings.updated' => 'settingsChangedV3' //Version 3.X Setting updated handler
);

public function registerEvents()
{
return self::$hooks;
}

public function settingsChangedV3($settings){
if($settings->getPluginName() === 'AdminNotification'){

public function settingsChangedV3($settings)
{
if ($settings->getPluginName() === 'AdminNotification') {
$this->setNotificationV3();
}
}

public function setNotificationV3(){
//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.
if(!class_exists('\Piwik\Settings\Plugin\SystemSettings')){ //If class doesn't exist just get out.
return;
}


public function setNotificationV3()
{
// 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.
if (!class_exists('\Piwik\Settings\Plugin\SystemSettings')) { //If class doesn't exist just get out.
return;
}

$settings = new SystemSettings();
//print_r($settings->enabled->getValue());

if($settings->enabled->getValue()){

$notification = new Notification($settings->message->getValue());
$notification->title = $settings->messageTitle->getValue();
$notification->context = $settings->context->getValue();
$notification->type = Notification::TYPE_PERSISTENT;
//$notification->priority = Notification::PRIORITY_MAX;

//echo "NOTIFY";
//print_r($notification);

Notification\Manager::notify('AdminNotification_notice', $notification);

}else{
//echo "NOTIFY CANCEL";
Notification\Manager::cancel('AdminNotification_notice');
}

if ($settings->enabled->getValue()) {
$notification = new Notification($settings->message->getValue());
$notification->title = $settings->messageTitle->getValue();
$notification->context = $settings->context->getValue();
$notification->type = Notification::TYPE_PERSISTENT;
//$notification->priority = Notification::PRIORITY_MAX;

//echo "NOTIFY";
//print_r($notification);

Notification\Manager::notify('AdminNotification_notice', $notification);
} else {
//echo "NOTIFY CANCEL";
Notification\Manager::cancel('AdminNotification_notice');
}
}
}
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
# Piwik AdminNotification Plugin
# Matomo AdminNotification Plugin

## Description
Adds the ability for Piwik administrators to include an informative message on all users' dashboards. This may be useful for communicating with users in larger shared environments. In our setup we were tracking 1,900 websites with 250 users. This is a solution we wrote to allow us to easily inform our users of maintenance windows.

Adds the ability for Matomo administrators to include an informative message on all users' dashboards. This may be useful for communicating with users in larger shared environments. In our setup we were tracking 1,900 websites with 250 users. This is a solution we wrote to allow us to easily inform our users of maintenance windows.

## Instructions
The easiest way to install is to find the plugin in the [Piwik Marketplace](http://plugins.piwik.org/).

The easiest way to install is to find the plugin in the [Matomo Marketplace](http://plugins.matomo.org/).

## Changelog

* 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
* 0.1.1 Cleanup. Removed plugin template verbiage from code files.
* 0.1.0 Initial Release

## Known Issues

* v3.0.0 Display/Update of notification requires logout/login to see change. (Notification API not working as expected during tests)

## License

GPL v3 / fair use

## Support
Please [report any issues](https://github.com/jbrule/piwikplugin-AdminNotification/issues). Pull requests welcome.

Please [report any issues](https://github.com/jbrule/Matomoplugin-AdminNotification/issues). Pull requests welcome.
101 changes: 0 additions & 101 deletions Settings.php

This file was deleted.

57 changes: 34 additions & 23 deletions SystemSettings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Piwik - free/libre analytics platform
*
Expand Down Expand Up @@ -28,7 +29,7 @@ class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings

/** @var Setting */
public $context;

/** @var Setting */
public $messageTitle;

Expand All @@ -41,13 +42,12 @@ protected function init()
$this->enabled = $this->createEnabledSetting();

$this->context = $this->createContextSetting();

$this->messageTitle = $this->createTitleSetting();

$this->message = $this->createMessageSetting();

}

private function createEnabledSetting()
{
return $this->makeSetting('enabled', $default = false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
Expand All @@ -57,32 +57,42 @@ private function createEnabledSetting()
$field->readableByCurrentUser = true;
});
}

private function createContextSetting()
{
return $this->makeSetting('context', $default = "info", FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = $this->t('ContextSettingTitle');
$field->condition = 'enabled';
$field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT;
$field->description = $this->t('ContextSettingDescription');
$field->availableValues = array(Notification::CONTEXT_INFO => Notification::CONTEXT_INFO,
return $this->makeSetting(
'context',
$default = "info",
FieldConfig::TYPE_STRING,
function (FieldConfig $field) {
$field->title = $this->t('ContextSettingTitle');
$field->condition = 'enabled';
$field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT;
$field->description = $this->t('ContextSettingDescription');
$field->availableValues = array(Notification::CONTEXT_INFO => Notification::CONTEXT_INFO,
Notification::CONTEXT_ERROR => Notification::CONTEXT_ERROR,
Notification::CONTEXT_SUCCESS => Notification::CONTEXT_SUCCESS,
Notification::CONTEXT_WARNING => Notification::CONTEXT_WARNING);
});
}
);
}

private function createTitleSetting()
{
return $this->makeSetting('title', $default = "Message from Piwik Administrator", FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = $this->t('TitleSettingTitle');
$field->condition = 'enabled';
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
return $this->makeSetting(
'title',
$default = "Message from Piwik Administrator",
FieldConfig::TYPE_STRING,
function (FieldConfig $field) {
$field->title = $this->t('TitleSettingTitle');
$field->condition = 'enabled';
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
//$field->uiControlAttributes = array("size"=> 65);
$field->description = $this->t('TitleSettingDescription');
});
$field->description = $this->t('TitleSettingDescription');
}
);
}

private function createMessageSetting()
{
return $this->makeSetting('message', $default = "", FieldConfig::TYPE_STRING, function (FieldConfig $field) {
Expand All @@ -92,8 +102,9 @@ private function createMessageSetting()
$field->description = $this->t('MessageSettingDescription');
});
}

private function t($translate_token){
return Piwik::translate("AdminNotification_".$translate_token);

private function t($translate_token)
{
return Piwik::translate("AdminNotification_" . $translate_token);
}
}
Loading