In Kunden-/Liefernaten-Stammdatenbericht Filtereinstellung für Land auch ...
[kivitendo-erp.git] / SL / Template / Plugin / L.pm
index f90a948..871e683 100644 (file)
@@ -6,6 +6,8 @@ use List::MoreUtils qw(apply);
 use List::Util qw(max);
 use Scalar::Util qw(blessed);
 
+use SL::Presenter;
+
 use strict;
 
 { # This will give you an id for identifying html tags and such.
@@ -28,8 +30,8 @@ sub _H {
 }
 
 sub _J {
-  my $string =  "" . shift;
-  $string    =~ s/\"/\\\"/g;
+  my $string = shift;
+  $string    =~ s/(\"|\'|\\)/\\$1/g;
   return $string;
 }
 
@@ -83,6 +85,15 @@ sub html_tag {
   return "<${tag}${attributes}>${content}</${tag}>";
 }
 
+sub img_tag {
+  my ($self, @slurp) = @_;
+  my %options = _hashify(@slurp);
+
+  $options{alt} ||= '';
+
+  return $self->html_tag('img', undef, %options);
+}
+
 sub select_tag {
   my $self            = shift;
   my $name            = shift;
@@ -102,6 +113,8 @@ sub select_tag {
   my $title_sub       = delete($attributes{title_sub});
   my $default_sub     = delete($attributes{default_sub});
 
+  my $with_empty      = delete($attributes{with_empty});
+  my $empty_title     = delete($attributes{empty_title});
 
   my %selected;
 
@@ -119,8 +132,8 @@ sub select_tag {
 
   my @options;
 
-  if ( delete($attributes{with_empty}) ) {
-    push(@options, [undef, $attributes{empty_title} || '']);
+  if ( $with_empty ) {
+    push(@options, [undef, $empty_title || '']);
   }
 
   my $normalize_entry = sub {
@@ -171,7 +184,7 @@ sub select_tag {
     my $title;
 
     if ( $value_title_sub ) {
-      ($value, $title) = $value_title_sub->($entry);
+      ($value, $title) = @{ $value_title_sub->($entry) };
     } else {
 
       $value = $normalize_entry->('value', $entry, $value_sub, $value_key);
@@ -270,7 +283,8 @@ sub input_tag {
 }
 
 sub hidden_tag {
-  return shift->input_tag(@_, type => 'hidden');
+  my ($self, $name, $value, @slurp) = @_;
+  return $self->input_tag($name, $value, _hashify(@slurp), type => 'hidden');
 }
 
 sub div_tag {
@@ -301,7 +315,9 @@ sub submit_tag {
   my ($self, $name, $value, @slurp) = @_;
   my %attributes = _hashify(@slurp);
 
-  $attributes{onclick} = "if (confirm('" . delete($attributes{confirm}) . "')) return true; else return false;" if $attributes{confirm};
+  if ( $attributes{confirm} ) {
+    $attributes{onclick} = 'return confirm("'. _J(delete($attributes{confirm})) .'");';
+  }
 
   return $self->input_tag($name, $value, %attributes, type => 'submit', class => 'submit');
 }
@@ -383,34 +399,10 @@ sub customer_picker {
   my ($self, $name, $value, %params) = @_;
   my $name_e    = _H($name);
 
-  $self->hidden_tag($name, (ref $value && $value->can('id')) ? $value->id : '') .
-  $self->input_tag("$name_e\_name", (ref $value && $value->can('name')) ? $value->name : '', %params) .
-  $self->javascript(<<JS);
-function autocomplete_customer (selector, column) {
-  \$(function(){ \$(selector).autocomplete({
-    source: function(req, rsp) {
-      \$.ajax({
-        url: 'controller.pl?action=Customer/ajax_autocomplete',
-        dataType: "json",
-        data: {
-          column: column,
-          term: req.term,
-          current: function() { \$('#$name_e').val() },
-          obsolete: 0,
-        },
-        success: function (data){ rsp(data) }
-      });
-    },
-    limit: 20,
-    delay: 50,
-    select: function(event, ui) {
-      \$('#$name_e').val(ui.item.id);
-      \$('#$name_e\_name').val(ui.item.name);
-    },
-  })});
-}
-autocomplete_customer('#$name_e\_name');
-JS
+  $::request->{layout}->add_javascripts('autocomplete_customer.js');
+
+  $self->hidden_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => 'customer_autocomplete') .
+  $self->input_tag("$name_e\_name", (ref $value && $value->can('name')) ? $value->name : '', %params);
 }
 
 # simple version with select_tag
@@ -473,27 +465,15 @@ sub tabbed {
 
     next if $tab eq '';
 
-    my $selected = $params{selected} == $i;
-    my $tab_id   = "__tab_id_$i";
-    push @header, $self->li_tag(
-      $self->link('', $tab->{name}, rel => $tab_id),
-        ($selected ? (class => 'selected') : ())
-    );
-    push @blocks, $self->div_tag($tab->{data},
-      id => $tab_id, class => 'tabcontent');
+    my $tab_id = "__tab_id_$i";
+    push @header, $self->li_tag($self->link('#' . $tab_id, $tab->{name}));
+    push @blocks, $self->div_tag($tab->{data}, id => $tab_id);
   }
 
   return '' unless @header;
-  return $self->ul_tag(
-    join('', @header), id => $id, class => 'shadetabs'
-  ) .
-  $self->div_tag(
-    join('', @blocks), class => 'tabcontentstyle'
-  ) .
-  $self->javascript(
-    qq|var $id = new ddtabcontent("$id");$id.setpersist(true);| .
-    qq|$id.setselectedClassTarget("link");$id.init();|
-  );
+
+  my $ul = $self->ul_tag(join('', @header), id => $id);
+  return $self->div_tag(join('', $ul, @blocks), class => 'tabwidget');
 }
 
 sub tab {
@@ -654,7 +634,7 @@ sub sortable_table_header {
   my $by_spec             = $sort_spec->{$by};
   my %current_sort_params = $controller->get_current_sort_params;
   my ($image, $new_dir)   = ('', $current_sort_params{dir});
-  my $title               = delete($params{title}) || $by_spec->{title};
+  my $title               = delete($params{title}) || $::locale->text($by_spec->{title});
 
   if ($current_sort_params{by} eq $by) {
     my $current_dir = $current_sort_params{dir} ? 'up' : 'down';
@@ -668,6 +648,27 @@ sub sortable_table_header {
   return '<a href="' . $controller->get_callback(%params) . '">' . _H($title) . $image . '</a>';
 }
 
+sub paginate_controls {
+  my ($self)          = @_;
+
+  my $controller      = $self->{CONTEXT}->stash->get('SELF');
+  my $paginate_spec   = $controller->get_paginate_spec;
+  my %paginate_params = $controller->get_current_paginate_params;
+
+  my %template_params = (
+    pages             => \%paginate_params,
+    url_maker         => sub {
+      my %url_params                                    = _hashify(@_);
+      $url_params{ $paginate_spec->{FORM_PARAMS}->[0] } = delete $url_params{page};
+      $url_params{ $paginate_spec->{FORM_PARAMS}->[1] } = delete $url_params{per_page} if exists $url_params{per_page};
+
+      return $controller->get_callback(%url_params);
+    },
+  );
+
+  return SL::Presenter->get->render('common/paginate', %template_params);
+}
+
 1;
 
 __END__
@@ -878,9 +879,6 @@ C<tab>. Example:
     L.tab(LxERP.t8('Custom Variables'), 'part/_cvar_tab.html', if => SELF.display_cvar_tab),
   ]) %]
 
-An optional attribute is C<selected>, which accepts the ordinal of a tab which
-should be selected by default.
-
 =item C<areainput_tag $name, $content, %PARAMS>
 
 Creates a generic input tag or textarea tag, depending on content size. The
@@ -1009,6 +1007,13 @@ underlying call to L<SL::Controller::Base::url_for>.
 See the documentation of L<SL::Controller::Helper::Sorted> for an
 overview and further usage instructions.
 
+=item C<paginate_controls>
+
+Create a set of links used to paginate a list view.
+
+See the documentation of L<SL::Controller::Helper::Paginated> for an
+overview and further usage instructions.
+
 =back
 
 =head2 CONVERSION FUNCTIONS