X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=inline;f=SL%2FMenu.pm;h=08727ce3664c58941bc58fddef04e53860bf2817;hb=ee56f1b20ab915671ded297f593c0e2e245bad70;hp=ab35fed1a48bfc108ece19b371c6be7ba34186c2;hpb=87ad1737166b8eb5ec26ec3ab27f686bdf4d3679;p=kivitendo-erp.git
diff --git a/SL/Menu.pm b/SL/Menu.pm
index ab35fed1a..08727ce36 100644
--- a/SL/Menu.pm
+++ b/SL/Menu.pm
@@ -1,318 +1,171 @@
-#=====================================================================
-# LX-Office ERP
-# Copyright (C) 2004
-# Based on SQL-Ledger Version 2.1.9
-# Web http://www.lx-office.org
-#
-#=====================================================================
-# SQL-Ledger Accounting
-# Copyright (C) 2001
-#
-# Author: Dieter Simader
-# Email: dsimader@sql-ledger.org
-# Web: http://www.sql-ledger.org
-#
-# Contributors:
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-#=====================================================================
-#
-# routines for menu items
-#
-#=====================================================================
-
-package Menu;
-
-use SL::Auth;
-use SL::Inifile;
+package SL::Menu;
use strict;
-sub new {
- $main::lxdebug->enter_sub();
-
- my ($type, $menufile) = @_;
-
- my $self = {};
- my $inifile = Inifile->new($menufile);
-
- map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
-
- bless $self, $type;
-
- $self->set_access();
-
- $main::lxdebug->leave_sub();
+use SL::Auth;
+use YAML ();
+use File::Spec;
+use SL::MoreCommon qw(uri_encode);
- return $self;
+our $yaml_xs;
+BEGIN {
+ $yaml_xs = eval { require YAML::XS };
}
-sub menuitem {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $item) = @_;
-
- my $module = $self->{$item}{module} || $form->{script};
- my $action = $self->{$item}{action} || "section_menu";
- my $target = $self->{$item}{target} || "";
+our %menu_cache;
- my $level = $form->escape($item);
-
- my $style = 'style="vertical-align:top"';
- my $target_token = ($target)
- ? "target='$target'" : '';
+sub new {
+ my ($package, $domain) = @_;
+
+ if (!$menu_cache{$domain}) {
+ my $path = File::Spec->catdir('menus', $domain);
+
+ opendir my $dir, $path or die "can't open $path: $!";
+ my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir;
+ close $dir;
+
+ my $nodes = [];
+ my $nodes_by_id = {};
+ for my $file (@files) {
+ my $data;
+ 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);
+ }
- my $href = ($self->{$item}{href})
- ? $form->escape($self->{$item}{href})
- : "$module?action=$action&level=$level";
- my @vars = ($self->{$item}{href})
- ? qw(module target href)
- : qw(module action target href);
+ my $self = bless {
+ nodes => $nodes,
+ by_id => $nodes_by_id,
+ }, $package;
-# map { delete $self->{$item}{$_} } @vars;
+ $self->build_tree;
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $href .= "&" . $form->escape($key, 1) . "=";
- my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $href .= $form->escape($value, 1);
+ $menu_cache{$domain} = $self;
+ } else {
+ $menu_cache{$domain}->clear_access;
}
- my $str = "";
-
- $main::lxdebug->leave_sub();
+ $menu_cache{$domain}->set_access;
- return $str;
+ return $menu_cache{$domain};
}
-sub menuitem_js {
- my ($self, $myconfig, $form, $item) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
+sub _merge {
+ my ($nodes, $by_id, $data) = @_;
- #if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
+ for my $node (@$data) {
+ my $id = $node->{id};
- #}
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
-
- my $level = $form->escape($item);
- my $str = qq|$module?action=$action&level=$level|;
- my @vars = qw(module action target href);
-
- if ($self->{$item}{href}) {
- $str = qq|$self->{$item}{href}|;
- @vars = qw(module target href);
- }
+ my $merge_to = $by_id->{$id};
- map { delete $self->{$item}{$_} } @vars;
+ if (!$merge_to) {
+ push @$nodes, $node;
+ $by_id->{$id} = $node;
+ next;
+ }
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $str .= "&" . $form->escape($key, 1) . "=";
- my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
+ # TODO make this a real recursive merge
+ # TODO add support for arrays
+
+ # merge keys except params
+ for my $key (keys %$node) {
+ if (ref $node->{$key}) {
+ if ('HASH' eq ref $node->{$key}) {
+ $merge_to->{$key} = {} if !exists $merge_to->{$key} || 'HASH' ne ref $merge_to->{$key};
+ for (keys %{ $node->{params} }) {
+ $merge_to->{$key}{$_} = $node->{params}{$_};
+ }
+ } else {
+ die "unsupported structure @{[ ref $node->{$key} ]}";
+ }
+ } else {
+ $merge_to->{$key} = $node->{$key};
+ }
+ }
}
-
- $str .= " ";
-
}
-sub menuitem_new {
- $main::lxdebug->enter_sub();
-
- my ($self, $name, $item) = @_;
-
- my $form = $main::form;
- my $myconfig = \%main::myconfig;
-
- my $module = $self->{$name}->{module} || $form->{script};
- my $action = $self->{$name}->{action};
+sub build_tree {
+ my ($self) = @_;
- $item->{target} = $self->{$name}->{target} || "main_window";
- $item->{href} = $self->{$name}->{href} || "${module}?action=" . $form->escape($action);
-
- my @vars = qw(module target href);
- push @vars, 'action' unless ($self->{$name}->{href});
-
- map { delete $self->{$name}{$_} } @vars;
-
- # add other params
- foreach my $key (keys %{ $self->{$name} }) {
- my ($value, $conf) = split(m/=/, $self->{$name}->{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $item->{href} .= "&" . $form->escape($key) . "=" . $form->escape($value);
+ # first, some sanity check. are all parents valid ids or empty?
+ for my $node ($self->nodes) {
+ next if !exists $node->{parent} || !$node->{parent} || $self->{by_id}->{$node->{id}};
+ die "menu: node $node->{id} has non-existent parent $node->{parent}";
}
- $main::lxdebug->leave_sub();
-}
-
-sub menuitem_v3 {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $item, $other) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
-
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
- }
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
+ my %by_parent;
+ # order them by parent
+ for my $node ($self->nodes) {
+ push @{ $by_parent{ $node->{parent} // '' } //= [] }, $node;
}
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
- }
-
- my $level = $form->escape($item);
-
- my $str = qq|escape($key, 1) . "=";
- my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
+ for (keys %by_parent) {
+ my $parent = $self->{by_id}{$_};
+ $parent->{children} = [ sort { $a->{order} <=> $b->{order} } @{ $by_parent{$_} } ];
}
- $str .= '"';
-
- if ($target) {
- $str .= qq| target="| . $form->quote($target) . qq|"|;
- }
+ _set_level_rec($tree->{children}, 0);
- if ($other) {
- foreach my $key (keys(%{$other})) {
- $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
- }
- }
-
- $str .= ">";
-
- $main::lxdebug->leave_sub();
-
- return $str;
+ $self->{tree} = $tree->{children};
}
-sub menuitem_XML {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $item, $other) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
-
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
- }
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
- }
-
- my $level = $form->escape($item);
-
- my $str = qq| link="$module?action=| . $form->escape($action) .
- qq|&level=| . $form->escape($level);
-
- my @vars = qw(module action target href);
+sub _set_level_rec {
+ my ($ary_ref, $level) = @_;
- if ($self->{$item}{href}) {
- $str = qq| link=$self->{$item}{href}|;
- @vars = qw(module target href);
+ for (@$ary_ref) {
+ $_->{level} = $level;
+ _set_level_rec($_->{children}, $level + 1) if $_->{children};
}
-
- map { delete $self->{$item}{$_} } @vars;
-
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $str .= "&" . $form->escape($key, 1) . "=";
- my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
- }
-
- $str .= '"';
-
-
-
- if ($other) {
- foreach my $key (keys(%{$other})) {
- $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
- }
- }
-
-
- $main::lxdebug->leave_sub();
-
- return $str;
}
-sub access_control {
- $main::lxdebug->enter_sub(2);
-
- my ($self, $myconfig, $menulevel) = @_;
-
- my @menu = ();
+sub nodes {
+ @{ $_[0]{nodes} }
+}
- if ($menulevel eq "") {
- @menu = grep { !/--/ } @{ $self->{ORDER} };
- } else {
- @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
- }
+sub tree_walk {
+ my ($self, $all) = @_;
- $main::lxdebug->leave_sub(2);
+ _tree_walk_rec($self->{tree}, $all);
+}
- return @menu;
+sub _tree_walk_rec {
+ my ($ary_ref, $all) = @_;
+ map { $_->{children} ? ($_, _tree_walk_rec($_->{children}, $all)) : ($_) } grep { $all || $_->{visible} } @$ary_ref;
}
sub parse_access_string {
- my $self = shift;
- my $key = shift;
- my $access = shift;
-
- my $form = $main::form;
- my $auth = $main::auth;
- my $myconfig = \%main::myconfig;
+ my ($self, $node) = @_;
my @stack;
my $cur_ary = [];
push @stack, $cur_ary;
- while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
+ my $access = $node->{access};
+
+ while ($access =~ m/^([a-z_\/]+|\||\&|\(|\)|\s+)/) {
my $token = $1;
substr($access, 0, length($1)) = "";
@@ -327,7 +180,7 @@ sub parse_access_string {
} elsif ($token eq ")") {
pop @stack;
if (!@stack) {
- $form->error("Error in menu.ini for entry ${key}: missing '('");
+ die "Error while parsing menu entry $node->{id}: missing '('";
}
$cur_ary = $stack[-1];
@@ -335,71 +188,70 @@ sub parse_access_string {
push @{$cur_ary}, $token;
} else {
- push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
+ if ($token =~ m{^ client / (.*) }x) {
+ push @{$cur_ary}, $self->parse_instance_conf_string($1);
+ } else {
+ push @{$cur_ary}, $::auth->check_right($::myconfig{login}, $token, 1);
+ }
}
}
if ($access) {
- $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
+ die "Error while parsing menu entry $node->{id}: unrecognized token at the start of '$access'\n";
}
if (1 < scalar @stack) {
- $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
+ die "Error while parsing menu entry $node->{id}: Missing ')'\n";
}
return SL::Auth::evaluate_rights_ary($stack[0]);
}
-sub set_access {
- my $self = shift;
-
- my $key;
+sub href_for_node {
+ my ($self, $node) = @_;
- foreach $key (@{ $self->{ORDER} }) {
- my $entry = $self->{$key};
+ return undef if !$node->{href} && !$node->{module} && !$node->{params};
- $entry->{GRANTED} = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
- $entry->{IS_MENU} = $entry->{submenu} || ($key !~ m/--/);
- $entry->{NUM_VISIBLE_CHILDREN} = 0;
+ my $href = $node->{href} || $node->{module} || 'controller.pl';
+ my @tokens;
- if ($key =~ m/--/) {
- my $parent = $key;
- substr($parent, rindex($parent, '--')) = '';
- $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
- }
-
- $entry->{VISIBLE} = $entry->{GRANTED};
+ while (my ($key, $value) = each %{ $node->{params} }) {
+ push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1);
}
- foreach $key (reverse @{ $self->{ORDER} }) {
- my $entry = $self->{$key};
-
- if ($entry->{IS_MENU}) {
- $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
- }
-
- next if (($key !~ m/--/) || !$entry->{VISIBLE});
-
- my $parent = $key;
- substr($parent, rindex($parent, '--')) = '';
- $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
- }
+ return join '?', $href, grep $_, join '&', @tokens;
+}
-# $self->dump_visible();
+sub name_for_node {
+ $::locale->text($_[1]{name})
+}
- $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
+sub parse_instance_conf_string {
+ my ($self, $setting) = @_;
+ return $::instance_conf->data->{$setting};
+}
- { no strict 'refs';
- # ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
- map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
+sub clear_access {
+ my ($self) = @_;
+ for my $node ($self->tree_walk("all")) {
+ delete $node->{visible};
+ delete $node->{visible_children};
}
}
-sub dump_visible {
- my $self = shift;
- foreach my $key (@{ $self->{ORDER} }) {
- my $entry = $self->{$key};
- $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
+sub set_access {
+ my ($self) = @_;
+ # 1. evaluate access for all
+ # 2. if a menu has no visible children, its not visible either
+
+ for my $node (reverse $self->tree_walk("all")) {
+ $node->{visible} = $node->{access} ? $self->parse_access_string($node)
+ : !$node->{children} ? 1
+ : $node->{visible_children} ? 1
+ : 0;
+ if ($node->{visible} && $node->{parent}) {
+ $self->{by_id}{ $node->{parent} }{visible_children} = 1;
+ }
}
}