word edit: fix image convert arrays
[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 });
8
9 use List::Util qw( pairs pairkeys );
10
11 my $db = eval {
12         my @dbinfo = (
13                 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
14         ) or die "database not configured\n";
15         require DBIx::Simple;
16         DBIx::Simple->new(@dbinfo[0..2], {
17                 RaiseError => 1,
18                 pg_enable_utf8 => 1,
19         });
20 } or Abort('Database error', 501, $@);
21
22 my @wordcols = (
23         form    => 'Translation',
24         ref     => 'Reference',
25         cat     => 'Category',
26         lang    => 'Language',
27         source  => 'Image URL',
28         thumb   => 'Convert options',
29         wptitle => 'Wikipedia',
30 );
31 my $find = $Request ? {id => $Request} : undef;
32
33 my $row;
34 if ($ENV{REQUEST_METHOD} eq 'POST') {
35         $row = {%post{ pairkeys @wordcols }};
36         $_ = length ? $_ : undef for values %{$row};
37         eval {
38                 my %res = (returning => $Request ? '*' : 'lang, cat');
39                 my $query = $find ? $db->update(word => $row, $find, \%res) :
40                         $db->insert(word => $row, \%res);
41                 $row = $query->hash;
42         } or Alert("Entry could not be saved", $@);
43 }
44 elsif ($find) {
45         $row = $db->select(word => '*', $find)->hash
46                 or Abort("Word not found", 404);
47 }
48
49 my $title = $find ? "entry <small>#$Request</small>" : 'new entry';
50 :>
51 <h1>Words <:= $title :></h1>
52
53 <form action="" method="post">
54 <dl>
55 <:
56
57 for my $col (pairs @wordcols) {
58         my $val = $row->{$col->key} // '';
59         $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
60         printf '<dt><label for="%s">%s</label></dt>'
61                 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
62                 $col->key, $col->value, $val;
63         say '</dd>';
64 }
65 :>
66 </dl>
67 <p><input type="submit" value="Save" /></p>
68 </form>