word edit: null priority to inherit from parent
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 15 Jun 2020 21:55:13 +0000 (23:55 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 20 Oct 2020 20:49:11 +0000 (22:49 +0200)
tools/word.pg.sql
writer.plp

index d18a99cb4b3172f464f5302832957e910ba9a678..3a0f158b5124f6dcd2d26590c61955ca4224471d 100644 (file)
@@ -13,7 +13,8 @@ CREATE TABLE word (
        lang       text        NOT NULL DEFAULT 'en',
        cat        integer              REFERENCES word (id),
        ref        integer              REFERENCES word (id),
        lang       text        NOT NULL DEFAULT 'en',
        cat        integer              REFERENCES word (id),
        ref        integer              REFERENCES word (id),
-       prio       smallint    NOT NULL DEFAULT '1',
+       prio       smallint             DEFAULT '1'
+                                       CHECK (prio >= 0 OR ref IS NOT NULL),
        grade      integer,
        cover      boolean     NOT NULL DEFAULT FALSE,
        source     text,
        grade      integer,
        cover      boolean     NOT NULL DEFAULT FALSE,
        source     text,
index 3c0b248bf9d859160269261912e745fa39bb16ad..a97bbfae4ec4d60e74c6a32750d311f57e72e9e8 100644 (file)
@@ -178,9 +178,14 @@ my %wordcol = (
        cat     => [{-label => 'Category'}, 'ref'],
        ref     => {-label => 'Reference'},
        prio    => [
        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'},
                'cover', 'grade',
        ],
        cover   => {-label => 'Highlighted', type => 'checkbox'},
@@ -250,6 +255,7 @@ elsif (defined $post{form}) {{
                                ref   => $row->{id},
                                lang  => $lang,
                                form  => $val,
                                ref   => $row->{id},
                                lang  => $lang,
                                form  => $val,
+                               prio  => undef,
                        );
                        $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
                        $db->insert(word => \%subrow);
                        );
                        $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
                        $db->insert(word => \%subrow);
@@ -322,9 +328,9 @@ elsif (defined $post{form}) {{
        }
 }}
 else {
        }
 }}
 else {
-       $row->{prio} //= 1;
        $row->{lang} //= $user->{editlang}->[0];
        $row->{$_} = $get{$_} for keys %get;
        $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 <small>#$row->{id}</small>" : 'new entry';
 }
 
 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
@@ -339,8 +345,7 @@ package Shiar_Sheet::FormRow {
                $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
 
                if (my $options = $attr->{-select}) {
                $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
 
                if (my $options = $attr->{-select}) {
-                       $options = { map {$_ => $options->[$_]} 0 .. $#{$options} }
-                               if ref $options eq 'ARRAY';
+                       $options = $options->(@_) if ref $options eq 'CODE';
                        $options->{$val} //= "unknown ($val)";  # preserve current
                        return (
                                sprintf('<select id="%s" name="%1$s">', $col),
                        $options->{$val} //= "unknown ($val)";  # preserve current
                        return (
                                sprintf('<select id="%s" name="%1$s">', $col),