4564a634206c0083c8a7cd51c7dbcf6a8c0a64c2
[freeside.git] / FS / FS / nas.pm
1 package FS::nas;
2
3 use strict;
4 use base qw( FS::m2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::export_nas;
7 use FS::part_export;
8
9 =head1 NAME
10
11 FS::nas - Object methods for nas records
12
13 =head1 SYNOPSIS
14
15   use FS::nas;
16
17   $record = new FS::nas \%hash;
18   $record = new FS::nas { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::nas object represents a RADIUS client.  FS::nas inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item nasnum - primary key
36
37 =item nasname - "NAS name", i.e. IP address
38
39 =item shortname - short descriptive name
40
41 =item type - the equipment vendor
42
43 =item ports
44
45 =item secret - the authentication secret for this client
46
47 =item server - virtual server name (optional)
48
49 =item community
50
51 =item description - a longer descriptive name
52
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new NAS.  To add the NAS to the database, see L<"insert">.
63
64 Note that this stores the hash reference, not a distinct copy of the hash it
65 points to.  You can ask the object for a copy with the I<hash> method.
66
67 =cut
68
69 # the new method can be inherited from FS::Record, if a table method is defined
70
71 sub table { 'nas'; }
72
73 =item insert
74
75 Adds this record to the database.  If there is an error, returns the error,
76 otherwise returns false.
77
78 =item delete
79
80 Delete this record from the database and remove all linked exports.
81
82 =cut
83
84 sub delete {
85   my $oldAutoCommit = $FS::UID::AutoCommit;
86   local $FS::UID::AutoCommit = 0;
87   my $dbh = dbh;
88
89   my $self = shift;
90   my $error = $self->process_m2m([])
91            || $self->SUPER::delete;
92
93   if ( $error ) {
94     $dbh->rollback;
95     return $error;
96   }
97   
98   $dbh->commit if $oldAutoCommit;
99   '';
100 }
101
102 =item replace OLD_RECORD
103
104 Replaces the OLD_RECORD with this one in the database.  If there is an error,
105 returns the error, otherwise returns false.
106
107 To change the list of linked exports, see the C<export_nas> method.
108
109 =cut
110
111 sub replace {
112   my $oldAutoCommit = $FS::UID::AutoCommit;
113   local $FS::UID::AutoCommit = 0;
114   my $dbh = dbh;
115
116   my ($self, $old) = @_;
117   $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
118
119   my $error;
120   foreach my $part_export ( $self->part_export ) {
121     $error ||= $part_export->export_nas_replace($self, $old);
122   }
123
124   $error ||= $self->SUPER::replace($old);
125
126   if ( $error ) {
127     $dbh->rollback;
128     return $error;
129   }
130
131   $dbh->commit if $oldAutoCommit;
132   '';
133 }
134
135 =item check
136
137 Checks all fields to make sure this is a valid NAS.  If there is
138 an error, returns the error, otherwise returns false.  Called by the insert
139 and replace methods.
140
141 =cut
142
143 # the check method should currently be supplied - FS::Record contains some
144 # data checking routines
145
146 sub check {
147   my $self = shift;
148
149   my $error = 
150     $self->ut_numbern('nasnum')
151     || $self->ut_text('nasname')
152     || $self->ut_textn('shortname')
153     || $self->ut_text('type')
154     || $self->ut_numbern('ports')
155     || $self->ut_text('secret')
156     || $self->ut_textn('server')
157     || $self->ut_textn('community')
158     || $self->ut_text('description')
159   ;
160   return $error if $error;
161
162   $self->SUPER::check;
163 }
164
165 =item part_export
166
167 Return all L<FS::part_export> objects to which this NAS is being exported.
168
169 =cut
170
171 sub part_export {
172   my $self = shift;
173   map { qsearchs('part_export', { exportnum => $_->exportnum }) } 
174         qsearch('export_nas', { nasnum => $self->nasnum})
175 }
176
177 =back
178
179 =head1 BUGS
180
181 =head1 SEE ALSO
182
183 L<FS::Record>, schema.html from the base documentation.
184
185 =cut
186
187 1;
188