word edit: copycat button to save and start a similar entry
[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 dd > img {
25         max-width: 300px;
26         display: block;
27 }
28 </style>
29 EOT
30 });
31
32 use List::Util qw( pairs pairkeys );
33
34 my $db = eval {
35         my @dbinfo = (
36                 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
37         ) or die "database not configured\n";
38         require DBIx::Simple;
39         DBIx::Simple->new(@dbinfo[0..2], {
40                 RaiseError => 1,
41                 pg_enable_utf8 => 1,
42         });
43 } or Abort('Database error', 501, $@);
44
45 my @wordcols = (
46         form    => 'Translation',
47         ref     => 'Reference',
48         cat     => 'Category',
49         lang    => 'Language',
50         source  => 'Image URL',
51         thumb   => 'Convert options',
52         wptitle => 'Wikipedia',
53 );
54 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
55
56 my $row;
57 if (exists $get{copy}) {
58         $row = {%fields{ qw(lang cat) }};
59 }
60 elsif ($ENV{REQUEST_METHOD} eq 'POST') {
61         $row = {%post{ pairkeys @wordcols }};
62         $_ = length ? $_ : undef for values %{$row};
63         eval {
64                 my %res = (returning => '*');
65                 my $query = $find ? $db->update(word => $row, $find, \%res) :
66                         $db->insert(word => $row, \%res);
67                 $row = $query->hash;
68         } or Alert("Entry could not be saved", $@);
69
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;
74                 $ua->agent('/');
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,
79                 ]);
80         }
81
82         my $thumbpath = "data/word/eng/$row->{form}.jpg";
83         if (-e $imgpath) {
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),
94                 ], "@cmds");
95         }
96 }
97 elsif ($find) {
98         $row = $db->select(word => '*', $find)->hash
99                 or Abort("Word not found", 404);
100 }
101
102 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
103 :>
104 <h1>Words <:= $title :></h1>
105
106 <form action="?" method="post">
107 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
108 <dl>
109 <:
110
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" :
120                 ();
121         say '</dd>';
122 }
123 :>
124 </dl>
125 <p>
126         <input type="submit" value="Save" />
127         <input type="submit" value="New" formaction="/writer?copy=cat" />
128 </p>
129 </form>