From: Frank Brehm Date: Mon, 24 Mar 2008 22:05:33 +0000 (+0000) Subject: Autorenliste Buch besser gemacht X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=41d2f1566ef4320fe2cedc50a4392ef26f631047;p=books.git Autorenliste Buch besser gemacht --- diff --git a/lib/FrBr/Books.pm b/lib/FrBr/Books.pm index c1a16aa..44d89b9 100644 --- a/lib/FrBr/Books.pm +++ b/lib/FrBr/Books.pm @@ -60,6 +60,9 @@ __PACKAGE__->config( 'session' => { 'dbic_class' => 'Schema::Session', 'expires' => 3600, + 'private_data' => [ + 'book_data_edit', + ], }, ); diff --git a/lib/FrBr/Books/Controller/Books.pm b/lib/FrBr/Books/Controller/Books.pm index f7a6405..d8b1036 100644 --- a/lib/FrBr/Books/Controller/Books.pm +++ b/lib/FrBr/Books/Controller/Books.pm @@ -136,31 +136,202 @@ sub form_new : Path('new') { 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; @@ -169,6 +340,26 @@ sub form_new : Path('new') { #------------------------------------------------------- +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 diff --git a/lib/FrBr/Books/Plugin/ConfigLoader.pm b/lib/FrBr/Books/Plugin/ConfigLoader.pm index b5b9924..cb62899 100644 --- a/lib/FrBr/Books/Plugin/ConfigLoader.pm +++ b/lib/FrBr/Books/Plugin/ConfigLoader.pm @@ -65,7 +65,7 @@ sub finalize_config { $log4perlconf .= "log4perl.appender.A1=Log::Log4perl::Appender::Screen\n"; } $log4perlconf .= "log4perl.appender.A1.layout=PatternLayout\n"; - $log4perlconf .= "log4perl.appender.A1.layout.ConversionPattern=[%d] [CookBook] [%p] %m%n\n"; + $log4perlconf .= "log4perl.appender.A1.layout.ConversionPattern=[%d] [Books] [%p] %m%n\n"; $c->log->debug( $K . "Log4perl-Konfiguration:\n" . $log4perlconf ) if $c->config->{'debug_level'} >= 1; $c->log( Catalyst::Log::Log4perl->new( \$log4perlconf ) ); diff --git a/root/src/books/book_form.tt2 b/root/src/books/book_form.tt2 index d4ea58d..f350ffe 100644 --- a/root/src/books/book_form.tt2 +++ b/root/src/books/book_form.tt2 @@ -23,16 +23,17 @@ Autoren des Buchs - [% FOR autor_id IN autor_list_no %] [% END %] -

-

-

- +

+

+

+

+ - [% FOR autor_id IN autor_list_book %] [% END %]