X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/0d4c5ff9be0016264c0448df5f3ad5c4d95adeaa..e26741ab236cbfaa89aa82d121c224137801a63b:/writer.plp diff --git a/writer.plp b/writer.plp index 3c0b248..b5f5032 100644 --- a/writer.plp +++ b/writer.plp @@ -5,117 +5,21 @@ Html({ version => '1.0', nocache => 1, raw => <<'EOT', - - + EOT }); use List::Util qw( pairs pairkeys ); +use Shiar_Sheet::FormRow; my $db = eval { - my @dbinfo = ( - 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse', - ) or die "database not configured\n"; - require DBIx::Simple; - DBIx::Simple->new(@dbinfo[0..2], { - RaiseError => 1, - pg_enable_utf8 => 1, - }); + require Shiar_Sheet::DB; + Shiar_Sheet::DB->connect; } or Abort('Database error', 501, $@); -$db->abstract->{array_datatypes}++; my $user = eval { + my $rootpath = ($ENV{REQUEST_URI} // '/writer') =~ s{(?new( -name => 'login', -value => '', - -path => '/writer', + -path => $rootpath, -expires => 'now', )->as_string)) { delete $cookie{login}; @@ -144,7 +48,7 @@ my $user = eval { my $httpcookie = CGI::Cookie->new( -name => 'login', -value => join(':', @{$found}{qw( username pass )}), - -path => '/writer', + -path => $rootpath, ) or die "prepared object is empty\n"; AddCookie($httpcookie->as_string); } or Abort(["Unable to create login cookie", $@], 403); @@ -169,6 +73,8 @@ my %lang = ( en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'], eo => ['★', 'esperanto'], ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'], + zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'], + la => ["\N{PUSHPIN}", 'latin'], ); my @wordcols = pairkeys my %wordcol = ( @@ -178,9 +84,14 @@ my %wordcol = ( cat => [{-label => 'Category'}, 'ref'], ref => {-label => 'Reference'}, prio => [ - {-label => 'Level', -select => [qw( - essential basic common distinctive rare invisible - )]}, + {-label => 'Level', -select => sub { + my ($row) = @_; + my @enum = qw[ essential basic common distinctive optional invisible ]; + return { + ('' => 'parent') x (defined $row->{ref}), + map { $_ => $enum[$_] } 0 .. $#enum + }; + }}, 'cover', 'grade', ], cover => {-label => 'Highlighted', type => 'checkbox'}, @@ -188,11 +99,28 @@ my %wordcol = ( form => {-label => 'Title'}, alt => {-label => 'Synonyms', -multiple => 1}, wptitle => {-label => 'Wikipedia'}, - source => {-label => 'Image'}, - thumb => {-label => 'Convert options', -multiple => 1}, + source => {-label => 'Image', -src => sub { + return "data/word/org/$_[0]->{id}.jpg"; + }}, + thumb => {-label => 'Convert options', -multiple => 1, -src => sub { + return "data/word/en/$_[0]->{id}.jpg"; + }}, + story => {-label => 'Story', type => 'textarea'}, ); -my ($find) = map {{id => $_}} $fields{id} || $Request || (); +if (my $search = $fields{q}) { + my %filter = (form => {ilike => '%'.$search.'%'}); + my $results = $db->select(word => '*', \%filter); + say '

Search

\n"; + exit; +} + +my ($find) = map {{id => $_}} $fields{id} || $Request || (); my $row; if ($find) { $row = $db->select(word => '*', $find)->hash @@ -250,15 +178,17 @@ elsif (defined $post{form}) {{ ref => $row->{id}, lang => $lang, form => $val, + prio => undef, ); $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand + $subrow{alt} = [split m{/}, $1] if $subrow{form} =~ s{/(\S.*)}{}; # /alternates shorthand $db->insert(word => \%subrow); delete $fields{$field}; } return 1; } or Alert('Error creating translation entries', $@); - my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source'); + my $imgpath = $wordcol{source}->{-src}->($row); my $reimage = eval { ($row->{source} // '') ne ($replace->{source} // '') or return; # copy changed remote url to local file @@ -277,8 +207,8 @@ elsif (defined $post{form}) {{ $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize $reimage++ if $fields{rethumb}; # force refresh - my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb'); if ($reimage) { + my $thumbpath = $wordcol{thumb}->{-src}->($row); if (-e $imgpath) { my $xyres = $row->{cover} ? '600x400' : '300x200'; my @cmds = @{ $row->{thumb} // [] }; @@ -299,7 +229,7 @@ elsif (defined $post{form}) {{ 'convert', $imgpath, -delete => '1--1', -background => 'white', - -gravity => @cmds ? 'northwest' : 'center', + -gravity => defined $row->{thumb} ? 'northwest' : 'center', @cmds, -resize => "$xyres^", -extent => $xyres, '-strip', -quality => '60%', -interlace => 'plane', @@ -322,74 +252,12 @@ elsif (defined $post{form}) {{ } }} else { - $row->{prio} //= 1; $row->{lang} //= $user->{editlang}->[0]; $row->{$_} = $get{$_} for keys %get; + $row->{prio} = defined $row->{ref} ? undef : 1 unless exists $row->{prio}; } my $title = $row->{id} ? "entry #$row->{id}" : 'new entry'; - -package Shiar_Sheet::FormRow { - use PLP::Functions 'EscapeHTML'; - - 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 = { map {$_ => $options->[$_]} 0 .. $#{$options} } - if ref $options eq 'ARRAY'; - $options->{$val} //= "unknown ($val)"; # preserve current - return ( - sprintf('', - ); - } - elsif ($attr->{type} eq 'checkbox') { - $html .= ' checked' if $val; - return sprintf( - join('', - '', - ), $col, $html, $attr->{-label} - ); - } - else { - my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple}; - return ( - (map { - sprintf('', $col, $_) - } $attr->{-label} // ()), - $multiple ? '' : (), - (map { - sprintf('', $col, EscapeHTML($_)) - } ref $val eq 'ARRAY' ? @{$val} : ()), - sprintf('', - $col, $multiple ? '' : EscapeHTML($val), $html - ), - $multiple ? '' : (), - (map { - sprintf '%s', - $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : ''; - } grep { -e } $row->imagepath($col)), - ); - } - } - - sub imagepath { - my ($row, $col) = @_; - return "data/word/org/$row->{id}.jpg" if $col eq 'source'; - return "data/word/en/$row->{form}.jpg" if $col eq 'thumb'; - return; - } -} bless $row, 'Shiar_Sheet::FormRow'; :>

Words <:= $title :>

@@ -407,6 +275,11 @@ for my $col (@wordcols) { printf '
  • ', $col, $title; printf ''; print $row->input($col => $attr); + if (my $imgsrc = $attr->{-src}) { + printf('%s', + $col, $_, $row->{form}, $col eq 'source' && ' hidden' + ) for grep { -e } $imgsrc->($row); + } print $row->input($_ => delete $wordcol{$_}) for @span; print ''; say '

  • '; @@ -457,7 +330,7 @@ my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}} while (my $ref = $parents->hash) { printf '
  • %s
  • ', $ref->{id}, Entity($ref->{form}); } -say "
  • $row->{form}
  • "; +say "
  • $_
  • " for Entity($row->{form}); my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id'); while (my $ref = $children->hash) { printf '
  • %s
  • ', $ref->{id}, Entity($ref->{form});