ACLs: finish group edit (agents + rights) & browse
[freeside.git] / FS / FS / m2name_Common.pm
1 package FS::m2name_Common;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use FS::Schema qw( dbdef );
6 use FS::Record qw( qsearch qsearchs ); #dbh );
7
8 @ISA = qw( FS::Record );
9
10 $DEBUG = 0;
11
12 =head1 NAME
13
14 FS::m2name_Common - Base class for tables with a related table listing names
15
16 =head1 SYNOPSIS
17
18 use FS::m2name_Common;
19
20 @ISA = qw( FS::m2name_Common );
21
22 =head1 DESCRIPTION
23
24 FS::m2name_Common is intended as a base class for classes which have a
25 related table that lists names.
26
27 =head1 METHODS
28
29 =over 4
30
31 =item process_m2name
32
33 =cut
34
35 sub process_m2name {
36   my( $self, %opt ) = @_;
37
38   my $self_pkey = $self->dbdef_table->primary_key;
39   my $link_sourcekey = $opt{'num_col'} || $self_pkey;
40
41   my $link_table = $self->_load_table($opt{'link_table'});
42
43   my $link_static = $opt{'link_static'} || {};
44
45   foreach my $name ( @{ $opt{'names_list'} } ) {
46
47     my $obj = qsearchs( $link_table, {
48         $link_sourcekey  => $self->$self_pkey(),
49         $opt{'name_col'} => $name,
50         %$link_static,
51     });
52
53     if ( $obj && ! $opt{'params'}->{"$link_table.$name"} ) {
54
55       my $d_obj = $obj; #need to save $obj for below.
56       my $error = $d_obj->delete;
57       die "error deleting $d_obj for $link_table.$name: $error" if $error;
58
59     } elsif ( $opt{'params'}->{"$link_table.$name"} && ! $obj ) {
60
61       #ok to clobber it now (but bad form nonetheless?)
62       #$obj = new "FS::$link_table" ( {
63       $obj = "FS::$link_table"->new( {
64         $link_sourcekey  => $self->$self_pkey(),
65         $opt{'name_col'} => $name,
66         %$link_static,
67       });
68       my $error = $obj->insert;
69       die "error inserting $obj for $link_table.$name: $error" if $error;
70     }
71
72   }
73
74   '';
75 }
76
77 sub _load_table {
78   my( $self, $table ) = @_;
79   eval "use FS::$table";
80   die $@ if $@;
81   $table;
82 }
83
84 =back
85
86 =head1 BUGS
87
88 =head1 SEE ALSO
89
90 L<FS::Record>
91
92 =cut
93
94 1;
95