summaryrefslogtreecommitdiff
path: root/FS/FS/m2name_Common.pm
blob: 7c9637e27b36d0d571fc6b34b304698328b2c51a (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
package FS::m2name_Common;

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

@ISA = qw( FS::Record );

$DEBUG = 0;

=head1 NAME

FS::m2name_Common - Base class for tables with a related table listing names

=head1 SYNOPSIS

use FS::m2name_Common;

@ISA = qw( FS::m2name_Common );

=head1 DESCRIPTION

FS::m2name_Common is intended as a base class for classes which have a
related table that lists names.

=head1 METHODS

=over 4

=item process_m2name

=cut

sub process_m2name {
  my( $self, %opt ) = @_;

  my $self_pkey = $self->dbdef_table->primary_key;
  my $link_sourcekey = $opt{'num_col'} || $self_pkey;

  my $link_table = $self->_load_table($opt{'link_table'});

  my $link_static = $opt{'link_static'} || {};

  foreach my $name ( @{ $opt{'names_list'} } ) {

    my $obj = qsearchs( $link_table, {
        $link_sourcekey  => $self->$self_pkey(),
        $opt{'name_col'} => $name,
        %$link_static,
    });

    if ( $obj && ! $opt{'params'}->{"$link_table.$name"} ) {

      my $d_obj = $obj; #need to save $obj for below.
      my $error = $d_obj->delete;
      die "error deleting $d_obj for $link_table.$name: $error" if $error;

    } elsif ( $opt{'params'}->{"$link_table.$name"} && ! $obj ) {

      #ok to clobber it now (but bad form nonetheless?)
      #$obj = new "FS::$link_table" ( {
      $obj = "FS::$link_table"->new( {
        $link_sourcekey  => $self->$self_pkey(),
        $opt{'name_col'} => $name,
        %$link_static,
      });
      my $error = $obj->insert;
      die "error inserting $obj for $link_table.$name: $error" if $error;
    }

  }

  '';
}

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

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>

=cut

1;