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, $attr) = @_;
212 my $val = $row->{$col} // '';
213 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
215 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
217 if (my $options = $attr->{-select}) {
219 sprintf('<select id="%s" name="%1$s">', $col),
220 (map { sprintf('<option value="%s"%s>%s</option>',
221 $_, $val eq $_ && ' selected', $options->[$_]
222 ) } 0 .. $#{$options}),
226 elsif ($attr->{type} eq 'checkbox') {
227 $html .= ' checked' if $val;
231 '<input name="%1$s" value="0" type="hidden" />',
232 '<input id="%s" name="%1$s" value="1"%s>',
234 ), $col, $html, $attr->{-label}
240 sprintf('<label for="%s">%s</label>', $col, $_)
241 } $attr->{-label} // ()),
242 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
243 $col, PLP::Functions::EscapeHTML($val), $html
246 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
247 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
248 } grep { -e } $row->imagepath($col)),
254 my ($row, $col) = @_;
255 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
256 return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
260 bless $row, 'Shiar_Sheet::FormRow';
262 <h1>Words <:= $title :></h1>
266 <form action="?" method="post">
267 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
270 for my $colinfo (pairs @wordcols) {
271 my ($col, $title) = @{$colinfo};
272 defined $title or next;
273 printf '<li><label for="%s">%s</label><p>', $col, $title;
274 printf '<span class=inline>';
275 if ($col eq 'prio') {
276 print $row->input($col => {-select => \@prioenum});
277 print $row->input(cover => {type => 'checkbox', -label => 'Highlighted'});
278 print $row->input(grade => {type => 'number', -label => 'Order'});
281 print $row->input($col);
282 print $row->input(ref => {-label => 'Reference'}) if $col eq 'cat';
289 my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
290 printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
291 while (my $row = $children->hash) {
292 printf '<li><label for="%s">%s</label> <a id="%1$s" href="%s">%s</a></li>',
293 "trans-$row->{lang}", $langflag{$row->{lang}},
294 "/writer/$row->{id}", Entity($row->{form});
296 say '</ul></div></li>';
301 <input type="submit" value="Save" />
302 <input type="submit" value="New" formaction="/writer?copy=cat" />
314 my $parents = $db->select(word => '*', {id => $row->{cat}});
315 while (my $ref = $parents->hash) {
316 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
318 say "<li><strong>$row->{form}</strong></li>";
319 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
320 while (my $ref = $children->hash) {
321 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
324 <li><form action="/writer">
325 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
326 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
327 <input type="submit" value="Add" />