| 1 | package App::Netdisco::DB::ResultSet::DeviceModule; | ||
| 2 | use base 'App::Netdisco::DB::ResultSet'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | =head1 ADDITIONAL METHODS | ||
| 8 | |||
| 9 | =head2 search_by_field( \%cond, \%attrs? ) | ||
| 10 | |||
| 11 | This variant of the standard C<search()> method returns a ResultSet of Device | ||
| 12 | Module entries. It is written to support web forms which accept fields that | ||
| 13 | match and locate Device Modules in the database. | ||
| 14 | |||
| 15 | The hashref parameter should contain fields from the Device Module table | ||
| 16 | which will be intelligently used in a search query. | ||
| 17 | |||
| 18 | In addition, you can provide the key C<matchall> which, given a True or False | ||
| 19 | value, controls whether fields must all match or whether any can match, to | ||
| 20 | select a row. | ||
| 21 | |||
| 22 | Supported keys: | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =item matchall | ||
| 27 | |||
| 28 | If a True value, fields must all match to return a given row of the Device | ||
| 29 | table, otherwise any field matching will cause the row to be included in | ||
| 30 | results. | ||
| 31 | |||
| 32 | =item description | ||
| 33 | |||
| 34 | Can match the C<description> field as a substring. | ||
| 35 | |||
| 36 | =item name | ||
| 37 | |||
| 38 | Can match the C<name> field as a substring. | ||
| 39 | |||
| 40 | =item type | ||
| 41 | |||
| 42 | Can match the C<type> field as a substring. | ||
| 43 | |||
| 44 | =item model | ||
| 45 | |||
| 46 | Can match the C<model> field as a substring (case sensitive). | ||
| 47 | |||
| 48 | =item serial | ||
| 49 | |||
| 50 | Can match the C<serial> field as a substring (case sensitive). | ||
| 51 | |||
| 52 | =item class | ||
| 53 | |||
| 54 | Will match exactly the C<class> field. | ||
| 55 | |||
| 56 | =item ips | ||
| 57 | |||
| 58 | List of Device IPs containing modules. | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub 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' => { '-like' => "\%$p->{model}\%" } ) | ||
| 90 | : () | ||
| 91 | ), | ||
| 92 | ( $p->{serial} | ||
| 93 | ? ( 'me.serial' => { '-like' => "\%$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 | |||
| 107 | 1; |