]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQ.pm
Notifications in admin interface
[pciids.git] / PciIds / DBQ.pm
1 package PciIds::DBQ;
2 use strict;
3 use warnings;
4 use base 'PciIds::DBQAny';
5
6 sub new( $ ) {
7         my( $dbh ) = @_;
8         my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
9         my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
10         return bless PciIds::DBQAny::new( $dbh, {
11                 'nodes-id' => $node.'id',
12                 'nodes-name' => $node.'name',
13                 'nodes-rid' => $node.'id DESC',
14                 'nodes-rname' => $node.'name DESC',
15                 'nodes-id-r' => $noder.'id',
16                 'nodes-name-r' => $noder.'name',
17                 'nodes-rid-r' => $noder.'id DESC',
18                 'nodes-rname-r' => $noder.'name DESC',
19                 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
20                 'login' => 'SELECT id FROM users WHERE login = ?',
21                 'email' => 'SELECT id FROM users WHERE email = ?',
22                 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
23                 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
24                 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
25                 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
26                 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
27                 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
28                 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
29                 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
30                 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
31                 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
32                 'history' => 'SELECT history.id, history.discussion, history.time, history.nodename, history.nodenote, history.seen, users.login, users.email FROM history LEFT OUTER JOIN users ON history.owner = users.id WHERE history.location = ? ORDER BY history.time',
33                 'admindump' => 'SELECT
34                         locations.id, locations.name, locations.note, locations.mainhistory, musers.email, main.discussion,
35                         history.id, history.discussion, history.nodename, history.nodenote, users.email
36                 FROM
37                         locations INNER JOIN history ON history.location = locations.id
38                         LEFT OUTER JOIN users ON history.owner = users.id
39                         LEFT OUTER JOIN history AS main ON locations.mainhistory = main.id
40                         LEFT OUTER JOIN users AS musers ON main.owner = musers.id
41                 WHERE history.seen = "0" AND locations.id LIKE ?
42                 ORDER BY locations.id, history.id
43                 LIMIT 100',#Dumps new discussion submits with their senders and corresponding main history and names
44                 'delete-hist' => 'DELETE FROM history WHERE id = ?',
45                 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
46                 'delete-item' => 'DELETE FROM locations WHERE id = ?',
47                 'set-mainhist' => 'UPDATE locations SET
48                                 mainhistory = ?,
49                                 name = ( SELECT nodename FROM history WHERE id = ? ),
50                                 note = ( SELECT nodenote FROM history WHERE id = ? )
51                         WHERE
52                                 id = ?',
53                 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
54                 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
55                 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
56                 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
57                 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
58                 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
59                 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
60                 'notify' => 'INSERT INTO pending (user, history, notification, reason) SELECT DISTINCT user, ?, ?, ? FROM notifications WHERE ( notification = 2 OR notification = ? ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
61                 'newtime-mail' => 'UPDATE users SET nextmail = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * mailgather ) WHERE nextmail < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 0 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
62                 'newtime-xmpp' => 'UPDATE users SET nextxmpp = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * xmppgather ) WHERE nextxmpp < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 1 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
63                 'mailout' => 'SELECT
64                                 pending.user, users.email,
65                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
66                                 auth.login, history.location, locations.name, locations.note
67                         FROM
68                                 pending
69                                 INNER JOIN users ON users.id = pending.user
70                                 INNER JOIN history ON history.id = pending.history
71                                 INNER JOIN locations ON history.location = locations.id
72                                 INNER JOIN users AS auth ON auth.id = history.owner
73                         WHERE
74                                 pending.notification = 0
75                                 AND users.nextmail <= ?
76                         ORDER BY
77                                 pending.user, pending.reason, history.time, history.location',
78                 'xmppout' => 'SELECT
79                                 pending.user, users.xmpp,
80                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
81                                 auth.login, history.location, locations.name, locations.note
82                         FROM
83                                 pending
84                                 INNER JOIN users ON users.id = pending.user
85                                 INNER JOIN history ON history.id = pending.history
86                                 INNER JOIN locations ON history.location = locations.id
87                                 INNER JOIN users AS auth ON auth.id = history.owner
88                         WHERE
89                                 pending.notification = 1
90                                 AND users.nextxmpp <= ?
91                         ORDER BY
92                                 pending.user, pending.reason, history.time, history.location',
93                 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
94                 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
95                 'time' => 'SELECT NOW()',
96                 'searchname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? ORDER BY l.id',
97                 'searchlocalname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? AND l.id LIKE ? ORDER BY l.id',
98                 'hasChildren' => 'SELECT DISTINCT 1 FROM locations WHERE parent = ?',
99                 'hasMain' => 'SELECT DISTINCT 1 FROM locations WHERE id = ? AND mainhistory IS NOT NULL'
100         } );
101 }
102
103 sub hasChildren( $$ ) {
104         my( $self, $parent ) = @_;
105         return scalar @{$self->query( 'hasChildren', [ $parent ] )};
106 }
107
108 sub hasMain( $$ ) {
109         my( $self, $id ) = @_;
110         return scalar @{$self->query( 'hasMain', [ $id ] )};
111 }
112
113 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
114
115 sub nodes( $$$$ ) {
116         my( $self, $parent, $args, $restrict ) = @_;
117         my $q = 'id';
118         $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
119         if( defined( $restrict ) && ( $restrict ne "" ) ) {
120                 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
121         } else {
122                 return $self->query( 'nodes-'.$q, [ $parent ] );
123         }
124 }
125
126 sub item( $$ ) {
127         my( $self, $id ) = @_;
128         my $result = $self->query( "item", [ $id ] );
129         if( scalar @{$result} ) {
130                 return $result->[ 0 ];
131         } else {
132                 return undef;
133         }
134 }
135
136 sub hasLogin( $$ ) {
137         my( $self, $login ) = @_;
138         my $result = $self->query( 'login', [ $login ] );
139         return scalar @{$result};
140 }
141
142 sub hasEmail( $$ ) {
143         my( $self, $email ) = @_;
144         my $result = $self->query( 'email', [ $email ] );
145         return scalar @{$result};
146 }
147
148 sub addUser( $$$$ ) {
149         my( $self, $login, $email, $passwd ) = @_;
150         eval {
151                 if( ( defined $login ) && ( $login ne '' ) ) {
152                         $self->command( 'adduser', [ $login, $email, $passwd ] );
153                 } else {
154                         $self->command( 'adduser-null', [ $email, $passwd ] );
155                 }
156         };
157         if( $@ ) {
158                 return 0;
159         } else {
160                 return $self->last();
161         }
162 }
163
164 sub getLogInfo( $$ ) {
165         my( $self, $info ) = @_;
166         my $data;
167         if( $info =~ /@/ ) {#An email address
168                 $data = $self->query( 'loginfomail', [ $info ] );
169         } else {
170                 $data = $self->query( 'loginfoname', [ $info ] );
171         }
172         if( scalar @{$data} ) {
173                 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
174                 my $logstring;
175                 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
176                 $email = $info if( $info =~ /@/ );
177                 return( $id, $passwd, $email, $logstring );
178         } else {
179                 return undef;
180         }
181 }
182
183 sub rights( $$ ) {
184         my( $self, $id ) = @_;
185         return $self->query( 'rights', [ $id ] );
186 }
187
188 sub setLastLog( $$$ ) {
189         my( $self, $id, $from ) = @_;
190         $self->command( 'setlastlog', [ $from, $id ] );
191 }
192
193 sub history( $$ ) {
194         my( $self, $addr ) = @_;
195         return $self->query( 'history', [ $addr ] );
196 }
197
198 sub submitItem( $$$ ) {
199         my( $self, $data, $auth ) = @_;
200         my( $addr ) = ( $data->{'address'} );
201         foreach( @{$addr->addressDeps()} ) {
202                 my( $dep, $error ) = @{$_};
203                 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
204         }
205         return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
206         eval {
207                 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
208                 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
209         };
210         if( $@ ) {
211                 $self->rollback();
212                 return( 'internal: '.$@, undef );
213         }
214         return( '', $self->last() );
215 }
216
217 sub submitHistory( $$$$ ) {
218         my( $self, $data, $auth, $address ) = @_;
219         if( $data->{'delete'} ) {
220                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
221         } else {
222                 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
223                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
224         }
225         return $self->last();
226 }
227
228 sub adminDump( $$ ) {
229         my( $self, $prefix ) = @_;
230         return $self->query( 'admindump', [ "$prefix%" ] );
231 }
232
233 sub deleteHistory( $$ ) {
234         my( $self, $id ) = @_;
235         $self->command( 'delete-hist', [ $id ] );
236 }
237
238 sub markChecked( $$ ) {
239         my( $self, $id ) = @_;
240         $self->command( 'mark-checked', [ $id ] );
241 }
242
243 sub deleteItem( $$ ) {
244         my( $self, $id ) = @_;
245         $self->command( 'delete-item', [ $id ] );
246 }
247
248 sub setMainHistory( $$$ ) {
249         my( $self, $location, $history ) = @_;
250         $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
251 }
252
253 sub resetInfo( $$ ) {
254         my( $self, $mail ) = @_;
255         my $result = $self->query( 'resetinfo', [ $mail ] );
256         if( scalar @{$result} ) {
257                 return ( @{$result->[0]} );
258         } else {
259                 return undef;
260         }
261 }
262
263 sub changePasswd( $$$ ) {
264         my( $self, $id, $passwd ) = @_;
265         $self->command( 'changepasswd', [ $passwd, $id ] );
266 }
267
268 sub profileData( $$ ) {
269         my( $self, $id ) = @_;
270         my %result;
271         ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
272         return \%result;
273 }
274
275 sub setEmail( $$$$ ) {
276         my( $self, $id, $email, $passwd ) = @_;
277         $self->command( 'setemail', [ $email, $passwd, $id ] );
278 }
279
280 sub pushProfile( $$$$$$ ) {
281         my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
282         $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
283 }
284
285 sub notificationsUser( $$ ) {
286         my( $self, $uid ) = @_;
287         return $self->query( 'notifuser', [ $uid ] );
288 }
289
290 sub getNotifData( $$$ ) {
291         my( $self, $uid, $location ) = @_;
292         my $result = $self->query( 'notifdata', [ $uid, $location ] );
293         if( @{$result} ) {
294                 my( $recursive, $notification, $way ) = @{$result->[0]};
295                 return {
296                         'recursive' => $recursive,
297                         'notification' => $notification,
298                         'way' => $way };
299         } else {
300                 return { 'recursive' => 1 };
301         }
302 }
303
304 sub submitNotification( $$$$ ) {
305         my( $self, $uid, $location, $data ) = @_;
306         $self->command( 'drop-notif', [ $uid, $location ] );
307         $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
308 }
309
310 sub pushNotifications( $$$$$ ) {
311         my( $self, $location, $history, $priority, $reason ) = @_;
312         $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
313         $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
314         $self->command( 'newtime-mail', [ $priority, $location, $location ] );
315         $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
316 }
317
318 sub mailNotifs( $$ ) {
319         my( $self, $time ) = @_;
320         return $self->query( 'mailout', [ $time ] );
321 }
322
323 sub xmppNotifs( $$ ) {
324         my( $self, $time ) = @_;
325         return $self->query( 'xmppout', [ $time ] );
326 }
327
328 sub time( $ ) {
329         my( $self ) = @_;
330         return $self->query( 'time', [] )->[0]->[0];
331 }
332
333 sub dropNotifs( $$ ) {
334         my( $self, $time ) = @_;
335         $self->command( 'dropnotifsmail', [ $time ] );
336         $self->command( 'dropnotifsxmpp', [ $time ] );
337 }
338
339 sub searchName( $$$ ) {
340         my( $self, $search, $prefix ) = @_;
341         return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
342         return $self->query( 'searchname', [ "%$search%" ] );
343 }
344
345 1;