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, $@);
109 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
110 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
111 eo => ['<span style="color:green">★</span>', 'esperanto'],
112 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
114 my @wordcols = pairkeys
116 lang => {-label => 'Language', -select => {
117 map { $_ => "@{$lang{$_}}" } keys %lang
119 cat => [{-label => 'Category'}, 'ref'],
120 ref => {-label => 'Reference'},
122 {-label => 'Level', -select => [qw(
123 essential basic common distinctive rare invisible
127 cover => {-label => 'Highlighted', type => 'checkbox'},
128 grade => {-label => 'Order', type => 'number'},
129 form => {-label => 'Title'},
130 alt => {-label => 'Synonyms'},
131 wptitle => {-label => 'Wikipedia'},
132 source => {-label => 'Image'},
133 thumb => {-label => 'Convert options'},
135 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
139 $row = $db->select(word => '*', $find)->hash
140 or Abort("Word not found", 404);
143 if (exists $get{copy}) {
144 $row = {%{$row}{ qw(prio lang cat) }};
146 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
148 $row = {%post{keys %wordcol}};
149 $_ = length ? $_ : undef for values %{$row};
152 if ($row->{ref} ne 'delete') {
154 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
158 $db->delete(word => $find);
159 Alert("Entry removed");
165 my %res = (returning => '*');
166 my $query = $find ? $db->update(word => $row, $find, \%res) :
167 $db->insert(word => $row, \%res);
170 Alert("Entry could not be saved", $@);
175 while (my ($lang, $val) = each %post) {
177 $lang =~ s/^trans-// or next;
178 $db->insert(word => {
183 delete $fields{$field};
186 } or Alert('Error creating translation entries', $@);
188 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
190 ($row->{source} // '') ne ($replace->{source} // '') or return;
191 # copy changed remote url to local file
192 unlink $imgpath if -e $imgpath;
193 my $download = $row->{source} or return 1;
194 require LWP::UserAgent;
195 my $ua = LWP::UserAgent->new;
197 my $status = $ua->mirror($download, $imgpath);
199 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
201 !$@ or Alert(["Source image not found", $@]);
203 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
204 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
205 $reimage++ if $fields{rethumb}; # force refresh
207 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
210 my $xyres = $row->{cover} ? '600x400' : '300x200';
211 my @cmds = @{ $row->{thumb} // [] };
212 if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
213 # replace option by permillage crop
214 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
215 splice @cmds, $cmdarg, 2, (
216 -set => 'option:distort:viewport' => sprintf(
217 '%%[fx:w*%s]x%%[fx:h*%s]+%%[fx:w*%s]+%%[fx:h*%s]',
218 ($dim[2] || 1) - $dim[0], # width = x2 - x1
219 ($dim[3] || 1) - $dim[1], # height = y2 - y1
220 @dim[0, 1] # offset = x1,y1
222 -distort => SRT => 0, # noop transform to apply viewport
227 -delete => '1--1', -background => 'white',
228 -gravity => @cmds ? 'northwest' : 'center',
230 -resize => "$xyres^", -extent => $xyres,
231 '-strip', -quality => '60%', -interlace => 'plane',
232 $imgpath => $thumbpath
237 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
239 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
241 "Thumbnail image not generated",
242 "Failed to convert source image.",
252 $row->{$_} = $get{$_} for keys %get;
255 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
257 package Shiar_Sheet::FormRow {
259 my ($row, $col, $attr) = @_;
260 my $val = $row->{$col} // '';
261 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
263 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
265 if (my $options = $attr->{-select}) {
266 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
267 if ref $options eq 'ARRAY';
268 $options->{$val} //= "unknown ($val)"; # preserve current
270 sprintf('<select id="%s" name="%1$s">', $col),
271 (map { sprintf('<option value="%s"%s>%s</option>',
272 $_, $val eq $_ && ' selected', $options->{$_}
273 ) } sort keys %{$options}),
277 elsif ($attr->{type} eq 'checkbox') {
278 $html .= ' checked' if $val;
282 '<input name="%1$s" value="0" type="hidden" />',
283 '<input id="%s" name="%1$s" value="1"%s>',
285 ), $col, $html, $attr->{-label}
291 sprintf('<label for="%s">%s</label>', $col, $_)
292 } $attr->{-label} // ()),
293 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
294 $col, PLP::Functions::EscapeHTML($val), $html
297 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
298 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
299 } grep { -e } $row->imagepath($col)),
305 my ($row, $col) = @_;
306 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
307 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
311 bless $row, 'Shiar_Sheet::FormRow';
313 <h1>Words <:= $title :></h1>
317 <form action="?" method="post">
318 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
321 for my $col (@wordcols) {
322 my $info = $wordcol{$col} or next;
323 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
324 my $title = ref $attr ? delete $attr->{-label} : $attr;
325 printf '<li><label for="%s">%s</label><p>', $col, $title;
326 printf '<span class=inline>';
327 print $row->input($col => $attr);
328 print $row->input($_ => delete $wordcol{$_}) for @span;
333 if ($row->{id} and not $row->{ref}) {
334 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
335 'trans', 'Translations';
336 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
337 while (my ($lang, $val) = each %fields) {
338 $lang =~ s/^trans-// or next;
339 push @children, { lang => $lang, form => $val };
341 for my $ref (@children) {
343 '<li><label for="%s" title="%3$s">%s </label>',
344 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
347 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
348 '<input id="%s" name="%1$s" value="%3$s" />',
349 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
352 say '</ul></div></li>';
357 <input type="submit" value="Save" />
358 <input type="submit" value="New" formaction="/writer?copy=cat" />
370 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
371 while (my $ref = $parents->hash) {
372 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
374 say "<li><strong>$row->{form}</strong></li>";
375 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
376 while (my $ref = $children->hash) {
377 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
380 <li><form action="/writer">
381 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
382 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
383 <input type="submit" value="Add" />