Part.pm - alte Funktion entfernt und POD angepasst
[kivitendo-erp.git] / SL / DB / Helper / ActsAsList.pm
index 8f3333e..abe11c0 100644 (file)
@@ -7,6 +7,7 @@ our @EXPORT = qw(move_position_up move_position_down add_to_list remove_from_lis
                  get_previous_in_list get_next_in_list get_full_list);
 
 use Carp;
                  get_previous_in_list get_next_in_list get_full_list);
 
 use Carp;
+use SL::X;
 
 my %list_spec;
 
 
 my %list_spec;
 
@@ -14,12 +15,13 @@ sub import {
   my ($class, @params)   = @_;
   my $importing = caller();
 
   my ($class, @params)   = @_;
   my $importing = caller();
 
+  configure_acts_as_list($importing, @params);
+
   $importing->before_save(  sub { SL::DB::Helper::ActsAsList::set_position(@_)    });
   $importing->before_delete(sub { SL::DB::Helper::ActsAsList::remove_position(@_) });
 
   $importing->before_save(  sub { SL::DB::Helper::ActsAsList::set_position(@_)    });
   $importing->before_delete(sub { SL::DB::Helper::ActsAsList::remove_position(@_) });
 
-  # Use 'goto' so that Exporter knows which module to import into via
-  # 'caller()'.
-  goto &Exporter::import;
+  # Don't 'goto' to Exporters import, it would try to parse @params
+  __PACKAGE__->export_to_level(1, $class, @EXPORT);
 }
 
 #
 }
 
 #
@@ -39,7 +41,7 @@ sub move_position_down {
 sub remove_from_list {
   my ($self) = @_;
 
 sub remove_from_list {
   my ($self) = @_;
 
-  my $worker = sub {
+  return $self->db->with_transaction(sub {
     remove_position($self);
 
     # Set to -1 manually because $self->update_attributes() would
     remove_position($self);
 
     # Set to -1 manually because $self->update_attributes() would
@@ -55,9 +57,7 @@ sub remove_from_list {
 SQL
     $self->db->dbh->do($sql, undef, $self->$primary_key_col);
     $self->$column(undef);
 SQL
     $self->db->dbh->do($sql, undef, $self->$primary_key_col);
     $self->$column(undef);
-  };
-
-  return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
+  });
 }
 
 sub add_to_list {
 }
 
 sub add_to_list {
@@ -108,12 +108,10 @@ sub add_to_list {
       ${group_by}
 SQL
 
       ${group_by}
 SQL
 
-  my $worker = sub {
+  return $self->db->with_transaction(sub {
     $self->db->dbh->do($query, undef, $new_position - 1, @values);
     $self->update_attributes($column => $new_position);
     $self->db->dbh->do($query, undef, $new_position - 1, @values);
     $self->update_attributes($column => $new_position);
-  };
-
-  return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
+  });
 }
 
 sub get_next_in_list {
 }
 
 sub get_next_in_list {
@@ -143,15 +141,17 @@ sub reorder_list {
 
   my $self   = ref($class_or_self) ? $class_or_self : $class_or_self->new;
   my $column = column_name($self);
 
   my $self   = ref($class_or_self) ? $class_or_self : $class_or_self->new;
   my $column = column_name($self);
-  my $result = $self->db->do_transaction(sub {
+  my $result = $self->db->with_transaction(sub {
     my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|;
     my $sth   = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr;
 
     foreach my $new_position (1 .. scalar(@ids)) {
     my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|;
     my $sth   = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr;
 
     foreach my $new_position (1 .. scalar(@ids)) {
-      $sth->execute($new_position, $ids[$new_position - 1]) || die $sth->errstr;
+      $sth->execute($new_position, $ids[$new_position - 1]) || die SL::X::DBUtilsError->new(error => $sth->errstr);
     }
 
     $sth->finish;
     }
 
     $sth->finish;
+
+    1;
   });
 
   return $result;
   });
 
   return $result;
@@ -322,7 +322,7 @@ column
 =head1 SYNOPSIS
 
   package SL::DB::SomeObject;
 =head1 SYNOPSIS
 
   package SL::DB::SomeObject;
-  use SL::DB::Helper::ActsAsList;
+  use SL::DB::Helper::ActsAsList [ PARAMS ];
 
   package SL::Controller::SomeController;
   ...
 
   package SL::Controller::SomeController;
   ...
@@ -347,7 +347,8 @@ in the table plus one.
 When the object is deleted all positions greater than the object's old
 position are decreased by one.
 
 When the object is deleted all positions greater than the object's old
 position are decreased by one.
 
-The column name to use can be configured via L<configure_acts_as_list>.
+C<PARAMS> will be given to L<configure_acts_as_list> and can be used to
+set the column name.
 
 =head1 CLASS FUNCTIONS
 
 
 =head1 CLASS FUNCTIONS
 
@@ -355,8 +356,8 @@ The column name to use can be configured via L<configure_acts_as_list>.
 
 =item C<configure_acts_as_list %params>
 
 
 =item C<configure_acts_as_list %params>
 
-Configures the mixin's behaviour. C<%params> can contain the following
-values:
+Configures the mixin's behaviour. Will get called automatically with the
+include parameters. C<%params> can contain the following values:
 
 =over 2
 
 
 =over 2