word edit: sql array values in multiple input fields
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 9 Jun 2020 07:29:03 +0000 (09:29 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 20 Oct 2020 20:49:11 +0000 (22:49 +0200)
writer.plp

index 7280142226a1c30dd19ddeb734e9825b02636f0e..62d26d762d0f27f78f12fbd7981a56b797f75a2d 100644 (file)
@@ -104,6 +104,7 @@ my $db = eval {
                pg_enable_utf8 => 1,
        });
 } or Abort('Database error', 501, $@);
+$db->abstract->{array_datatypes}++;
 
 my %lang = (
        nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
@@ -127,10 +128,10 @@ my %wordcol = (
        cover   => {-label => 'Highlighted', type => 'checkbox'},
        grade   => {-label => 'Order', type => 'number'},
        form    => {-label => 'Title'},
-       alt     => {-label => 'Synonyms'},
+       alt     => {-label => 'Synonyms', -multiple => 1},
        wptitle => {-label => 'Wikipedia'},
        source  => {-label => 'Image'},
-       thumb   => {-label => 'Convert options'},
+       thumb   => {-label => 'Convert options', -multiple => 1},
 );
 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
 
@@ -146,6 +147,9 @@ if (exists $get{copy}) {
 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
        my $replace = $row;
        $row = {%post{keys %wordcol}};
+       $row->{$_} = [grep {length} @{ $post{"\@$_"} }] for grep {
+               ref $wordcol{$_} eq 'HASH' and $wordcol{$_}->{-multiple}
+       } keys %wordcol;
        $_ = length ? $_ : undef for values %{$row};
 
        if (!$row->{form}) {
@@ -255,10 +259,11 @@ else {
 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
 
 package Shiar_Sheet::FormRow {
+       use PLP::Functions 'EscapeHTML';
+
        sub input {
                my ($row, $col, $attr) = @_;
                my $val = $row->{$col} // '';
-               $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
                my $html = '';
                $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
 
@@ -290,8 +295,11 @@ package Shiar_Sheet::FormRow {
                                (map {
                                        sprintf('<label for="%s">%s</label>', $col, $_)
                                } $attr->{-label} // ()),
+                               (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, PLP::Functions::EscapeHTML($val), $html
+                                       $col, ref $val eq 'ARRAY' ? '' : EscapeHTML($val), $html
                                ),
                                (map {
                                        sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',