1<?php 2// must be run within DokuWiki 3if (!defined('DOKU_INC')) die(); 4 5if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 6require_once DOKU_PLUGIN . 'syntax.php'; 7 8/** 9 * All DokuWiki plugins to extend the parser/rendering mechanism 10 * need to inherit from this class 11 */ 12class action_plugin_approve_notification extends DokuWiki_Action_Plugin 13{ 14 public function register(Doku_Event_Handler $controller) 15 { 16 $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source'); 17 $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications'); 18 $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies'); 19 20 21 } 22 23 public function add_notifications_source(Doku_Event $event) 24 { 25 $event->data[] = 'approve'; 26 } 27 28 public function add_notification_cache_dependencies(Doku_Event $event) 29 { 30 if (!in_array('approve', $event->data['plugins'])) return; 31 32 try { 33 /** @var \helper_plugin_approve_db $db_helper */ 34 $db_helper = plugin_load('helper', 'approve_db'); 35 $sqlite = $db_helper->getDB(); 36 $event->data['dependencies'][] = $sqlite->getAdapter()->getDbFile(); 37 } catch (Exception $e) { 38 msg($e->getMessage(), -1); 39 return; 40 } 41 } 42 43 public function add_notifications(Doku_Event $event) 44 { 45 if (!in_array('approve', $event->data['plugins'])) return; 46 47 try { 48 /** @var \helper_plugin_approve_db $db_helper */ 49 $db_helper = plugin_load('helper', 'approve_db'); 50 $sqlite = $db_helper->getDB(); 51 } catch (Exception $e) { 52 msg($e->getMessage(), -1); 53 return; 54 } 55 56 $user = $event->data['user']; 57 58 $is_ready_for_approval = ''; 59 if ($this->getConf('ready_for_approval_notification')) { 60 $is_ready_for_approval = 'AND revision.ready_for_approval IS NOT NULL'; 61 } 62 $q = 'SELECT page.page, revision.rev 63 FROM page INNER JOIN revision ON page.page = revision.page 64 WHERE page.hidden = 0 AND page.approver=? 65 AND revision.current=1 AND revision.approved IS NULL ' . $is_ready_for_approval; 66 $res = $sqlite->query($q, $user); 67 68 $notifications = $sqlite->res2arr($res); 69 70 foreach ($notifications as $notification) { 71 $page = $notification['page']; 72 $rev = $notification['rev']; 73 74 $link = '<a class="wikilink1" href="' . wl($page, '', true) . '">'; 75 if (useHeading('content')) { 76 $heading = p_get_first_heading($page); 77 if (!blank($heading)) { 78 $link .= $heading; 79 } else { 80 $link .= noNSorNS($page); 81 } 82 } else { 83 $link .= noNSorNS($page); 84 } 85 $link .= '</a>'; 86 $full = sprintf($this->getLang('notification full'), $link); 87 $event->data['notifications'][] = [ 88 'plugin' => 'approve', 89 'id' => $page.':'.$rev, 90 'full' => $full, 91 'brief' => $link, 92 'timestamp' => (int)$rev 93 ]; 94 } 95 } 96} 97