besseres interface und delegating für layouts, inline accessoren
[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   'array'                 => [
9     'add_stylesheets_inline' => { interface => 'add', hash_key => 'stylesheets_inline' },
10     'add_javascripts_inline' => { interface => 'add', hash_key => 'javascripts_inline' },
11     'sub_layouts',
12     'add_sub_layouts'         => { interface => 'add', hash_key => 'sub_layouts' },
13   ],
14 );
15
16 use SL::Menu;
17
18 my %menu_cache;
19
20 sub new {
21   my ($class, @slurp) = @_;
22
23   my $self = $class->SUPER::new(@slurp);
24 }
25
26 sub init_menu {
27   Menu->new('menu.ini');
28 }
29
30 ##########################################
31 #  inheritable/overridable
32 ##########################################
33
34 sub pre_content {
35   join '', map { $_->pre_content } $_[0]->sub_layouts;
36 }
37
38 sub start_content {
39   join '', map { $_->start_content } $_[0]->sub_layouts;
40 }
41
42 sub end_content {
43   join '', map { $_->end_content } $_[0]->sub_layouts;
44 }
45
46 sub post_content {
47   join '', map { $_->post_content } $_[0]->sub_layouts;
48 }
49
50 sub stylesheets_inline {
51   ( map { $_->stylesheets_inline } $_[0]->sub_layouts ),
52   @{ $_[0]->{stylesheets_inline} || [] };
53 }
54
55 sub javascripts_inline {
56   ( map { $_->javascripts_inline } $_[0]->sub_layouts ),
57   @{ $_[0]->{javascripts_inline} || [] };
58 }
59
60
61 #########################################
62 # Interface
63 ########################################
64
65 sub use_stylesheet {
66   my $self = shift;
67   push @{ $self->{stylesheets} ||= [] }, @_ if @_;
68   @{ $self->{stylesheets} ||= [] };
69 }
70
71 sub stylesheets {
72   my ($self) = @_;
73   my $css_path = $self->get_stylesheet_for_user;
74
75   return grep { $_ } map { $self->_find_stylesheet($_, $css_path)  }
76     $self->use_stylesheet, map { $_->stylesheets } $self->sub_layouts;
77 }
78
79 sub _find_stylesheet {
80   my ($self, $stylesheet, $css_path) = @_;
81
82   return "$css_path/$stylesheet" if -f "$css_path/$stylesheet";
83   return "css/$stylesheet"       if -f "css/$stylesheet";
84   return $stylesheet             if -f $stylesheet;
85 }
86
87 sub get_stylesheet_for_user {
88   my $css_path = 'css';
89   if (my $user_style = $::myconfig{stylesheet}) {
90     $user_style =~ s/\.css$//; # nuke trailing .css, this is a remnand of pre 2.7.0 stylesheet handling
91     if (-d "$css_path/$user_style" &&
92         -f "$css_path/$user_style/main.css") {
93       $css_path = "$css_path/$user_style";
94     } else {
95       $css_path = "$css_path/lx-office-erp";
96     }
97   } else {
98     $css_path = "$css_path/lx-office-erp";
99   }
100   $::myconfig{css_path} = $css_path; # needed for menunew, FIXME: don't do this here
101
102   return $css_path;
103 }
104
105
106 sub use_javascript {
107   my $self = shift;
108   push @{ $self->{javascripts} ||= [] }, @_ if @_;
109   @{ $self->{javascripts} ||= [] };
110 }
111
112 sub javascripts {
113   my ($self) = @_;
114
115   return map { $self->_find_javascript($_)  }
116     $self->use_javascript, map { $_->javascripts } $self->sub_layouts;
117 }
118
119 sub _find_javascript {
120   my ($self, $javascript) = @_;
121
122   return "js/$javascript"        if -f "js/$javascript";
123   return $javascript             if -f $javascript;
124 }
125
126
127 ############################################
128 # track state of form header
129 ############################################
130
131 sub header_done {
132   $_[0]{_header_done} = 1;
133 }
134
135 sub need_footer {
136   $_[0]{_header_done};
137 }
138
139 1;