autoload methods returning foreign records, RT#13971
[freeside.git] / FS / FS / svc_Tower_Mixin.pm
1 package FS::svc_Tower_Mixin;
2
3 use strict;
4
5 =item tower_sector
6
7 =item tower_sector_sql HASHREF
8
9 Class method which returns a list of WHERE clause fragments to 
10 search for services with tower/sector given by HASHREF.  Can 
11 contain 'towernum' and 'sectornum' keys, either of which can be 
12 an arrayref or a single value.  To use this, the search needs to
13 join to tower_sector.
14
15 towernum or sectornum can also contain 'none' to allow null values.
16
17 =cut
18
19 sub tower_sector_sql {
20   my( $class, $params ) = @_;
21   return () unless keys %$params;
22
23   my @where = ();
24   for my $field (qw(towernum sectornum)) {
25     my $value = $params->{$field} or next;
26     if ( ref $value and grep { $_ } @$value ) {
27       my $in = join(',', map { /^(\d+)$/ ? $1 : () } @$value);
28       my @orwhere;
29       push @orwhere, "tower_sector.$field IN ($in)" if $in;
30       push @orwhere, "tower_sector.$field IS NULL" if grep /^none$/, @$value;
31       push @where, '( '.join(' OR ', @orwhere).' )';
32     }
33     elsif ( $value =~ /^(\d+)$/ ) {
34       push @where, "tower_sector.$field = $1";
35     }
36     elsif ( $value eq 'none' ) {
37       push @where, "tower_sector.$field IS NULL";
38     }
39   }
40   @where;
41 }
42
43 1;