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 $row = {%post{keys %wordcol}};
158 $row->{$_} = [grep {length} @{ $post{"\@$_"} }] for grep {
159 ref $wordcol{$_} eq 'HASH' and $wordcol{$_}->{-multiple}
161 $_ = length ? $_ : undef for values %{$row};
164 if ($row->{ref} ne 'delete') {
166 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
170 $db->delete(word => $find);
171 Alert("Entry removed");
177 my %res = (returning => '*');
178 my $query = $find ? $db->update(word => $row, $find, \%res) :
179 $db->insert(word => $row, \%res);
182 Alert("Entry could not be saved", $@);
187 while (my ($lang, $val) = each %post) {
189 $lang =~ s/^trans-// or next;
190 $db->insert(word => {
195 delete $fields{$field};
198 } or Alert('Error creating translation entries', $@);
200 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
202 ($row->{source} // '') ne ($replace->{source} // '') or return;
203 # copy changed remote url to local file
204 unlink $imgpath if -e $imgpath;
205 my $download = $row->{source} or return 1;
206 require LWP::UserAgent;
207 my $ua = LWP::UserAgent->new;
209 my $status = $ua->mirror($download, $imgpath);
211 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
213 !$@ or Alert(["Source image not found", $@]);
215 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
216 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
217 $reimage++ if $fields{rethumb}; # force refresh
219 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
222 my $xyres = $row->{cover} ? '600x400' : '300x200';
223 my @cmds = @{ $row->{thumb} // [] };
224 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
225 # replace option by permillage crop
226 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
227 splice @cmds, $cmdarg, 2, (
228 -set => 'option:distort:viewport' => sprintf(
229 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
230 ($dim[2] || 1) - $dim[0], # width = x2 - x1
231 ($dim[3] || 1) - $dim[1], # height = y2 - y1
232 @dim[0, 1] # offset = x1,y1
234 -distort => SRT => 0, # noop transform to apply viewport
239 -delete => '1--1', -background => 'white',
240 -gravity => @cmds ? 'northwest' : 'center',
242 -resize => "$xyres^", -extent => $xyres,
243 '-strip', -quality => '60%', -interlace => 'plane',
244 $imgpath => $thumbpath
249 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
251 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
253 "Thumbnail image not generated",
254 "Failed to convert source image.",
264 $row->{$_} = $get{$_} for keys %get;
267 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
269 package Shiar_Sheet::FormRow {
270 use PLP::Functions 'EscapeHTML';
273 my ($row, $col, $attr) = @_;
274 my $val = $row->{$col} // '';
276 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
278 if (my $options = $attr->{-select}) {
279 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
280 if ref $options eq 'ARRAY';
281 $options->{$val} //= "unknown ($val)"; # preserve current
283 sprintf('<select id="%s" name="%1$s">', $col),
284 (map { sprintf('<option value="%s"%s>%s</option>',
285 $_, $val eq $_ && ' selected', $options->{$_}
286 ) } sort keys %{$options}),
290 elsif ($attr->{type} eq 'checkbox') {
291 $html .= ' checked' if $val;
295 '<input name="%1$s" value="0" type="hidden" />',
296 '<input id="%s" name="%1$s" value="1"%s>',
298 ), $col, $html, $attr->{-label}
304 sprintf('<label for="%s">%s</label>', $col, $_)
305 } $attr->{-label} // ()),
306 ref $val ne 'ARRAY' ? () : '<span class="inline multiinput">',
308 sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
309 } ref $val eq 'ARRAY' ? @{$val} : ()),
310 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
311 $col, ref $val eq 'ARRAY' ? '' : EscapeHTML($val), $html
313 ref $val ne 'ARRAY' ? () : '</span>',
315 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
316 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
317 } grep { -e } $row->imagepath($col)),
323 my ($row, $col) = @_;
324 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
325 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
329 bless $row, 'Shiar_Sheet::FormRow';
331 <h1>Words <:= $title :></h1>
335 <form action="?" method="post">
336 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
339 for my $col (@wordcols) {
340 my $info = $wordcol{$col} or next;
341 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
342 my $title = ref $attr ? delete $attr->{-label} : $attr;
343 printf '<li><label for="%s">%s</label><p>', $col, $title;
344 printf '<span class=inline>';
345 print $row->input($col => $attr);
346 print $row->input($_ => delete $wordcol{$_}) for @span;
351 if ($row->{id} and not $row->{ref}) {
352 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
353 'trans', 'Translations';
354 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
355 while (my ($lang, $val) = each %fields) {
356 $lang =~ s/^trans-// or next;
357 push @children, { lang => $lang, form => $val };
359 for my $ref (@children) {
361 '<li><label for="%s" title="%3$s">%s </label>',
362 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
365 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
366 '<input id="%s" name="%1$s" value="%3$s" />',
367 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
370 say '</ul></div></li>';
375 <input type="submit" value="Save" />
376 <input type="submit" value="New" formaction="/writer?copy=cat" />
388 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
389 while (my $ref = $parents->hash) {
390 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
392 say "<li><strong>$row->{form}</strong></li>";
393 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
394 while (my $ref = $children->hash) {
395 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
398 <li><form action="/writer">
399 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
400 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
401 <input type="submit" value="Add" />