word/edit: preserve unknown json values
[sheet.git] / word / edit.plp
index f7d39485c992ae1f60373d2ca445534772122cd0..4a6daaad4a19ab503cd0680ec13479e11baa7934 100644 (file)
@@ -72,6 +72,7 @@ my $user = eval {
 };
 
 my %lang = (
+       '' => ['(reference)'],
        nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
        en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
        eo => [qq'<span style="color:green">\N{BLACK STAR}</span>', 'esperanto'],
@@ -89,7 +90,9 @@ my %wordcol = (
        prio    => [
                {-label => 'Level', -select => sub {
                        my ($row) = @_;
-                       my @enum = qw[ essential basic common distinctive optional invisible ];
+                       my @enum = qw[
+                               essential ubiquitous basic common distinctive specialised rare invisible
+                       ];
                        return {
                                ('' => 'parent') x (defined $row->{ref}),
                                map { $_ => $enum[$_] } 0 .. $#enum
@@ -106,13 +109,15 @@ my %wordcol = (
                return "data/word/org/$_[0]->{id}.jpg";
        }},
        convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
-               return "data/word/en/$_[0]->{id}.jpg";
+               return "data/word/32/$_[0]->{id}.jpg";
        }},
+       crop32  => {-json => 'image', type => 'hidden'}, # set by javascript interface
        story   => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
 );
 
 if (my $search = $fields{q}) {
-       my %filter = (form => {ilike => '%'.$search.'%'});
+       my %filter = $search eq '^' ? (cat => undef, ref => undef) :
+               (form => {ilike => '%'.parseinput($search).'%'});
        my $results = $db->select(word => '*', \%filter);
        say '<h1>Search</h1><ul>';
        printf("<li><small>%s</small> %s %s</li>\n",
@@ -147,7 +152,6 @@ elsif (defined $post{form}) {{
                my @val = map { parseinput($_) } $post{'@'.$col}->@*;
                my $val = $colinfo->{-multiple} && @val ? \@val : $val[-1];
                if (my $jsoncol = $colinfo->{-json}) {
-                       defined $val and
                        $row->{$jsoncol}->{$col} = $val;  # hash will be encoded
                }
                else {
@@ -155,9 +159,15 @@ elsif (defined $post{form}) {{
                }
        }
        my $imagecol = $row->{image};  # backup image subcolumns
-       ref $_ eq 'HASH' and $_ = encode_json($_) for values %{$row};
+       while (my ($col, $val) = each %{$row}) {
+               # convert json subcolumns to database string
+               ref $val eq 'HASH' or next;
+               $val = { %{decode_json($_)}, %{$val} } for $replace->{$col} // ();  # preserve unknown
+               defined $val->{$_} or delete $val->{$_} for keys %{$val};  # delete emptied
+               $row->{$col} = encode_json($val);
+       }
 
-       if (!$row->{form}) {
+       if (!$row->{form} and $row->{lang}) {
                if ($row->{ref} ne 'delete') {
                        Alert("Empty title",
                                "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
@@ -214,7 +224,7 @@ elsif (defined $post{form}) {{
        $reimage++ if $fields{rethumb};  # force refresh
        if ($reimage) {
                eval {
-                       $image->convert($wordcol{convert}->{-src}->($row), $imagecol->{convert});
+                       $image->generate($wordcol{convert}->{-src}->($row), $imagecol);
                } or do {
                        my ($warn, @details) = ref $@ ? @{$@} : $@;
                        Alert([ "Thumbnail image not generated", $warn ], @details);
@@ -222,13 +232,13 @@ elsif (defined $post{form}) {{
        }
 }}
 else {
-       $row->{lang} //= $user->{editlang}->[0];
+       $row->{lang} //= $user->{editlang}->[0] unless exists $row->{lang};
        $row->{$_} = $get{$_} for keys %get;
-       $row->{prio} = defined $row->{ref} ? undef : 1 unless exists $row->{prio};
+       $row->{prio} = defined $row->{ref} ? undef : 4 unless exists $row->{prio};
 }
 
 eval {
-       my $imagerow = $row->{image} && decode_json(delete $row->{image}) || {};
+       my $imagerow = $row->{image} && JSON->new->decode(delete $row->{image}) || {};
        while (my ($col, $val) = each %{$imagerow}) {
                $row->{$col} = $val;
        }
@@ -255,9 +265,12 @@ for my $col (@wordcols) {
                printf '<span class=inline>';
                print $row->input($col => $attr);
                if (my $imgsrc = $attr->{-src}) {
-                       printf('<img id="%spreview" src="/%s" alt="%s"%s />',
-                               $col, $_, $row->{form}, $col eq 'source' && ' hidden'
+                       my $hide = $col eq 'source';
+                       printf '<span id="%spreview">', $col unless $hide;
+                       printf('<img src="/%s" alt="%s"%s />',
+                               $_, $row->{form}, $hide && qq( id="${col}preview" hidden)
                        ) for grep { -e } $imgsrc->($row);
+                       printf '</span>' unless $hide;
                }
                print $row->input($_ => delete $wordcol{$_}) for @span;
                print '</span>';
@@ -265,7 +278,7 @@ for my $col (@wordcols) {
 }
 
 if (not $row->{ref}) {
-       printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
+       printf '<li><label for="%s">%s</label><div><ul class="inline multiinput" id="%1$s">',
                'trans', 'Translations';
        my @children = !$row->{id} ? () :
                $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
@@ -321,6 +334,10 @@ while (my $ref = $children->hash) {
        <input type="submit" value="Add" />
 </form></li>
 </ul>
+
+<form id="search">
+       <input type="search" name="q" value="" placeholder="search" /><button type="submit">🔍</button>
+</form>
 </section>
 <:
 }