Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 manual_addr - set to 'Y' to allow services linked to this router 
44 to have any IP address, rather than one in an address block belonging 
45 to the router.
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Create a new record.  To add the record to the database, see "insert".
56
57 =cut
58
59 sub table { 'router'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 =item delete
67
68 Deletes this record from the database.  If there is an error, returns the
69 error, otherwise returns false.
70
71 =item replace OLD_RECORD
72
73 Replaces OLD_RECORD with this one in the database.  If there is an error,
74 returns the error, otherwise returns false.
75
76 =item check
77
78 Checks all fields to make sure this is a valid record.  If there is an error,
79 returns the error, otherwise returns false.  Called by the insert and replace
80 methods.
81
82 =cut
83
84 sub check {
85   my $self = shift;
86
87   my $error =
88     $self->ut_numbern('routernum')
89     || $self->ut_text('routername')
90     || $self->ut_enum('manual_addr', [ '', 'Y' ])
91     || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration')
92   ;
93   return $error if $error;
94
95   $self->SUPER::check;
96 }
97
98 =item delete
99
100 Deletes this router if and only if no address blocks (see L<FS::addr_block>)
101 are currently allocated to it.
102
103 =cut
104
105 sub delete {
106     my $self = shift;
107
108     return 'Router has address blocks allocated to it' if $self->addr_block;
109     
110     local $SIG{HUP} = 'IGNORE';
111     local $SIG{INT} = 'IGNORE';
112     local $SIG{QUIT} = 'IGNORE';
113     local $SIG{TERM} = 'IGNORE';
114     local $SIG{TSTP} = 'IGNORE';
115     local $SIG{PIPE} = 'IGNORE';
116
117     my $oldAutoCommit = $FS::UID::AutoCommit;
118     local $FS::UID::AutoCommit = 0;
119     my $dbh = dbh;
120     
121     my $error = $self->SUPER::delete;
122     if ( $error ) {
123        $dbh->rollback if $oldAutoCommit;
124        return $error;
125     }
126   
127     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128     '';
129 }
130
131 =item addr_block
132
133 Returns a list of FS::addr_block objects (address blocks) associated
134 with this object.
135
136 =item auto_addr_block
137
138 Returns a list of address blocks on which auto-assignment of IP addresses
139 is enabled.
140
141 =cut
142
143 sub addr_block {
144   my $self = shift;
145   return qsearch('addr_block', { routernum => $self->routernum });
146 }
147
148 sub auto_addr_block {
149   my $self = shift;
150   return () if $self->manual_addr;
151   return qsearch('addr_block', { routernum => $self->routernum,
152                                  manual_flag => '' });
153 }
154
155 =item part_svc_router
156
157 Returns a list of FS::part_svc_router objects associated with this 
158 object.  This is unlikely to be useful for any purpose other than retrieving 
159 the associated FS::part_svc objects.  See below.
160
161 =cut
162
163 sub part_svc_router {
164   my $self = shift;
165   return qsearch('part_svc_router', { routernum => $self->routernum });
166 }
167
168 =item part_svc
169
170 Returns a list of FS::part_svc objects associated with this object.
171
172 =cut
173
174 sub part_svc {
175   my $self = shift;
176   return map { qsearchs('part_svc', { svcpart => $_->svcpart }) }
177       $self->part_svc_router;
178 }
179
180 =item agent
181
182 Returns the agent associated with this router, if any.
183
184 =cut
185
186 sub agent {
187   qsearchs('agent', { 'agentnum' => shift->agentnum });
188 }
189
190 =back
191
192 =head1 SEE ALSO
193
194 FS::svc_broadband, FS::router, FS::addr_block, FS::part_svc,
195 schema.html from the base documentation.
196
197 =cut
198
199 1;
200