- my $thumbpath = "data/word/eng/$row->{form}.jpg";
- if (-e $imgpath) {
- my @cmds = @{ $row->{thumb} // [] };
- unshift @cmds, -gravity => @cmds ? 'northwest' : 'center';
- unshift @cmds, 'convert';
- push @cmds, -resize => '300x200^', -extent => '300x200';
- push @cmds, '-strip', -quality => '60%', -interlace => 'plane';
- push @cmds, $imgpath, $thumbpath;
- my $status = system @cmds;
- $status == 0 or Alert([
- "Thumbnail image not generated",
- "Failed to convert source image, error code ".($status >> 8),
- ], "@cmds");
+ sub input {
+ my ($row, $col, $attr) = @_;
+ my $val = $row->{$col} // '';
+ my $html = '';
+ $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
+
+ if (my $options = $attr->{-select}) {
+ $options = $options->(@_) if ref $options eq 'CODE';
+ $options->{$val} //= "unknown ($val)"; # preserve current
+ return (
+ sprintf('<select id="%s" name="%1$s">', $col),
+ (map { sprintf('<option value="%s"%s>%s</option>',
+ $_, $val eq $_ && ' selected', $options->{$_}
+ ) } sort keys %{$options}),
+ '</select>',
+ );
+ }
+ elsif ($attr->{type} eq 'checkbox') {
+ $html .= ' checked' if $val;
+ return sprintf(
+ join('',
+ '<label>',
+ '<input name="%1$s" value="0" type="hidden" />',
+ '<input id="%s" name="%1$s" value="1"%s>',
+ ' %s</label>',
+ ), $col, $html, $attr->{-label}
+ );
+ }
+ else {
+ my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
+ return (
+ (map {
+ sprintf('<label for="%s">%s</label>', $col, $_)
+ } $attr->{-label} // ()),
+ $multiple ? '<span class="inline multiinput">' : (),
+ (map {
+ sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
+ } ref $val eq 'ARRAY' ? @{$val} : ()),
+ sprintf('<input id="%s" name="%1$s" value="%s"%s />',
+ $col, $multiple ? '' : EscapeHTML($val), $html
+ ),
+ $multiple ? '</span>' : (),
+ (map {
+ sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
+ $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
+ } grep { -e } $row->imagepath($col)),
+ );
+ }