communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[freeside.git] / FS / FS / m2m_Common.pm
index 5dc2a8e..6774a48 100644 (file)
@@ -1,7 +1,7 @@
 package FS::m2m_Common;
 
 use strict;
-use vars qw( @ISA $DEBUG );
+use vars qw( @ISA $DEBUG $me );
 use FS::Schema qw( dbdef );
 use FS::Record qw( qsearch qsearchs dbh );
 
@@ -9,6 +9,7 @@ use FS::Record qw( qsearch qsearchs dbh );
 #@ISA = qw( FS::Record );
 
 $DEBUG = 0;
+$me = '[FS::m2m_Common]';
 
 =head1 NAME
 
@@ -25,8 +26,9 @@ use FS::m2m_Common;
 FS::m2m_Common is intended as a mixin class for classes which have a
 many-to-many relationship with another table (via a linking table).
 
-Note: It is currently assumed that the link table contains two fields
-named the same as the primary keys of ths base and target tables.
+It is currently assumed that the link table contains two fields named the same
+as the primary keys of the base and target tables, but you can ovverride this
+assumption if your table is different.
 
 =head1 METHODS
 
@@ -36,26 +38,50 @@ named the same as the primary keys of ths base and target tables.
 
 Available options:
 
-link_table (required) - 
+=over 4
+
+=item link_table (required)
+
+=item target_table (required)
 
-target_table (required) - 
+=item params (required)
 
-params (required) - hashref; keys are primary key values in target_table (values are boolean).  For convenience, keys may optionally be prefixed with the name
-of the primary key, as in agentnum54 instead of 54, or passed as an arrayref
+hashref; keys are primary key values in target_table (values are boolean).  For convenience, keys may optionally be prefixed with the name
+of the primary key, as in "agentnum54" instead of "54", or passed as an arrayref
 of values.
 
+=item base_field (optional)
+
+base field, defaults to primary key of this base table
+
+=item target_field (optional)
+
+target field, defaults to the primary key of the target table
+
+=item hashref (optional)
+
+static hashref further qualifying the m2m fields
+
 =cut
 
 sub process_m2m {
   my( $self, %opt ) = @_;
 
+  #use Data::Dumper;
+  #warn "$me process_m2m called on $self with options:\n". Dumper(%opt)
+  warn "$me process_m2m called on $self"
+    if $DEBUG;
+
   my $self_pkey = $self->dbdef_table->primary_key;
-  my %hash = ( $self_pkey => $self->$self_pkey() );
+  my $base_field = $opt{'base_field'} || $self_pkey;
+  my $hashref = $opt{'hashref'} || {};
+  $hashref->{$base_field} = $self->$self_pkey();
 
   my $link_table = $self->_load_table($opt{'link_table'});
 
   my $target_table = $self->_load_table($opt{'target_table'});
-  my $target_pkey = dbdef->table($target_table)->primary_key;
+  my $target_field = $opt{'target_field'}
+                     || dbdef->table($target_table)->primary_key;
 
   if ( ref($opt{'params'}) eq 'ARRAY' ) {
     $opt{'params'} = { map { $_=>1 } @{$opt{'params'}} };
@@ -74,12 +100,12 @@ sub process_m2m {
 
   foreach my $del_obj (
     grep { 
-           my $targetnum = $_->$target_pkey();
+           my $targetnum = $_->$target_field();
            (    ! $opt{'params'}->{$targetnum}
-             && ! $opt{'params'}->{"$target_pkey$targetnum"}
+             && ! $opt{'params'}->{"$target_field$targetnum"}
            );
          }
-         qsearch( $link_table, \%hash )
+         qsearch( $link_table, $hashref )
   ) {
     my $error = $del_obj->delete;
     if ( $error ) {
@@ -89,16 +115,16 @@ sub process_m2m {
   }
 
   foreach my $add_targetnum (
-    grep { ! qsearchs( $link_table, { %hash, $target_pkey => $_ } ) }
-    map  { /^($target_pkey)?(\d+)$/; $2; }
-    grep { /^($target_pkey)?(\d+)$/ }
+    grep { ! qsearchs( $link_table, { %$hashref, $target_field => $_ } ) }
+    map  { /^($target_field)?(\d+)$/; $2; }
+    grep { /^($target_field)?(\d+)$/ }
     grep { $opt{'params'}->{$_} }
     keys %{ $opt{'params'} }
   ) {
 
     my $add_obj = "FS::$link_table"->new( {
-      %hash
-      $target_pkey => $add_targetnum,
+      %$hashref
+      $target_field => $add_targetnum,
     });
     my $error = $add_obj->insert;
     if ( $error ) {