From dd92616a516d41f9a84d080891f18d5a58754060 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Mon, 15 Jun 2020 23:55:13 +0200 Subject: [PATCH] word edit: null priority to inherit from parent --- tools/word.pg.sql | 3 ++- writer.plp | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/word.pg.sql b/tools/word.pg.sql index d18a99c..3a0f158 100644 --- a/tools/word.pg.sql +++ b/tools/word.pg.sql @@ -13,7 +13,8 @@ CREATE TABLE word ( 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, diff --git a/writer.plp b/writer.plp index 3c0b248..a97bbfa 100644 --- a/writer.plp +++ b/writer.plp @@ -178,9 +178,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'}, @@ -250,6 +255,7 @@ elsif (defined $post{form}) {{ ref => $row->{id}, lang => $lang, form => $val, + prio => undef, ); $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand $db->insert(word => \%subrow); @@ -322,9 +328,9 @@ 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'; @@ -339,8 +345,7 @@ package Shiar_Sheet::FormRow { $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('