init_sub_layouts ist sauberer
[kivitendo-erp.git] / SL / Layout / Javascript.pm
1 package SL::Layout::Javascript;
2
3 use strict;
4 use parent qw(SL::Layout::Base);
5
6 use List::Util qw(max);
7 use URI;
8
9 sub init_sub_layouts {
10   [ SL::Layout::None->new ]
11 }
12
13 sub pre_content {
14   &display
15 }
16
17 sub start_content {
18   "<div id='content'>\n";
19 }
20
21 sub end_content {
22   "</div>\n";
23 }
24
25 sub stylesheets {
26   $_[0]->add_stylesheets(qw(
27     dhtmlsuite/menu-item.css
28     dhtmlsuite/menu-bar.css
29   ));
30   $_[0]->SUPER::stylesheets;
31 }
32
33 sub display {
34   my ($self) = @_;
35   my $form     = $main::form;
36
37   my $callback            = $form->unescape($form->{callback});
38   $callback               = URI->new($callback)->rel($callback) if $callback;
39   $callback               = "login.pl?action=company_logo"      if $callback =~ /^(\.\/)?$/;
40
41   $self->render("menu/menunew", { partial => 1, no_output => 1 },
42     force_ul_width  => 1,
43     date            => $self->clock_line,
44     menu_items      => $self->acc_menu,
45     callback        => $callback,
46   );
47 }
48
49 sub clock_line {
50   my $form     = $main::form;
51
52   my ($Sekunden, $Minuten,   $Stunden,   $Monatstag, $Monat,
53       $Jahr,     $Wochentag, $Jahrestag, $Sommerzeit)
54     = localtime(time);
55   $Monat     += 1;
56   $Jahrestag += 1;
57   $Monat     = $Monat < 10     ? $Monat     = "0" . $Monat     : $Monat;
58   $Monatstag = $Monatstag < 10 ? $Monatstag = "0" . $Monatstag : $Monatstag;
59   $Jahr += 1900;
60   my @Wochentage = ("Sonntag",    "Montag",  "Dienstag", "Mittwoch",
61                     "Donnerstag", "Freitag", "Samstag");
62   my @Monatsnamen = ("",       "Januar",    "Februar", "M&auml;rz",
63                      "April",  "Mai",       "Juni",    "Juli",
64                      "August", "September", "Oktober", "November",
65                      "Dezember");
66   return
67       $Wochentage[$Wochentag] . ", der "
68     . $Monatstag . "."
69     . $Monat . "."
70     . $Jahr . " - ";
71 }
72
73 sub acc_menu {
74   my ($self) = @_;
75
76   my $menu      = $self->menu;
77
78   my $all_items = [];
79   $self->create_menu($menu, $all_items);
80
81   my $item = { 'subitems' => $all_items };
82   calculate_width($item);
83
84   return $all_items;
85 }
86
87 sub calculate_width {
88   my $item           = shift;
89
90   $item->{max_width} = max map { length $_->{title} } @{ $item->{subitems} };
91
92   foreach my $subitem (@{ $item->{subitems} }) {
93     calculate_width($subitem) if ($subitem->{subitems});
94   }
95 }
96
97 sub create_menu {
98   my ($self, $menu, $all_items, $parent, $depth) = @_;
99   my $html;
100
101   my $form     = $main::form;
102   my %myconfig = %main::myconfig;
103
104   die if ($depth * 1 > 5);
105
106   my @menuorder  = $menu->access_control(\%myconfig, $parent);
107   $parent       .= "--" if ($parent);
108
109   foreach my $name (@menuorder) {
110     substr($name, 0, length($parent), "");
111     next if (($name eq "") || ($name =~ /--/));
112
113     my $menu_item = $menu->{"${parent}${name}"};
114     my $item      = { 'title' => $::locale->text($name) };
115     push @{ $all_items }, $item;
116
117     if ($menu_item->{submenu} || !defined($menu_item->{module}) || ($menu_item->{module} eq "menu.pl")) {
118       $item->{subitems} = [];
119       $item->{image} = _icon_path("$name.png");
120       $self->create_menu($menu, $item->{subitems}, "${parent}${name}", $depth * 1 + 1);
121
122     } else {
123       $item->{image} = _icon_path("${parent}${name}.png");
124       $menu->menuitem_new("${parent}${name}", $item);
125     }
126   }
127 }
128
129 sub _icon_path {
130   my ($label, $size) = @_;
131
132   $size ||= 16;
133
134   my $img = "image/icons/${size}x${size}/$label";
135
136   return unless -f $img;
137   return $img;
138 }
139
140 1;