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 nld => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'dutch'],
110 eng => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
111 epo => ['<span style="color:green">★</span>', 'esperanto'],
113 my @wordcols = pairkeys
115 lang => {-label => 'Language', -select => {
116 map { $_ => "@{$lang{$_}}" } keys %lang
118 cat => [{-label => 'Category'}, 'ref'],
119 ref => {-label => 'Reference'},
121 {-label => 'Level', -select => [qw(
122 essential basic common distinctive rare invisible
126 cover => {-label => 'Highlighted', type => 'checkbox'},
127 grade => {-label => 'Order', type => 'number'},
130 wptitle => 'Wikipedia',
132 thumb => 'Convert options',
134 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
138 $row = $db->select(word => '*', $find)->hash
139 or Abort("Word not found", 404);
142 if (exists $get{copy}) {
143 $row = {%{$row}{ qw(prio lang cat) }};
145 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
147 $row = {%post{keys %wordcol}};
148 $_ = length ? $_ : undef for values %{$row};
151 my %res = (returning => '*');
152 my $query = $find ? $db->update(word => $row, $find, \%res) :
153 $db->insert(word => $row, \%res);
156 Alert("Entry could not be saved", $@);
161 while (my ($lang, $val) = each %post) {
163 $lang =~ s/^trans-// or next;
164 $db->insert(word => {
169 delete $fields{$field};
172 } or Alert('Error creating translation entries', $@);
174 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
176 ($row->{source} // '') ne ($replace->{source} // '') or return;
177 # copy changed remote url to local file
178 unlink $imgpath if -e $imgpath;
179 my $download = $row->{source} or return 1;
180 require LWP::UserAgent;
181 my $ua = LWP::UserAgent->new;
183 my $status = $ua->mirror($download, $imgpath);
185 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
187 !$@ or Alert(["Source image not found", $@]);
189 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
190 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
191 $reimage++ if $fields{rethumb}; # force refresh
193 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
196 my $xyres = $row->{cover} ? '600x400' : '300x200';
197 my @cmds = @{ $row->{thumb} // [] };
200 -delete => '1--1', -background => 'white',
201 -gravity => @cmds ? 'northwest' : 'center',
203 -resize => "$xyres^", -extent => $xyres,
204 '-strip', -quality => '60%', -interlace => 'plane',
205 $imgpath => $thumbpath
210 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
212 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
214 "Thumbnail image not generated",
215 "Failed to convert source image.",
225 $row->{$_} = $get{$_} for keys %get;
228 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
230 package Shiar_Sheet::FormRow {
232 my ($row, $col, $attr) = @_;
233 my $val = $row->{$col} // '';
234 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
236 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
238 if (my $options = $attr->{-select}) {
239 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
240 if ref $options eq 'ARRAY';
241 $options->{$val} //= "unknown ($val)"; # preserve current
243 sprintf('<select id="%s" name="%1$s">', $col),
244 (map { sprintf('<option value="%s"%s>%s</option>',
245 $_, $val eq $_ && ' selected', $options->{$_}
246 ) } sort keys %{$options}),
250 elsif ($attr->{type} eq 'checkbox') {
251 $html .= ' checked' if $val;
255 '<input name="%1$s" value="0" type="hidden" />',
256 '<input id="%s" name="%1$s" value="1"%s>',
258 ), $col, $html, $attr->{-label}
264 sprintf('<label for="%s">%s</label>', $col, $_)
265 } $attr->{-label} // ()),
266 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
267 $col, PLP::Functions::EscapeHTML($val), $html
270 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
271 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
272 } grep { -e } $row->imagepath($col)),
278 my ($row, $col) = @_;
279 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
280 return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
284 bless $row, 'Shiar_Sheet::FormRow';
286 <h1>Words <:= $title :></h1>
290 <form action="?" method="post">
291 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
294 for my $col (@wordcols) {
295 my $info = $wordcol{$col} or next;
296 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
297 my $title = ref $attr ? delete $attr->{-label} : $attr;
298 printf '<li><label for="%s">%s</label><p>', $col, $title;
299 printf '<span class=inline>';
300 print $row->input($col => $attr);
301 print $row->input($_ => delete $wordcol{$_}) for @span;
306 if ($row->{id} and not $row->{ref}) {
307 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
308 'trans', 'Translations';
309 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
310 while (my ($lang, $val) = each %fields) {
311 $lang =~ s/^trans-// or next;
312 push @children, { lang => $lang, form => $val };
314 for my $ref (@children) {
316 '<li><label for="%s" title="%3$s">%s </label>',
317 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
320 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
321 '<input id="%s" name="%1$s" value="%3$s" />',
322 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
325 say '</ul></div></li>';
330 <input type="submit" value="Save" />
331 <input type="submit" value="New" formaction="/writer?copy=cat" />
343 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
344 while (my $ref = $parents->hash) {
345 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
347 say "<li><strong>$row->{form}</strong></li>";
348 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
349 while (my $ref = $children->hash) {
350 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
353 <li><form action="/writer">
354 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
355 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
356 <input type="submit" value="Add" />