4 use base 'PciIds::DBQAny';
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
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
49 name = ( SELECT nodename FROM history WHERE id = ? ),
50 note = ( SELECT nodenote FROM history WHERE 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 ) ) )',
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
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
74 pending.notification = 0
75 AND users.nextmail <= ?
77 pending.user, pending.reason, history.time, history.location',
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
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
89 pending.notification = 1
90 AND users.nextxmpp <= ?
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'
103 sub hasChildren( $$ ) {
104 my( $self, $parent ) = @_;
105 return scalar @{$self->query( 'hasChildren', [ $parent ] )};
109 my( $self, $id ) = @_;
110 return scalar @{$self->query( 'hasMain', [ $id ] )};
113 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
116 my( $self, $parent, $args, $restrict ) = @_;
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.'%' ] );
122 return $self->query( 'nodes-'.$q, [ $parent ] );
127 my( $self, $id ) = @_;
128 my $result = $self->query( "item", [ $id ] );
129 if( scalar @{$result} ) {
130 return $result->[ 0 ];
137 my( $self, $login ) = @_;
138 my $result = $self->query( 'login', [ $login ] );
139 return scalar @{$result};
143 my( $self, $email ) = @_;
144 my $result = $self->query( 'email', [ $email ] );
145 return scalar @{$result};
148 sub addUser( $$$$ ) {
149 my( $self, $login, $email, $passwd ) = @_;
151 if( ( defined $login ) && ( $login ne '' ) ) {
152 $self->command( 'adduser', [ $login, $email, $passwd ] );
154 $self->command( 'adduser-null', [ $email, $passwd ] );
160 return $self->last();
164 sub getLogInfo( $$ ) {
165 my( $self, $info ) = @_;
167 if( $info =~ /@/ ) {#An email address
168 $data = $self->query( 'loginfomail', [ $info ] );
170 $data = $self->query( 'loginfoname', [ $info ] );
172 if( scalar @{$data} ) {
173 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
175 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
176 $email = $info if( $info =~ /@/ );
177 return( $id, $passwd, $email, $logstring );
184 my( $self, $id ) = @_;
185 return $self->query( 'rights', [ $id ] );
188 sub setLastLog( $$$ ) {
189 my( $self, $id, $from ) = @_;
190 $self->command( 'setlastlog', [ $from, $id ] );
194 my( $self, $addr ) = @_;
195 return $self->query( 'history', [ $addr ] );
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 );
205 return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
207 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
208 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
212 return( 'internal: '.$@, undef );
214 return( '', $self->last() );
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 );
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 );
225 return $self->last();
228 sub adminDump( $$ ) {
229 my( $self, $prefix ) = @_;
230 return $self->query( 'admindump', [ "$prefix%" ] );
233 sub deleteHistory( $$ ) {
234 my( $self, $id ) = @_;
235 $self->command( 'delete-hist', [ $id ] );
238 sub markChecked( $$ ) {
239 my( $self, $id ) = @_;
240 $self->command( 'mark-checked', [ $id ] );
243 sub deleteItem( $$ ) {
244 my( $self, $id ) = @_;
245 $self->command( 'delete-item', [ $id ] );
248 sub setMainHistory( $$$ ) {
249 my( $self, $location, $history ) = @_;
250 $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
253 sub resetInfo( $$ ) {
254 my( $self, $mail ) = @_;
255 my $result = $self->query( 'resetinfo', [ $mail ] );
256 if( scalar @{$result} ) {
257 return ( @{$result->[0]} );
263 sub changePasswd( $$$ ) {
264 my( $self, $id, $passwd ) = @_;
265 $self->command( 'changepasswd', [ $passwd, $id ] );
268 sub profileData( $$ ) {
269 my( $self, $id ) = @_;
271 ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
275 sub setEmail( $$$$ ) {
276 my( $self, $id, $email, $passwd ) = @_;
277 $self->command( 'setemail', [ $email, $passwd, $id ] );
280 sub pushProfile( $$$$$$ ) {
281 my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
282 $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
285 sub notificationsUser( $$ ) {
286 my( $self, $uid ) = @_;
287 return $self->query( 'notifuser', [ $uid ] );
290 sub getNotifData( $$$ ) {
291 my( $self, $uid, $location ) = @_;
292 my $result = $self->query( 'notifdata', [ $uid, $location ] );
294 my( $recursive, $notification, $way ) = @{$result->[0]};
296 'recursive' => $recursive,
297 'notification' => $notification,
300 return { 'recursive' => 1 };
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 );
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 ] );
318 sub mailNotifs( $$ ) {
319 my( $self, $time ) = @_;
320 return $self->query( 'mailout', [ $time ] );
323 sub xmppNotifs( $$ ) {
324 my( $self, $time ) = @_;
325 return $self->query( 'xmppout', [ $time ] );
330 return $self->query( 'time', [] )->[0]->[0];
333 sub dropNotifs( $$ ) {
334 my( $self, $time ) = @_;
335 $self->command( 'dropnotifsmail', [ $time ] );
336 $self->command( 'dropnotifsxmpp', [ $time ] );
339 sub searchName( $$$ ) {
340 my( $self, $search, $prefix ) = @_;
341 return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
342 return $self->query( 'searchname', [ "%$search%" ] );