1<?php
2/**
3 * Tag Alerts plugin main file
4 *
5 * @author   Simon DELAGE <sdelage@gmail.com>
6 * @license: CC Attribution-Share Alike 3.0 Unported <http://creativecommons.org/licenses/by-sa/3.0/>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12
13require_once (DOKU_PLUGIN . 'action.php');
14
15class action_plugin_tagalerts extends DokuWiki_Action_Plugin{
16
17    function register(Doku_Event_Handler $controller) {
18        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'AFTER', $this, 'init', array());
19        $controller->register_hook('TPL_TOC_RENDER', 'AFTER', $this, 'alert', array());
20        $controller->register_hook('PLUGIN_TAG_LINK', 'AFTER', $this, 'link', array());
21        $controller->register_hook('CONFMANAGER_CONFIGFILES_REGISTER', 'BEFORE',  $this, 'addConfigFile', array());
22    }
23
24    function init(&$event, $param) {
25        global $ID;
26        global $conf;
27
28        $tagplugin = plugin_load('helper', 'tag');
29        if(is_null($tagplugin)) {
30            msg($this->getLang('tag_required'), -1);
31            return false;
32        }
33        // Fetch tags for the page; stop proceeding when no tags specified
34        $tags = p_get_metadata($ID, 'subject', METADATA_DONT_RENDER);
35        if(is_null($tags)) true;
36
37        foreach($event->data['meta'] as &$meta) {
38            if($meta['name'] == 'keywords') {
39                // Get an array of page's tags
40                $this->pagetags = explode(',', $meta['content']);
41            }
42        }
43        // Load special messages from ...tagalerts/conf/tagalerts.conf to global conf
44        $specAlertsFile = DOKU_CONF.'tagalerts.conf';
45        if (@file_exists($specAlertsFile)) {
46            $conf['plugin']['tagalerts']['specAlerts'] = confToHash($specAlertsFile);
47        }
48    }
49
50    function alert(&$event, $param) {
51        global $conf;
52        global $ACT;
53
54        if ((($this->getConf('action') == "messages") or ($this->getConf('forcemsg') != null)) & ($ACT == "show")) {
55            // Get an array of triggers from settings (make sure the list is well formated: no blanks between triggers and no '_' in triggers)
56            $errorTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('error'))));
57            $infoTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('info'))));
58            $successTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('success'))));
59            $notifyTriggers = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('notify'))));
60            // Get matches between page tags and triggers (don't preserve keys)
61            $tagalerts = array();
62            $tagalerts['error'] = array_values((array_intersect($this->pagetags, $errorTriggers)));
63            $tagalerts['info'] = array_values((array_intersect($this->pagetags, $infoTriggers)));
64            $tagalerts['success'] = array_values((array_intersect($this->pagetags, $successTriggers)));
65            $tagalerts['notify'] = array_values((array_intersect($this->pagetags, $notifyTriggers)));
66            foreach($tagalerts as $type=>$tags) {
67                for ($i = 0; $i < count($tags); $i++) {
68                    $underscored = str_replace(' ', '_', $tags[$i]);
69                    if ((isset($tags[$i])) and (($this->getConf('action') == "messages") or (strpos($this->getConf('forcemsg'), $underscored) !== false))) {
70                        // Alert from conf file
71                        if (isset($conf['plugin']['tagalerts']['specAlerts'][$underscored])) {
72                            $msg = $conf['plugin']['tagalerts']['specAlerts'][$underscored];
73                        // Or from localized $conf
74                        } else {
75                            $msg = $this->getLang('tagalerts').$tags[$i].".";
76                        }
77                        echo '<div class="tag'.$type.'">'.hsc($msg).'</div>';
78                    }
79                }
80            }
81        }
82    }
83
84    function link(&$event) {
85        global $conf;
86        global $ACT;
87
88        if (($this->getConf('action') == "inline") & ($ACT == "show")) {
89            $href = $event->data['href'];
90            $class = $event->data['class'];
91            $tooltip = $event->data['tooltip'];
92            $title = $event->data['title'];
93            // CLASS
94            // Get an array of notification triggers from 'notify' option (make sure the list is well formated: no blanks between triggers and no '_' in triggers)
95            $triggers = array();
96            $triggers['error'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('error'))));
97            $triggers['info'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('info'))));
98            $triggers['success'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('success'))));
99            $triggers['notify'] = explode(',',str_replace('_', ' ', str_replace(', ', ',', $this->getConf('notify'))));
100            foreach($triggers as $type=>$val) {
101                if (in_array($title, $val)) {
102                    $class = $class.' tag'.$type;
103                }
104            }
105            // TOOLTIP
106            if (isset($conf['plugin']['tagalerts']['specAlerts'][$title])) {
107                $tooltip = $conf['plugin']['tagalerts']['specAlerts'][$title]." (".$tooltip.")";
108            }
109            // RESULT
110            $event->data = array(
111                'href' => $href,
112                'class' => $class,
113                'tooltip' => $tooltip,
114                'title' => $title
115            );
116        }
117    }
118
119    // Register the plugin conf file in ConfManager Plugin
120    public function addConfigFile(Doku_Event $event, $params) {
121        if (class_exists('ConfigManagerTwoLine')) {
122            $config = new ConfigManagerTwoLine('Tag Alerts', $this->getLang('confdescription'), DOKU_CONF . 'tagalerts.conf');
123            $event->data[] = $config;
124        }
125    }
126}
127