word edit: form stylesheet for aligned input fields
[sheet.git] / writer.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'words cheat sheet admin',
5         version => '1.0',
6         nocache => 1,
7         raw => <<'EOT',
8 <style>
9 dd > input {
10         width: 32em;
11         max-width: 100%;
12         padding: 1ex;
13         font-family: monospace;
14 }
15 dl > dt, dl > dd {
16         float: none;
17         display: inline-block;
18         box-sizing: border-box;
19         width: 50%;
20         margin: 0;
21         line-height: 4ex;
22         vertical-align: text-top;
23 }
24 </style>
25 EOT
26 });
27
28 use List::Util qw( pairs pairkeys );
29
30 my $db = eval {
31         my @dbinfo = (
32                 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
33         ) or die "database not configured\n";
34         require DBIx::Simple;
35         DBIx::Simple->new(@dbinfo[0..2], {
36                 RaiseError => 1,
37                 pg_enable_utf8 => 1,
38         });
39 } or Abort('Database error', 501, $@);
40
41 my @wordcols = (
42         form    => 'Translation',
43         ref     => 'Reference',
44         cat     => 'Category',
45         lang    => 'Language',
46         source  => 'Image URL',
47         thumb   => 'Convert options',
48         wptitle => 'Wikipedia',
49 );
50 my $find = $Request ? {id => $Request} : undef;
51
52 my $row;
53 if ($ENV{REQUEST_METHOD} eq 'POST') {
54         $row = {%post{ pairkeys @wordcols }};
55         $_ = length ? $_ : undef for values %{$row};
56         eval {
57                 my %res = (returning => $Request ? '*' : 'lang, cat');
58                 my $query = $find ? $db->update(word => $row, $find, \%res) :
59                         $db->insert(word => $row, \%res);
60                 $row = $query->hash;
61         } or Alert("Entry could not be saved", $@);
62 }
63 elsif ($find) {
64         $row = $db->select(word => '*', $find)->hash
65                 or Abort("Word not found", 404);
66 }
67
68 my $title = $find ? "entry <small>#$Request</small>" : 'new entry';
69 :>
70 <h1>Words <:= $title :></h1>
71
72 <form action="" method="post">
73 <dl>
74 <:
75
76 for my $col (pairs @wordcols) {
77         my $val = $row->{$col->key} // '';
78         $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
79         printf '<dt><label for="%s">%s</label></dt>'
80                 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
81                 $col->key, $col->value, $val;
82         say '</dd>';
83 }
84 :>
85 </dl>
86 <p><input type="submit" value="Save" /></p>
87 </form>