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

V3 compatibility #4

Merged
merged 2 commits into from
Sep 11, 2017
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
57 changes: 51 additions & 6 deletions AdminNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace Piwik\Plugins\AdminNotification;

use Piwik\Piwik;
use Piwik\Notification;

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

//2.15 includes a new function for registering hooks. 2.15 wi
//2.15 includes a new function for registering hooks.
public function registerEvents()
{
return self::$hooks;
Expand All @@ -27,9 +30,15 @@ public function getListHooksRegistered()
return self::$hooks;
}

function postLoad()
{
//Piwik::addAction('SystemSettings.updated', array($this, 'settingsChangedV3'));
}

public function setNotification(){

$settings = API::getInstance();

$enabled = $settings->isEnabled();

if ($enabled) {
Expand All @@ -44,9 +53,45 @@ public function setNotification(){
$notification->context = $context;
$notification->type = Notification::TYPE_PERSISTENT;
$notification->priority = Notification::PRIORITY_MAX;
Notification\Manager::notify('admin_message', $notification);
Notification\Manager::notify('AdminNotification_notice', $notification);
}else{
Notification\Manager::cancel('admin_message');
Notification\Manager::cancel('AdminNotification_notice');
}
}

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;
}

$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');
}
}
}
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#Piwik AdminNotification Plugin
##Description
# Piwik 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 maintainance windows.

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

##Changelog
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
## Changelog
* 3.0.0 Piwik v3 compatible. Effort was made to maintain backwords compatibility. This should work all the way back to 2.12.x
* 4.0.1.2 Tested with Piwik v2.15 and included new registerEvents() hook for compatibility with Piwik 3.0
* 5.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
Expand Down
99 changes: 99 additions & 0 deletions SystemSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\AdminNotification;

use Piwik\Piwik;
use Piwik\Notification;
use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;

/**
* Defines Settings for AdminNotification.
*
* Usage like this:
* $settings = new SystemSettings();
* $settings->metric->getValue();
* $settings->description->getValue();
*/
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{
/** @var Setting */
public $enabled;

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

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

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

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) {
$field->title = $this->t('EnabledSettingTitle');
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
$field->description = $this->t('EnabledSettingDescription');
$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,
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;
//$field->uiControlAttributes = array("size"=> 65);
$field->description = $this->t('TitleSettingDescription');
});
}

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');
});
}

private function t($translate_token){
return Piwik::translate("AdminNotification_".$translate_token);
}
}
4 changes: 2 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"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.",
"EnabledSettingTitle": "Enabled",
"EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required in v3.x)",
"MessageSettingTitle": "Message",
"MessageSettingDescription": "Message to display to users",
"ContextSettingTitle": "Context",
Expand Down
2 changes: 1 addition & 1 deletion lang/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"AdminNotification": {
"PluginDescription": "Places a message on all user's dashboards.",
"EnabledSettingTitle": "使用可能",
"EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login.",
"EnabledSettingDescription": "If enabled, the provided message will be displayed to users upon login. (Logoff/Logon required in v3.x)",
"MessageSettingTitle": "メッセージ",
"MessageSettingDescription": "Message to display to users",
"ContextSettingTitle": "コンテキスト",
Expand Down
41 changes: 22 additions & 19 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"name": "AdminNotification",
"version": "0.1.2",
"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": ["admin message", "notification"],
"homepage": "https://github.com/jbrule/piwikplugin-AdminNotification",
"theme": false,
"require": {
"piwik": ">=2.12.0"
},
"authors": [
{
"name": "Josh Brule",
"email": "",
"homepage": "https://www.linkedin.com/pub/joshua-brule/15/326/9b9"
}
]
}
{
"name": "AdminNotification",
"version": "3.0.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": [
"admin message",
"notification"
],
"homepage": "https:\/\/github.com\/jbrule\/piwikplugin-AdminNotification",
"theme": false,
"require": {
"piwik": ">=2.12.0,<4.0.0-b1"
},
"authors": [
{
"name": "Josh Brule",
"email": "",
"homepage": "https:\/\/github.com\/jbrule"
}
]
}