return 1;
}
+ # Angaben zur Buchserie aus der Datenbank holen
my $sliste = get_serienliste( $c, 'get_books' => 1, 'serien_id' => $sid );
$c->log->debug( get_output_string( $K, "Erhaltene Serienliste: ", $sliste ) ) if $c->stash->{'debug_level'} >= 2;
#-------------------------------------------------------
+=head2 delete_serie_form( )
+
+Loescht eine vorhandene Buchserie.
+
+=cut
+
+sub delete_serie_form : Path('delete') {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ unless ( $c->user_exists() ) {
+ $c->detach('/not_allowed');
+ return;
+ }
+
+ $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'};
+ $self->add_serienlist_menu($c) if $c->session->{'from_serie_list'};
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/serie/delete"),
+ 'name' => "Löschen"
+ };
+
+ $c->stash->{'error_message'} = '';
+
+ my $sid = to_int( $c->request->params->{'delete_serie_id'} || $c->session->{'delete_serie_id'} || 0 );
+ unless ( $sid ) {
+ $c->stash->{'template'} = 'error.tt2';
+ $c->stash->{'error'} = 'Keine oder ungültige Buchserien-Id übergeben.';
+ return 1;
+ }
+ $c->session->{'delete_serie_id'} = $sid;
+
+ # Angaben zur Buchserie aus der Datenbank holen
+ my $sliste = get_serienliste( $c, 'serien_id' => $sid );
+ $c->log->debug( get_output_string( $K, "Erhaltene Serienliste: ", $sliste ) ) if $c->stash->{'debug_level'} >= 2;
+
+ unless ( $sliste and scalar( @$sliste ) ) {
+ $c->stash->{'template'} = 'error.tt2';
+ $c->stash->{'error'} = 'Zur übergebenen Buchserien-Id wurde nichts gefunden.';
+ return 1;
+ }
+
+ my $serie = $sliste->[0];
+ $c->log->debug( get_output_string( $K, "Buchserie aus Datenbank: ", $serie ) ) if $c->stash->{'debug_level'} >= 2;
+
+ my $anzahl_buecher = $c->model('Schema::Buch2serie')->count( { 'serien_id' => $sid, } );
+
+ if ( $anzahl_buecher ) {
+ $c->stash->{'template'} = 'error.tt2';
+ $c->stash->{'error'} = 'Zur übergebenen Buchserie gehören noch Bücher.';
+ return 1;
+ }
+
+ $c->stash->{'return_target'} = $c->session->{'return_target_serie_save'} or $c->web_path("/serie/list");
+
+ $c->stash->{'serie'} = $serie;
+ $sliste = undef;
+
+ $c->stash->{'template'} = 'serie/delete.tt2';
+ push @{$c->stash->{'cssfiles'}}, 'serie/view.css';
+
+ if ( $c->request->params->{'really_delete_serie'} and $c->request->params->{'really_delete_serie'} eq 'yes' and $c->request->params->{'do_delete'} ) {
+ eval {
+ delete_serie( $c, $sid );
+ };
+ if ( $@ ) {
+ $c->stash->{'error_message'} = $@;
+ $c->log->warn( "Fehler beim Löschen der Buchserie: " . $@ );
+ return undef;
+ }
+ $c->stash->{'template'} = 'serie/delete_success.tt2';
+ }
+
+}
+
+#-------------------------------------------------------
+
=head2 form_edit( )
Aendern einer vorhandenen Buchserie.
return unless ( $c->stash->{'book_list'} and $c->stash->{'book_array_sorted'} );
+ $c->stash->{'selected_book_ids'} = {};
+ my $bids = ref( $c->request->params->{'book_to_list'} ) eq 'ARRAY' ? $c->request->params->{'book_to_list'} : [$c->request->params->{'book_to_list'}];
+ for my $buch_id ( @$bids ) {
+ $c->stash->{'selected_book_ids'}{$buch_id} = 1;
+ }
+ $bids = ref( $c->request->params->{'book_from_list'} ) eq 'ARRAY' ? $c->request->params->{'book_from_list'} : [$c->request->params->{'book_from_list'}];
+ for my $buch_id ( @$bids ) {
+ $c->stash->{'selected_book_ids'}{$buch_id} = 1;
+ }
+
my %Buch = ();
my $i = 1;
for my $buch_id ( @{$c->session->{'serie_data_edit'}{'book_ids'}} ) {
my %Buch;
+ $c->stash->{'serie_edit'}{'book_ids'} = [];
+
# Buch-Id's eintragen, die zur Serie gehoeren
$c->stash->{'books_in_list'} = [];
for my $buch_id ( @{$c->session->{'serie_data_edit'}{'book_ids'}} ) {
next unless $c->stash->{'book_list'}{$buch_id};
push @{$c->stash->{'books_in_list'}}, $buch_id;
+ push @{$c->stash->{'serie_edit'}{'book_ids'}}, $buch_id;
$Buch{$buch_id} = 1;
}
$c->log->debug( get_output_string( $K . "Bücher dieser Buchserie: ", $c->stash->{'books_in_list'} ) );
@EXPORT = qw(
&get_serienliste
&save_serie
+ &delete_serie
);
#%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
my $K = ( caller(0) )[3] . "(): ";
$c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+ $c->log->debug( get_output_string( $K, "Zu speichernde Serie: ", $serie ) ) if $c->stash->{'debug_level'} >= 2;
my $storage = $c->stash->{'storage'};
+ my $save_books = ( $serie->{'id'} and $serie->{'book_ids'} and ref($serie->{'book_ids'}) and ref($serie->{'book_ids'}) eq 'ARRAY') ? 1 : 0;
+ $c->log->debug( $K . "Speichern der Bücher erforderlich: " . ( $save_books ? 'ja' : 'nein' ) . "." ) if $c->stash->{'debug_level'} > 2;
my $save_func = sub {
my ( $storage, $dbh, $serien_id, $serien_name, $serien_desc ) = @_;
};
$storage->dbh_do( $save_func, $serie->{'id'}, $serie->{'name'}, $serie->{'descr'} );
+ my $serien_id = $storage->last_insert_id();
+ return $serien_id unless $serien_id;
- return $storage->last_insert_id();
+ save_serien_buecher( $c, $serien_id, $serie->{'book_ids'} ) if $save_books;
+
+ return $serien_id;
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_serien_buecher( $c, $serien_id, @$book_ids )
+
+=cut
+
+sub save_serien_buecher {
+
+ my $c = shift;
+ my $serien_id = shift;
+ my $book_ids = shift;
+
+ my $K = ( caller(0) )[3] . "(): ";
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ my $storage = $c->stash->{'storage'};
+ my $dbh = $storage->dbh();
+
+ my $anzahl = 0;
+ $anzahl = scalar( @$book_ids );
+
+ my $in = '';
+ $in = '(' . join( ", ", map { $dbh->quote($_) } @$book_ids ) . ")" if $anzahl;
+
+ my $sql;
+ $sql = <<ENDE;
+DELETE FROM `buch2serie`
+ WHERE `serien_id` = ?
+ENDE
+ $sql .= " AND `buch_id` NOT IN " . $in . "\n" if $anzahl;
+
+ my $qparams = [];
+ push @$qparams, $serien_id;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute( @$qparams );
+
+ return 1 unless $anzahl;
+
+ my $i = 0;
+
+ $sql = <<ENDE;
+INSERT INTO `buch2serie` (
+ `buch_id`, `serien_id`, `ord_num`
+ ) VALUES (
+ ?, ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `ord_num` = ?
+ENDE
+
+ $sth = $dbh->prepare($sql);
+
+ for my $buch_id ( @$book_ids ) {
+
+ $qparams = [];
+ push @$qparams, $buch_id;
+ push @$qparams, $serien_id;
+ push @$qparams, $i;
+ push @$qparams, $i;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ $sth->execute( @$qparams );
+
+ $i++;
+ }
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 delete_serie( $c, $serien_id )
+
+Loescht die Buchserie mit der uebergebenen Serien-Id
+
+=cut
+
+sub delete_serie {
+
+ my $c = shift;
+ my $serien_id = shift;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen fuer Serien-Id " . ( defined $serien_id ? $serien_id : '<undef>' ) . "." ) if $c->stash->{'debug_level'} > 2;
+
+ return undef unless $serien_id and to_int($serien_id);
+ $serien_id = to_int($serien_id);
+
+ my $schema = $c->model('Schema')->{'schema'};
+
+ # Raus, wenn in der Serie noch Buecher sind ...
+ if ( $c->model('Schema::Buch2serie')->count( { 'serien_id' => $serien_id, } ) ) {
+ my $msg = 'In der Buchserie sind noch Bücher enthalten.';
+ die $msg . "\n";
+ }
+
+ # Das eigentliche Löschen
+ my $coderef = sub {
+ $c->model('Schema::Serien')->search( { 'id' => $serien_id } )->delete();
+ };
+
+ my $rs;
+ eval {
+ $rs = $schema->txn_do($coderef);
+ };
+
+ if ($@) { # Transaction failed
+ my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+ die $msg . "\n";
+ return undef;
+ }
+ return 1;
}
</tr><tr>
<td><select name="book_to_list" size="15">
<option value="">-- Buch zum Hinzufügen wählen --</option>[% FOR book_id IN books_not_in_list %]
- <option value="[% book_id %]">[% book_list.$book_id %]</option>
+ <option value="[% book_id %]"[% IF selected_book_ids.$book_id %] selected[% END %]>[% book_list.$book_id %]</option>
[% END %]</select></td>
<td><input type="submit" name="zur_serie" value="⇒" class="shift_button" title="Zur Serie hinzufügen" />
<br /><br />
</td>
<td><select name="book_from_list" size="15">
<option value="">-- Buch zum Entfernen von der Serie wählen --</option>[% FOR book_id IN books_in_list %]
- <option value="[% book_id %]">[% book_list.$book_id %]</option>
+ <option value="[% book_id %]"[% IF selected_book_ids.$book_id %] selected[% END %]>[% book_list.$book_id %]</option>
[% END %]</select></td>
</tr></table></td>
[% END %]</tr><tr>