TopQuickSearch: typisierte Suche gefixt
[kivitendo-erp.git] / SL / Controller / TopQuickSearch / Article.pm
1 package SL::Controller::TopQuickSearch::Article;
2
3 use strict;
4 use parent qw(Rose::Object);
5
6 use SL::Locale::String qw(t8);
7 use SL::DB::Part;
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Base;
10
11 use Rose::Object::MakeMethods::Generic (
12   'scalar --get_set_init' => [ qw(parts models part) ],
13 );
14
15 sub auth { 'part_service_assembly_edit' }
16
17 sub name { 'article' }
18
19 sub description_config { t8('Articles') }
20
21 sub description_field { t8('Articles') }
22
23 sub query_autocomplete {
24   my ($self) = @_;
25
26   my $objects = $self->models->get;
27
28   [
29     map {
30      value       => $_->displayable_name,
31      label       => $_->displayable_name,
32      id          => $_->id,
33     }, @$objects
34   ];
35 }
36
37 sub select_autocomplete {
38   redirect_to_part($::form->{id});
39 }
40
41 sub do_search {
42   my ($self) = @_;
43
44   my $objects = $self->models->get;
45
46   return !@$objects     ? ()
47        : @$objects == 1 ? $self->redirect_to_part($objects->[0]->id)
48        :                  $self->redirect_to_search($::form->{term});
49 }
50
51 sub redirect_to_search {
52   my ($self, $term) = @_;
53
54   SL::Controller::Base->new->url_for(
55     controller   => 'ic.pl',
56     action       => 'generate_report',
57     all          => $term,
58     (searchitems => $self->type) x!!$self->type,
59   );
60 }
61
62 sub redirect_to_part {
63   my ($self, $term) = @_;
64
65   SL::Controller::Base->new->url_for(
66     controller => 'ic.pl',
67     action     => 'edit',
68     id         => $term,
69   );
70 }
71
72 sub type {
73   ()
74 }
75
76 sub init_models {
77   my ($self) = @_;
78
79   SL::Controller::Helper::GetModels->new(
80     controller => $self,
81     model      => 'Part',
82     source     => {
83       filter => {
84         (type => $self->type) x!!$self->type,
85         'all:substr:multi::ilike' => $::form->{term},
86       },
87     },
88     sorted     => {
89       _default   => {
90         by  => 'partnumber',
91         dir => 1,
92       },
93       partnumber => t8('Partnumber'),
94     },
95     paginated  => {
96       per_page => 10,
97     },
98   )
99 }
100
101 1;