xref: /plugin/structnotification/action/notification.php (revision ac63f65f0ad9ef7501cf66526694ca3162fc808c)
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 = $this->replacePlaceholders($message, $values);
115                        $message_html = p_render('xhtml',p_get_instructions($message), $info);
116                        $event->data['notifications'][] = [
117                            'plugin' => 'structnotification',
118                            'id' => $predicate['id'] . ':'. $schema . ':' . $pid . ':'  . $rawDate,
119                            'full' => $message_html,
120                            'brief' => $message_html,
121                            'timestamp' => (int) strtotime($rawDate)
122                        ];
123                    }
124                }
125            } catch (Exception $e) {
126                msg($e->getMessage(), -1);
127                return;
128            }
129        }
130    }
131
132    /**
133     * @param array $users
134     * @param array $groups
135     * @return array
136     */
137    protected function users_set($user_and_groups) {
138        /** @var DokuWiki_Auth_Plugin $auth */
139        global $auth;
140
141        $user_and_groups_set = array_map('trim', explode(',', $user_and_groups));
142        $users = [];
143        $groups = [];
144        foreach ($user_and_groups_set as $user_or_group) {
145            if ($user_or_group[0] == '@') {
146                $groups[] = substr($user_or_group, 1);
147            } else {
148                $users[] = $user_or_group;
149            }
150        }
151        $set = [];
152
153        $all_users = $auth->retrieveUsers();
154        foreach ($all_users as $user => $info) {
155            if (in_array($user, $users)) {
156                $set[$user] = $info;
157            } elseif (array_intersect($groups, $info['grps'])) {
158                $set[$user] = $info;
159            }
160        }
161
162        return $set;
163    }
164
165    protected function predicateTrue($date, $operator, $days) {
166        $date = date('Y-m-d', strtotime($date));
167
168        switch ($operator) {
169            case 'before':
170                $days = date('Y-m-d', strtotime("+$days days"));
171                return $days >= $date;
172            case 'after':
173                $days = date('Y-m-d', strtotime("-$days days"));
174                return $date <= $days;
175            default:
176                return false;
177        }
178    }
179
180    protected function replacePlaceholders($message, $values) {
181        $patterns = [];
182        $replacements = [];
183        /* @var Value $value */
184        foreach ($values as $value) {
185            $schema = $value->getColumn()->getTable();
186            $label = $value->getColumn()->getLabel();
187            $patterns[] = "/@@$schema.$label@@/";
188            $replacements[] = $value->getDisplayValue();
189        }
190
191        return preg_replace($patterns, $replacements, $message);
192    }
193
194}
195
196