TopQuickSearch nach allen Telefonnummern: Leerzeichen ignorieren.
[kivitendo-erp.git] / SL / Controller / TopQuickSearch / PhoneNumber.pm
1 package SL::Controller::TopQuickSearch::PhoneNumber;
2
3 use strict;
4 use parent qw(SL::Controller::TopQuickSearch::Base);
5
6 use SL::Controller::TopQuickSearch::Customer;
7 use SL::Controller::TopQuickSearch::Vendor;
8 use SL::DB::Customer;
9 use SL::DB::Vendor;
10 use SL::Locale::String qw(t8);
11 use SL::Util qw(trim);
12
13 sub auth { undef }
14
15 sub name { 'phone_number' }
16
17 sub description_config { t8('All phone numbers') }
18
19 sub description_field { t8('All phone numbers') }
20
21 sub query_autocomplete {
22   my ($self) = @_;
23
24   my @results;
25   my $search_term = trim($::form->{term});
26   $search_term    =~ s{\p{WSpace}+}{}g;
27   $search_term    = join ' *', split(//, $search_term);
28
29   foreach my $model (qw(Customer Vendor)) {
30     my $manager = 'SL::DB::Manager::' . $model;
31     my $result  = $manager->get_all(
32       query => [ or => [ 'obsolete' => 0, 'obsolete' => undef ],
33                  or => [ phone                     => { imatch => $search_term },
34                          fax                       => { imatch => $search_term },
35                          'contacts.cp_phone1'      => { imatch => $search_term },
36                          'contacts.cp_phone2'      => { imatch => $search_term },
37                          'contacts.cp_fax'         => { imatch => $search_term },
38                          'contacts.cp_mobile1'     => { imatch => $search_term },
39                          'contacts.cp_mobile2'     => { imatch => $search_term },
40                          'contacts.cp_satphone'    => { imatch => $search_term },
41                          'contacts.cp_satfax'      => { imatch => $search_term },
42                          'contacts.cp_privatphone' => { imatch => $search_term },
43                  ] ],
44       with_objects => ['contacts']);
45
46     push @results, map {
47       value => $_->displayable_name,
48       label => $_->displayable_name,
49       id    => lc($model) . '_' . $_->id,
50     }, @$result;
51   }
52
53   return \@results;
54 }
55
56 sub select_autocomplete {
57   my ($self) = @_;
58
59   if ($::form->{id} =~ m{^(customer|vendor)_(\d+)$}) {
60     my $type      = $1;
61     my $id        = $2;
62     $::form->{id} = $id;
63
64     if ($type eq 'customer') {
65       SL::Controller::TopQuickSearch::Customer->new->select_autocomplete;
66     } elsif ($type eq 'vendor') {
67       SL::Controller::TopQuickSearch::Vendor->new->select_autocomplete;
68     }
69   }
70 }
71
72 sub do_search {
73   my ($self) = @_;
74
75   my $results = $self->query_autocomplete;
76
77   if (@$results == 1) {
78     $::form->{id} = $results->[0]{id};
79     return $self->select_autocomplete;
80   }
81 }
82
83 # TODO: result overview page
84
85 1;