dokuwiki-plugin-tagalerts/action.php

127 lines
5.9 KiB
PHP
Raw Normal View History

2015-06-12 23:49:18 +00:00
<?php
/**
* Tag Alerts plugin main file
*
2019-01-25 20:52:09 +00:00
* @author Simon DELAGE <sdelage@gmail.com>
2015-06-12 23:49:18 +00:00
* @license: CC Attribution-Share Alike 3.0 Unported <http://creativecommons.org/licenses/by-sa/3.0/>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'action.php');
class action_plugin_tagalerts extends DokuWiki_Action_Plugin{
function register(Doku_Event_Handler $controller) {
2015-06-12 23:49:18 +00:00
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'AFTER', $this, 'init', array());
$controller->register_hook('TPL_TOC_RENDER', 'AFTER', $this, 'alert', array());
2015-06-14 01:06:52 +00:00
$controller->register_hook('PLUGIN_TAG_LINK', 'AFTER', $this, 'link', array());
2015-06-12 23:49:18 +00:00
$controller->register_hook('CONFMANAGER_CONFIGFILES_REGISTER', 'BEFORE', $this, 'addConfigFile', array());
}
function init(&$event, $param) {
global $ID;
global $conf;
$tagplugin = plugin_load('helper', 'tag');
if(is_null($tagplugin)) {
2015-06-12 23:49:18 +00:00
msg($this->getLang('tag_required'), -1);
return false;
}
// Fetch tags for the page; stop proceeding when no tags specified
$tags = p_get_metadata($ID, 'subject', METADATA_DONT_RENDER);
if(is_null($tags)) true;
foreach($event->data['meta'] as &$meta) {
if($meta['name'] == 'keywords') {
// Get an array of page's tags
$this->pagetags = explode(',', $meta['content']);
}
2015-06-20 07:58:39 +00:00
}
2015-06-14 01:06:52 +00:00
// Load special messages from ...tagalerts/conf/tagalerts.conf to global conf
2015-06-20 07:58:39 +00:00
$specAlertsFile = DOKU_CONF.'tagalerts.conf';
2015-06-12 23:49:18 +00:00
if (@file_exists($specAlertsFile)) {
$conf['plugin']['tagalerts']['specAlerts'] = confToHash($specAlertsFile);
}
}
function alert(&$event, $param) {
global $conf;
2015-06-14 01:06:52 +00:00
global $ACT;
2015-06-12 23:49:18 +00:00
if ((($this->getConf('action') == "messages") or ($this->getConf('forcemsg') != null)) & ($ACT == "show")) {
// Get an array of triggers from settings (make sure the list is well formated: no blanks between triggers and no '_' in triggers)
2015-06-14 01:06:52 +00:00
$errorTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('error'))));
$infoTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('info'))));
$successTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('success'))));
$notifyTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('notify'))));
// Get matches between page tags and triggers (don't preserve keys)
$tagalerts = array();
$tagalerts['error'] = array_values((array_intersect($this->pagetags, $errorTriggers)));
$tagalerts['info'] = array_values((array_intersect($this->pagetags, $infoTriggers)));
$tagalerts['success'] = array_values((array_intersect($this->pagetags, $successTriggers)));
$tagalerts['notify'] = array_values((array_intersect($this->pagetags, $notifyTriggers)));
foreach($tagalerts as $type=>$tags) {
for ($i = 0; $i < count($tags); $i++) {
$underscored = str_replace(' ', '_', $tags[$i]);
if ((isset($tags[$i])) and (($this->getConf('action') == "messages") or (strpos($this->getConf('forcemsg'), $underscored) !== false))) {
// Alert from conf file
if (isset($conf['plugin']['tagalerts']['specAlerts'][$underscored])) {
$msg = $conf['plugin']['tagalerts']['specAlerts'][$underscored];
// Or from localized $conf
} else {
$msg = $this->getLang('tagalerts').$tags[$i].".";
}
echo '<div class="tag'.$type.'">'.hsc($msg).'</div>';
2015-06-12 23:49:18 +00:00
}
}
}
}
}
2015-06-14 01:06:52 +00:00
function link(&$event) {
global $conf;
global $ACT;
if (($this->getConf('action') == "inline") & ($ACT == "show")) {
$href = $event->data['href'];
$class = $event->data['class'];
$tooltip = $event->data['tooltip'];
$title = $event->data['title'];
// CLASS
// Get an array of notification triggers from 'notify' option (make sure the list is well formated: no blanks between triggers and no '_' in triggers)
$triggers = array();
$triggers['error'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('error'))));
$triggers['info'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('info'))));
$triggers['success'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('success'))));
$triggers['notify'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('notify'))));
foreach($triggers as $type=>$val) {
if (in_array($title, $val)) {
$class = $class.' tag'.$type;
}
}
// TOOLTIP
if (isset($conf['plugin']['tagalerts']['specAlerts'][$title])) {
$tooltip = $conf['plugin']['tagalerts']['specAlerts'][$title]." (".$tooltip.")";
}
// RESULT
$event->data = array(
'href' => $href,
'class' => $class,
'tooltip' => $tooltip,
'title' => $title
);
}
}
2015-06-12 23:49:18 +00:00
// Register the plugin conf file in ConfManager Plugin
public function addConfigFile(Doku_Event $event, $params) {
if (class_exists('ConfigManagerTwoLine')) {
2015-06-20 07:58:39 +00:00
$config = new ConfigManagerTwoLine('Tag Alerts', $this->getLang('confdescription'), DOKU_CONF . 'tagalerts.conf');
2015-06-12 23:49:18 +00:00
$event->data[] = $config;
}
}
}