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