push @{$c->stash->{'cssfiles'}}, 'books/form.css';
$c->stash->{'error_message'} = '';
- $c->stash->{'book_edit'} = {};
- $c->stash->{'book_edit'}{'title'} = 'Neues Buch';
- my $autor_list_complete = get_author_list($c);
+ $self->prepare_data_structures($c);
- $c->stash->{'autor_list_book'} = [];
- $c->stash->{'autor_list_no'} = [ map { $_->{'id'} } @$autor_list_complete ];
+ $self->bookdata_cgi2session($c);
- $c->stash->{'autor_list'} = {};
- for my $autor ( @$autor_list_complete ) {
- $c->stash->{'autor_list'}{ $autor->{'id'} } = $autor->{'name'};
+ $c->stash->{'book_edit'} = {} unless $c->stash->{'book_edit'};
+ $self->bookdata_session2stash($c);
+ $c->stash->{'book_edit'}{'title'} = "Neues Buch" unless $c->stash->{'book_edit'}{'title'};
+
+ unless ( $c->request->params->{'book_form_sent'} ) {
+ return 1;
}
- $c->log->debug( get_output_string( $K . "Autoren gesamt: ", $autor_list_complete ) );
- $autor_list_complete = undef;
- $c->log->debug( get_output_string( $K . "Autoren-Hash: ", $c->stash->{'autor_list'} ) );
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub bookdata_session2stash : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = __PACKAGE__ . "::bookdata_session2stash(): ";
+
+ $c->stash->{'book_edit'} = {} unless $c->stash->{'book_edit'};
+ $c->stash->{'book_edit'}{'title'} = $c->session->{'book_data_edit'}{'title'} if defined $c->session->{'book_data_edit'}{'title'};
+ $c->stash->{'book_edit'}{'id'} = $c->session->{'book_data_edit'}{'id'} if $c->session->{'book_data_edit'}{'id'};
+
+ my %Autor;
+
+ $c->stash->{'autor_list_book'} = [];
+ for my $autor_id ( @{$c->session->{'book_data_edit'}{'autoren'}} ) {
+ push @{$c->stash->{'autor_list_book'}}, $autor_id;
+ $Autor{$autor_id} = 1;
+ }
$c->log->debug( get_output_string( $K . "Autoren des Buches: ", $c->stash->{'autor_list_book'} ) );
+
+ $c->stash->{'autor_list_no'} = [];
+ for my $autor_id ( sort { lc($c->stash->{'autor_list'}{$a}) cmp lc($c->stash->{'autor_list'}{$b}) } keys %{$c->stash->{'autor_list'}} ) {
+ push @{$c->stash->{'autor_list_no'}}, $autor_id unless $Autor{$autor_id};
+ }
$c->log->debug( get_output_string( $K . "Autoren, die fehlen: ", $c->stash->{'autor_list_no'} ) );
- unless ( $c->request->params->{'book_form_sent'} ) {
- return 1;
+ $c->stash->{'book_edit'}{'title'} = $c->session->{'book_data_edit'}{'title'};
+ $c->stash->{'book_edit'}{'untertitel'} = $c->session->{'book_data_edit'}{'untertitel'};
+ $c->stash->{'book_edit'}{'original_title'} = $c->session->{'book_data_edit'}{'original_title'};
+ $c->stash->{'book_edit'}{'isbn'} = $c->session->{'book_data_edit'}{'isbn'};
+ $c->stash->{'book_edit'}{'book_nr'} = $c->session->{'book_data_edit'}{'book_nr'};
+ $c->stash->{'book_edit'}{'ausgabejahr'} = $c->session->{'book_data_edit'}{'ausgabejahr'};
+ $c->stash->{'book_edit'}{'druckjahr'} = $c->session->{'book_data_edit'}{'druckjahr'};
+ $c->stash->{'book_edit'}{'seiten'} = $c->session->{'book_data_edit'}{'seiten'};
+ $c->stash->{'book_edit'}{'preis'} = $c->session->{'book_data_edit'}{'preis'};
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub bookdata_cgi2session : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = __PACKAGE__ . "::bookdata_cgi2session(): ";
+
+ return 1 unless $c->request->params->{'book_form_sent'};
+
+ # Basis anlegen, wenn notwendig
+ $c->session->{'book_data_edit'} = {} unless $c->session->{'book_data_edit'};
+
+ # Buch-Id eintragen, wenn notwendig
+ $c->session->{'book_data_edit'}{'id'} = $c->request->params->{'book_id'} if $c->request->params->{'book_id'};
+
+ # Autoren aus den CGI-Feldern uebernehmen
+ $c->session->{'book_data_edit'}{'autoren'} = [] unless $c->session->{'book_data_edit'}{'autoren'};
+
+ my %Autor;
+ my $i = 1;
+ for my $autor_id ( @{$c->session->{'book_data_edit'}{'autoren'}} ) {
+ $Autor{$autor_id} = $i;
+ $i++;
}
if ( $c->request->params->{'wird_autor'} ) {
- return 1;
+ if ( $c->request->params->{'autor_not'} ) {
+ my $autoren = ref($c->request->params->{'autor_not'}) eq 'ARRAY' ? $c->request->params->{'autor_not'} : [$c->request->params->{'autor_not'}];
+ $c->log->debug( get_output_string( $K . "Fuege Autor hinzu: ", $autoren ) );
+ for my $autor_id ( @$autoren ) {
+ next unless $autor_id;
+ unless ( $Autor{$autor_id} ) {
+ push @{$c->session->{'book_data_edit'}{'autoren'}}, $autor_id;
+ $Autor{$autor_id} = scalar( @{$c->session->{'book_data_edit'}{'autoren'}} );
+ }
+ }
+ }
+ }
+ elsif ( $c->request->params->{'entf_autor'} ) {
+ if ( $c->request->params->{'autor_book'} ) {
+ my $autoren = ref($c->request->params->{'autor_book'}) eq 'ARRAY' ? $c->request->params->{'autor_book'} : [$c->request->params->{'autor_book'}];
+ $c->log->debug( get_output_string( $K . "Loesche Autor: ", $autoren ) );
+ for my $autor_id ( @$autoren ) {
+ next unless $autor_id;
+ $i = $Autor{$autor_id};
+ next unless $i;
+ splice( @{$c->session->{'book_data_edit'}{'autoren'}}, $i - 1, 1 );
+ $i = 1;
+ %Autor = ();
+ for my $aid ( @{$c->session->{'book_data_edit'}{'autoren'}} ) {
+ $Autor{$aid} = $i;
+ $i++;
+ }
+ }
+ }
+ }
+ elsif ( $c->request->params->{'nach_oben'} ) {
+ if ( $c->request->params->{'autor_book'} ) {
+ my $autoren = ref($c->request->params->{'autor_book'}) eq 'ARRAY' ? $c->request->params->{'autor_book'} : [$c->request->params->{'autor_book'}];
+ $c->log->debug( get_output_string( $K . "Bewege Autor nach oben: ", $autoren ) );
+ for my $autor_id ( @$autoren ) {
+ next unless $autor_id;
+ $c->log->debug( get_output_string( $K . "Autoren vorher: ", $c->session->{'book_data_edit'}{'autoren'} ) );
+ my $new_list = [];
+ my $last_aid = undef;
+ for my $aid ( @{$c->session->{'book_data_edit'}{'autoren'}} ) {
+ unless ( $last_aid ) {
+ $last_aid = $aid;
+ next;
+ }
+ if ( $aid == $autor_id ) {
+ push @$new_list, $aid;
+ next;
+ }
+ push @$new_list, $last_aid;
+ $last_aid = $aid;
+ }
+ push @$new_list, $last_aid;
+ $c->session->{'book_data_edit'}{'autoren'} = $new_list;
+ $c->log->debug( get_output_string( $K . "Autoren hinterher: ", $c->session->{'book_data_edit'}{'autoren'} ) );
+ }
+ }
+ }
+ elsif ( $c->request->params->{'nach_unten'} ) {
+ if ( $c->request->params->{'autor_book'} ) {
+ my $autoren = ref($c->request->params->{'autor_book'}) eq 'ARRAY' ? $c->request->params->{'autor_book'} : [$c->request->params->{'autor_book'}];
+ $c->log->debug( get_output_string( $K . "Bewege Autor nach unten: ", $autoren ) );
+ for my $autor_id ( @$autoren ) {
+ next unless $autor_id;
+ $c->log->debug( get_output_string( $K . "Autoren vorher: ", $c->session->{'book_data_edit'}{'autoren'} ) );
+ my $new_list = [];
+ my $last_aid = undef;
+ for my $aid ( @{$c->session->{'book_data_edit'}{'autoren'}} ) {
+ unless ( $last_aid ) {
+ $last_aid = $aid;
+ next;
+ }
+ unless ( $aid == $autor_id ) {
+ push @$new_list, $aid;
+ next;
+ }
+ push @$new_list, $last_aid;
+ $last_aid = $aid;
+ }
+ push @$new_list, $last_aid;
+ $c->session->{'book_data_edit'}{'autoren'} = $new_list;
+ $c->log->debug( get_output_string( $K . "Autoren hinterher: ", $c->session->{'book_data_edit'}{'autoren'} ) );
+ }
+ }
+ }
+
+ # Buchtitel
+ $c->session->{'book_data_edit'}{'title'} = $c->request->params->{'book_title'} if defined $c->request->params->{'book_title'};
+
+ # Untertitel
+ $c->session->{'book_data_edit'}{'untertitel'} = $c->request->params->{'book_untertitel'} if defined $c->request->params->{'book_untertitel'};
+
+ # Original-Titel
+ $c->session->{'book_data_edit'}{'original_title'} = $c->request->params->{'book_original_title'} if defined $c->request->params->{'book_original_title'};
+
+ # ISBN
+ $c->session->{'book_data_edit'}{'isbn'} = $c->request->params->{'book_isbn'} if defined $c->request->params->{'book_isbn'};
+
+ # Buch-Nr. (Buchklub usw.)
+ $c->session->{'book_data_edit'}{'book_nr'} = $c->request->params->{'book_nr'} if defined $c->request->params->{'book_nr'};
+
+ # Ausgabejahr
+ $c->session->{'book_data_edit'}{'ausgabejahr'} = $c->request->params->{'book_ajahr'} if defined $c->request->params->{'book_ajahr'};
+
+ # Druckjahr
+ $c->session->{'book_data_edit'}{'druckjahr'} = $c->request->params->{'book_djahr'} if defined $c->request->params->{'book_djahr'};
+
+ # Seiten
+ $c->session->{'book_data_edit'}{'seiten'} = $c->request->params->{'book_seiten'} if defined $c->request->params->{'book_seiten'};
+
+ # Preis
+ if ( defined $c->request->params->{'book_preis'} ) {
+ my $preis = to_float($c->request->params->{'book_preis'});
+ $c->session->{'book_data_edit'}{'preis'} = $preis;
}
return 1;
#-------------------------------------------------------
+sub prepare_data_structures : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = __PACKAGE__ . "::prepare_data_structures(): ";
+
+ my $autor_list_complete = get_author_list($c);
+ $c->log->debug( get_output_string( $K . "Autoren gesamt: ", $autor_list_complete ) );
+
+ $c->stash->{'autor_list'} = {};
+ for my $autor ( @$autor_list_complete ) {
+ $c->stash->{'autor_list'}{ $autor->{'id'} } = $autor->{'name'};
+ }
+ $c->log->debug( get_output_string( $K . "Autoren-Hash: ", $c->stash->{'autor_list'} ) );
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
=head1 AUTHOR
Frank Brehm