1 package FS::part_export;
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::part_export_option;
13 FS::part_export - Object methods for part_export records
19 $record = new FS::part_export \%hash;
20 $record = new FS::part_export { 'column' => 'value' };
22 ($new_record, $options) = $template_recored->clone( $svcpart );
24 $error = $record->insert( { 'option' => 'value' } );
25 $error = $record->insert( \%options );
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::part_export object represents an export of Freeside data to an external
36 provisioning system. FS::part_export inherits from FS::Record. The following
37 fields are currently supported:
41 =item exportnum - primary key
43 =item svcpart - Service definition (see L<FS::part_svc>) to which this export applies
45 =item machine - Machine name
47 =item exporttype - Export type
49 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
59 Creates a new export. To add the export to the database, see L<"insert">.
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to. You can ask the object for a copy with the I<hash> method.
66 # the new method can be inherited from FS::Record, if a table method is defined
68 sub table { 'part_export'; }
72 An alternate constructor. Creates a new export by duplicating an existing
73 export. The given svcpart is assigned to the new export.
75 Returns a list consisting of the new export object and a hashref of options.
81 my $class = ref($self);
82 my %hash = $self->hash;
83 $hash{'exportnum'} = '';
84 $hash{'svcpart'} = shift;
85 ( $class->new( \%hash ),
86 { map { $_->optionname => $_->optionvalue }
87 qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
94 Adds this record to the database. If there is an error, returns the error,
95 otherwise returns false.
97 If a hash reference of options is supplied, part_export_option records are
98 created (see L<FS::part_export_option>).
102 #false laziness w/queue.pm
106 local $SIG{HUP} = 'IGNORE';
107 local $SIG{INT} = 'IGNORE';
108 local $SIG{QUIT} = 'IGNORE';
109 local $SIG{TERM} = 'IGNORE';
110 local $SIG{TSTP} = 'IGNORE';
111 local $SIG{PIPE} = 'IGNORE';
113 my $oldAutoCommit = $FS::UID::AutoCommit;
114 local $FS::UID::AutoCommit = 0;
117 my $error = $self->SUPER::insert;
119 $dbh->rollback if $oldAutoCommit;
123 foreach my $optionname ( keys %{$options} ) {
124 my $part_export_option = new FS::part_export_option ( {
125 'exportnum' => $self->exportnum,
126 'optionname' => $optionname,
127 'optionvalue' => $options->{$optionname},
129 $error = $part_export_option->insert;
131 $dbh->rollback if $oldAutoCommit;
136 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
144 Delete this record from the database.
148 #foreign keys would make this much less tedious... grr dumb mysql
151 local $SIG{HUP} = 'IGNORE';
152 local $SIG{INT} = 'IGNORE';
153 local $SIG{QUIT} = 'IGNORE';
154 local $SIG{TERM} = 'IGNORE';
155 local $SIG{TSTP} = 'IGNORE';
156 local $SIG{PIPE} = 'IGNORE';
158 my $oldAutoCommit = $FS::UID::AutoCommit;
159 local $FS::UID::AutoCommit = 0;
162 my $error = $self->SUPER::delete;
164 $dbh->rollback if $oldAutoCommit;
168 foreach my $part_export_option ( $self->part_export_option ) {
169 my $error = $part_export_option->delete;
171 $dbh->rollback if $oldAutoCommit;
176 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
182 =item replace OLD_RECORD HASHREF
184 Replaces the OLD_RECORD with this one in the database. If there is an error,
185 returns the error, otherwise returns false.
187 If a hash reference of options is supplied, part_export_option records are
188 created or modified (see L<FS::part_export_option>).
196 local $SIG{HUP} = 'IGNORE';
197 local $SIG{INT} = 'IGNORE';
198 local $SIG{QUIT} = 'IGNORE';
199 local $SIG{TERM} = 'IGNORE';
200 local $SIG{TSTP} = 'IGNORE';
201 local $SIG{PIPE} = 'IGNORE';
203 my $oldAutoCommit = $FS::UID::AutoCommit;
204 local $FS::UID::AutoCommit = 0;
207 my $error = $self->SUPER::replace($old);
209 $dbh->rollback if $oldAutoCommit;
213 foreach my $optionname ( keys %{$options} ) {
214 my $old = qsearchs( 'part_export_option', {
215 'exportnum' => $self->exportnum,
216 'optionname' => $optionname,
218 my $new = new FS::part_export_option ( {
219 'exportnum' => $self->exportnum,
220 'optionname' => $optionname,
221 'optionvalue' => $options->{$optionname},
223 $new->optionnum($old->optionnum) if $old;
224 my $error = $old ? $new->replace($old) : $new->insert;
226 $dbh->rollback if $oldAutoCommit;
231 #remove extraneous old options
233 grep { !exists $options->{$_->optionname} } $old->part_export_option
235 my $error = $opt->delete;
237 $dbh->rollback if $oldAutoCommit;
242 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
250 Checks all fields to make sure this is a valid export. If there is
251 an error, returns the error, otherwise returns false. Called by the insert
259 $self->ut_numbern('exportnum')
260 || $self->ut_domain('machine')
261 || $self->ut_number('svcpart')
262 || $self->ut_alpha('exporttype')
264 return $error if $error;
266 return "Unknown svcpart: ". $self->svcpart
267 unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
269 $self->machine =~ /^([\w\-\.]*)$/
270 or return "Illegal machine: ". $self->machine;
273 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
283 Returns the service definition (see L<FS::part_svc>) for this export.
289 qsearchs('part_svc', { svcpart => $self->svcpart } );
292 =item part_export_option
294 Returns all options as FS::part_export_option objects (see
295 L<FS::part_export_option>).
299 sub part_export_option {
301 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
306 Returns a list of option names and values suitable for assigning to a hash.
312 map { $_->optionname => $_->optionvalue } $self->part_export_option;
315 =item option OPTIONNAME
317 Returns the option value for the given name, or the empty string.
323 my $part_export_option =
324 qsearchs('part_export_option', {
325 exportnum => $self->exportnum,
328 $part_export_option ? $part_export_option->optionvalue : '';
333 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
334 EXPORTTYPE is the object's I<exporttype> field. There should be better docs
335 on how to create new exports (and they should live in their own files and be
336 autoloaded-on-demand), but until then, see L</NEW EXPORT CLASSES>.
342 my $exporttype = $self->exporttype;
343 my $class = ref($self). "::$exporttype";
345 bless($self, $class);
348 =item export_insert SVC_OBJECT
355 $self->_export_insert(@_);
361 # my $method = $AUTOLOAD;
362 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
363 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
364 # $self->$method(@_);
367 =item export_replace NEW OLD
374 $self->_export_replace(@_);
384 $self->_export_delete(@_);
387 #fallbacks providing useful error messages intead of infinite loops
390 return "_export_insert: unknown export type ". $self->exporttype;
393 sub _export_replace {
395 return "_export_replace: unknown export type ". $self->exporttype;
400 return "_export_delete: unknown export type ". $self->exporttype;
405 =head1 NEW EXPORT CLASSES
407 Should be added to httemplate/edit/part_export.cgi and a module should
408 be FS/FS/part_export/ (an example may be found in eg/export_template.pm)
414 Hmm... cust_export class (not necessarily a database table...) ... ?
418 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
419 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.