summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Part_Search.pm
blob: 927c67c027a6a2b9566ed95b5038928aa0393329 (plain)
  1. package LedgerSMB::RESTXML::Document::Part_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. description=>$terms{_keyword},
  20. partnumber=>$terms{_keyword},
  21. );
  22. }
  23. my $sql = 'SELECT id,description,partnumber FROM parts WHERE '.join(' OR ',map { "$_ like ?" } sort keys %terms);
  24. my $res = $dbh->prepare($sql);
  25. $res->execute(map { "$terms{$_}\%" } sort keys %terms) or return $handler->error($dbh->errstr);
  26. my @rows;
  27. my $row;
  28. push @rows, $row while $row = $res->fetchrow_hashref();
  29. $res->finish();
  30. $handler->respond(XML::Twig::Elt->new('Part_Search_Response',{'xmlns:xlink'=>"http://www.w3.org/1999/xlink"},map {
  31. $self->hash_to_twig({name=>'Part',root_attr=>{'xlink:href'=>"Part/$_->{id}"}, hash=>$_});
  32. } @rows));
  33. }
  34. 1;