From aff2fab92bb188d54de27f741dfe837330ea49ba Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 11 Nov 2008 22:01:41 +0000 Subject: [PATCH] Bindungsart dazu, mit Speichern begonnen --- lib/FrBr/Books/Controller/Books.pm | 96 ++++++++++++++++++- lib/FrBr/Books/Util/Bindungsart.pm | 127 +++++++++++++++++++++++++ lib/FrBr/Books/Util/Book.pm | 143 ++++++++++++++++++++++++++++- root/src/books/book_form.tt2 | 6 ++ root/src/books/save_success.tt2 | 17 ++++ 5 files changed, 384 insertions(+), 5 deletions(-) create mode 100644 lib/FrBr/Books/Util/Bindungsart.pm create mode 100644 root/src/books/save_success.tt2 diff --git a/lib/FrBr/Books/Controller/Books.pm b/lib/FrBr/Books/Controller/Books.pm index 517d90e..6166a23 100644 --- a/lib/FrBr/Books/Controller/Books.pm +++ b/lib/FrBr/Books/Controller/Books.pm @@ -10,6 +10,7 @@ use base 'Catalyst::Controller'; use FrBr::Common; use FrBr::Books::Util::Author; use FrBr::Books::Util::Book; +use FrBr::Books::Util::Bindungsart; use FrBr::Books::Util::Category; use FrBr::Books::Util::Serie; use FrBr::Books::Util::Ort; @@ -158,10 +159,52 @@ sub form_new : Path('new') { return 1; } + return $self->do_save_book($c); + +} + +#------------------------------------------------------- + +sub do_save_book : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + my $book = $c->stash->{'book_edit'}; + + $book->{'autoren'} = $c->stash->{'autor_list_book'}; + $book->{'kategorien'} = $c->stash->{'kategorie_list_book'}; + $book->{'serien'} = $c->stash->{'serienliste_book'}; + $c->log->debug( get_output_string( $K . "Buchangaben, die gesichert werden sollen: ", $book ) ); + + return 1 unless $self->check_formparams( $c, $book ); + + return 1; + +} + +#------------------------------------------------------- + +sub check_formparams : Private { + + my ( $self, $c, $book ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + unless ( $book->{'title'} ) { + $c->stash->{'error_message'} = "Kein Buchtitel angegeben."; + return undef; + } + + unless ( scalar( @{$book->{'autoren'}} ) ) { + $c->stash->{'error_message'} = "Kein Buchautor angegeben."; + return undef; + } + return 1; } + #------------------------------------------------------- sub bookdata_session2stash : Private { @@ -222,12 +265,17 @@ sub bookdata_session2stash : Private { $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'}{'verlags_id'} = $c->session->{'book_data_edit'}{'verlags_id'}; + $c->stash->{'book_edit'}{'orts_id'} = $c->session->{'book_data_edit'}{'orts_id'}; + $c->stash->{'book_edit'}{'bindungsart_id'} = $c->session->{'book_data_edit'}{'bindungsart_id'}; $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'}; + $c->stash->{'book_edit'}{'waehrungs_id'} = $c->session->{'book_data_edit'}{'waehrungs_id'}; + + $c->log->debug( get_output_string( $K . "Buch-Infos im Stash: ", $c->stash->{'book_edit'} ) ); return 1; @@ -345,17 +393,38 @@ sub bookdata_cgi2session : Private { } # Buchtitel - $c->session->{'book_data_edit'}{'title'} = $c->request->params->{'book_title'} if defined $c->request->params->{'book_title'}; + if ( defined $c->request->params->{'book_title'} ) { + my $title = $c->request->params->{'book_title'}; + $title =~ s/^\s+//; + $title =~ s/\s+$//; + $c->session->{'book_data_edit'}{'title'} = $title; + } # Untertitel - $c->session->{'book_data_edit'}{'untertitel'} = $c->request->params->{'book_untertitel'} if defined $c->request->params->{'book_untertitel'}; + if ( defined $c->request->params->{'book_untertitel'} ) { + my $title = $c->request->params->{'book_untertitel'}; + $title =~ s/^\s+//; + $title =~ s/\s+$//; + $c->session->{'book_data_edit'}{'untertitel'} = $title; + } # Original-Titel - $c->session->{'book_data_edit'}{'original_title'} = $c->request->params->{'book_original_title'} if defined $c->request->params->{'book_original_title'}; + if ( defined $c->request->params->{'book_original_title'} ) { + my $title = $c->request->params->{'book_original_title'}; + $title =~ s/^\s+//; + $title =~ s/\s+$//; + $c->session->{'book_data_edit'}{'original_title'} = $title; + } # Verlag $c->session->{'book_data_edit'}{'verlags_id'} = $c->request->params->{'verlags_id'} if defined $c->request->params->{'verlags_id'}; + # Aufbewahrungsort + $c->session->{'book_data_edit'}{'orts_id'} = $c->request->params->{'orts_id'} if defined $c->request->params->{'orts_id'}; + + # Bindungsart + $c->session->{'book_data_edit'}{'bindungsart_id'} = $c->request->params->{'bindungsart_id'} if defined $c->request->params->{'bindungsart_id'}; + # Kategorien $c->session->{'book_data_edit'}{'kategorien'} = [] unless $c->session->{'book_data_edit'}{'kategorien'}; @@ -441,7 +510,12 @@ sub bookdata_cgi2session : Private { } # ISBN - $c->session->{'book_data_edit'}{'isbn'} = $c->request->params->{'book_isbn'} if defined $c->request->params->{'book_isbn'}; + if ( defined $c->request->params->{'book_isbn'} ) { + my $isbn = $c->request->params->{'book_isbn'}; + $isbn =~ s/^\s+//; + $isbn =~ s/\s+$//; + $c->session->{'book_data_edit'}{'isbn'} = $isbn; + } # Buch-Nr. (Buchklub usw.) $c->session->{'book_data_edit'}{'book_nr'} = $c->request->params->{'book_nr'} if defined $c->request->params->{'book_nr'}; @@ -455,6 +529,9 @@ sub bookdata_cgi2session : Private { # Seiten $c->session->{'book_data_edit'}{'seiten'} = $c->request->params->{'book_seiten'} if defined $c->request->params->{'book_seiten'}; + # Waehrung + $c->session->{'book_data_edit'}{'waehrungs_id'} = $c->request->params->{'waehrungs_id'} if defined $c->request->params->{'waehrungs_id'}; + # Preis if ( defined $c->request->params->{'book_preis'} ) { my $preis = to_float($c->request->params->{'book_preis'}); @@ -516,6 +593,17 @@ sub prepare_data_structures : Private { } $c->log->debug( get_output_string( $K . "Aufbewahrungsorte: ", $c->stash->{'ortsliste'} ) ); + # Bindungsarten zusammensammeln + my $bart_liste = get_bindungsarten_list($c); + $c->log->debug( get_output_string( $K . "Bindungsarten gesamt: ", $bart_liste ) ); + $c->stash->{'bindungsartenliste'} = {}; + $c->stash->{'bindungsarten_ids'} = []; + for my $bart ( @$bart_liste ) { + push @{$c->stash->{'bindungsarten_ids'}}, $bart->{'id'}; + $c->stash->{'bindungsartenliste'}{$bart->{'id'}} = $bart->{'name'}; + } + $c->log->debug( get_output_string( $K . "Bindungsarten: ", $c->stash->{'bindungsartenliste'} ) ); + # Buchserien zusamensammeln my $serienliste = get_serienliste($c); $c->log->debug( get_output_string( $K . "Serien gesamt: ", $serienliste ) ); diff --git a/lib/FrBr/Books/Util/Bindungsart.pm b/lib/FrBr/Books/Util/Bindungsart.pm new file mode 100644 index 0000000..cfee297 --- /dev/null +++ b/lib/FrBr/Books/Util/Bindungsart.pm @@ -0,0 +1,127 @@ +package FrBr::Books::Util::Bindungsart; + +# $Id$ +# $URL$ + +use strict; +use warnings; + +use FrBr::Common; + +# Export-Deklarationen + +BEGIN { + + use Exporter(); + our ( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); + + # set the version for version checking + $VERSION = 0.1; + my ($rev) = '$Revision$' =~ /(\d+)/; + $VERSION = sprintf( $VERSION . ".%d", $rev ); + + @ISA = qw(Exporter); + @EXPORT = qw( + &get_bindungsarten_list + ); + + #%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], + + # your exported package globals go here, + # as well as any optionally exported functions + #@EXPORT_OK = qw($Var1 %Hashit &func3); +} ## end BEGIN + +our @EXPORT_OK; + +=head1 NAME + +FrBr::Books::Util::Bindungsart - Modul fuer Funktionen rund um Bindungsarten + +=head1 METHODS + +=cut + +#----------------------------------------------------------------------------------- + +=head2 get_bindungsarten_list( $c, $params ) + +Sammelt alle Bindungsarten zusammen. + +Folgende benannte Parameter koennen ueber $params uebergeben werden: + +Rueckgabe: Eine Array-Ref von Hash-Refs mit allen Bindungsarten, die den uebergebenen Suchkriterien entsprechen: + + $res = [ + { 'id' => 1, + 'name' => 'Hartcover', + }, + { 'id' => 2, + ... + }, + ... + ]; + +Die Liste ist nach den Bindungsart-Namen alphabetisch sortiert. + +=cut + +sub get_bindungsarten_list { + + my $c = shift; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; + + my $params = {}; + if ( ref($_[0]) and ref($_[0]) eq 'HASH' ) { + $params = $_[0]; + } + else { + %$params = @_; + } + $c->log->debug( get_output_string( $K, "Uebergebene Parameter: ", $params ) ) if $c->stash->{'debug_level'} >= 2; + + my $list = []; + + my $search_params = undef; + + my $other_params = {}; + $other_params->{'order_by'} = [ 'art_name' ]; + $other_params->{'select'} = [ + 'id', + 'art_name', + ]; + $other_params->{'as'} = [ + 'id', + 'art_name', + ]; + + for my $art ( $c->model('Schema::Bindungsarten')->search( $search_params, $other_params )->all() ) { + my $bart = {}; + $bart->{'id'} = $art->id(); + $bart->{'name'} = $art->art_name(); + push @$list, $bart; + } + + return $list; +} + +#----------------------------------------------------------------------------------- + +=head1 AUTHOR + +Frank Brehm + +=head1 LICENSE + +This library is free software, you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +1; + +__END__ + +# vim: noai : ts=4 fenc=utf-8 filetype=perl expandtab : diff --git a/lib/FrBr/Books/Util/Book.pm b/lib/FrBr/Books/Util/Book.pm index 7459baa..20bb780 100644 --- a/lib/FrBr/Books/Util/Book.pm +++ b/lib/FrBr/Books/Util/Book.pm @@ -23,6 +23,7 @@ BEGIN { @ISA = qw(Exporter); @EXPORT = qw( &get_booklist + &save_book ); #%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], @@ -63,7 +64,7 @@ Rueckgabe: Eine Array-Ref mit allen Buchtiteln, die den uebergebenen Suchkriteri sub get_booklist { my $c = shift; - my $K = __PACKAGE__ . "::get_booklist(): "; + my $K = ( caller(0) )[3] . "(): "; $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; @@ -245,6 +246,146 @@ sub get_booklist { #----------------------------------------------------------------------------------- +=head2 save_book( $c, $book ) + + $book = { + 'ausgabejahr' => '2004', + 'autoren' => [ + '11' + ], + 'bindungsart_id' => 4, + 'book_nr' => '', + 'druckjahr' => '2006', + 'isbn' => '3-492-70076-4', + 'kategorien' => [ + '2' + ], + 'original_title' => '', + 'orts_id' => '2', + 'preis' => 14, + 'seiten' => '640', + 'serien' => [ + '4' + ], + 'title' => 'Die Zwerge', + 'untertitel' => '', + 'verlags_id' => '8', + 'waehrungs_id' => '1' + }; + +=cut + +sub save_book { + + my $c = shift; + my $book = shift; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; + + my $storage = $c->stash->{'storage'}; + + my $save_func = sub { + + my $storage = shift; + my $dbh = shift; + my $buch = shift; + + my $sql = <{'id'}; + push @params, $buch->{'title'}; + push @params, $buch->{'original_title'}; + push @params, $buch->{'untertitel'}; + push @params, $buch->{'verlags_id'}; + push @params, $buch->{'orts_id'}; + push @params, $buch->{'bindungsart_id'}; + push @params, $buch->{'isbn'}; + push @params, $buch->{'book_nr'}; + push @params, $buch->{'ausgabejahr'}; + push @params, $buch->{'druckjahr'}; + push @params, $buch->{'seiten'}; + push @params, $buch->{'preis'}; + push @params, $buch->{'waehrungs_id'}; + + push @params, $buch->{'title'}; + push @params, $buch->{'original_title'}; + push @params, $buch->{'verlags_id'}; + push @params, $buch->{'untertitel'}; + push @params, $buch->{'orts_id'}; + push @params, $buch->{'bindungsart_id'}; + push @params, $buch->{'isbn'}; + push @params, $buch->{'book_nr'}; + push @params, $buch->{'ausgabejahr'}; + push @params, $buch->{'druckjahr'}; + push @params, $buch->{'seiten'}; + push @params, $buch->{'preis'}; + push @params, $buch->{'waehrungs_id'}; + + if ( $storage->debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @params ) . "\n"; + warn $text; + } + + my $sth = $dbh->prepare($sql); + $sth->execute( @params ); + + }; + + $storage->dbh_do( $save_func, $book ); + + my $book_id = $storage->last_insert_id(); + + return undef unless $book_id; + + return $book_id; + +} + +#----------------------------------------------------------------------------------- + +=head2 save_buch_autoren( $c, $book_id, @$autor_ids ) + +=cut + +sub save_buch_autoren { + + my $c = shift; + my $book_id = shift; + my $autor_ids = shift; + + my $K = ( caller(0) )[3] . "(): "; + + + +} + +#----------------------------------------------------------------------------------- + =head1 AUTHOR Frank Brehm diff --git a/root/src/books/book_form.tt2 b/root/src/books/book_form.tt2 index 2019296..b41615f 100644 --- a/root/src/books/book_form.tt2 +++ b/root/src/books/book_form.tt2 @@ -92,6 +92,12 @@ function goto_new_ort() { [% FOR ortsid IN orts_ids %] [% END %]   + + Bindungsart: + Kategorie(n): diff --git a/root/src/books/save_success.tt2 b/root/src/books/save_success.tt2 new file mode 100644 index 0000000..523efbb --- /dev/null +++ b/root/src/books/save_success.tt2 @@ -0,0 +1,17 @@ +[%# + + save_success.tt2 - Template fuer erfolgreiches Speichern des Buches + + vim: noai : ts=4 fenc=utf-8 filetype=html expandtab : + + $Id$ + $URL$ + +-%] +
+ +Das Buch "[% book_edit.title %] wurde erfolgreich gespeichert. + +

OK

+ +
-- 2.39.5