1package App::Netdisco::DB::ResultSet::DeviceModule;
2use base 'App::Netdisco::DB::ResultSet';
3
4use strict;
5use warnings;
6
7=head1 ADDITIONAL METHODS
8
9=head2 search_by_field( \%cond, \%attrs? )
10
11This variant of the standard C<search()> method returns a ResultSet of Device
12Module entries. It is written to support web forms which accept fields that
13match and locate Device Modules in the database.
14
15The hashref parameter should contain fields from the Device Module table
16which will be intelligently used in a search query.
17
18In addition, you can provide the key C<matchall> which, given a True or False
19value, controls whether fields must all match or whether any can match, to
20select a row.
21
22Supported keys:
23
24=over 4
25
26=item matchall
27
28If a True value, fields must all match to return a given row of the Device
29table, otherwise any field matching will cause the row to be included in
30results.
31
32=item description
33
34Can match the C<description> field as a substring.
35
36=item name
37
38Can match the C<name> field as a substring.
39
40=item type
41
42Can match the C<type> field as a substring.
43
44=item model
45
46Can match the C<model> field as a substring.
47
48=item serial
49
50Can match the C<serial> field as a substring.
51
52=item class
53
54Will match exactly the C<class> field.
55
56=item ips
57
58List of Device IPs containing modules.
59
60=back
61
62=cut
63
64sub search_by_field {
65    my ( $rs, $p, $attrs ) = @_;
66
67    die "condition parameter to search_by_field must be hashref\n"
68        if ref {} ne ref $p
69            or 0 == scalar keys %$p;
70
71    my $op = $p->{matchall} ? '-and' : '-or';
72
73    return $rs->search_rs( {}, $attrs )->search(
74        {   $op => [
75                (   $p->{description}
76                    ? ( 'me.description' =>
77                            { '-ilike' => "\%$p->{description}\%" } )
78                    : ()
79                ),
80                (   $p->{name}
81                    ? ( 'me.name' => { '-ilike' => "\%$p->{name}\%" } )
82                    : ()
83                ),
84                (   $p->{type}
85                    ? ( 'me.type' => { '-ilike' => "\%$p->{type}\%" } )
86                    : ()
87                ),
88                (   $p->{model}
89                    ? ( 'me.model' => { '-ilike' => "\%$p->{model}\%" } )
90                    : ()
91                ),
92                (   $p->{serial}
93                    ? ( 'me.serial' => { '-ilike' => "\%$p->{serial}\%" } )
94                    : ()
95                ),
96
97                (   $p->{class}
98                    ? ( 'me.class' => { '-in' => $p->{class} } )
99                    : ()
100                ),
101                ( $p->{ips} ? ( 'me.ip' => { '-in' => $p->{ips} } ) : () ),
102            ],
103        }
104    );
105}
106
1071;
108