Funktion 'is_sales' in SL::DB::{Invoice,PurchaseInvoice}
[kivitendo-erp.git] / SL / Template / Plugin / L.pm
index f90a948..4ed65ab 100644 (file)
@@ -28,8 +28,8 @@ sub _H {
 }
 
 sub _J {
-  my $string =  "" . shift;
-  $string    =~ s/\"/\\\"/g;
+  my $string = shift;
+  $string    =~ s/(\"|\'|\\)/\\$1/g;
   return $string;
 }
 
@@ -83,6 +83,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 +111,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 +130,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 +182,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);
@@ -301,7 +312,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 +396,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
@@ -654,7 +643,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 +657,29 @@ 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);
+    },
+  );
+
+  my $output;
+  $controller->_template_obj->process('templates/webpages/common/paginate.html', \%template_params, \$output);
+  return $output;
+}
+
 1;
 
 __END__
@@ -1009,6 +1021,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