4 title => 'words cheat sheet admin',
13 font-family: monospace;
17 display: inline-block;
18 box-sizing: border-box;
22 vertical-align: text-top;
32 use List::Util qw( pairs pairkeys );
36 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
37 ) or die "database not configured\n";
39 DBIx::Simple->new(@dbinfo[0..2], {
43 } or Abort('Database error', 501, $@);
46 form => 'Translation',
50 source => 'Image URL',
51 thumb => 'Convert options',
52 wptitle => 'Wikipedia',
54 my $find = $Request ? {id => $Request} : undef;
57 if ($ENV{REQUEST_METHOD} eq 'POST') {
58 $row = {%post{ pairkeys @wordcols }};
59 $_ = length ? $_ : undef for values %{$row};
61 my %res = (returning => '*');
62 my $query = $find ? $db->update(word => $row, $find, \%res) :
63 $db->insert(word => $row, \%res);
65 } or Alert("Entry could not be saved", $@);
67 my $imgpath = "data/word/org/$row->{id}.jpg";
68 if (my $download = $row->{source} and !-e $imgpath) {
69 require LWP::UserAgent;
70 my $ua = LWP::UserAgent->new;
72 my $status = $ua->mirror($download, $imgpath);
73 $status->is_success or Alert([
74 "Source image not found",
75 "Download from <q>$download</q> failed: ".$status->status_line,
79 my $thumbpath = "data/word/eng/$row->{form}.jpg";
81 my @cmds = @{ $row->{thumb} // [] };
82 unshift @cmds, -gravity => @cmds ? 'northwest' : 'center';
83 unshift @cmds, 'convert';
84 push @cmds, -resize => '300x200^', -extent => '300x200';
85 push @cmds, '-strip', -quality => '60%', -interlace => 'plane';
86 push @cmds, $imgpath, $thumbpath;
87 my $status = system @cmds;
88 $status == 0 or Alert([
89 "Thumbnail image not generated",
90 "Failed to convert source image, error code ".($status >> 8),
94 $row = {map {$_ => $row->{$_}} qw(lang cat)} if not $Request;
97 $row = $db->select(word => '*', $find)->hash
98 or Abort("Word not found", 404);
101 my $title = $row ? "entry <small>#$row->{id}</small>" : 'new entry';
103 <h1>Words <:= $title :></h1>
105 <form action="" method="post">
109 for my $col (pairs @wordcols) {
110 my $val = $row->{$col->key} // '';
111 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
112 printf '<dt><label for="%s">%s</label></dt>'
113 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
114 $col->key, $col->value, $val;
115 -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
116 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
117 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
123 <p><input type="submit" value="Save" /></p>