summaryrefslogtreecommitdiff
path: root/FS/FS/m2m_Common.pm
blob: 6774a48e2293f91f209f0f448da7a5a09330b443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package FS::m2m_Common;

use strict;
use vars qw( @ISA $DEBUG $me );
use FS::Schema qw( dbdef );
use FS::Record qw( qsearch qsearchs dbh );

#hmm.  well.  we seem to be used as a mixin.
#@ISA = qw( FS::Record );

$DEBUG = 0;
$me = '[FS::m2m_Common]';

=head1 NAME

FS::m2m_Common - Mixin class for classes in a many-to-many relationship

=head1 SYNOPSIS

use FS::m2m_Common;

@ISA = qw( FS::m2m_Common FS::Record );

=head1 DESCRIPTION

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).

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

=over 4

=item process_m2m OPTION => VALUE, ...

Available options:

=over 4

=item link_table (required)

=item target_table (required)

=item 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
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 $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_field = $opt{'target_field'}
                     || dbdef->table($target_table)->primary_key;

  if ( ref($opt{'params'}) eq 'ARRAY' ) {
    $opt{'params'} = { map { $_=>1 } @{$opt{'params'}} };
  }

  local $SIG{HUP} = 'IGNORE';
  local $SIG{INT} = 'IGNORE';
  local $SIG{QUIT} = 'IGNORE';
  local $SIG{TERM} = 'IGNORE';
  local $SIG{TSTP} = 'IGNORE';
  local $SIG{PIPE} = 'IGNORE';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  foreach my $del_obj (
    grep { 
           my $targetnum = $_->$target_field();
           (    ! $opt{'params'}->{$targetnum}
             && ! $opt{'params'}->{"$target_field$targetnum"}
           );
         }
         qsearch( $link_table, $hashref )
  ) {
    my $error = $del_obj->delete;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      return $error;
    }
  }

  foreach my $add_targetnum (
    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( {
      %$hashref, 
      $target_field => $add_targetnum,
    });
    my $error = $add_obj->insert;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      return $error;
    }
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
  '';
}

sub _load_table {
  my( $self, $table ) = @_;
  eval "use FS::$table";
  die $@ if $@;
  $table;
}

#=item target_table
#
#=cut
#
#sub target_table {
#  my $self = shift;
#  my $target_table = $self->_target_table;
#  eval "use FS::$target_table";
#  die $@ if $@;
#  $target_table;
#}

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>

=cut

1;