459ccddc9d3aebfcaf8ee1022502aa9611d5d59c
[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 # end of main
48
49 sub display {
50   $::lxdebug->enter_sub;
51
52   my $callback  = $::form->unescape($::form->{callback});
53   $callback     = URI->new($callback)->rel($callback) if $callback;
54   $callback     = "login.pl?action=company_logo"      if $callback =~ /^(\.\/)?$/;
55   my $framesize = _calc_framesize();
56
57   $::form->header(doctype => 'frameset');
58
59   print qq|
60 <frameset rows="28px,*" cols="*" framespacing="0" frameborder="0">
61   <frame  src="controller.pl?action=FrameHeader/header" scrolling="NO">
62   <frameset cols="$framesize,*" framespacing="0" frameborder="0" border="0" id="menuframe" name="menuframe">
63     <frame src="$::form->{script}?action=acc_menu" name="acc_menu"  scrolling="auto" noresize marginwidth="0">
64     <frame src="$callback" name="main_window" scrolling="auto">
65   </frameset>
66   <noframes>
67   You need a browser that can read frames to see this page.
68   </noframes>
69 </frameset>
70 </HTML>
71 |;
72
73   $::lxdebug->leave_sub;
74 }
75
76 sub acc_menu {
77   $::lxdebug->enter_sub;
78
79   my $framesize    = _calc_framesize() - 2;
80   my $menu         = Menu->new("menu.ini");
81   $::form->{title} = $::locale->text('kivitendo');
82   $::form->header;
83
84   my $sections = [ section_menu($menu) ];
85
86   print $::form->parse_html_template('menu/menu', {
87     framesize => $framesize,
88     sections  => $sections,
89   });
90
91   $::lxdebug->leave_sub;
92 }
93
94 sub section_menu {
95   $::lxdebug->enter_sub;
96   my ($menu, $level, $id_prefix) = @_;
97   my @menuorder = $menu->access_control(\%::myconfig, $level);
98   my @items;
99
100   my $id = 0;
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 $spacer     = "s" . (0 + $item =~ s/--/--/g);
107     my $label_icon = $level . "--" . $label . ".png";
108
109     next if $level && $item ne "$level--$label";
110
111     $label         = $::locale->text($label);
112
113     $menuitem->{module} ||= $::form->{script};
114     $menuitem->{action} ||= "section_menu";
115     $menuitem->{target} ||= "main_window";
116     $menuitem->{href}   ||= "$menuitem->{module}?action=$menuitem->{action}";
117
118     # add other params
119     foreach my $key (keys %$menuitem) {
120       next if $key =~ /target|module|action|href/;
121       $menuitem->{href} .= "&" . $::form->escape($key, 1) . "=";
122       my ($value, $conf) = split(/=/, $menuitem->{$key}, 2);
123       $value = $::myconfig{$value} . "/$conf" if ($conf);
124       $menuitem->{href} .= $::form->escape($value, 1);
125     }
126
127     my $anchor = $menuitem->{href};
128
129     my %common_args = (
130         label   => $label,
131         spacer  => $spacer,
132         target  => $menuitem->{target},
133         item_id => "$id_prefix\_$id",
134         height  => 16,
135     );
136
137     if (!$level) { # toplevel
138       push @items, { %common_args,
139         img      =>  make_image(icon => $item . '.png', size => 24, label => $label),
140         height   => 24,
141         class    => 'm',
142       };
143       push @items, section_menu($menu, $item, "$id_prefix\_$id");
144     } elsif ($menuitem->{submenu}) {
145       push @items, { %common_args,
146         img      => make_image(submenu => 1),
147         class    => 'sm',
148       };
149       push @items, section_menu($menu, $item, "$id_prefix\_$id");
150     } elsif ($menuitem->{module}) {
151       push @items, { %common_args,
152         img     => make_image(label => $label, icon => $label_icon),
153         href    => $anchor,
154         class   => 'i',
155       };
156     }
157   } continue {
158     $id++;
159   }
160
161   $::lxdebug->leave_sub;
162   return @items;
163 }
164
165 sub make_image {
166   my (%params) = @_;
167
168   my $icon   = $params{icon};
169   my $size   = $params{size}   || 16;
170
171   return unless _show_images();
172
173   my $icon_found = $icon && -f _icon_path($icon, $size);
174
175   return  {
176     src     => $icon_found ? _icon_path($icon, $size) : "image/unterpunkt.png",
177     alt     => $params{label},
178     width   => $icon_found ? $size : 24,
179     height  => $icon_found ? $size : 15,
180   }
181 }
182
183 sub _calc_framesize {
184   my $is_lynx_browser   = $ENV{HTTP_USER_AGENT} =~ /links/i;
185   my $is_mobile_browser = $ENV{HTTP_USER_AGENT} =~ /mobile/i;
186   my $is_mobile_style   = $::form->{stylesheet} =~ /mobile/i;
187
188   return  $is_mobile_browser && $is_mobile_style ?  130
189         : $is_lynx_browser                       ?  240
190         :                                           200;
191 }
192
193 sub _show_images {
194   # don't show images in links
195   _calc_framesize() != 240;
196 }
197
198 sub _icon_path {
199   my ($label, $size) = @_;
200
201   $size ||= 16;
202
203   return "image/icons/${size}x${size}/$label";
204 }
205
206 1;
207
208 __END__