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 input:not([type=submit]) {
40 font-family: monospace;
46 padding: .3rem .2rem; /* TODO: input */
65 background: rgba(0, 0, 0, .8);
66 border: 1px solid #CCC;
74 align-items: baseline;
75 margin: 0 -1ex; /* inner gap */
95 display: inline-block;
99 <script src="/writer.js"></script>
103 use List::Util qw( pairs pairkeys );
107 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
108 ) or die "database not configured\n";
109 require DBIx::Simple;
110 DBIx::Simple->new(@dbinfo[0..2], {
114 } or Abort('Database error', 501, $@);
115 $db->abstract->{array_datatypes}++;
118 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
119 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
120 eo => ['<span style="color:green">★</span>', 'esperanto'],
121 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
123 my @wordcols = pairkeys
125 lang => {-label => 'Language', -select => {
126 map { $_ => "@{$lang{$_}}" } keys %lang
128 cat => [{-label => 'Category'}, 'ref'],
129 ref => {-label => 'Reference'},
131 {-label => 'Level', -select => [qw(
132 essential basic common distinctive rare invisible
136 cover => {-label => 'Highlighted', type => 'checkbox'},
137 grade => {-label => 'Order', type => 'number'},
138 form => {-label => 'Title'},
139 alt => {-label => 'Synonyms', -multiple => 1},
140 wptitle => {-label => 'Wikipedia'},
141 source => {-label => 'Image'},
142 thumb => {-label => 'Convert options', -multiple => 1},
144 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
148 $row = $db->select(word => '*', $find)->hash
149 or Abort("Word not found", 404);
152 if (exists $get{copy}) {
153 $row = {%{$row}{ qw(prio lang cat) }};
155 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
157 return if not length $_[0];
159 return Encode::decode_utf8($_[0]);
164 ref $wordcol{$_} eq 'HASH' && $wordcol{$_}->{-multiple} ?
165 [ map { parseinput($_) } $post{'@'.$_}->@* ] :
166 scalar parseinput($post{$_})
170 if ($row->{ref} ne 'delete') {
172 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
176 $db->delete(word => $find);
177 Alert("Entry removed");
183 my %res = (returning => '*');
184 my $query = $find ? $db->update(word => $row, $find, \%res) :
185 $db->insert(word => $row, \%res);
188 Alert("Entry could not be saved", $@);
193 while (my ($lang, $val) = each %post) {
195 $lang =~ s/^trans-// or next;
196 $db->insert(word => {
200 }) for parseinput($val);
201 delete $fields{$field};
204 } or Alert('Error creating translation entries', $@);
206 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
208 ($row->{source} // '') ne ($replace->{source} // '') or return;
209 # copy changed remote url to local file
210 unlink $imgpath if -e $imgpath;
211 my $download = $row->{source} or return 1;
212 require LWP::UserAgent;
213 my $ua = LWP::UserAgent->new;
215 my $status = $ua->mirror($download, $imgpath);
217 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
219 !$@ or Alert(["Source image not found", $@]);
221 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
222 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
223 $reimage++ if $fields{rethumb}; # force refresh
225 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
228 my $xyres = $row->{cover} ? '600x400' : '300x200';
229 my @cmds = @{ $row->{thumb} // [] };
230 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
231 # replace option by permillage crop
232 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
233 splice @cmds, $cmdarg, 2, (
234 -set => 'option:distort:viewport' => sprintf(
235 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
236 ($dim[2] || 1) - $dim[0], # width = x2 - x1
237 ($dim[3] || 1) - $dim[1], # height = y2 - y1
238 @dim[0, 1] # offset = x1,y1
240 -distort => SRT => 0, # noop transform to apply viewport
245 -delete => '1--1', -background => 'white',
246 -gravity => @cmds ? 'northwest' : 'center',
248 -resize => "$xyres^", -extent => $xyres,
249 '-strip', -quality => '60%', -interlace => 'plane',
250 $imgpath => $thumbpath
255 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
257 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
259 "Thumbnail image not generated",
260 "Failed to convert source image.",
270 $row->{$_} = $get{$_} for keys %get;
273 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
275 package Shiar_Sheet::FormRow {
276 use PLP::Functions 'EscapeHTML';
279 my ($row, $col, $attr) = @_;
280 my $val = $row->{$col} // '';
282 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
284 if (my $options = $attr->{-select}) {
285 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
286 if ref $options eq 'ARRAY';
287 $options->{$val} //= "unknown ($val)"; # preserve current
289 sprintf('<select id="%s" name="%1$s">', $col),
290 (map { sprintf('<option value="%s"%s>%s</option>',
291 $_, $val eq $_ && ' selected', $options->{$_}
292 ) } sort keys %{$options}),
296 elsif ($attr->{type} eq 'checkbox') {
297 $html .= ' checked' if $val;
301 '<input name="%1$s" value="0" type="hidden" />',
302 '<input id="%s" name="%1$s" value="1"%s>',
304 ), $col, $html, $attr->{-label}
310 sprintf('<label for="%s">%s</label>', $col, $_)
311 } $attr->{-label} // ()),
312 ref $val ne 'ARRAY' ? () : '<span class="inline multiinput">',
314 sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
315 } ref $val eq 'ARRAY' ? @{$val} : ()),
316 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
317 $col, ref $val eq 'ARRAY' ? '' : EscapeHTML($val), $html
319 ref $val ne 'ARRAY' ? () : '</span>',
321 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
322 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
323 } grep { -e } $row->imagepath($col)),
329 my ($row, $col) = @_;
330 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
331 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
335 bless $row, 'Shiar_Sheet::FormRow';
337 <h1>Words <:= $title :></h1>
341 <form action="?" method="post">
342 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
345 for my $col (@wordcols) {
346 my $info = $wordcol{$col} or next;
347 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
348 my $title = ref $attr ? delete $attr->{-label} : $attr;
349 printf '<li><label for="%s">%s</label><p>', $col, $title;
350 printf '<span class=inline>';
351 print $row->input($col => $attr);
352 print $row->input($_ => delete $wordcol{$_}) for @span;
357 if ($row->{id} and not $row->{ref}) {
358 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
359 'trans', 'Translations';
360 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
361 while (my ($lang, $val) = each %fields) {
362 $lang =~ s/^trans-// or next;
363 push @children, { lang => $lang, form => $val };
365 for my $ref (@children) {
367 '<li><label for="%s" title="%3$s">%s </label>',
368 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
371 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
372 '<input id="%s" name="%1$s" value="%3$s" />',
373 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
376 say '</ul></div></li>';
381 <input type="submit" value="Save" />
382 <input type="submit" value="New" formaction="/writer?copy=cat" />
394 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
395 while (my $ref = $parents->hash) {
396 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
398 say "<li><strong>$row->{form}</strong></li>";
399 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
400 while (my $ref = $children->hash) {
401 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
404 <li><form action="/writer">
405 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
406 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
407 <input type="submit" value="Add" />