X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FLayout%2FBase.pm;h=7932aff5bbd70aba2e822c619c6e96ddde59ee28;hb=fee5532a132c44dcfc1743393cba00c8e3397176;hp=56d5b8e2fc94923c666851fac602db63d504998a;hpb=b6fd15a8dc44f9b09d5a2bce766cda14b87c6e13;p=kivitendo-erp.git diff --git a/SL/Layout/Base.pm b/SL/Layout/Base.pm index 56d5b8e2f..7932aff5b 100644 --- a/SL/Layout/Base.pm +++ b/SL/Layout/Base.pm @@ -1,20 +1,24 @@ package SL::Layout::Base; use strict; -use parent qw(SL::Controller::Base); +use parent qw(Rose::Object); + +use List::MoreUtils qw(uniq); +use Time::HiRes qw(); use Rose::Object::MakeMethods::Generic ( - 'scalar --get_set_init' => qw(menu), + 'scalar --get_set_init' => [ qw(menu auto_reload_resources_param) ], 'scalar' => qw(focus), 'array' => [ 'add_stylesheets_inline' => { interface => 'add', hash_key => 'stylesheets_inline' }, 'add_javascripts_inline' => { interface => 'add', hash_key => 'javascripts_inline' }, - 'sub_layouts', - 'add_sub_layouts' => { interface => 'add', hash_key => 'sub_layouts' }, + 'sub_layouts', => { interface => 'get_set_init' }, + 'add_sub_layouts' => { interface => 'add', hash_key => 'sub_layouts' }, ], ); use SL::Menu; +use SL::Presenter; my %menu_cache; @@ -25,7 +29,14 @@ sub new { } sub init_menu { - Menu->new('menu.ini'); + my @menu_files = qw(menus/erp.ini); + unshift @menu_files, 'menus/crm.ini' if $::instance_conf->crm_installed; + Menu->new(@menu_files); +} + +sub init_auto_reload_resources_param { + return '' unless $::lx_office_conf{debug}->{auto_reload_resources}; + return sprintf('?rand=%d-%d-%d', Time::HiRes::gettimeofday(), int(rand 1000000000000)); } ########################################## @@ -49,20 +60,26 @@ sub post_content { } sub stylesheets_inline { - ( map { $_->stylesheets_inline } $_[0]->sub_layouts ), + uniq ( map { $_->stylesheets_inline } $_[0]->sub_layouts ), @{ $_[0]->{stylesheets_inline} || [] }; } sub javascripts_inline { - ( map { $_->javascripts_inline } $_[0]->sub_layouts ), + uniq ( map { $_->javascripts_inline } $_[0]->sub_layouts ), @{ $_[0]->{javascripts_inline} || [] }; } +sub init_sub_layouts { [] } + ######################################### # Interface ######################################## +sub add_stylesheets { + &use_stylesheet; +} + sub use_stylesheet { my $self = shift; push @{ $self->{stylesheets} ||= [] }, @_ if @_; @@ -73,7 +90,7 @@ sub stylesheets { my ($self) = @_; my $css_path = $self->get_stylesheet_for_user; - return grep { $_ } map { $self->_find_stylesheet($_, $css_path) } + return uniq grep { $_ } map { $self->_find_stylesheet($_, $css_path) } $self->use_stylesheet, map { $_->stylesheets } $self->sub_layouts; } @@ -93,16 +110,19 @@ sub get_stylesheet_for_user { -f "$css_path/$user_style/main.css") { $css_path = "$css_path/$user_style"; } else { - $css_path = "$css_path/lx-office-erp"; + $css_path = "$css_path/kivitendo"; } } else { - $css_path = "$css_path/lx-office-erp"; + $css_path = "$css_path/kivitendo"; } $::myconfig{css_path} = $css_path; # needed for menunew, FIXME: don't do this here return $css_path; } +sub add_javascripts { + &use_javascript +} sub use_javascript { my $self = shift; @@ -113,8 +133,8 @@ sub use_javascript { sub javascripts { my ($self) = @_; - return map { $self->_find_javascript($_) } - $self->use_javascript, map { $_->javascripts } $self->sub_layouts; + return uniq grep { $_ } map { $self->_find_javascript($_) } + map({ $_->javascripts } $self->sub_layouts), $self->use_javascript; } sub _find_javascript { @@ -137,4 +157,175 @@ sub need_footer { $_[0]{_header_done}; } +sub presenter { + SL::Presenter->get; +} + 1; + +__END__ + +=encoding utf-8 + +=head1 NAME + +SL::Layout::Base - Base class for layouts + +=head1 SYNOPSIS + + package SL::Layout::MyLayout; + + use parent qw(SL::Layout::Base); + +=head1 DESCRIPTION + +For a description about the external interface of layouts in general see +L. + +This is a base class for layouts in general. It provides the basic interface +and some capabilities to extend and cascade layouts. + + +=head1 IMPLEMENTING LAYOUT CALLBACKS + +There are eight callbacks (C, C, C, +C, C, C, C, +C) which are documented in L. If +you are writing a new simple layout, you can just override some of them like +this: + + package SL::Layout::MyEvilLayout; + + sub pre_content { + '

