X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FMenu.pm;h=b91611242ff4c889791dcacaa72a8ab8bed47c67;hb=965a8ee142a6cd21c3056eacaa01a01818032c3e;hp=8cc9c7e06071f8a307ea14bd80549f79a15ba831;hpb=bf7f935de20f360843aa2a7b1e4563248766de8a;p=kivitendo-erp.git diff --git a/SL/Menu.pm b/SL/Menu.pm index 8cc9c7e06..b91611242 100644 --- a/SL/Menu.pm +++ b/SL/Menu.pm @@ -21,18 +21,31 @@ sub new { my $path = File::Spec->catdir('menus', $domain); opendir my $dir, $path or die "can't open $path: $!"; - my @files = sort grep -f "$path/$_", readdir $dir; + my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir; close $dir; my $nodes = []; my $nodes_by_id = {}; for my $file (@files) { my $data; - if ($yaml_xs) { - $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file)); - } else { - $data = YAML::LoadFile(File::Spec->catfile($path, $file)); - } + eval { + if ($yaml_xs) { + $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file)); + } else { + $data = YAML::LoadFile(File::Spec->catfile($path, $file)); + } + 1; + } or do { + die "Error while parsing $file: $@"; + }; + + # check if this file is internally consistent. + die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO get better diag to user + + # in particular duplicate ids tend to come up as a user error when editing the menu files + #my %uniq_ids; + #$uniq_ids{$_->{id}}++ && die "Error in $file: duplicate id $_->{id}" for @$data; + _merge($nodes, $nodes_by_id, $data); } @@ -57,8 +70,6 @@ sub new { sub _merge { my ($nodes, $by_id, $data) = @_; - die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO check this sooner, to get better diag to user - for my $node (@$data) { my $id = $node->{id}; @@ -103,7 +114,7 @@ sub build_tree { my %by_parent; # order them by parent for my $node ($self->nodes) { - push @{ $by_parent{ $node->{parent} } //= [] }, $node; + push @{ $by_parent{ $node->{parent} // '' } //= [] }, $node; } my $tree = { }; @@ -201,14 +212,16 @@ sub href_for_node { return undef if !$node->{href} && !$node->{module} && !$node->{params}; - my $href = $node->{href} || $node->{module} || 'controller.pl'; - my @tokens; + return $node->{href_for_node} ||= do { + my $href = $node->{href} || $node->{module} || 'controller.pl'; + my @tokens; - while (my ($key, $value) = each %{ $node->{params} }) { - push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1); - } + while (my ($key, $value) = each %{ $node->{params} }) { + push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1); + } - return join '?', $href, grep $_, join '&', @tokens; + join '?', $href, grep $_, join '&', @tokens; + } } sub name_for_node {