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'},
131 wptitle => 'Wikipedia',
133 thumb => '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} // [] };
214 -delete => '1--1', -background => 'white',
215 -gravity => @cmds ? 'northwest' : 'center',
217 -resize => "$xyres^", -extent => $xyres,
218 '-strip', -quality => '60%', -interlace => 'plane',
219 $imgpath => $thumbpath
224 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
226 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
228 "Thumbnail image not generated",
229 "Failed to convert source image.",
239 $row->{$_} = $get{$_} for keys %get;
242 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
244 package Shiar_Sheet::FormRow {
246 my ($row, $col, $attr) = @_;
247 my $val = $row->{$col} // '';
248 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
250 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
252 if (my $options = $attr->{-select}) {
253 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
254 if ref $options eq 'ARRAY';
255 $options->{$val} //= "unknown ($val)"; # preserve current
257 sprintf('<select id="%s" name="%1$s">', $col),
258 (map { sprintf('<option value="%s"%s>%s</option>',
259 $_, $val eq $_ && ' selected', $options->{$_}
260 ) } sort keys %{$options}),
264 elsif ($attr->{type} eq 'checkbox') {
265 $html .= ' checked' if $val;
269 '<input name="%1$s" value="0" type="hidden" />',
270 '<input id="%s" name="%1$s" value="1"%s>',
272 ), $col, $html, $attr->{-label}
278 sprintf('<label for="%s">%s</label>', $col, $_)
279 } $attr->{-label} // ()),
280 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
281 $col, PLP::Functions::EscapeHTML($val), $html
284 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
285 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
286 } grep { -e } $row->imagepath($col)),
292 my ($row, $col) = @_;
293 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
294 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
298 bless $row, 'Shiar_Sheet::FormRow';
300 <h1>Words <:= $title :></h1>
304 <form action="?" method="post">
305 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
308 for my $col (@wordcols) {
309 my $info = $wordcol{$col} or next;
310 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
311 my $title = ref $attr ? delete $attr->{-label} : $attr;
312 printf '<li><label for="%s">%s</label><p>', $col, $title;
313 printf '<span class=inline>';
314 print $row->input($col => $attr);
315 print $row->input($_ => delete $wordcol{$_}) for @span;
320 if ($row->{id} and not $row->{ref}) {
321 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
322 'trans', 'Translations';
323 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
324 while (my ($lang, $val) = each %fields) {
325 $lang =~ s/^trans-// or next;
326 push @children, { lang => $lang, form => $val };
328 for my $ref (@children) {
330 '<li><label for="%s" title="%3$s">%s </label>',
331 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
334 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
335 '<input id="%s" name="%1$s" value="%3$s" />',
336 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
339 say '</ul></div></li>';
344 <input type="submit" value="Save" />
345 <input type="submit" value="New" formaction="/writer?copy=cat" />
357 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
358 while (my $ref = $parents->hash) {
359 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
361 say "<li><strong>$row->{form}</strong></li>";
362 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
363 while (my $ref = $children->hash) {
364 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
367 <li><form action="/writer">
368 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
369 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
370 <input type="submit" value="Add" />