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