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 nld => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'dutch'],
110 eng => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
111 epo => ['<span style="color:green">★</span>', 'esperanto'],
113 my @wordcols = pairkeys
115 lang => {-label => 'Language', -select => {
116 map { $_ => "@{$lang{$_}}" } keys %lang
118 cat => [{-label => 'Category'}, 'ref'],
119 ref => {-label => 'Reference'},
121 {-label => 'Level', -select => [qw(
122 essential basic common distinctive rare invisible
126 cover => {-label => 'Highlighted', type => 'checkbox'},
127 grade => {-label => 'Order', type => 'number'},
130 wptitle => 'Wikipedia',
132 thumb => 'Convert options',
134 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
138 $row = $db->select(word => '*', $find)->hash
139 or Abort("Word not found", 404);
142 if (exists $get{copy}) {
143 $row = {%{$row}{ qw(prio lang cat) }};
145 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
147 $row = {%post{keys %wordcol}};
148 $_ = length ? $_ : undef for values %{$row};
151 my %res = (returning => '*');
152 my $query = $find ? $db->update(word => $row, $find, \%res) :
153 $db->insert(word => $row, \%res);
156 Alert("Entry could not be saved", $@);
160 my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
162 ($row->{source} // '') ne ($replace->{source} // '') or return;
163 # copy changed remote url to local file
164 unlink $imgpath if -e $imgpath;
165 my $download = $row->{source} or return 1;
166 require LWP::UserAgent;
167 my $ua = LWP::UserAgent->new;
169 my $status = $ua->mirror($download, $imgpath);
171 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
173 !$@ or Alert(["Source image not found", $@]);
175 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
176 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
177 $reimage++ if $fields{rethumb}; # force refresh
179 my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
182 my $xyres = $row->{cover} ? '600x400' : '300x200';
183 my @cmds = @{ $row->{thumb} // [] };
186 -delete => '1--1', -background => 'white',
187 -gravity => @cmds ? 'northwest' : 'center',
189 -resize => "$xyres^", -extent => $xyres,
190 '-strip', -quality => '60%', -interlace => 'plane',
191 $imgpath => $thumbpath
196 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
198 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
200 "Thumbnail image not generated",
201 "Failed to convert source image.",
211 $row->{$_} = $get{$_} for keys %get;
214 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
216 package Shiar_Sheet::FormRow {
218 my ($row, $col, $attr) = @_;
219 my $val = $row->{$col} // '';
220 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
222 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
224 if (my $options = $attr->{-select}) {
225 $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
226 if ref $options eq 'ARRAY';
227 $options->{$val} //= "unknown ($val)"; # preserve current
229 sprintf('<select id="%s" name="%1$s">', $col),
230 (map { sprintf('<option value="%s"%s>%s</option>',
231 $_, $val eq $_ && ' selected', $options->{$_}
232 ) } sort keys %{$options}),
236 elsif ($attr->{type} eq 'checkbox') {
237 $html .= ' checked' if $val;
241 '<input name="%1$s" value="0" type="hidden" />',
242 '<input id="%s" name="%1$s" value="1"%s>',
244 ), $col, $html, $attr->{-label}
250 sprintf('<label for="%s">%s</label>', $col, $_)
251 } $attr->{-label} // ()),
252 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
253 $col, PLP::Functions::EscapeHTML($val), $html
256 sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
257 $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
258 } grep { -e } $row->imagepath($col)),
264 my ($row, $col) = @_;
265 return "data/word/org/$row->{id}.jpg" if $col eq 'source';
266 return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
270 bless $row, 'Shiar_Sheet::FormRow';
272 <h1>Words <:= $title :></h1>
276 <form action="?" method="post">
277 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
280 for my $col (@wordcols) {
281 my $info = $wordcol{$col} or next;
282 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
283 my $title = ref $attr ? delete $attr->{-label} : $attr;
284 printf '<li><label for="%s">%s</label><p>', $col, $title;
285 printf '<span class=inline>';
286 print $row->input($col => $attr);
287 print $row->input($_ => delete $wordcol{$_}) for @span;
293 my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
294 printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
295 while (my $row = $children->hash) {
297 '<li><label for="%1$s" title="%5$s">%4$s</label>' .
298 ' <a id="%s" href="%s">%s</a></li>',
299 "trans-$row->{lang}", "/writer/$row->{id}", Entity($row->{form}),
300 @{$lang{ $row->{lang} }}, # flag, name
303 say '</ul></div></li>';
308 <input type="submit" value="Save" />
309 <input type="submit" value="New" formaction="/writer?copy=cat" />
321 my $parents = $db->select(word => '*', {id => $row->{cat}});
322 while (my $ref = $parents->hash) {
323 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
325 say "<li><strong>$row->{form}</strong></li>";
326 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
327 while (my $ref = $children->hash) {
328 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
331 <li><form action="/writer">
332 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
333 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
334 <input type="submit" value="Add" />