summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Customer_Search.pm
blob: 7456f9d36b6ca55973b199cf00e4a40a6aa8bf7c (plain)
  1. package LedgerSMB::RESTXML::Document::Customer_Search;
  2. use strict;
  3. use warnings;
  4. use base qw(LedgerSMB::RESTXML::Document::Base);
  5. use LedgerSMB::Log;
  6. sub handle_get {
  7. my ($self, $args) = @_;
  8. my $user = $args->{user};
  9. my $dbh = $args->{dbh};
  10. my $handler = $args->{handler};
  11. my $query = $handler->read_query();
  12. my %terms;
  13. for my $field ($query->param()) {
  14. # TODO: BIG GAPING HOLE HERE.
  15. $terms{$field} = $query->param($field);
  16. }
  17. if($terms{_keyword}) {
  18. %terms = (
  19. name=>$terms{_keyword},
  20. customernumber=>$terms{_keyword},
  21. contact=>$terms{_keyword}
  22. );
  23. }
  24. my $sql = 'SELECT id,name,phone,customernumber FROM customer WHERE '.join(' OR ',map { "$_ like ?" } sort keys %terms);
  25. my $res = $dbh->prepare($sql);
  26. $res->execute(map { "$terms{$_}\%" } sort keys %terms) or return $handler->error($dbh->errstr);
  27. my @rows;
  28. my $row;
  29. push @rows, $row while $row = $res->fetchrow_hashref();
  30. $res->finish();
  31. $handler->respond(XML::Twig::Elt->new('Customer_Search_Response',{'xmlns:xlink'=>"http://www.w3.org/1999/xlink"},map {
  32. $self->hash_to_twig({name=>'Customer',root_attr=>{'xlink:href'=>"Customer/$_->{id}"}, hash=>$_});
  33. } @rows));
  34. }
  35. 1;