1b818d1bf06d5accc5f1a8d5b7893884bd9bfeae
[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->{target} ||= "main_window";
117
118     my $anchor     = $menu->menuitem(\%::myconfig, $::form, $item, $level);
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 = "<a href='menu.pl?action=acc_menu&level=$ml_' class='nohover' title='$label'>";
124
125       push @items, make_item(a => $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(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, a => $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(img => $image, a => $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 make_item {
147   my %params = @_;
148   $params{a}      ||= '';
149   $params{spacer} ||= '';
150   $params{height} ||= 16;
151
152   return {
153     %params,
154     chunks => [ multiline($params{label}) ],
155   };
156 }
157
158 # multi line hack, sschoeling jul06
159 # if a label is too long, try to split it at whitespaces, then join it to chunks of less
160 # than 20 chars and store it in an array.
161 # use this array later instead of the &nbsp;-ed label
162 sub multiline {
163   my ($label) = @_;
164   my @chunks;
165   my $l = 20;
166   for (split / /, $label) {
167     $l += length $_;
168     if ($l < 20) {
169       $chunks[-1] .= " $_";
170     } else {
171       $l = length $_;
172       push @chunks, $_;
173     }
174   }
175   return @chunks;
176 }
177
178 sub make_image {
179   my (%params) = @_;
180
181   my $label  = $params{label};
182   my $icon   = $params{icon};
183   my $size   = $params{size}   || 16;
184
185   return unless _show_images();
186
187   my $icon_found = $icon && -f _icon_path($icon, $size);
188
189   my $image_url = $icon_found ? _icon_path($icon, $size) : "image/unterpunkt.png";
190   my $width     = $icon_found ? $size : 24;
191
192   my $padding   = $size == 16 && $icon_found ? $nbsp x 2
193                 : $size == 24                ? $nbsp
194                 :                            '';
195
196   return "<img src='$image_url' title='$label' width='$width' height='$size'>$padding";
197 }
198
199 sub _calc_framesize {
200   my $is_lynx_browser   = $ENV{HTTP_USER_AGENT} =~ /links/i;
201   my $is_mobile_browser = $ENV{HTTP_USER_AGENT} =~ /mobile/i;
202   my $is_mobile_style   = $::form->{stylesheet} =~ /mobile/i;
203
204   return  $is_mobile_browser && $is_mobile_style ?  130
205         : $is_lynx_browser                       ?  240
206         :                                           200;
207 }
208
209 sub _show_images {
210   # don't show images in links
211   _calc_framesize() != 240;
212 }
213
214 sub _icon_path {
215   my ($label, $size) = @_;
216
217   $size ||= 16;
218
219   return "image/icons/${size}x${size}/$label";
220 }
221
222 1;
223
224 __END__