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 my %res = (returning => '*');
153 my $query = $find ? $db->update(word => $row, $find, \%res) :
154 $db->insert(word => $row, \%res);
157 Alert("Entry could not be saved", $@);
162 while (my ($lang, $val) = each %post) {
164 $lang =~ s/^trans-// or next;
165 $db->insert(word => {
170 delete $fields{$field};
173 } or Alert('Error creating translation entries', $@);
175 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
177 ($row->{source} // '') ne ($replace->{source} // '') or return;
178 # copy changed remote url to local file
179 unlink $imgpath if -e $imgpath;
180 my $download = $row->{source} or return 1;
181 require LWP::UserAgent;
182 my $ua = LWP::UserAgent->new;
184 my $status = $ua->mirror($download, $imgpath);
186 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
188 !$@ or Alert(["Source image not found", $@]);
190 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
191 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
192 $reimage++ if $fields{rethumb}; # force refresh
194 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
197 my $xyres = $row->{cover} ? '600x400' : '300x200';
198 my @cmds = @{ $row->{thumb} // [] };
201 -delete => '1--1', -background => 'white',
202 -gravity => @cmds ? 'northwest' : 'center',
204 -resize => "$xyres^", -extent => $xyres,
205 '-strip', -quality => '60%', -interlace => 'plane',
206 $imgpath => $thumbpath
211 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
213 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
215 "Thumbnail image not generated",
216 "Failed to convert source image.",
226 $row->{$_} = $get{$_} for keys %get;
229 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
231 package Shiar_Sheet::FormRow {
233 my ($row, $col, $attr) = @_;
234 my $val = $row->{$col} // '';
235 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
237 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
239 if (my $options = $attr->{-select}) {
240 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
241 if ref $options eq 'ARRAY';
242 $options->{$val} //= "unknown ($val)"; # preserve current
244 sprintf('<select id="%s" name="%1$s">', $col),
245 (map { sprintf('<option value="%s"%s>%s</option>',
246 $_, $val eq $_ && ' selected', $options->{$_}
247 ) } sort keys %{$options}),
251 elsif ($attr->{type} eq 'checkbox') {
252 $html .= ' checked' if $val;
256 '<input name="%1$s" value="0" type="hidden" />',
257 '<input id="%s" name="%1$s" value="1"%s>',
259 ), $col, $html, $attr->{-label}
265 sprintf('<label for="%s">%s</label>', $col, $_)
266 } $attr->{-label} // ()),
267 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
268 $col, PLP::Functions::EscapeHTML($val), $html
271 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
272 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
273 } grep { -e } $row->imagepath($col)),
279 my ($row, $col) = @_;
280 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
281 return "data/word/en/$row->{form}.jpg" if $col eq 'thumb';
285 bless $row, 'Shiar_Sheet::FormRow';
287 <h1>Words <:= $title :></h1>
291 <form action="?" method="post">
292 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
295 for my $col (@wordcols) {
296 my $info = $wordcol{$col} or next;
297 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
298 my $title = ref $attr ? delete $attr->{-label} : $attr;
299 printf '<li><label for="%s">%s</label><p>', $col, $title;
300 printf '<span class=inline>';
301 print $row->input($col => $attr);
302 print $row->input($_ => delete $wordcol{$_}) for @span;
307 if ($row->{id} and not $row->{ref}) {
308 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
309 'trans', 'Translations';
310 my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
311 while (my ($lang, $val) = each %fields) {
312 $lang =~ s/^trans-// or next;
313 push @children, { lang => $lang, form => $val };
315 for my $ref (@children) {
317 '<li><label for="%s" title="%3$s">%s </label>',
318 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
321 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
322 '<input id="%s" name="%1$s" value="%3$s" />',
323 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
326 say '</ul></div></li>';
331 <input type="submit" value="Save" />
332 <input type="submit" value="New" formaction="/writer?copy=cat" />
344 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
345 while (my $ref = $parents->hash) {
346 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
348 say "<li><strong>$row->{form}</strong></li>";
349 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
350 while (my $ref = $children->hash) {
351 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
354 <li><form action="/writer">
355 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
356 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
357 <input type="submit" value="Add" />