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