customer_picker
authorSven Schöling <s.schoeling@linet-services.de>
Tue, 11 Oct 2011 12:58:25 +0000 (14:58 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Tue, 11 Oct 2011 13:00:53 +0000 (15:00 +0200)
testpage ist zu Dokumentationszwecken noch dabei, aber nicht mehr durch action
erreichbar.

SL/Controller/Customer.pm [new file with mode: 0644]
SL/Form.pm
SL/Template/Plugin/L.pm
templates/webpages/ct/ajax_autocomplete2.html [new file with mode: 0644]
templates/webpages/ct/testpage.html [new file with mode: 0644]

diff --git a/SL/Controller/Customer.pm b/SL/Controller/Customer.pm
new file mode 100644 (file)
index 0000000..4358080
--- /dev/null
@@ -0,0 +1,27 @@
+package SL::Controller::Customer;
+
+use strict;
+use parent qw(SL::Controller::Base);
+
+use SL::DB::Customer;
+
+# safety
+__PACKAGE__->run_before(sub { $::auth->assert('customer_vendor_edit') });
+
+sub action_ajax_autocomplete {
+  my ($self, %params) = @_;
+
+  my $limit  = $::form->{limit}  || 20;
+  my $type   = $::form->{type} || {};
+  my $query  = { ilike => "%$::form->{term}%" };
+  my @filter;
+  push @filter, ($::form->{column})
+    ? ($::form->{column} => $query)
+    : (or => [ customernumber => $query, name => $query ]);
+
+  $self->{customers} = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
+  $self->{value} = $::form->{column} || 'name';
+
+  $self->render('ct/ajax_autocomplete2', { no_layout => 1 });
+}
+
index a23bb43..b21f94c 100644 (file)
@@ -701,7 +701,9 @@ sub header {
                 '<script type="text/javascript" src="js/jscalendar/calendar.js"></script>',
                 '<script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>',
                 '<script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>',
-                '<script type="text/javascript" src="js/part_selection.js"></script>';
+                '<script type="text/javascript" src="js/part_selection.js"></script>',
+                '<script type="text/javascript" src="js/jquery-ui.js"></script>',
+                '<style "type=text/css">@import url("css/ui-lightness/jquery-ui-1.8.12.custom.css")</style>';
   push @header, $self->{javascript} if $self->{javascript};
   push @header, map { $_->show_javascript } @{ $self->{AJAX} || [] };
   push @header, "<script type='text/javascript'>function fokus(){ document.$self->{fokus}.focus(); }</script>" if $self->{fokus};
index 0ed2a87..476b60a 100644 (file)
@@ -291,6 +291,40 @@ sub date_tag {
   ) : '');
 }
 
+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
+}
+
 sub javascript_tag {
   my $self = shift;
   my $code = '';
diff --git a/templates/webpages/ct/ajax_autocomplete2.html b/templates/webpages/ct/ajax_autocomplete2.html
new file mode 100644 (file)
index 0000000..f4a44e3
--- /dev/null
@@ -0,0 +1,11 @@
+[%- USE HTML %][% USE JSON %][
+[%- FOREACH customer = SELF.customers %]
+ {
+   "value": [% customer.${SELF.value}.json %],
+   "label": [% customer.displayable_name.json %],
+   "id": [% customer.id.json %],
+   "customernumber": [% customer.customernumber.json %],
+   "name": [% customer.name.json %]
+  }[% ',' UNLESS loop.last %]
+[%- END %]
+]
diff --git a/templates/webpages/ct/testpage.html b/templates/webpages/ct/testpage.html
new file mode 100644 (file)
index 0000000..e8ba838
--- /dev/null
@@ -0,0 +1,40 @@
+[% USE L %]
+[% USE T8 %]
+[% USE LxERP %]
+[% L.javascript_tag('jquery-ui') %]
+<link rel="stylesheet" href="css/ui-lightness/jquery-ui-1.8.12.custom.css" type="text/css" />
+
+<p>Pick a customer</p>
+id: [% L.input_tag('customer_id', '') %]
+nr: [% L.input_tag('customer_customernumber', '') %]
+desc: [% L.input_tag('customer_name', '') %]
+
+<script type='text/javascript'>
+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() { $('#customer_id').val() },
+          obsolete: 0,
+        },
+        success: function (data){ rsp(data) }
+      });
+    },
+    limit: 20,
+    delay: 50,
+    select: function(event, ui) {
+      $('#customer_id').val(ui.item.id);
+      $('#customer_customernumber').val(ui.item.customernumber);
+      $('#customer_name').val(ui.item.name);
+    },
+  })});
+}
+//autocomplete_customer('#customer_customernumber', 'customernumber');
+autocomplete_customer('#customer_name', '');
+</script>
+