1<?php 2/** 3 * DokuWiki Plugin notification (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Szymon Olewniczak <it@rid.pl> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) { 11 die(); 12} 13 14class syntax_plugin_notification_list extends DokuWiki_Syntax_Plugin 15{ 16 function getType() { 17 return 'substition'; 18 } 19 20 function getSort() { 21 return 20; 22 } 23 24 function PType() { 25 return 'block'; 26 } 27 28 function connectTo($mode) { 29 $this->Lexer->addSpecialPattern('----+ *notification list *-+\n.*?----+', $mode,'plugin_notification_list'); 30 } 31 32 function handle($match, $state, $pos, Doku_Handler $handler){ 33 $lines = explode("\n", $match); 34 array_shift($lines); 35 array_pop($lines); 36 37 $params = [ 38 'plugin' => '.*', 39 'user' => '$USER$', 40 'full' => true, 41 'date' => '%Y-%m-%d' 42 ]; 43 foreach ($lines as $line) { 44 $pair = explode(':', $line, 2); 45 if (count($pair) < 2) { 46 continue; 47 } 48 $key = trim($pair[0]); 49 $value = trim($pair[1]); 50 51 if ($key == 'full') { 52 $value = $value == '0' ? false : true; 53 } 54 55 $params[$key] = $value; 56 } 57 return $params; 58 } 59 60 /** 61 * Render xhtml output or metadata 62 * 63 * @param string $mode Renderer mode (supported modes: xhtml) 64 * @param Doku_Renderer $renderer The renderer 65 * @param array $data The data from the handler() function 66 * 67 * @return bool If rendering was successful. 68 */ 69 70 public function render($mode, Doku_Renderer $renderer, $data) 71 { 72 if (!$data) { 73 return false; 74 } 75 76 $method = 'render'.ucfirst($mode); 77 if (method_exists($this, $method)) { 78 call_user_func([$this, $method], $renderer, $data); 79 return true; 80 } 81 return false; 82 } 83 84 /** 85 * @param $pattern 86 * @return array 87 */ 88 protected function getNotificationPlugins($pattern) 89 { 90 $plugins = []; 91 trigger_event('PLUGIN_NOTIFICATION_REGISTER_SOURCE', $plugins); 92 $plugins = preg_grep('/' . $pattern . '/', $plugins); 93 94 return $plugins; 95 } 96 97 /** 98 * Render metadata 99 * 100 * @param Doku_Renderer $renderer The renderer 101 * @param array $data The data from the handler() function 102 */ 103 public function renderMetadata(Doku_Renderer $renderer, $data) 104 { 105 $plugin_name = $this->getPluginName(); 106 107 $plugins = $this->getNotificationPlugins($data['plugin']); 108 $old_plugins = $renderer->meta['plugin'][$plugin_name]['plugins']; 109 if (!$old_plugins) { 110 $old_plugins = []; 111 } 112 113 $renderer->meta['plugin'][$plugin_name]['plugins'] = array_unique(array_merge($plugins, $old_plugins)); 114 if ($data['user'] == '$USER$') { 115 $renderer->meta['plugin'][$plugin_name]['dynamic user'] = true; 116 } 117 } 118 119 /** 120 * Render xhtml 121 * 122 * @param Doku_Renderer $renderer The renderer 123 * @param array $data The data from the handler() function 124 */ 125 public function renderXhtml(Doku_Renderer $renderer, $data) 126 { 127 global $INFO; 128 129 $plugins = $this->getNotificationPlugins($data['plugin']); 130 131 if ($data['user'] == '$USER$') { 132 $data['user'] = $INFO['client']; 133 } 134 135 $notifications_data = [ 136 'plugins' => $plugins, 137 'user' => $data['user'], 138 'notifications' => [] 139 ]; 140 trigger_event('PLUGIN_NOTIFICATION_GATHER', $notifications_data); 141 142 $notifications = $notifications_data['notifications']; 143 144 if (!$notifications) { 145 $renderer->doc .= $this->getLang('no notifications'); 146 return; 147 } 148 149 $renderer->doc .= '<ul>'; 150 151 usort($notifications, function($a, $b) { 152 if ($a['timestamp'] == $b['timestamp']) { 153 return 0; 154 } 155 return ($a['timestamp'] > $b['timestamp']) ? -1 : 1; 156 }); 157 158 foreach ($notifications as $notification) { 159 $content = $notification[$data['full'] ? 'full' : 'brief']; 160 $timestamp = $notification['timestamp']; 161 162 $date = ''; 163 if ($data['date']) { 164 $date = strftime($data['date'], $timestamp); 165 } 166 167// $li .= p_render($mode, p_get_instructions($content), $info); 168 $renderer->doc .= "<li class=\"level1\"><div class=\"li\">$date $content</div></li>"; 169 } 170 $renderer->doc .= '</ul>'; 171 } 172} 173 174