allow svc_broadband to link directly to a router, #14698
[freeside.git] / FS / FS / router.pm
1 package FS::router;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs qsearch dbh );
6 use FS::addr_block;
7
8 @ISA = qw( FS::Record FS::m2m_Common );
9
10 =head1 NAME
11
12 FS::router - Object methods for router records
13
14 =head1 SYNOPSIS
15
16   use FS::router;
17
18   $record = new FS::router \%hash;
19   $record = new FS::router { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::router record describes a broadband router, such as a DSLAM or a wireless
32  access point.  FS::router inherits from FS::Record.  The following 
33 fields are currently supported:
34
35 =over 4
36
37 =item routernum - primary key
38
39 =item routername - descriptive name for the router
40
41 =item svcnum - svcnum of the owning FS::svc_broadband, if appropriate
42
43 =item auto_addr - flag to automatically assign IP addresses to services
44 linked to this router ('Y' or null).
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Create a new record.  To add the record to the database, see "insert".
55
56 =cut
57
58 sub table { 'router'; }
59
60 =item insert
61
62 Adds this record to the database.  If there is an error, returns the error,
63 otherwise returns false.
64
65 =item delete
66
67 Deletes this record from the database.  If there is an error, returns the
68 error, otherwise returns false.
69
70 =item replace OLD_RECORD
71
72 Replaces OLD_RECORD with this one in the database.  If there is an error,
73 returns the error, otherwise returns false.
74
75 =item check
76
77 Checks all fields to make sure this is a valid record.  If there is an error,
78 returns the error, otherwise returns false.  Called by the insert and replace
79 methods.
80
81 =cut
82
83 sub check {
84   my $self = shift;
85
86   my $error =
87     $self->ut_numbern('routernum')
88     || $self->ut_text('routername')
89     || $self->ut_enum('auto_addr', [ '', 'Y' ])
90     || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration')
91   ;
92   return $error if $error;
93
94   $self->SUPER::check;
95 }
96
97 =item delete
98
99 Deletes this router if and only if no address blocks (see L<FS::addr_block>)
100 are currently allocated to it.
101
102 =cut
103
104 sub delete {
105     my $self = shift;
106
107     return 'Router has address blocks allocated to it' if $self->addr_block;
108     
109     local $SIG{HUP} = 'IGNORE';
110     local $SIG{INT} = 'IGNORE';
111     local $SIG{QUIT} = 'IGNORE';
112     local $SIG{TERM} = 'IGNORE';
113     local $SIG{TSTP} = 'IGNORE';
114     local $SIG{PIPE} = 'IGNORE';
115
116     my $oldAutoCommit = $FS::UID::AutoCommit;
117     local $FS::UID::AutoCommit = 0;
118     my $dbh = dbh;
119     
120     my $error = $self->SUPER::delete;
121     if ( $error ) {
122        $dbh->rollback if $oldAutoCommit;
123        return $error;
124     }
125   
126     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
127     '';
128 }
129
130 =item addr_block
131
132 Returns a list of FS::addr_block objects (address blocks) associated
133 with this object.
134
135 =item auto_addr_block
136
137 Returns a list of address blocks on which auto-assignment of IP addresses
138 is enabled.
139
140 =cut
141
142 sub addr_block {
143   my $self = shift;
144   return qsearch('addr_block', { routernum => $self->routernum });
145 }
146
147 sub auto_addr_block {
148   my $self = shift;
149   return () if !$self->auto_addr;
150   return qsearch('addr_block', { routernum => $self->routernum,
151                                  manual_flag => '' });
152 }
153
154 =item part_svc_router
155
156 Returns a list of FS::part_svc_router objects associated with this 
157 object.  This is unlikely to be useful for any purpose other than retrieving 
158 the associated FS::part_svc objects.  See below.
159
160 =cut
161
162 sub part_svc_router {
163   my $self = shift;
164   return qsearch('part_svc_router', { routernum => $self->routernum });
165 }
166
167 =item part_svc
168
169 Returns a list of FS::part_svc objects associated with this object.
170
171 =cut
172
173 sub part_svc {
174   my $self = shift;
175   return map { qsearchs('part_svc', { svcpart => $_->svcpart }) }
176       $self->part_svc_router;
177 }
178
179 =item agent
180
181 Returns the agent associated with this router, if any.
182
183 =cut
184
185 sub agent {
186   qsearchs('agent', { 'agentnum' => shift->agentnum });
187 }
188
189 =back
190
191 =head1 SEE ALSO
192
193 FS::svc_broadband, FS::router, FS::addr_block, FS::part_svc,
194 schema.html from the base documentation.
195
196 =cut
197
198 1;
199