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, $@);
111 ref => undef, # included with cat
114 cover => undef, # included with prio
117 wptitle => 'Wikipedia',
119 thumb => 'Convert options',
121 my @prioenum = qw( essential basic common distinctive rare invisible );
123 nld => "\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}",
124 eng => "\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}",
125 epo => '<span style="color:green">★</span>',
127 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
131 $row = $db->select(word => '*', $find)->hash
132 or Abort("Word not found", 404);
135 if (exists $get{copy}) {
136 $row = {%{$row}{ qw(prio lang cat) }};
138 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
140 $row = {%post{ pairkeys @wordcols }};
141 $_ = length ? $_ : undef for values %{$row};
144 my %res = (returning => '*');
145 my $query = $find ? $db->update(word => $row, $find, \%res) :
146 $db->insert(word => $row, \%res);
149 Alert("Entry could not be saved", $@);
153 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
155 ($row->{source} // '') ne ($replace->{source} // '') or return;
156 # copy changed remote url to local file
157 unlink $imgpath if -e $imgpath;
158 my $download = $row->{source} or return 1;
159 require LWP::UserAgent;
160 my $ua = LWP::UserAgent->new;
162 my $status = $ua->mirror($download, $imgpath);
164 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
166 !$@ or Alert(["Source image not found", $@]);
168 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
169 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
170 $reimage++ if $fields{rethumb}; # force refresh
172 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
175 my $xyres = $row->{cover} ? '600x400' : '300x200';
176 my @cmds = @{ $row->{thumb} // [] };
179 -delete => '1--1', -background => 'white',
180 -gravity => @cmds ? 'northwest' : 'center',
182 -resize => "$xyres^", -extent => $xyres,
183 '-strip', -quality => '60%', -interlace => 'plane',
184 $imgpath => $thumbpath
189 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
191 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
193 "Thumbnail image not generated",
194 "Failed to convert source image.",
204 $row->{$_} = $get{$_} for keys %get;
207 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
209 package Shiar_Sheet::FormRow {
211 my ($row, $col, $type, $label) = @_;
212 my $val = $row->{$col} // '';
213 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
214 if (ref $type eq 'ARRAY') {
216 sprintf('<select id="%s" name="%1$s">', $col),
217 (map { sprintf('<option value="%s"%s>%s</option>',
218 $_, $val eq $_ && ' selected', $type->[$_]
223 elsif ($type eq 'checkbox') {
227 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
228 '<input id="%s" name="%1$s" value="1" type="%s"%s>',
230 ), $col, $type, !!$val && ' checked', $label
235 $html .= qq( $_="$type->{$_}") for sort keys %{$type // {}};
237 $label ? sprintf('<label for="%s">%s</label>', $col, $label) : (),
238 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
239 $col, PLP::Functions::EscapeHTML($val), $html
242 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
243 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
244 } grep { -e } $row->imagepath($col)),
250 my ($row, $col) = @_;
251 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
252 return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
256 bless $row, 'Shiar_Sheet::FormRow';
258 <h1>Words <:= $title :></h1>
262 <form action="?" method="post">
263 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
266 for my $colinfo (pairs @wordcols) {
267 my ($col, $title) = @{$colinfo};
268 defined $title or next;
269 printf '<li><label for="%s">%s</label><p>', $col, $title;
270 printf '<span class=inline>';
271 if ($col eq 'prio') {
272 print $row->input($col => \@prioenum);
273 print $row->input(cover => 'checkbox', 'Highlighted');
274 print $row->input(grade => {type => 'number'}, 'Order');
277 print $row->input($col);
278 print $row->input(ref => {}, 'Reference') if $col eq 'cat';
285 my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
286 printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
287 while (my $row = $children->hash) {
288 printf '<li><label for="%s">%s</label> <a id="%1$s" href="%s">%s</a></li>',
289 "trans-$row->{lang}", $langflag{$row->{lang}},
290 "/writer/$row->{id}", Entity($row->{form});
292 say '</ul></div></li>';
297 <input type="submit" value="Save" />
298 <input type="submit" value="New" formaction="/writer?copy=cat" />
310 my $parents = $db->select(word => '*', {id => $row->{cat}});
311 while (my $ref = $parents->hash) {
312 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
314 say "<li><strong>$row->{form}</strong></li>";
315 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
316 while (my $ref = $children->hash) {
317 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
320 <li><form action="/writer">
321 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
322 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
323 <input type="submit" value="Add" />