menuitem nach menu.pl verlagert. links in template generiert
[kivitendo-erp.git] / bin / mozilla / menu.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 ######################################################################
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Christopher Browne
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #######################################################################
30 #
31 # the frame layout with refractured menu
32 #
33 # CHANGE LOG:
34 #   DS. 2002-03-25  Created
35 #  2004-12-14 - New Optik - Marco Welter <mawe@linux-studio.de>
36 #  2010-08-19 - Icons for sub entries and single click behavior, unlike XUL-Menu
37 #               JS switchable HTML-menu - Sven Donath <lxo@dexo.de>
38 #######################################################################
39
40 use strict;
41
42 use SL::Menu;
43 use Data::Dumper;
44 use URI;
45
46 use List::MoreUtils qw(apply);
47
48 my $menufile = "menu.ini";
49 my $nbsp     = '&nbsp;';
50 my $mainlevel;
51
52 # end of main
53
54 sub display {
55   $::lxdebug->enter_sub;
56
57   my $callback  = $::form->unescape($::form->{callback});
58   $callback     = URI->new($callback)->rel($callback) if $callback;
59   $callback     = "login.pl?action=company_logo"      if $callback =~ /^(\.\/)?$/;
60   my $framesize = _calc_framesize();
61
62   $::form->header(doctype => 'frameset');
63
64   print qq|
65 <frameset rows="28px,*" cols="*" framespacing="0" frameborder="0">
66   <frame  src="controller.pl?action=FrameHeader/header" scrolling="NO">
67   <frameset cols="$framesize,*" framespacing="0" frameborder="0" border="0" id="menuframe" name="menuframe">
68     <frame src="$::form->{script}?action=acc_menu" name="acc_menu"  scrolling="auto" noresize marginwidth="0">
69     <frame src="$callback" name="main_window" scrolling="auto">
70   </frameset>
71   <noframes>
72   You need a browser that can read frames to see this page.
73   </noframes>
74 </frameset>
75 </HTML>
76 |;
77
78   $::lxdebug->leave_sub;
79 }
80
81 sub acc_menu {
82   $::lxdebug->enter_sub;
83
84   my $framesize    = _calc_framesize() - 2;
85   my $menu         = Menu->new("menu.ini");
86   $mainlevel       = $::form->{level};
87   $::form->{title} = $::locale->text('kivitendo');
88   $::form->header;
89
90   print $::form->parse_html_template('menu/menu', {
91     framesize => $framesize,
92     sections  => [ section_menu($menu) ],
93   });
94
95   $::lxdebug->leave_sub;
96 }
97
98 sub section_menu {
99   $::lxdebug->enter_sub;
100   my ($menu, $level) = @_;
101   my @menuorder = $menu->access_control(\%::myconfig, $level);
102   my @items;
103
104   for my $item (@menuorder) {
105     my $menuitem   = $menu->{$item};
106     my $label      = apply { s/.*--// } $item;
107     my $ml         = apply { s/--.*// } $item;
108     my $show       = $ml eq $mainlevel;
109     my $spacer     = $nbsp x (($item =~ s/--/--/g) * 2);
110     my $label_icon = $level . "--" . $label . ".png";
111
112     next if $level && $item ne "$level--$label";
113
114     $label         = $::locale->text($label);
115
116     menuitem($menuitem);
117
118     my $anchor = $menuitem->{href};
119
120     if (!$level) { # toplevel
121       my $ml_    = $::form->escape($ml);
122       my $image  = make_image(icon => $item . '.png', size => 24, label => $label);
123       my $anchor = "menu.pl?action=acc_menu&level=$ml_";
124
125       push @items, make_item(href => $anchor, img => $image, label => $label, height => 24, class => 'menu');
126       push @items, section_menu($menu, $item);
127
128     } elsif ($menuitem->{submenu}) {
129       my $image = make_image(submenu => 1);
130       if ($mainlevel && $item =~ /^\Q$mainlevel\E/) {
131         push @items, make_item(target => $menuitem->{target}, spacer => $spacer, bold => 1, img => $image, label => $label, class => 'submenu') if $show;
132         push @items, section_menu($menu, $item);
133       } else {
134         push @items, make_item(spacer => $spacer, href => $anchor, img => $image, label => $label . '&nbsp;...', class => 'submenu') if $show;
135       }
136     } elsif ($menuitem->{module}) {
137       my $image = make_image(label => $label, icon => $label_icon);
138       push @items, make_item(target => $menuitem->{target}, img => $image, href => $anchor, spacer => $spacer, label => $label, class => 'item') if $show;
139       push @items, section_menu($menu, $item) if $show && $::form->{$item} && $::form->{level} eq $item;
140     }
141   }
142   $::lxdebug->leave_sub;
143   return @items;
144 }
145
146 sub menuitem {
147   my ($item, $name) = @_;
148
149   $item->{module} ||= $::form->{script};
150   $item->{action} ||= "section_menu";
151   $item->{target} ||= "main_window";
152   $item->{href}   ||= "$item->{module}?action=$item->{action}";
153
154   # add other params
155   foreach my $key (keys %$item) {
156     next if $key =~ /target|module|action|href/;
157     $item->{href} .= "&" . $::form->escape($key, 1) . "=";
158     my ($value, $conf) = split(/=/, $item->{$key}, 2);
159     $value = $::myconfig{$value} . "/$conf" if ($conf);
160     $item->{href} .= $::form->escape($value, 1);
161   }
162
163 #  my $str = "<a href='$href' $target_token>";
164 #
165 #  return $str;
166 }
167
168 sub make_item {
169   my %params = @_;
170   $params{a}      ||= '';
171   $params{spacer} ||= '';
172   $params{height} ||= 16;
173
174   return {
175     %params,
176     chunks => [ multiline($params{label}) ],
177   };
178 }
179
180 # multi line hack, sschoeling jul06
181 # if a label is too long, try to split it at whitespaces, then join it to chunks of less
182 # than 20 chars and store it in an array.
183 # use this array later instead of the &nbsp;-ed label
184 sub multiline {
185   my ($label) = @_;
186   my @chunks;
187   my $l = 20;
188   for (split / /, $label) {
189     $l += length $_;
190     if ($l < 20) {
191       $chunks[-1] .= " $_";
192     } else {
193       $l = length $_;
194       push @chunks, $_;
195     }
196   }
197   return @chunks;
198 }
199
200 sub make_image {
201   my (%params) = @_;
202
203   my $label  = $params{label};
204   my $icon   = $params{icon};
205   my $size   = $params{size}   || 16;
206
207   return unless _show_images();
208
209   my $icon_found = $icon && -f _icon_path($icon, $size);
210
211   my $image_url = $icon_found ? _icon_path($icon, $size) : "image/unterpunkt.png";
212   my $width     = $icon_found ? $size : 24;
213
214   my $padding   = $size == 16 && $icon_found ? $nbsp x 2
215                 : $size == 24                ? $nbsp
216                 :                            '';
217
218   return "<img src='$image_url' title='$label' width='$width' height='$size'>$padding";
219 }
220
221 sub _calc_framesize {
222   my $is_lynx_browser   = $ENV{HTTP_USER_AGENT} =~ /links/i;
223   my $is_mobile_browser = $ENV{HTTP_USER_AGENT} =~ /mobile/i;
224   my $is_mobile_style   = $::form->{stylesheet} =~ /mobile/i;
225
226   return  $is_mobile_browser && $is_mobile_style ?  130
227         : $is_lynx_browser                       ?  240
228         :                                           200;
229 }
230
231 sub _show_images {
232   # don't show images in links
233   _calc_framesize() != 240;
234 }
235
236 sub _icon_path {
237   my ($label, $size) = @_;
238
239   $size ||= 16;
240
241   return "image/icons/${size}x${size}/$label";
242 }
243
244 1;
245
246 __END__