img -> css scaling, muss aber noch gefixt werden
[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 URI;
44
45 use List::MoreUtils qw(apply);
46
47 my $nbsp     = '&nbsp;';
48 my $mainlevel;
49
50 # end of main
51
52 sub display {
53   $::lxdebug->enter_sub;
54
55   my $callback  = $::form->unescape($::form->{callback});
56   $callback     = URI->new($callback)->rel($callback) if $callback;
57   $callback     = "login.pl?action=company_logo"      if $callback =~ /^(\.\/)?$/;
58   my $framesize = _calc_framesize();
59
60   $::form->header(doctype => 'frameset');
61
62   print qq|
63 <frameset rows="28px,*" cols="*" framespacing="0" frameborder="0">
64   <frame  src="controller.pl?action=FrameHeader/header" scrolling="NO">
65   <frameset cols="$framesize,*" framespacing="0" frameborder="0" border="0" id="menuframe" name="menuframe">
66     <frame src="$::form->{script}?action=acc_menu" name="acc_menu"  scrolling="auto" noresize marginwidth="0">
67     <frame src="$callback" name="main_window" scrolling="auto">
68   </frameset>
69   <noframes>
70   You need a browser that can read frames to see this page.
71   </noframes>
72 </frameset>
73 </HTML>
74 |;
75
76   $::lxdebug->leave_sub;
77 }
78
79 sub acc_menu {
80   $::lxdebug->enter_sub;
81
82   my $framesize    = _calc_framesize() - 2;
83   my $menu         = Menu->new("menu.ini");
84   $mainlevel       = $::form->{level};
85   $::form->{title} = $::locale->text('kivitendo');
86   $::form->header;
87
88   print $::form->parse_html_template('menu/menu', {
89     framesize => $framesize,
90     sections  => [ section_menu($menu) ],
91   });
92
93   $::lxdebug->leave_sub;
94 }
95
96 sub section_menu {
97   $::lxdebug->enter_sub;
98   my ($menu, $level) = @_;
99   my @menuorder = $menu->access_control(\%::myconfig, $level);
100   my @items;
101
102   for my $item (@menuorder) {
103     my $menuitem   = $menu->{$item};
104     my $label      = apply { s/.*--// } $item;
105     my $ml         = apply { s/--.*// } $item;
106     my $show       = $ml eq $mainlevel;
107     my $spacer     = $nbsp x (($item =~ s/--/--/g) * 2);
108     my $label_icon = $level . "--" . $label . ".png";
109
110     next if $level && $item ne "$level--$label";
111
112     $label         = $::locale->text($label);
113
114     $menuitem->{module} ||= $::form->{script};
115     $menuitem->{action} ||= "section_menu";
116     $menuitem->{target} ||= "main_window";
117     $menuitem->{href}   ||= "$menuitem->{module}?action=$menuitem->{action}";
118
119     # add other params
120     foreach my $key (keys %$menuitem) {
121       next if $key =~ /target|module|action|href/;
122       $menuitem->{href} .= "&" . $::form->escape($key, 1) . "=";
123       my ($value, $conf) = split(/=/, $menuitem->{$key}, 2);
124       $value = $::myconfig{$value} . "/$conf" if ($conf);
125       $menuitem->{href} .= $::form->escape($value, 1);
126     }
127
128     my $anchor = $menuitem->{href};
129
130     if (!$level) { # toplevel
131       my $ml_    = $::form->escape($ml);
132       my $image  = make_image(icon => $item . '.png', size => 24, label => $label);
133       my $anchor = "menu.pl?action=acc_menu&level=$ml_";
134
135       push @items, make_item(href => $anchor, img => $image, label => $label, height => 24, class => 'menu');
136       push @items, section_menu($menu, $item);
137
138     } elsif ($menuitem->{submenu}) {
139       my $image = make_image(submenu => 1);
140       if ($mainlevel && $item =~ /^\Q$mainlevel\E/) {
141         push @items, make_item(target => $menuitem->{target}, spacer => $spacer, img => $image, label => $label, class => 'submenu') if $show;
142         push @items, section_menu($menu, $item);
143       } else {
144         push @items, make_item(spacer => $spacer, href => $anchor, img => $image, label => $label . '&nbsp;...', class => 'submenu') if $show;
145       }
146     } elsif ($menuitem->{module}) {
147       my $image = make_image(label => $label, icon => $label_icon);
148       push @items, make_item(target => $menuitem->{target}, img => $image, href => $anchor, spacer => $spacer, label => $label, class => 'item') if $show;
149       push @items, section_menu($menu, $item) if $show && $::form->{$item} && $::form->{level} eq $item;
150     }
151   }
152   $::lxdebug->leave_sub;
153   return @items;
154 }
155
156 sub make_item {
157   my %params = @_;
158   $params{a}      ||= '';
159   $params{spacer} ||= '';
160   $params{height} ||= 16;
161
162   return {
163     %params,
164     chunks => [ multiline($params{label}) ],
165   };
166 }
167
168 # multi line hack, sschoeling jul06
169 # if a label is too long, try to split it at whitespaces, then join it to chunks of less
170 # than 20 chars and store it in an array.
171 # use this array later instead of the &nbsp;-ed label
172 sub multiline {
173   my ($label) = @_;
174   my @chunks;
175   my $l = 20;
176   for (split / /, $label) {
177     $l += length $_;
178     if ($l < 20) {
179       $chunks[-1] .= " $_";
180     } else {
181       $l = length $_;
182       push @chunks, $_;
183     }
184   }
185   return @chunks;
186 }
187
188 sub make_image {
189   my (%params) = @_;
190
191   my $icon   = $params{icon};
192   my $size   = $params{size}   || 16;
193
194   return unless _show_images();
195
196   my $icon_found = $icon && -f _icon_path($icon, $size);
197   my $padding    = $size == 16 && $icon_found ? $nbsp x 2
198                  : $size == 24                ? $nbsp
199                  :                            '';
200
201   return  {
202     src     => $icon_found ? _icon_path($icon, $size) : "image/unterpunkt.png",
203     alt     => $params{label},
204     width   => $icon_found ? $size : 24,
205     height  => $size,
206     padding => $padding,
207   }
208 }
209
210 sub _calc_framesize {
211   my $is_lynx_browser   = $ENV{HTTP_USER_AGENT} =~ /links/i;
212   my $is_mobile_browser = $ENV{HTTP_USER_AGENT} =~ /mobile/i;
213   my $is_mobile_style   = $::form->{stylesheet} =~ /mobile/i;
214
215   return  $is_mobile_browser && $is_mobile_style ?  130
216         : $is_lynx_browser                       ?  240
217         :                                           200;
218 }
219
220 sub _show_images {
221   # don't show images in links
222   _calc_framesize() != 240;
223 }
224
225 sub _icon_path {
226   my ($label, $size) = @_;
227
228   $size ||= 16;
229
230   return "image/icons/${size}x${size}/$label";
231 }
232
233 1;
234
235 __END__