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 'full' => true, 40 'date' => 'Y-m-d' 41 ]; 42 foreach ($lines as $line) { 43 $pair = explode(':', $line, 2); 44 if (count($pair) < 2) { 45 continue; 46 } 47 $key = trim($pair[0]); 48 $value = trim($pair[1]); 49 50 if ($key == 'full') { 51 $value = $value == '0' ? false : true; 52 } 53 54 $params[$key] = $value; 55 } 56 return $params; 57 } 58 59 /** 60 * Render xhtml output or metadata 61 * 62 * @param string $mode Renderer mode (supported modes: xhtml) 63 * @param Doku_Renderer $renderer The renderer 64 * @param array $data The data from the handler() function 65 * 66 * @return bool If rendering was successful. 67 */ 68 69 public function render($mode, Doku_Renderer $renderer, $data) 70 { 71 if (!$data) { 72 return false; 73 } 74 75 $method = 'render'.ucfirst($mode); 76 if (method_exists($this, $method)) { 77 call_user_func([$this, $method], $renderer, $data); 78 return true; 79 } 80 return false; 81 } 82 83 /** 84 * @param $pattern 85 * @return array 86 */ 87 protected function getNotificationPlugins($pattern) 88 { 89 $plugins = []; 90 trigger_event('PLUGIN_NOTIFICATION_REGISTER_SOURCE', $plugins); 91 $plugins = preg_grep('/' . $pattern . '/', $plugins); 92 93 return $plugins; 94 } 95 96 /** 97 * Render metadata 98 * 99 * @param Doku_Renderer $renderer The renderer 100 * @param array $data The data from the handler() function 101 */ 102 public function renderMetadata(Doku_Renderer $renderer, $data) 103 { 104 $plugin_name = $this->getPluginName(); 105 106 $plugins = $this->getNotificationPlugins($data['plugin']); 107 $old_plugins = $renderer->meta['plugin'][$plugin_name]; 108 if (!$old_plugins) { 109 $old_plugins = []; 110 } 111 112 $renderer->meta['plugin'][$plugin_name] = array_unique(array_merge($plugins, $old_plugins)); 113 } 114 115 /** 116 * Render xhtml 117 * 118 * @param Doku_Renderer $renderer The renderer 119 * @param array $data The data from the handler() function 120 */ 121 public function renderXhtml(Doku_Renderer $renderer, $data) 122 { 123 global $INFO; 124 125 $plugins = $this->getNotificationPlugins($data['plugin']); 126 127 $notifications_data = [ 128 'plugins' => $plugins, 129 'user' => $INFO['client'], 130 'notifications' => [] 131 ]; 132 trigger_event('PLUGIN_NOTIFICATION_GATHER', $notifications_data); 133 134 $notifications = $notifications_data['notifications']; 135 136 if (!$notifications) { 137 $renderer->doc .= $this->getLang('no notifications'); 138 return; 139 } 140 141 $renderer->doc .= '<ul>'; 142 143 usort($notifications, function($a, $b) { 144 if ($a['timestamp'] == $b['timestamp']) { 145 return 0; 146 } 147 return ($a['timestamp'] > $b['timestamp']) ? -1 : 1; 148 }); 149 150 foreach ($notifications as $notification) { 151 $content = $notification[$data['full'] ? 'full' : 'brief']; 152 $timestamp = $notification['timestamp']; 153 154 $date = ''; 155 if ($data['date']) { 156 $date = strftime($data['date'], $timestamp); 157 } 158 159// $li .= p_render($mode, p_get_instructions($content), $info); 160 $renderer->doc .= "<li class=\"level1\"><div class=\"li\">$date $content</div></li>"; 161 } 162 $renderer->doc .= '</ul>'; 163 } 164} 165 166