12 $::lxdebug = LXDebug->new;
15 'menus/erp.ini' => 'menus/user/00-erp.yaml',
16 'menus/crm.ini' => 'menus/user/10-crm.yaml',
17 'menus/admin.ini' => 'menus/admin/00-admin.yaml',
20 my %known_arguments = (
23 INSTANCE_CONF => 'INSTANCE_CONF',
30 sub translate_to_yaml {
31 my ($menu_file, $new_file) = @_;
35 my $menu = Inifile->new($menu_file);
36 my @menu_items = map { +{ %{ $menu->{$_} }, ID => $_ } } @{ delete $menu->{ORDER} };
38 for my $item (@menu_items) {
40 my @tokens = split /--/, delete $item->{ID};
41 my $name = pop @tokens;
42 my $parent = join '_', map { lc $_ } @tokens;
43 my $id = join '_', grep $_, $parent, lc $name;
45 # move unknown arguments to param subhash
46 my @keys = keys %$item;
49 next if $known_arguments{$_};
50 $params{$_} = delete $item->{$_};
53 $item->{params} = \%params if keys %params;
56 for (keys %known_arguments) {
57 next unless exists $item->{$_};
58 my $val = delete $item->{$_};
59 $item->{ $known_arguments{$_} } = $val;
63 if ($item->{submenu}) {
64 delete $item->{submenu};
67 # sanitize INSTANCE_CONF
68 if ($item->{INSTANCE_CONF}) {
69 my $instance_conf = delete $item->{INSTANCE_CONF};
70 if ($item->{access}) {
71 if ($item->{access} =~ /\W/) {
72 $item->{access} = "client/$instance_conf & ( $item->{access} )";
74 $item->{access} = "client/$instance_conf & $item->{access}";
77 $item->{access} = "client/$instance_conf";
81 # make controller.pl implicit
82 if ($item->{module} && $item->{module} eq 'controller.pl') {
83 delete $item->{module};
88 $item->{id} =~ s/[^\w]+/_/g;
91 $item->{name} = $name;
95 $item->{parent} = $parent;
96 $item->{parent} =~ s/[^\w]+/_/g if $item->{parent};
100 $item->{order} = 100 * ++$counter{ $item->{parent} };
103 if ($menu_file =~ /crm/) {
104 $menu_items[0]{order} = 50; # crm first
107 open my $out_file, '>:utf8', $new_file or die $!;
108 print $out_file yaml_dump(\@menu_items);
113 # YAML dumps keys lexically sorted, which isn't what we want.
114 # we want this order:
128 # ...oh and we want action in params first
131 # 1. parent is what is used to anchor. one could argue that id should be
132 # first, but parent is easier for understanding structure.
133 # 2. after parent the logical structure is
135 # 2. stuff related to vidual presentation (name/icon)
136 # 3. stuff needed for logical presentaion (order/access)
137 # 4. stuff related to the action after clicking it
138 # 3. without parent and href (the second is pretty rare) the keys are nicely
139 # ascending in length, which is very easy to parse visually.
142 for my $node (@$ary_ref) {
144 for my $key (@order) {
145 next unless exists $node->{$key};
146 $yaml .= ($first++ ? ' ' : '- ') . $key . ": ";
147 if (!ref $node->{$key}) {
148 $yaml .= $node->{$key} . "\n";
151 for ('action', grep !/^action$/, keys %{ $node->{$key} }) {
152 next unless exists $node->{$key}{$_};
153 $yaml .= " $_: $node->{$key}{$_}\n";
163 while (my ($in, $out) = each(%menu_files)) {
164 translate_to_yaml($in, $out);