4 title => 'words cheat sheet admin',
11 grid: auto-flow / min-content repeat(10, auto);
16 border-spacing: 0 2px;
25 form > ul li > label {
29 form > ul li > label + * {
35 box-sizing: border-box;
38 font-family: monospace;
41 padding: .3rem .2rem; /* TODO: input */
55 background: rgba(0, 0, 0, .8);
56 border: 1px solid #CCC;
65 margin: 0 -1ex; /* inner gap */
75 -margin-left: 1em; /* flex gap */
81 display: inline-block;
85 <script src="/writer.js"></script>
89 use List::Util qw( pairs pairkeys );
93 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
94 ) or die "database not configured\n";
96 DBIx::Simple->new(@dbinfo[0..2], {
100 } or Abort('Database error', 501, $@);
105 ref => undef, # included with cat
107 cover => undef, # included with prio
108 form => 'Translation',
110 wptitle => 'Wikipedia',
112 thumb => 'Convert options',
114 my @prioenum = qw( essential basic common distinctive rare invisible );
115 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
119 $row = $db->select(word => '*', $find)->hash
120 or Abort("Word not found", 404);
123 if (exists $get{copy}) {
124 $row = {%{$row}{ qw(prio lang cat) }};
126 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
128 $row = {%post{ pairkeys @wordcols }};
129 $_ = length ? $_ : undef for values %{$row};
132 my %res = (returning => '*');
133 my $query = $find ? $db->update(word => $row, $find, \%res) :
134 $db->insert(word => $row, \%res);
137 Alert("Entry could not be saved", $@);
141 my $imgpath = "data/word/org/$row->{id}.jpg";
143 ($row->{source} // '') ne ($replace->{source} // '') or return;
144 # copy changed remote url to local file
145 unlink $imgpath if -e $imgpath;
146 my $download = $row->{source} or return 1;
147 require LWP::UserAgent;
148 my $ua = LWP::UserAgent->new;
150 my $status = $ua->mirror($download, $imgpath);
152 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
154 !$@ or Alert(["Source image not found", $@]);
156 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
157 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
158 $reimage++ if $fields{rethumb}; # force refresh
160 my $thumbpath = "data/word/eng/$row->{form}.jpg";
163 my $xyres = $row->{cover} ? '600x400' : '300x200';
164 my @cmds = @{ $row->{thumb} // [] };
167 -delete => '1--1', -background => 'white',
168 -gravity => @cmds ? 'northwest' : 'center',
170 -resize => "$xyres^", -extent => $xyres,
171 '-strip', -quality => '60%', -interlace => 'plane',
172 $imgpath => $thumbpath
177 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
179 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
181 "Thumbnail image not generated",
182 "Failed to convert source image.",
192 $row->{$_} = $get{$_} for keys %get;
195 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
197 <h1>Words <:= $title :></h1>
201 <form action="?" method="post">
202 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
206 for my $colinfo (pairs @wordcols) {
207 my ($col, $title) = @{$colinfo};
208 defined $title or next;
209 my $val = $row->{$col} // '';
210 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
211 printf '<li><label for="%s">%s</label><p>', $col, $title;
212 printf '<span class=inline>';
213 if ($col eq 'prio') {
214 printf '<select id="%s" name="%1$s">', $col;
215 printf('<option value="%s"%s>%s</option>',
216 $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
217 ) for 0 .. $#prioenum;
222 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
223 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
226 'cover', !!$row->{cover} && ' checked', 'Highlighted'
230 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
231 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form}
232 for $col eq 'thumb' ? "data/word/eng/$row->{form}.jpg" : ();
233 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" />',
234 'ref', 'Reference', Entity($row->{ref})
238 -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
239 $col, $_, $row->{form}
240 ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
246 <input type="submit" value="Save" />
247 <input type="submit" value="New" formaction="/writer?copy=cat" />
259 my $parents = $db->select(word => '*', {id => $row->{cat}});
260 while (my $ref = $parents->hash) {
261 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
263 say "<li><strong>$row->{form}</strong></li>";
264 my $children = $db->select(word => '*', {cat => $row->{id}});
265 while (my $ref = $children->hash) {
266 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
269 <li><form action="/writer">
270 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
271 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
272 <input type="submit" value="Add" />