[freeside-commits] freeside/FS/FS m2m_Common.pm, 1.2, 1.3 part_pkg_link.pm, NONE, 1.1

Ivan,,, ivan at wavetail.420.am
Tue Apr 15 17:16:54 PDT 2008


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv17115/FS

Modified Files:
	m2m_Common.pm 
Added Files:
	part_pkg_link.pm 
Log Message:
new package def editor

Index: m2m_Common.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/m2m_Common.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- m2m_Common.pm	26 Jan 2007 08:04:37 -0000	1.2
+++ m2m_Common.pm	16 Apr 2008 00:16:52 -0000	1.3
@@ -25,8 +25,9 @@
 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 +37,45 @@
 
 Available options:
 
-link_table (required) - 
+=over 4
 
-target_table (required) - 
+=item link_table (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
+=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 ) = @_;
 
   my $self_pkey = $self->dbdef_table->primary_key;
-  my %hash = ( $self_pkey => $self->$self_pkey() );
+  my $base_field = $opt{'base_field'} || $self_pkey;
+  my %hash = $opt{'hashref'} || {};
+  $hash{$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,9 +94,9 @@
 
   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 )
@@ -89,16 +109,16 @@
   }
 
   foreach my $add_targetnum (
-    grep { ! qsearchs( $link_table, { %hash, $target_pkey => $_ } ) }
-    map  { /^($target_pkey)?(\d+)$/; $2; }
-    grep { /^($target_pkey)?(\d+)$/ }
+    grep { ! qsearchs( $link_table, { %hash, $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,
+      $target_field => $add_targetnum,
     });
     my $error = $add_obj->insert;
     if ( $error ) {

--- NEW FILE: part_pkg_link.pm ---
package FS::part_pkg_link;

use strict;
use vars qw( @ISA );
use FS::Record qw( qsearchs );
use FS::part_pkg;

@ISA = qw(FS::Record);

=head1 NAME

FS::part_pkg_link - Object methods for part_pkg_link records

=head1 SYNOPSIS

  use FS::part_pkg_link;

  $record = new FS::part_pkg_link \%hash;
  $record = new FS::part_pkg_link { 'column' => 'value' };

  $error = $record->insert;

  $error = $new_record->replace($old_record);

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::part_pkg_link object represents an link from one package definition to
another.  FS::part_pkg_link inherits from FS::Record.  The following fields are
currently supported:

=over 4

=item pkglinknum

primary key

=item src_pkgpart

Source package (see L<FS::part_pkg>)

=item dst_pkgpart

Destination package (see L<FS::part_pkg>)

=item link_type

Link type - currently, "bill" (source package bills a line item from target
package), or "svc" (source package includes services from target package).

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new link.  To add the link to the database, see L<"insert">.

Note that this stores the hash reference, not a distinct copy of the hash it
points to.  You can ask the object for a copy with the I<hash> method.

=cut

# the new method can be inherited from FS::Record, if a table method is defined

sub table { 'part_pkg_link'; }

=item insert

Adds this record to the database.  If there is an error, returns the error,
otherwise returns false.

=cut

# the insert method can be inherited from FS::Record

=item delete

Delete this record from the database.

=cut

# the delete method can be inherited from FS::Record

=item replace OLD_RECORD

Replaces the OLD_RECORD with this one in the database.  If there is an error,
returns the error, otherwise returns false.

=cut

# the replace method can be inherited from FS::Record

=item check

Checks all fields to make sure this is a valid link.  If there is
an error, returns the error, otherwise returns false.  Called by the insert
and replace methods.

=cut

# the check method should currently be supplied - FS::Record contains some
# data checking routines

sub check {
  my $self = shift;

  my $error = 
    $self->ut_numbern('pkglinknum')
    || $self->ut_foreign_key('src_pkgpart', 'part_pkg', 'pkgpart')
    || $self->ut_foreign_key('dst_pkgpart', 'part_pkg', 'pkgpart')
    || $self->ut_text('link_type', [ 'bill', 'svc' ] )
  ;
  return $error if $error;

  $self->SUPER::check;
}

=item src_pkg

Returns the source part_pkg object (see L<FS::part_pkg>).

=cut

sub src_pkg {
  my $self = shift;
  qsearchs('part_pkg', { 'pkgpart' => $self->src_pkgpart } );
}

=item dst_pkg

Returns the source part_pkg object (see L<FS::part_pkg>).

=cut

sub dst_pkg {
  my $self = shift;
  qsearchs('part_pkg', { 'pkgpart' => $self->dst_pkgpart } );
}

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>, schema.html from the base documentation.

=cut

1;




More information about the freeside-commits mailing list