1<?php 2/** 3 * DokuWiki Plugin structnotification (Action 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 10use dokuwiki\plugin\struct\meta\Search; 11use dokuwiki\plugin\struct\meta\Value; 12 13if (!defined('DOKU_INC')) { 14 die(); 15} 16 17class action_plugin_structnotification_notification extends DokuWiki_Action_Plugin 18{ 19 20 /** 21 * Registers a callback function for a given event 22 * 23 * @param Doku_Event_Handler $controller DokuWiki's event controller object 24 * 25 * @return void 26 */ 27 public function register(Doku_Event_Handler $controller) 28 { 29 $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source'); 30 $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications'); 31 $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies'); 32 33 34 } 35 36 public function add_notifications_source(Doku_Event $event) 37 { 38 $event->data[] = 'structnotification'; 39 } 40 41 public function add_notification_cache_dependencies(Doku_Event $event) 42 { 43 if (!in_array('structnotification', $event->data['plugins'])) return; 44 45 try { 46 /** @var \helper_plugin_structnotification_db $db_helper */ 47 $db_helper = plugin_load('helper', 'structnotification_db'); 48 $sqlite = $db_helper->getDB(); 49 $event->data['dependencies'][] = $sqlite->getAdapter()->getDbFile(); 50 } catch (Exception $e) { 51 msg($e->getMessage(), -1); 52 return; 53 } 54 } 55 56 protected function getValueByLabel($values, $label) 57 { 58 /* @var Value $value */ 59 foreach ($values as $value) { 60 $colLabel = $value->getColumn()->getLabel(); 61 if ($colLabel == $label) { 62 return $value->getRawValue(); 63 } 64 } 65 //nothing found 66 throw new Exception("column: $label not found in values"); 67 } 68 69 70 public function add_notifications(Doku_Event $event) 71 { 72 if (!in_array('structnotification', $event->data['plugins'])) return; 73 74 try { 75 /** @var \helper_plugin_structnotification_db$db_helper */ 76 $db_helper = plugin_load('helper', 'structnotification_db'); 77 $sqlite = $db_helper->getDB(); 78 } catch (Exception $e) { 79 msg($e->getMessage(), -1); 80 return; 81 } 82 83 $user = $event->data['user']; 84 85 $q = 'SELECT * FROM predicate'; 86 $res = $sqlite->query($q); 87 88 $predicates = $sqlite->res2arr($res); 89 90 foreach ($predicates as $predicate) { 91 $schema = $predicate['schema']; 92 $field = $predicate['field']; 93 $operator = $predicate['operator']; 94 $days = $predicate['days']; 95 $users_and_groups = $predicate['users_and_groups']; 96 $message = $predicate['message']; 97 98 $users_set = $this->users_set($users_and_groups); 99 if (!isset($users_set[$user])) continue; 100 101 try { 102 $search = new Search(); 103 $search->addSchema($schema); 104 $search->addColumn('*'); 105 $result = $search->execute(); 106 $result_pids = $search->getPids(); 107 108 /* @var Value[] $row */ 109 for ($i=0; $i<count($result); $i++) { 110 $values = $result[$i]; 111 $pid = $result_pids[$i]; 112 $rawDate = $this->getValueByLabel($values, $field); 113 if ($this->predicateTrue($rawDate, $operator, $days)) { 114 $message_html = p_render('xhtml',p_get_instructions($message), $info); 115 $event->data['notifications'][] = [ 116 'plugin' => 'structnotification', 117 'id' => $predicate['id'] . ':'. $schema . ':' . $pid . ':' . $rawDate, 118 'full' => $message_html, 119 'brief' => $message_html, 120 'timestamp' => (int) strtotime($rawDate) 121 ]; 122 } 123 } 124 } catch (Exception $e) { 125 msg($e->getMessage(), -1); 126 return; 127 } 128 } 129 } 130 131 /** 132 * @param array $users 133 * @param array $groups 134 * @return array 135 */ 136 protected function users_set($user_and_groups) { 137 /** @var DokuWiki_Auth_Plugin $auth */ 138 global $auth; 139 140 $user_and_groups_set = array_map('trim', explode(',', $user_and_groups)); 141 $users = []; 142 $groups = []; 143 foreach ($user_and_groups_set as $user_or_group) { 144 if ($user_or_group[0] == '@') { 145 $groups[] = substr($user_or_group, 1); 146 } else { 147 $users[] = $user_or_group; 148 } 149 } 150 $set = []; 151 152 $all_users = $auth->retrieveUsers(); 153 foreach ($all_users as $user => $info) { 154 if (in_array($user, $users)) { 155 $set[$user] = $info; 156 } elseif (array_intersect($groups, $info['grps'])) { 157 $set[$user] = $info; 158 } 159 } 160 161 return $set; 162 } 163 164 protected function predicateTrue($date, $operator, $days) { 165 $date = date('Y-m-d', strtotime($date)); 166 167 switch ($operator) { 168 case 'before': 169 $days = date('Y-m-d', strtotime("+$days days")); 170 return $days >= $date; 171 case 'after': 172 $days = date('Y-m-d', strtotime("-$days days")); 173 return $date <= $days; 174 default: 175 return false; 176 } 177 } 178 179} 180 181