export NAS table to sqlradius, #14697
[freeside.git] / FS / FS / part_export.pm
1 package FS::part_export;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG %exports );
5 use Exporter;
6 use Tie::IxHash;
7 use base qw( FS::option_Common FS::m2m_Common ); # m2m for 'export_nas'
8 use FS::Record qw( qsearch qsearchs dbh );
9 use FS::part_svc;
10 use FS::part_export_option;
11 use FS::export_svc;
12
13 #for export modules, though they should probably just use it themselves
14 use FS::queue;
15
16 @EXPORT_OK = qw(export_info);
17
18 $DEBUG = 0;
19
20 =head1 NAME
21
22 FS::part_export - Object methods for part_export records
23
24 =head1 SYNOPSIS
25
26   use FS::part_export;
27
28   $record = new FS::part_export \%hash;
29   $record = new FS::part_export { 'column' => 'value' };
30
31   #($new_record, $options) = $template_recored->clone( $svcpart );
32
33   $error = $record->insert( { 'option' => 'value' } );
34   $error = $record->insert( \%options );
35
36   $error = $new_record->replace($old_record);
37
38   $error = $record->delete;
39
40   $error = $record->check;
41
42 =head1 DESCRIPTION
43
44 An FS::part_export object represents an export of Freeside data to an external
45 provisioning system.  FS::part_export inherits from FS::Record.  The following
46 fields are currently supported:
47
48 =over 4
49
50 =item exportnum - primary key
51
52 =item exportname - Descriptive name
53
54 =item machine - Machine name 
55
56 =item exporttype - Export type
57
58 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
59
60 =back
61
62 =head1 METHODS
63
64 =over 4
65
66 =item new HASHREF
67
68 Creates a new export.  To add the export to the database, see L<"insert">.
69
70 Note that this stores the hash reference, not a distinct copy of the hash it
71 points to.  You can ask the object for a copy with the I<hash> method.
72
73 =cut
74
75 # the new method can be inherited from FS::Record, if a table method is defined
76
77 sub table { 'part_export'; }
78
79 =cut
80
81 #=item clone SVCPART
82 #
83 #An alternate constructor.  Creates a new export by duplicating an existing
84 #export.  The given svcpart is assigned to the new export.
85 #
86 #Returns a list consisting of the new export object and a hashref of options.
87 #
88 #=cut
89 #
90 #sub clone {
91 #  my $self = shift;
92 #  my $class = ref($self);
93 #  my %hash = $self->hash;
94 #  $hash{'exportnum'} = '';
95 #  $hash{'svcpart'} = shift;
96 #  ( $class->new( \%hash ),
97 #    { map { $_->optionname => $_->optionvalue }
98 #        qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
99 #    }
100 #  );
101 #}
102
103 =item insert HASHREF
104
105 Adds this record to the database.  If there is an error, returns the error,
106 otherwise returns false.
107
108 If a hash reference of options is supplied, part_export_option records are
109 created (see L<FS::part_export_option>).
110
111 =item delete
112
113 Delete this record from the database.
114
115 =cut
116
117 #foreign keys would make this much less tedious... grr dumb mysql
118 sub delete {
119   my $self = shift;
120   local $SIG{HUP} = 'IGNORE';
121   local $SIG{INT} = 'IGNORE';
122   local $SIG{QUIT} = 'IGNORE';
123   local $SIG{TERM} = 'IGNORE';
124   local $SIG{TSTP} = 'IGNORE';
125   local $SIG{PIPE} = 'IGNORE';
126
127   my $oldAutoCommit = $FS::UID::AutoCommit;
128   local $FS::UID::AutoCommit = 0;
129   my $dbh = dbh;
130
131   my $error = $self->SUPER::delete;
132   if ( $error ) {
133     $dbh->rollback if $oldAutoCommit;
134     return $error;
135   }
136
137   foreach my $export_svc ( $self->export_svc ) {
138     my $error = $export_svc->delete;
139     if ( $error ) {
140       $dbh->rollback if $oldAutoCommit;
141       return $error;
142     }
143   }
144
145   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
146
147   '';
148
149 }
150
151 =item check
152
153 Checks all fields to make sure this is a valid export.  If there is
154 an error, returns the error, otherwise returns false.  Called by the insert
155 and replace methods.
156
157 =cut
158
159 sub check {
160   my $self = shift;
161   my $error = 
162     $self->ut_numbern('exportnum')
163     || $self->ut_textn('exportname')
164     || $self->ut_domain('machine')
165     || $self->ut_alpha('exporttype')
166   ;
167   return $error if $error;
168
169   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
170   $self->nodomain($1);
171
172   $self->deprecated(1); #BLAH
173
174   #check exporttype?
175
176   $self->SUPER::check;
177 }
178
179 =item label
180
181 Returns a label for this export, "exportname||exportype (machine)".  
182
183 =cut
184
185 sub label {
186   my $self = shift;
187   ($self->exportname || $self->exporttype ). ' ('. $self->machine. ')';
188 }
189
190 #=item part_svc
191 #
192 #Returns the service definition (see L<FS::part_svc>) for this export.
193 #
194 #=cut
195 #
196 #sub part_svc {
197 #  my $self = shift;
198 #  qsearchs('part_svc', { svcpart => $self->svcpart } );
199 #}
200
201 sub part_svc {
202   use Carp;
203   croak "FS::part_export::part_svc deprecated";
204   #confess "FS::part_export::part_svc deprecated";
205 }
206
207 =item svc_x
208
209 Returns a list of associated FS::svc_* records.
210
211 =cut
212
213 sub svc_x {
214   my $self = shift;
215   map { $_->svc_x } $self->cust_svc;
216 }
217
218 =item cust_svc
219
220 Returns a list of associated FS::cust_svc records.
221
222 =cut
223
224 sub cust_svc {
225   my $self = shift;
226   map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
227     grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
228       $self->export_svc;
229 }
230
231 =item export_svc
232
233 Returns a list of associated FS::export_svc records.
234
235 =cut
236
237 sub export_svc {
238   my $self = shift;
239   qsearch('export_svc', { 'exportnum' => $self->exportnum } );
240 }
241
242 =item export_device
243
244 Returns a list of associated FS::export_device records.
245
246 =cut
247
248 sub export_device {
249   my $self = shift;
250   qsearch('export_device', { 'exportnum' => $self->exportnum } );
251 }
252
253 =item part_export_option
254
255 Returns all options as FS::part_export_option objects (see
256 L<FS::part_export_option>).
257
258 =cut
259
260 sub part_export_option {
261   my $self = shift;
262   $self->option_objects;
263 }
264
265 =item options 
266
267 Returns a list of option names and values suitable for assigning to a hash.
268
269 =item option OPTIONNAME
270
271 Returns the option value for the given name, or the empty string.
272
273 =item _rebless
274
275 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
276 EXPORTTYPE is the object's I<exporttype> field.  There should be better docs
277 on how to create new exports, but until then, see L</NEW EXPORT CLASSES>.
278
279 =cut
280
281 sub _rebless {
282   my $self = shift;
283   my $exporttype = $self->exporttype;
284   my $class = ref($self). "::$exporttype";
285   eval "use $class;";
286   #die $@ if $@;
287   bless($self, $class) unless $@;
288   $self;
289 }
290
291 #these should probably all go away, just let the subclasses define em
292
293 =item export_insert SVC_OBJECT
294
295 =cut
296
297 sub export_insert {
298   my $self = shift;
299   #$self->rebless;
300   $self->_export_insert(@_);
301 }
302
303 #sub AUTOLOAD {
304 #  my $self = shift;
305 #  $self->rebless;
306 #  my $method = $AUTOLOAD;
307 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
308 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
309 #  $self->$method(@_);
310 #}
311
312 =item export_replace NEW OLD
313
314 =cut
315
316 sub export_replace {
317   my $self = shift;
318   #$self->rebless;
319   $self->_export_replace(@_);
320 }
321
322 =item export_delete
323
324 =cut
325
326 sub export_delete {
327   my $self = shift;
328   #$self->rebless;
329   $self->_export_delete(@_);
330 }
331
332 =item export_suspend
333
334 =cut
335
336 sub export_suspend {
337   my $self = shift;
338   #$self->rebless;
339   $self->_export_suspend(@_);
340 }
341
342 =item export_unsuspend
343
344 =cut
345
346 sub export_unsuspend {
347   my $self = shift;
348   #$self->rebless;
349   $self->_export_unsuspend(@_);
350 }
351
352 #fallbacks providing useful error messages intead of infinite loops
353 sub _export_insert {
354   my $self = shift;
355   return "_export_insert: unknown export type ". $self->exporttype;
356 }
357
358 sub _export_replace {
359   my $self = shift;
360   return "_export_replace: unknown export type ". $self->exporttype;
361 }
362
363 sub _export_delete {
364   my $self = shift;
365   return "_export_delete: unknown export type ". $self->exporttype;
366 }
367
368 #call svcdb-specific fallbacks
369
370 sub _export_suspend {
371   my $self = shift;
372   #warn "warning: _export_suspened unimplemented for". ref($self);
373   my $svc_x = shift;
374   my $new = $svc_x->clone_suspended;
375   $self->_export_replace( $new, $svc_x );
376 }
377
378 sub _export_unsuspend {
379   my $self = shift;
380   #warn "warning: _export_unsuspend unimplemented for ". ref($self);
381   my $svc_x = shift;
382   my $old = $svc_x->clone_kludge_unsuspend;
383   $self->_export_replace( $svc_x, $old );
384 }
385
386 =item export_links SVC_OBJECT ARRAYREF
387
388 Adds a list of web elements to ARRAYREF specific to this export and SVC_OBJECT.
389 The elements are displayed in the UI to lead the the operator to external
390 configuration, monitoring, and similar tools.
391
392 =item export_getsettings SVC_OBJECT SETTINGS_HASHREF DEFAUTS_HASHREF
393
394 Adds a hashref of settings to SETTINGSREF specific to this export and
395 SVC_OBJECT.  The elements can be displayed in the UI on the service view.
396
397 DEFAULTSREF is a hashref with the same keys where true values indicate the
398 setting is a default (and thus can be displayed in the UI with less emphasis,
399 or hidden by default).
400
401 =cut
402
403 =item weight
404
405 Returns the 'weight' element from the export's %info hash, or 0 if there is 
406 no weight defined.
407
408 =cut
409
410 sub weight {
411   my $self = shift;
412   export_info()->{$self->exporttype}->{'weight'} || 0;
413 }
414
415 =back
416
417 =head1 SUBROUTINES
418
419 =over 4
420
421 =item export_info [ SVCDB ]
422
423 Returns a hash reference of the exports for the given I<svcdb>, or if no
424 I<svcdb> is specified, for all exports.  The keys of the hash are
425 I<exporttype>s and the values are again hash references containing information
426 on the export:
427
428   'desc'     => 'Description',
429   'options'  => {
430                   'option'  => { label=>'Option Label' },
431                   'option2' => { label=>'Another label' },
432                 },
433   'nodomain' => 'Y', #or ''
434   'notes'    => 'Additional notes',
435
436 =cut
437
438 sub export_info {
439   #warn $_[0];
440   return $exports{$_[0]} || {} if @_;
441   #{ map { %{$exports{$_}} } keys %exports };
442   my $r = { map { %{$exports{$_}} } keys %exports };
443 }
444
445
446 sub _upgrade_data {  #class method
447   my ($class, %opts) = @_;
448
449   my @part_export_option = qsearch('part_export_option', { 'optionname' => 'overlimit_groups' });
450   foreach my $opt ( @part_export_option ) {
451     next if $opt->optionvalue =~ /^[\d\s]+$/ || !$opt->optionvalue;
452     my @groupnames = split(' ',$opt->optionvalue);
453     my @groupnums;
454     my $error = '';
455     foreach my $groupname ( @groupnames ) {
456         my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
457         unless ( $g ) {
458             $g = new FS::radius_group {
459                             'groupname' => $groupname,
460                             'description' => $groupname,
461                             };
462             $error = $g->insert;
463             die $error if $error;
464         }
465         push @groupnums, $g->groupnum;
466     }
467     $opt->optionvalue(join(' ',@groupnums));
468     $error = $opt->replace;
469     die $error if $error;
470   }
471 }
472
473 #=item exporttype2svcdb EXPORTTYPE
474 #
475 #Returns the applicable I<svcdb> for an I<exporttype>.
476 #
477 #=cut
478 #
479 #sub exporttype2svcdb {
480 #  my $exporttype = $_[0];
481 #  foreach my $svcdb ( keys %exports ) {
482 #    return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
483 #  }
484 #  '';
485 #}
486
487 #false laziness w/part_pkg & cdr
488 foreach my $INC ( @INC ) {
489   foreach my $file ( glob("$INC/FS/part_export/*.pm") ) {
490     warn "attempting to load export info from $file\n" if $DEBUG;
491     $file =~ /\/(\w+)\.pm$/ or do {
492       warn "unrecognized file in $INC/FS/part_export/: $file\n";
493       next;
494     };
495     my $mod = $1;
496     my $info = eval "use FS::part_export::$mod; ".
497                     "\\%FS::part_export::$mod\::info;";
498     if ( $@ ) {
499       die "error using FS::part_export::$mod (skipping): $@\n" if $@;
500       next;
501     }
502     unless ( keys %$info ) {
503       warn "no %info hash found in FS::part_export::$mod, skipping\n"
504         unless $mod =~ /^(passwdfile|null|.+_Common)$/; #hack but what the heck
505       next;
506     }
507     warn "got export info from FS::part_export::$mod: $info\n" if $DEBUG;
508     no strict 'refs';
509     foreach my $svc (
510       ref($info->{'svc'}) ? @{$info->{'svc'}} : $info->{'svc'}
511     ) {
512       unless ( $svc ) {
513         warn "blank svc for FS::part_export::$mod (skipping)\n";
514         next;
515       }
516       $exports{$svc}->{$mod} = $info;
517     }
518   }
519 }
520
521 =back
522
523 =head1 NEW EXPORT CLASSES
524
525 A module should be added in FS/FS/part_export/ (an example may be found in
526 eg/export_template.pm)
527
528 =head1 BUGS
529
530 Hmm... cust_export class (not necessarily a database table...) ... ?
531
532 deprecated column...
533
534 =head1 SEE ALSO
535
536 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
537 L<FS::svc_domain>,
538 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
539
540 =cut
541
542 1;
543