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;
44 padding: .3rem .2rem; /* TODO: input */
58 background: rgba(0, 0, 0, .8);
59 border: 1px solid #CCC;
68 margin: 0 -1ex; /* inner gap */
78 -margin-left: 1em; /* flex gap */
84 display: inline-block;
88 <script src="/writer.js"></script>
92 use List::Util qw( pairs pairkeys );
96 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
97 ) or die "database not configured\n";
99 DBIx::Simple->new(@dbinfo[0..2], {
103 } or Abort('Database error', 501, $@);
108 ref => undef, # included with cat
111 cover => undef, # included with prio
112 form => 'Translation',
114 wptitle => 'Wikipedia',
116 thumb => 'Convert options',
118 my @prioenum = qw( essential basic common distinctive rare invisible );
119 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
123 $row = $db->select(word => '*', $find)->hash
124 or Abort("Word not found", 404);
127 if (exists $get{copy}) {
128 $row = {%{$row}{ qw(prio lang cat) }};
130 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
132 $row = {%post{ pairkeys @wordcols }};
133 $_ = length ? $_ : undef for values %{$row};
136 my %res = (returning => '*');
137 my $query = $find ? $db->update(word => $row, $find, \%res) :
138 $db->insert(word => $row, \%res);
141 Alert("Entry could not be saved", $@);
145 my $imgpath = "data/word/org/$row->{id}.jpg";
147 ($row->{source} // '') ne ($replace->{source} // '') or return;
148 # copy changed remote url to local file
149 unlink $imgpath if -e $imgpath;
150 my $download = $row->{source} or return 1;
151 require LWP::UserAgent;
152 my $ua = LWP::UserAgent->new;
154 my $status = $ua->mirror($download, $imgpath);
156 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
158 !$@ or Alert(["Source image not found", $@]);
160 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
161 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
162 $reimage++ if $fields{rethumb}; # force refresh
164 my $thumbpath = "data/word/eng/$row->{form}.jpg";
167 my $xyres = $row->{cover} ? '600x400' : '300x200';
168 my @cmds = @{ $row->{thumb} // [] };
171 -delete => '1--1', -background => 'white',
172 -gravity => @cmds ? 'northwest' : 'center',
174 -resize => "$xyres^", -extent => $xyres,
175 '-strip', -quality => '60%', -interlace => 'plane',
176 $imgpath => $thumbpath
181 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
183 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
185 "Thumbnail image not generated",
186 "Failed to convert source image.",
196 $row->{$_} = $get{$_} for keys %get;
199 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
201 <h1>Words <:= $title :></h1>
205 <form action="?" method="post">
206 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
210 for my $colinfo (pairs @wordcols) {
211 my ($col, $title) = @{$colinfo};
212 defined $title or next;
213 my $val = $row->{$col} // '';
214 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
215 printf '<li><label for="%s">%s</label><p>', $col, $title;
216 printf '<span class=inline>';
217 if ($col eq 'prio') {
218 printf '<select id="%s" name="%1$s">', $col;
219 printf('<option value="%s"%s>%s</option>',
220 $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
221 ) for 0 .. $#prioenum;
226 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
227 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
230 'cover', !!$row->{cover} && ' checked', 'Highlighted'
232 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" type="number" />',
233 'grade', 'Order', Entity($row->{grade})
237 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
238 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form}
239 for $col eq 'thumb' ? "data/word/eng/$row->{form}.jpg" : ();
240 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" />',
241 'ref', 'Reference', Entity($row->{ref})
245 -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
246 $col, $_, $row->{form}
247 ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
253 <input type="submit" value="Save" />
254 <input type="submit" value="New" formaction="/writer?copy=cat" />
266 my $parents = $db->select(word => '*', {id => $row->{cat}});
267 while (my $ref = $parents->hash) {
268 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
270 say "<li><strong>$row->{form}</strong></li>";
271 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
272 while (my $ref = $children->hash) {
273 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
276 <li><form action="/writer">
277 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
278 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
279 <input type="submit" value="Add" />