This is MY page now

' + } + + sub post_content { + '

Brought to you by my own layout class

' + } + + +To preserve the sanitizing effects of C and C you should instead do the following: + + sub stylesheets { + $_[0]->add_stylesheets(qw(mystyle1.css mystyle2.css); + $_[0]->SUPER::stylesheets; + } + +If you want to add something to a different layout, you should write a sub +layout and add it to the other layouts. + + +=head1 SUB LAYOUTS + +Layouts can be aggregated, so that common elements can be used in different +layouts. Currently this is used for the L sub layout, +which contains a lot of the stylesheets and javascripts necessary. Another +example is the L layout, which is used to generate a +common top bar for all menu types. + +To add a sub layout to your layout just overwrite the sub_layout method: + + package SL::Layout::MyFinalLayout; + + sub init_sub_layout { + [ + SL::Layout::None->new, + SL::Layout::MyEvilLayout->new, + ] + } + +You can also add a sublayout at runtime: + + $layout->add_sub_layout(SL::Layout::SideBar->new); + +The standard implementation for the callbacks will see to it that the contents +of all sub layouts will get rendered. + + +=head1 COMBINING SUB LAYOUTS AND OWN BEHAVIOUR + +This is still somewhat rough, and improvements are welcome. + +For the C<*_content> callbacks this works if you just remember to dispatch to the base method: + + sub post_content { + return $_[0]->render_status_bar . + $_[0]->SUPER::post_content + } + +For the stylesheet and javascript callbacks things are hard, because of the +backwards compatibility, and the built-in sanity checks. The best way currently +is to just add your content and dispatch to the base method. + + sub stylesheets { + $_[0]->add_stylesheets(qw(mystyle1.css mystyle2.css); + $_[0]->SUPER::stylesheets; + } + +=head1 GORY DETAILS ABOUT JAVASCRIPT AND STYLESHEET OVERLOADING + +The original code used to store one stylehsheet in C<< $form->{stylesheet} >> and +allowed/expected authors of potential C controllers to change +that into their own modified stylesheet. + +This was at some point cleaned up into a method C which took a +string of space separated stylesheets and processed them into the response. + +A lot of controllers are still using this methods so the layout interface +supports it to change as few controller code as possible, while providing the +more intuitive C method. + +At the same time the following things need to be possible: + +=over 4 + +=item 1. + +Runtime additions. + + $layout->add_stylesheets(...) + +Since add_stylesheets adds to C<< $self->{stylesheets} >> there must be a way to read +from it. Currently this is the deprecated C. + +=item 2. + +Overriding Callbacks + +A leaf layout should be able to override a callback to return a list. + +=item 3. + +Sanitizing + +C needs to retain it's sanitizing behaviour. + +=item 4. + +Aggregation + +The standard implementation should be able to collect from sub layouts. + +=item 5. + +Preserving of Inclusion Order + +Since there is currently no standard way of mixing own content and including +sub layouts, this has to be done manually. Certain things like jquery get added +in L so that they get rendered first. + +=back + +The current implementation provides no good candidate for overriding in sub +classes, which should be changed. The other points work pretty well. + +=head1 BUGS + +None yet, if you don't count the horrible stylesheet/javascript interface. + +=head1 AUTHOR + +Sven Schöling Es.schoeling@linet-services.deE + +=cut