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, $@);
108 my @wordcols = pairkeys
111 cat => [{-label => 'Category'}, 'ref'],
112 ref => {-label => 'Reference'},
114 {-label => 'Level', -select => [qw(
115 essential basic common distinctive rare invisible
119 cover => {-label => 'Highlighted', type => 'checkbox'},
120 grade => {-label => 'Order', type => 'number'},
123 wptitle => 'Wikipedia',
125 thumb => 'Convert options',
128 nld => "\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}",
129 eng => "\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}",
130 epo => '<span style="color:green">★</span>',
132 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
136 $row = $db->select(word => '*', $find)->hash
137 or Abort("Word not found", 404);
140 if (exists $get{copy}) {
141 $row = {%{$row}{ qw(prio lang cat) }};
143 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
145 $row = {%post{keys %wordcol}};
146 $_ = length ? $_ : undef for values %{$row};
149 my %res = (returning => '*');
150 my $query = $find ? $db->update(word => $row, $find, \%res) :
151 $db->insert(word => $row, \%res);
154 Alert("Entry could not be saved", $@);
158 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
160 ($row->{source} // '') ne ($replace->{source} // '') or return;
161 # copy changed remote url to local file
162 unlink $imgpath if -e $imgpath;
163 my $download = $row->{source} or return 1;
164 require LWP::UserAgent;
165 my $ua = LWP::UserAgent->new;
167 my $status = $ua->mirror($download, $imgpath);
169 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
171 !$@ or Alert(["Source image not found", $@]);
173 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
174 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
175 $reimage++ if $fields{rethumb}; # force refresh
177 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
180 my $xyres = $row->{cover} ? '600x400' : '300x200';
181 my @cmds = @{ $row->{thumb} // [] };
184 -delete => '1--1', -background => 'white',
185 -gravity => @cmds ? 'northwest' : 'center',
187 -resize => "$xyres^", -extent => $xyres,
188 '-strip', -quality => '60%', -interlace => 'plane',
189 $imgpath => $thumbpath
194 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
196 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
198 "Thumbnail image not generated",
199 "Failed to convert source image.",
209 $row->{$_} = $get{$_} for keys %get;
212 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
214 package Shiar_Sheet::FormRow {
216 my ($row, $col, $attr) = @_;
217 my $val = $row->{$col} // '';
218 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
220 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
222 if (my $options = $attr->{-select}) {
224 sprintf('<select id="%s" name="%1$s">', $col),
225 (map { sprintf('<option value="%s"%s>%s</option>',
226 $_, $val eq $_ && ' selected', $options->[$_]
227 ) } 0 .. $#{$options}),
231 elsif ($attr->{type} eq 'checkbox') {
232 $html .= ' checked' if $val;
236 '<input name="%1$s" value="0" type="hidden" />',
237 '<input id="%s" name="%1$s" value="1"%s>',
239 ), $col, $html, $attr->{-label}
245 sprintf('<label for="%s">%s</label>', $col, $_)
246 } $attr->{-label} // ()),
247 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
248 $col, PLP::Functions::EscapeHTML($val), $html
251 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
252 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
253 } grep { -e } $row->imagepath($col)),
259 my ($row, $col) = @_;
260 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
261 return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
265 bless $row, 'Shiar_Sheet::FormRow';
267 <h1>Words <:= $title :></h1>
271 <form action="?" method="post">
272 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
275 for my $col (@wordcols) {
276 my $info = $wordcol{$col} or next;
277 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
278 my $title = ref $attr ? delete $attr->{-label} : $attr;
279 printf '<li><label for="%s">%s</label><p>', $col, $title;
280 printf '<span class=inline>';
281 print $row->input($col => $attr);
282 print $row->input($_ => delete $wordcol{$_}) for @span;
288 my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
289 printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
290 while (my $row = $children->hash) {
291 printf '<li><label for="%s">%s</label> <a id="%1$s" href="%s">%s</a></li>',
292 "trans-$row->{lang}", $langflag{$row->{lang}},
293 "/writer/$row->{id}", Entity($row->{form});
295 say '</ul></div></li>';
300 <input type="submit" value="Save" />
301 <input type="submit" value="New" formaction="/writer?copy=cat" />
313 my $parents = $db->select(word => '*', {id => $row->{cat}});
314 while (my $ref = $parents->hash) {
315 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
317 say "<li><strong>$row->{form}</strong></li>";
318 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
319 while (my $ref = $children->hash) {
320 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
323 <li><form action="/writer">
324 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
325 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
326 <input type="submit" value="Add" />