Entwickleroption "debug.auto_reload_resources" implementiert
[kivitendo-erp.git] / SL / Layout / Base.pm
1 package SL::Layout::Base;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use List::MoreUtils qw(uniq);
7 use Time::HiRes qw();
8
9 use Rose::Object::MakeMethods::Generic (
10   'scalar --get_set_init' => [ qw(menu auto_reload_resources_param) ],
11   'scalar'                => qw(focus),
12   'array'                 => [
13     'add_stylesheets_inline' => { interface => 'add', hash_key => 'stylesheets_inline' },
14     'add_javascripts_inline' => { interface => 'add', hash_key => 'javascripts_inline' },
15     'sub_layouts',           => { interface => 'get_set_init' },
16     'add_sub_layouts'        => { interface => 'add', hash_key => 'sub_layouts' },
17   ],
18 );
19
20 use SL::Menu;
21
22 my %menu_cache;
23
24 sub new {
25   my ($class, @slurp) = @_;
26
27   my $self = $class->SUPER::new(@slurp);
28 }
29
30 sub init_menu {
31   Menu->new('menu.ini');
32 }
33
34 sub init_auto_reload_resources_param {
35   return '' unless $::lx_office_conf{debug}->{auto_reload_resources};
36   return sprintf('?rand=%d-%d-%d', Time::HiRes::gettimeofday(), int(rand 1000000000000));
37 }
38
39 ##########################################
40 #  inheritable/overridable
41 ##########################################
42
43 sub pre_content {
44   join '', map { $_->pre_content } $_[0]->sub_layouts;
45 }
46
47 sub start_content {
48   join '', map { $_->start_content } $_[0]->sub_layouts;
49 }
50
51 sub end_content {
52   join '', map { $_->end_content } $_[0]->sub_layouts;
53 }
54
55 sub post_content {
56   join '', map { $_->post_content } $_[0]->sub_layouts;
57 }
58
59 sub stylesheets_inline {
60   uniq ( map { $_->stylesheets_inline } $_[0]->sub_layouts ),
61   @{ $_[0]->{stylesheets_inline} || [] };
62 }
63
64 sub javascripts_inline {
65   uniq ( map { $_->javascripts_inline } $_[0]->sub_layouts ),
66   @{ $_[0]->{javascripts_inline} || [] };
67 }
68
69 sub init_sub_layouts { [] }
70
71
72 #########################################
73 # Interface
74 ########################################
75
76 sub add_stylesheets {
77   &use_stylesheet;
78 }
79
80 sub use_stylesheet {
81   my $self = shift;
82   push @{ $self->{stylesheets} ||= [] }, @_ if @_;
83   @{ $self->{stylesheets} ||= [] };
84 }
85
86 sub stylesheets {
87   my ($self) = @_;
88   my $css_path = $self->get_stylesheet_for_user;
89
90   return uniq grep { $_ } map { $self->_find_stylesheet($_, $css_path)  }
91     $self->use_stylesheet, map { $_->stylesheets } $self->sub_layouts;
92 }
93
94 sub _find_stylesheet {
95   my ($self, $stylesheet, $css_path) = @_;
96
97   return "$css_path/$stylesheet" if -f "$css_path/$stylesheet";
98   return "css/$stylesheet"       if -f "css/$stylesheet";
99   return $stylesheet             if -f $stylesheet;
100 }
101
102 sub get_stylesheet_for_user {
103   my $css_path = 'css';
104   if (my $user_style = $::myconfig{stylesheet}) {
105     $user_style =~ s/\.css$//; # nuke trailing .css, this is a remnand of pre 2.7.0 stylesheet handling
106     if (-d "$css_path/$user_style" &&
107         -f "$css_path/$user_style/main.css") {
108       $css_path = "$css_path/$user_style";
109     } else {
110       $css_path = "$css_path/kivitendo";
111     }
112   } else {
113     $css_path = "$css_path/kivitendo";
114   }
115   $::myconfig{css_path} = $css_path; # needed for menunew, FIXME: don't do this here
116
117   return $css_path;
118 }
119
120 sub add_javascripts {
121   &use_javascript
122 }
123
124 sub use_javascript {
125   my $self = shift;
126   push @{ $self->{javascripts} ||= [] }, @_ if @_;
127   @{ $self->{javascripts} ||= [] };
128 }
129
130 sub javascripts {
131   my ($self) = @_;
132
133   return uniq map { $self->_find_javascript($_)  }
134     map({ $_->javascripts } $self->sub_layouts), $self->use_javascript;
135 }
136
137 sub _find_javascript {
138   my ($self, $javascript) = @_;
139
140   return "js/$javascript"        if -f "js/$javascript";
141   return $javascript             if -f $javascript;
142 }
143
144
145 ############################################
146 # track state of form header
147 ############################################
148
149 sub header_done {
150   $_[0]{_header_done} = 1;
151 }
152
153 sub need_footer {
154   $_[0]{_header_done};
155 }
156
157 1;