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 + * {
36 box-sizing: border-box;
39 input:not([type=submit]) {
41 font-family: monospace;
47 padding: .3rem .2rem; /* TODO: input */
66 background: rgba(0, 0, 0, .8);
67 border: 1px solid #CCC;
75 align-items: baseline;
76 margin: 0 -1ex; /* inner gap */
96 display: inline-block;
100 <script src="/writer.js"></script>
104 use List::Util qw( pairs pairkeys );
108 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
109 ) or die "database not configured\n";
110 require DBIx::Simple;
111 DBIx::Simple->new(@dbinfo[0..2], {
115 } or Abort('Database error', 501, $@);
116 $db->abstract->{array_datatypes}++;
119 my $cookiedata = $cookie{login} or return;
120 my ($name, $key) = split /[:\v]/, DecodeURI($cookiedata);
121 my %rowmatch = (username => $name, pass => $key);
122 $db->select(login => '*', \%rowmatch)->hash;
123 } or Abort('Login required', 403);
126 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
127 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
128 eo => ['<span style="color:green">★</span>', 'esperanto'],
129 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
131 my @wordcols = pairkeys
133 lang => {-label => 'Language', -select => {
134 map { $_ => "@{$lang{$_}}" } keys %lang
136 cat => [{-label => 'Category'}, 'ref'],
137 ref => {-label => 'Reference'},
139 {-label => 'Level', -select => [qw(
140 essential basic common distinctive rare invisible
144 cover => {-label => 'Highlighted', type => 'checkbox'},
145 grade => {-label => 'Order', type => 'number'},
146 form => {-label => 'Title'},
147 alt => {-label => 'Synonyms', -multiple => 1},
148 wptitle => {-label => 'Wikipedia'},
149 source => {-label => 'Image'},
150 thumb => {-label => 'Convert options', -multiple => 1},
152 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
156 $row = $db->select(word => '*', $find)->hash
157 or Abort("Word not found", 404);
160 if (exists $get{copy}) {
161 $row = {%{$row}{ qw(prio lang cat) }};
163 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
165 return if not length $_[0];
167 return Encode::decode_utf8($_[0]);
172 ref $wordcol{$_} eq 'HASH' && $wordcol{$_}->{-multiple} ?
173 [ map { parseinput($_) } $post{'@'.$_}->@* ] :
174 scalar parseinput($post{$_})
178 if ($row->{ref} ne 'delete') {
180 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
184 $db->delete(word => $find);
185 Alert("Entry removed");
191 my %res = (returning => '*');
192 $row->{creator} = $user->{id} unless $find;
193 $row->{updated} = ['now()'];
194 my $query = $find ? $db->update(word => $row, $find, \%res) :
195 $db->insert(word => $row, \%res);
198 Alert("Entry could not be saved", $@);
203 while (my ($lang, $val) = each %post) {
205 $lang =~ s/^trans-// or next;
206 $db->insert(word => {
210 }) for parseinput($val);
211 delete $fields{$field};
214 } or Alert('Error creating translation entries', $@);
216 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
218 ($row->{source} // '') ne ($replace->{source} // '') or return;
219 # copy changed remote url to local file
220 unlink $imgpath if -e $imgpath;
221 my $download = $row->{source} or return 1;
222 require LWP::UserAgent;
223 my $ua = LWP::UserAgent->new;
225 my $status = $ua->mirror($download, $imgpath);
227 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
229 !$@ or Alert(["Source image not found", $@]);
231 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
232 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
233 $reimage++ if $fields{rethumb}; # force refresh
235 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
238 my $xyres = $row->{cover} ? '600x400' : '300x200';
239 my @cmds = @{ $row->{thumb} // [] };
240 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
241 # replace option by permillage crop
242 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
243 splice @cmds, $cmdarg, 2, (
244 -set => 'option:distort:viewport' => sprintf(
245 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
246 ($dim[2] || 1) - $dim[0], # width = x2 - x1
247 ($dim[3] || 1) - $dim[1], # height = y2 - y1
248 @dim[0, 1] # offset = x1,y1
250 -distort => SRT => 0, # noop transform to apply viewport
255 -delete => '1--1', -background => 'white',
256 -gravity => @cmds ? 'northwest' : 'center',
258 -resize => "$xyres^", -extent => $xyres,
259 '-strip', -quality => '60%', -interlace => 'plane',
260 $imgpath => $thumbpath
265 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
267 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
269 "Thumbnail image not generated",
270 "Failed to convert source image.",
280 $row->{$_} = $get{$_} for keys %get;
283 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
285 package Shiar_Sheet::FormRow {
286 use PLP::Functions 'EscapeHTML';
289 my ($row, $col, $attr) = @_;
290 my $val = $row->{$col} // '';
292 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
294 if (my $options = $attr->{-select}) {
295 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
296 if ref $options eq 'ARRAY';
297 $options->{$val} //= "unknown ($val)"; # preserve current
299 sprintf('<select id="%s" name="%1$s">', $col),
300 (map { sprintf('<option value="%s"%s>%s</option>',
301 $_, $val eq $_ && ' selected', $options->{$_}
302 ) } sort keys %{$options}),
306 elsif ($attr->{type} eq 'checkbox') {
307 $html .= ' checked' if $val;
311 '<input name="%1$s" value="0" type="hidden" />',
312 '<input id="%s" name="%1$s" value="1"%s>',
314 ), $col, $html, $attr->{-label}
318 my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
321 sprintf('<label for="%s">%s</label>', $col, $_)
322 } $attr->{-label} // ()),
323 $multiple ? '<span class="inline multiinput">' : (),
325 sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
326 } ref $val eq 'ARRAY' ? @{$val} : ()),
327 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
328 $col, $multiple ? '' : EscapeHTML($val), $html
330 $multiple ? '</span>' : (),
332 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
333 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
334 } grep { -e } $row->imagepath($col)),
340 my ($row, $col) = @_;
341 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
342 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
346 bless $row, 'Shiar_Sheet::FormRow';
348 <h1>Words <:= $title :></h1>
352 <form action="?" method="post">
353 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
356 for my $col (@wordcols) {
357 my $info = $wordcol{$col} or next;
358 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
359 my $title = ref $attr ? delete $attr->{-label} : $attr;
360 printf '<li><label for="%s">%s</label><p>', $col, $title;
361 printf '<span class=inline>';
362 print $row->input($col => $attr);
363 print $row->input($_ => delete $wordcol{$_}) for @span;
368 if ($row->{id} and not $row->{ref}) {
369 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
370 'trans', 'Translations';
371 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
372 while (my ($lang, $val) = each %fields) {
373 $lang =~ s/^trans-// or next;
374 push @children, { lang => $lang, form => $val };
376 for my $ref (@children) {
378 '<li><label for="%s" title="%3$s">%s </label>',
379 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
382 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
383 '<input id="%s" name="%1$s" value="%3$s" />',
384 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
387 say '</ul></div></li>';
392 <input type="submit" value="Save" />
393 <input type="submit" value="New" formaction="/writer?copy=cat" />
405 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
406 while (my $ref = $parents->hash) {
407 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
409 say "<li><strong>$row->{form}</strong></li>";
410 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
411 while (my $ref = $children->hash) {
412 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
415 <li><form action="/writer">
416 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
417 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
418 <input type="submit" value="Add" />