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 */
64 background: rgba(0, 0, 0, .8);
65 border: 1px solid #CCC;
73 align-items: baseline;
74 margin: 0 -1ex; /* inner gap */
87 display: inline-block;
91 <script src="/writer.js"></script>
95 use List::Util qw( pairs pairkeys );
99 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
100 ) or die "database not configured\n";
101 require DBIx::Simple;
102 DBIx::Simple->new(@dbinfo[0..2], {
106 } or Abort('Database error', 501, $@);
107 $db->abstract->{array_datatypes}++;
110 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
111 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
112 eo => ['<span style="color:green">★</span>', 'esperanto'],
113 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
115 my @wordcols = pairkeys
117 lang => {-label => 'Language', -select => {
118 map { $_ => "@{$lang{$_}}" } keys %lang
120 cat => [{-label => 'Category'}, 'ref'],
121 ref => {-label => 'Reference'},
123 {-label => 'Level', -select => [qw(
124 essential basic common distinctive rare invisible
128 cover => {-label => 'Highlighted', type => 'checkbox'},
129 grade => {-label => 'Order', type => 'number'},
130 form => {-label => 'Title'},
131 alt => {-label => 'Synonyms', -multiple => 1},
132 wptitle => {-label => 'Wikipedia'},
133 source => {-label => 'Image'},
134 thumb => {-label => 'Convert options', -multiple => 1},
136 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
140 $row = $db->select(word => '*', $find)->hash
141 or Abort("Word not found", 404);
144 if (exists $get{copy}) {
145 $row = {%{$row}{ qw(prio lang cat) }};
147 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
149 $row = {%post{keys %wordcol}};
150 $row->{$_} = [grep {length} @{ $post{"\@$_"} }] for grep {
151 ref $wordcol{$_} eq 'HASH' and $wordcol{$_}->{-multiple}
153 $_ = length ? $_ : undef for values %{$row};
156 if ($row->{ref} ne 'delete') {
158 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
162 $db->delete(word => $find);
163 Alert("Entry removed");
169 my %res = (returning => '*');
170 my $query = $find ? $db->update(word => $row, $find, \%res) :
171 $db->insert(word => $row, \%res);
174 Alert("Entry could not be saved", $@);
179 while (my ($lang, $val) = each %post) {
181 $lang =~ s/^trans-// or next;
182 $db->insert(word => {
187 delete $fields{$field};
190 } or Alert('Error creating translation entries', $@);
192 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
194 ($row->{source} // '') ne ($replace->{source} // '') or return;
195 # copy changed remote url to local file
196 unlink $imgpath if -e $imgpath;
197 my $download = $row->{source} or return 1;
198 require LWP::UserAgent;
199 my $ua = LWP::UserAgent->new;
201 my $status = $ua->mirror($download, $imgpath);
203 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
205 !$@ or Alert(["Source image not found", $@]);
207 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
208 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
209 $reimage++ if $fields{rethumb}; # force refresh
211 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
214 my $xyres = $row->{cover} ? '600x400' : '300x200';
215 my @cmds = @{ $row->{thumb} // [] };
216 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
217 # replace option by permillage crop
218 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
219 splice @cmds, $cmdarg, 2, (
220 -set => 'option:distort:viewport' => sprintf(
221 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
222 ($dim[2] || 1) - $dim[0], # width = x2 - x1
223 ($dim[3] || 1) - $dim[1], # height = y2 - y1
224 @dim[0, 1] # offset = x1,y1
226 -distort => SRT => 0, # noop transform to apply viewport
231 -delete => '1--1', -background => 'white',
232 -gravity => @cmds ? 'northwest' : 'center',
234 -resize => "$xyres^", -extent => $xyres,
235 '-strip', -quality => '60%', -interlace => 'plane',
236 $imgpath => $thumbpath
241 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
243 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
245 "Thumbnail image not generated",
246 "Failed to convert source image.",
256 $row->{$_} = $get{$_} for keys %get;
259 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
261 package Shiar_Sheet::FormRow {
262 use PLP::Functions 'EscapeHTML';
265 my ($row, $col, $attr) = @_;
266 my $val = $row->{$col} // '';
268 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
270 if (my $options = $attr->{-select}) {
271 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
272 if ref $options eq 'ARRAY';
273 $options->{$val} //= "unknown ($val)"; # preserve current
275 sprintf('<select id="%s" name="%1$s">', $col),
276 (map { sprintf('<option value="%s"%s>%s</option>',
277 $_, $val eq $_ && ' selected', $options->{$_}
278 ) } sort keys %{$options}),
282 elsif ($attr->{type} eq 'checkbox') {
283 $html .= ' checked' if $val;
287 '<input name="%1$s" value="0" type="hidden" />',
288 '<input id="%s" name="%1$s" value="1"%s>',
290 ), $col, $html, $attr->{-label}
296 sprintf('<label for="%s">%s</label>', $col, $_)
297 } $attr->{-label} // ()),
299 sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
300 } ref $val eq 'ARRAY' ? @{$val} : ()),
301 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
302 $col, ref $val eq 'ARRAY' ? '' : EscapeHTML($val), $html
305 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
306 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
307 } grep { -e } $row->imagepath($col)),
313 my ($row, $col) = @_;
314 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
315 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
319 bless $row, 'Shiar_Sheet::FormRow';
321 <h1>Words <:= $title :></h1>
325 <form action="?" method="post">
326 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
329 for my $col (@wordcols) {
330 my $info = $wordcol{$col} or next;
331 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
332 my $title = ref $attr ? delete $attr->{-label} : $attr;
333 printf '<li><label for="%s">%s</label><p>', $col, $title;
334 printf '<span class=inline>';
335 print $row->input($col => $attr);
336 print $row->input($_ => delete $wordcol{$_}) for @span;
341 if ($row->{id} and not $row->{ref}) {
342 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
343 'trans', 'Translations';
344 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
345 while (my ($lang, $val) = each %fields) {
346 $lang =~ s/^trans-// or next;
347 push @children, { lang => $lang, form => $val };
349 for my $ref (@children) {
351 '<li><label for="%s" title="%3$s">%s </label>',
352 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
355 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
356 '<input id="%s" name="%1$s" value="%3$s" />',
357 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
360 say '</ul></div></li>';
365 <input type="submit" value="Save" />
366 <input type="submit" value="New" formaction="/writer?copy=cat" />
378 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
379 while (my $ref = $parents->hash) {
380 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
382 say "<li><strong>$row->{form}</strong></li>";
383 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
384 while (my $ref = $children->hash) {
385 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
388 <li><form action="/writer">
389 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
390 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
391 <input type="submit" value="Add" />