chiark / gitweb /
wip
[nj-awaymsg.git] / mason / save
1 <%init>
2 use AwayMsg;
3 use Data::Dumper;
4 db_connect();
5 www_begin($r,$m);
6 </%init><%args>
7 $save => 0
8 $cancel => 0
9 $textid => undef
10 $desc
11 $subject
12 $text
13 </%args><%perl>
14 if (!$save) {
15     $m->redirect("main");
16     return;
17 }
18 my $ins;
19
20 $dbh->do('BEGIN');
21
22 if ($textid ne 'new') {
23     my $dtextq = $dbh->prepare("DELETE FROM texts WHERE textid = ?");
24     my $ndeleted = $dtextq->execute($textid);
25     if (!$ndeleted) {
26         $textid = 'new';
27     }
28 }
29
30 if ($textid eq 'new') {
31     $textid = time;
32     my $ntextq = $dbh->prepare("SELECT textid FROM texts".
33                                " ORDER BY textid DESC LIMIT 1");
34     $ntextq->execute();
35     my $ntextrow = $ntextq->fetchrow_hashref();
36     if ($ntextrow) {
37         my $mintextid = $ntextrow->{'textid'} + 1;
38         $textid = $mintextid if $textid < $mintextid;
39     }
40 }
41
42 print STDERR Dumper([$textid, $desc, $subject, $text]);
43
44 my $dinsertq = $dbh->prepare("INSERT INTO texts (textid, desc, subject, text)".
45                              " VALUES (?,?,?,?)");
46 $dinsertq->execute($textid, $desc, $subject, $text);
47
48 my $tdeleteq = $dbh->prepare("DELETE FROM addresses WHERE textid = ?");
49 $tdeleteq->execute($textid);
50
51 my $tinsertq = $dbh->prepare("INSERT INTO addresses (emailaddr, textid)".
52                              " VALUES (?,?)");
53 my $tlistq = $dbh->prepare("SELECT * FROM config");
54 $tlistq->execute();
55 while (my $row = $tlistq->fetchrow_hashref()) {
56     if ($ARGS{"u_".hquote($row->{'emailaddr'})}) {
57         $tinsertq->execute($row->{'emailaddr'}, $textid);
58     }
59 }
60
61 db_commit();
62
63 </%perl><& htmlhead, subpage => "Saved \"$desc\"" &>
64 Message and configuration saved.
65 <p>
66 <form action="main" method="get">
67 <input type="submit" value="OK">
68 </form>