stylesheet/javascript handling verbessert
[kivitendo-erp.git] / SL / Controller / Layout / Base.pm
1 package SL::Controller::Layout::Base;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use Rose::Object::MakeMethods::Generic (
7   'scalar --get_set_init' => qw(menu),
8 );
9
10 use SL::Menu;
11
12 my %menu_cache;
13
14 sub new {
15   my ($class, @slurp) = @_;
16
17   my $self = $class->SUPER::new(@slurp);
18 }
19
20 sub init_menu {
21   Menu->new('menu.ini');
22 }
23
24 ##########################################
25 #  inheritable/overridable
26 ##########################################
27
28 sub pre_content {
29 }
30
31 sub start_content {
32 }
33
34 sub end_content {
35 }
36
37 sub post_content {
38 }
39
40 sub stylesheets_inline {
41 }
42
43 sub javascript_inline {
44 }
45
46 #########################################
47 # Interface
48 ########################################
49
50 sub use_stylesheet {
51   my $self = shift;
52   push @{ $self->{stylesheets} ||= [] }, @_ if @_;
53   @{ $self->{stylesheets} ||= [] };
54 }
55
56 sub stylesheets {
57   my ($self) = @_;
58   my $css_path = $self->get_stylesheet_for_user;
59
60   return grep { $_ } map { $self->_find_stylesheet($_, $css_path)  } $self->use_stylesheet;
61 }
62
63 sub _find_stylesheet {
64   my ($self, $stylesheet, $css_path) = @_;
65
66   return "$css_path/$stylesheet" if -f "$css_path/$stylesheet";
67   return "css/$stylesheet"       if -f "css/$stylesheet";
68   return $stylesheet             if -f $stylesheet;
69 }
70
71 sub get_stylesheet_for_user {
72   my $css_path = 'css';
73   if (my $user_style = $::myconfig{stylesheet}) {
74     $user_style =~ s/\.css$//; # nuke trailing .css, this is a remnand of pre 2.7.0 stylesheet handling
75     if (-d "$css_path/$user_style" &&
76         -f "$css_path/$user_style/main.css") {
77       $css_path = "$css_path/$user_style";
78     } else {
79       $css_path = "$css_path/lx-office-erp";
80     }
81   } else {
82     $css_path = "$css_path/lx-office-erp";
83   }
84   $::myconfig{css_path} = $css_path; # needed for menunew, FIXME: don't do this here
85
86   return $css_path;
87 }
88
89
90 sub use_javascript {
91   my $self = shift;
92   $::lxdebug->dump(0,  "class", \@_);
93   push @{ $self->{javascripts} ||= [] }, @_ if @_;
94   @{ $self->{javascripts} ||= [] };
95 }
96
97 sub javascripts {
98   my ($self) = @_;
99
100   $::lxdebug->dump(0,  "called", [ map { $self->find_javascript($_)  } $self->use_javascript ]);
101   return map { $self->_find_javascript($_)  } $self->use_javascript;
102 }
103
104 sub _find_javascript {
105   my ($self, $javascript) = @_;
106
107   return "js/$javascript"        if -f "js/$javascript";
108   return $javascript             if -f $javascript;
109 }
110
111 1;