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 = "data/word/org/$row->{id}.jpg";
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 = "data/word/eng/$row->{form}.jpg";
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 <h1>Words <:= $title :></h1>
213 <form action="?" method="post">
214 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
218 for my $colinfo (pairs @wordcols) {
219 my ($col, $title) = @{$colinfo};
220 defined $title or next;
221 my $val = $row->{$col} // '';
222 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
223 printf '<li><label for="%s">%s</label><p>', $col, $title;
224 printf '<span class=inline>';
225 if ($col eq 'prio') {
226 printf '<select id="%s" name="%1$s">', $col;
227 printf('<option value="%s"%s>%s</option>',
228 $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
229 ) for 0 .. $#prioenum;
234 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
235 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
238 'cover', !!$row->{cover} && ' checked', 'Highlighted'
240 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" type="number" />',
241 'grade', 'Order', Entity($row->{grade})
245 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
246 -e and printf '<img id="%spreview" src="/%s" alt="%s" />', $col, $_, $row->{form}
247 for $col eq 'thumb' ? "data/word/eng/$row->{form}.jpg" : ();
248 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" />',
249 'ref', 'Reference', Entity($row->{ref})
253 -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
254 $col, $_, $row->{form}
255 ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
260 my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
261 printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
262 while (my $row = $children->hash) {
263 printf '<li><label for="%s">%s</label> <a id="%1$s" href="%s">%s</a></li>',
264 "trans-$row->{lang}", $langflag{$row->{lang}},
265 "/writer/$row->{id}", Entity($row->{form});
267 say '</ul></div></li>';
272 <input type="submit" value="Save" />
273 <input type="submit" value="New" formaction="/writer?copy=cat" />
285 my $parents = $db->select(word => '*', {id => $row->{cat}});
286 while (my $ref = $parents->hash) {
287 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
289 say "<li><strong>$row->{form}</strong></li>";
290 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
291 while (my $ref = $children->hash) {
292 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
295 <li><form action="/writer">
296 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
297 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
298 <input type="submit" value="Add" />