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) = map {{id => $_}} $fields{id} || $Request || ();
57 if (exists $get{copy}) {
58 $row = {%fields{ qw(lang cat) }};
60 elsif ($ENV{REQUEST_METHOD} eq 'POST') {
61 $row = {%post{ pairkeys @wordcols }};
62 $_ = length ? $_ : undef for values %{$row};
64 my %res = (returning => '*');
65 my $query = $find ? $db->update(word => $row, $find, \%res) :
66 $db->insert(word => $row, \%res);
68 } or Alert("Entry could not be saved", $@);
70 my $imgpath = "data/word/org/$row->{id}.jpg";
71 if (my $download = $row->{source} and !-e $imgpath) {
72 require LWP::UserAgent;
73 my $ua = LWP::UserAgent->new;
75 my $status = $ua->mirror($download, $imgpath);
76 $status->is_success or Alert([
77 "Source image not found",
78 "Download from <q>$download</q> failed: ".$status->status_line,
82 my $thumbpath = "data/word/eng/$row->{form}.jpg";
84 my @cmds = @{ $row->{thumb} // [] };
85 unshift @cmds, -gravity => @cmds ? 'northwest' : 'center';
86 unshift @cmds, 'convert';
87 push @cmds, -resize => '300x200^', -extent => '300x200';
88 push @cmds, '-strip', -quality => '60%', -interlace => 'plane';
89 push @cmds, $imgpath, $thumbpath;
90 my $status = system @cmds;
91 $status == 0 or Alert([
92 "Thumbnail image not generated",
93 "Failed to convert source image, error code ".($status >> 8),
98 $row = $db->select(word => '*', $find)->hash
99 or Abort("Word not found", 404);
102 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
104 <h1>Words <:= $title :></h1>
106 <form action="?" method="post">
107 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
111 for my $col (pairs @wordcols) {
112 my $val = $row->{$col->key} // '';
113 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
114 printf '<dt><label for="%s">%s</label></dt>'
115 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
116 $col->key, $col->value, Entity($val);
117 -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
118 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
119 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
126 <input type="submit" value="Save" />
127 <input type="submit" value="New" formaction="/writer?copy=cat" />