From 67328f17e5ae9cf82d16bdfec31c22419164e68d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 18 Mar 2008 13:20:32 +0000 Subject: [PATCH] Buchliste sauber --- lib/FrBr/Books/Controller/Books.pm | 89 +---------- lib/FrBr/Books/Db/Autor2buch.pm | 2 + lib/FrBr/Books/Db/Buch2kategorie.pm | 2 + lib/FrBr/Books/Db/Buch2serie.pm | 3 + lib/FrBr/Books/Util/Book.pm | 235 ++++++++++++++++++++++++++++ root/src/books/list.tt2 | 7 +- 6 files changed, 252 insertions(+), 86 deletions(-) create mode 100644 lib/FrBr/Books/Util/Book.pm diff --git a/lib/FrBr/Books/Controller/Books.pm b/lib/FrBr/Books/Controller/Books.pm index bd114fb..a9feebe 100644 --- a/lib/FrBr/Books/Controller/Books.pm +++ b/lib/FrBr/Books/Controller/Books.pm @@ -8,6 +8,7 @@ use warnings; use base 'Catalyst::Controller'; use FrBr::Common; +use FrBr::Books::Util::Book; =head1 NAME @@ -40,9 +41,6 @@ Fetch all book objects and pass to books/list.tt2 in stash to be displayed sub list : Local { - # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst - # 'Context' that's used to 'glue together' the various components - # that make up the application my ($self, $c) = @_; my $K = __PACKAGE__ . "::list(): "; $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; @@ -50,87 +48,12 @@ sub list : Local { $c->stash->{'cssfiles'} = [] unless $c->stash->{'cssfiles'}; push @{$c->stash->{'cssfiles'}}, 'books/styles.css'; - my $search_params = undef; - my $other_params = {}; - $other_params->{'order_by'} = [ 'title' ]; - $other_params->{'join'} = [ 'waehrung', 'verlag' ]; - $other_params->{'select'} = [ - 'me.id', - 'me.title', - 'me.title_original', - 'me.verlags_id', - 'me.isbn', - 'me.buch_nr', - 'me.ausgabejahr', - 'me.druckjahr', - 'me.preis', - 'me.waehrungs_id', - 'waehrung.waehrungs_kuerzel', - 'waehrung.waehrungs_name', - 'waehrung.umrechnung_in_euro', - 'verlag.name_short AS `verlagsname_short`', - 'verlag.name_long AS `verlagsname_long`', - ]; - $other_params->{'as'} = [ - 'id', - 'title', - 'title_original', - 'verlags_id', - 'isbn', - 'buch_nr', - 'ausgabejahr', - 'druckjahr', - 'preis', - 'waehrungs_id', - 'waehrungs_kuerzel', - 'waehrungs_name', - 'umrechnung_in_euro', - 'verlagsname_short', - 'verlagsname_long', - ]; - $c->stash->{'books'} = []; - for my $book ( $c->model('Schema::Buecher')->search( $search_params, $other_params )->all() ) { - my $buch = {}; - $buch->{'id'} = $book->id(); - $buch->{'title'} = $book->title(); - $buch->{'title_original'} = $book->title_original(); - $buch->{'verlags_id'} = $book->verlags_id(); - $buch->{'isbn'} = $book->isbn(); - $buch->{'buch_nr'} = $book->buch_nr(); - $buch->{'ausgabejahr'} = $book->ausgabejahr(); - $buch->{'druckjahr'} = $book->druckjahr(); - $buch->{'preis'} = $book->preis(); - $buch->{'waehrungs_id'} = $book->waehrungs_id(); - $buch->{'waehrungs_kuerzel'} = $book->get_column('waehrungs_kuerzel'); - $buch->{'waehrungs_name'} = $book->get_column('waehrungs_name'); - $buch->{'umrechnung_in_euro'} = $book->get_column('umrechnung_in_euro'); - $buch->{'verlagsname_short'} = $book->get_column('verlagsname_short'); - $buch->{'verlagsname_long'} = $book->get_column('verlagsname_long'); - push @{$c->stash->{'books'}}, $buch; - } - - $c->stash->{'autoren'} = {}; - for my $autor ( $c->model('Schema::Autoren')->all() ) { - my $au = {}; - my $id = $autor->id; - $au->{'id'} = $id; - $au->{'autor_name'} = $autor->autor_name; - $au->{'autor_descr'} = $autor->autor_descr; - $c->log->debug( $K . get_output_string( "Autor gefunden: ", $au ) ) if $c->stash->{'debug_level'} > 3; - $c->stash->{'autoren'}{$id} = $au; - } - $c->stash->{'buch2autor'} = {}; - for my $ref ( $c->model('Schema::Autor2buch')->search(undef, { 'order_by' => 'ord_num' } )->all() ) { - my $bid = $ref->buch_id; - my $aid = $ref->autor_id; - $c->stash->{'buch2autor'}{$bid} = [] unless $c->stash->{'buch2autor'}{$bid}; - push @{$c->stash->{'buch2autor'}{$bid}}, $c->stash->{'autoren'}{$aid}{'autor_name'}; - } - - # Set the TT template to use. You will almost always want to do this - # in your action methods (action methods respond to user input in - # your controllers). + my $buchliste = get_booklist( $c ); + $c->log->debug( get_output_string( $K, "Erhaltene Buchliste: ", $buchliste ) ) if $c->stash->{'debug_level'} >= 2; + $c->stash->{'books'} = $buchliste; + $c->stash->{'template'} = 'books/list.tt2'; + } diff --git a/lib/FrBr/Books/Db/Autor2buch.pm b/lib/FrBr/Books/Db/Autor2buch.pm index 84d83c4..7c43f8f 100644 --- a/lib/FrBr/Books/Db/Autor2buch.pm +++ b/lib/FrBr/Books/Db/Autor2buch.pm @@ -19,6 +19,8 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("buch_id", ["buch_id", "autor_id"]); +__PACKAGE__->might_have( 'buch' => 'FrBr::Books::Db::Buecher', { 'foreign.id' => 'self.buch_id' } ); +__PACKAGE__->might_have( 'autor' => 'FrBr::Books::Db::Autoren', { 'foreign.id' => 'self.autor_id' } ); # Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-03-17 17:53:52 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ndVfBKXzEtRRzVPU1qAspg diff --git a/lib/FrBr/Books/Db/Buch2kategorie.pm b/lib/FrBr/Books/Db/Buch2kategorie.pm index e61f62b..0f40976 100644 --- a/lib/FrBr/Books/Db/Buch2kategorie.pm +++ b/lib/FrBr/Books/Db/Buch2kategorie.pm @@ -18,6 +18,8 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("buch_id", ["buch_id", "kategorie_id"]); +__PACKAGE__->might_have( 'buch' => 'FrBr::Books::Db::Buecher', { 'foreign.id' => 'self.buch_id' } ); +__PACKAGE__->might_have( 'kategorie' => 'FrBr::Books::Db::Kategorien', { 'foreign.id' => 'self.kategorie_id' } ); # Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-03-17 17:53:52 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rYT7qQgF4WvVJQgCDUurIw diff --git a/lib/FrBr/Books/Db/Buch2serie.pm b/lib/FrBr/Books/Db/Buch2serie.pm index 0220903..e04c1e6 100644 --- a/lib/FrBr/Books/Db/Buch2serie.pm +++ b/lib/FrBr/Books/Db/Buch2serie.pm @@ -20,6 +20,9 @@ __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("buch_id", ["buch_id", "serien_id"]); +__PACKAGE__->might_have( 'buch' => 'FrBr::Books::Db::Buecher', { 'foreign.id' => 'self.buch_id' } ); +__PACKAGE__->might_have( 'serie' => 'FrBr::Books::Db::Serien', { 'foreign.id' => 'self.serien_id' } ); + # Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-03-17 17:53:52 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:0ZVowzPrhFa/hKjsW/G9RQ diff --git a/lib/FrBr/Books/Util/Book.pm b/lib/FrBr/Books/Util/Book.pm new file mode 100644 index 0000000..ec0f277 --- /dev/null +++ b/lib/FrBr/Books/Util/Book.pm @@ -0,0 +1,235 @@ +package FrBr::Books::Util::Book; + +# $Id: Calendar.pm 263 2007-12-07 15:57:34Z fbrehm $ +# $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_booklist + ); + + #%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::Book - Modul fuer Funktionen rund ums Buch + +=head1 METHODS + +=cut + +#----------------------------------------------------------------------------------- + +=head2 get_booklist( $c, $params ) + +Sammelt alle Buecher zusammen. + +Folgende benannte Parameter koennen ueber $params uebergeben werden: + + - title - Ein konkreter Buchtitel (auch SQL-Metazeichen erlaubt) + - title_original - Ein konkreter Original-Buchtitel (auch SQL-Metazeichen erlaubt) + (Hinweis: wenn sowohl 'title' als auch 'title_original' uebergeben werden, werden + diese ODER-verknuepft) + + +Rueckgabe: Eine Array-Ref mit allen Buchtiteln, die den uebergebenen Suchkriterien entsprechen. + +=cut + +sub get_booklist { + + my $c = shift; + my $K = __PACKAGE__ . "::get_booklist(): "; + + $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 $buchmap = {}, + + my $b_search_params = undef; + my $other_params = {}; + $other_params->{'order_by'} = [ 'title' ]; + $other_params->{'join'} = [ 'waehrung', 'verlag' ]; + $other_params->{'select'} = [ + 'me.id', + 'me.title', + 'me.title_original', + 'me.verlags_id', + 'me.isbn', + 'me.buch_nr', + 'me.ausgabejahr', + 'me.druckjahr', + 'me.preis', + 'me.waehrungs_id', + 'waehrung.waehrungs_kuerzel', + 'waehrung.waehrungs_name', + 'waehrung.umrechnung_in_euro', + 'verlag.name_short AS `verlagsname_short`', + 'verlag.name_long AS `verlagsname_long`', + ]; + $other_params->{'as'} = [ + 'id', + 'title', + 'title_original', + 'verlags_id', + 'isbn', + 'buch_nr', + 'ausgabejahr', + 'druckjahr', + 'preis', + 'waehrungs_id', + 'waehrungs_kuerzel', + 'waehrungs_name', + 'umrechnung_in_euro', + 'verlagsname_short', + 'verlagsname_long', + ]; + + my $i = 0; + for my $book ( $c->model('Schema::Buecher')->search( $b_search_params, $other_params )->all() ) { + my $buch = {}; + my $id = $book->id(); + $buch->{'id'} = $id; + $buch->{'title'} = $book->title(); + $buch->{'title_original'} = $book->title_original(); + $buch->{'verlags_id'} = $book->verlags_id(); + $buch->{'isbn'} = $book->isbn(); + $buch->{'buch_nr'} = $book->buch_nr(); + $buch->{'ausgabejahr'} = $book->ausgabejahr(); + $buch->{'druckjahr'} = $book->druckjahr(); + $buch->{'preis'} = $book->preis(); + $buch->{'waehrungs_id'} = $book->waehrungs_id(); + $buch->{'waehrungs_kuerzel'} = $book->get_column('waehrungs_kuerzel'); + $buch->{'waehrungs_name'} = $book->get_column('waehrungs_name'); + $buch->{'umrechnung_in_euro'} = $book->get_column('umrechnung_in_euro'); + $buch->{'verlagsname_short'} = $book->get_column('verlagsname_short'); + $buch->{'verlagsname_long'} = $book->get_column('verlagsname_long'); + $buch->{'autoren'} = []; + $buch->{'kategorien'} = []; + $buch->{'serien'} = []; + push @$list, $buch; + $buchmap->{$id} = $i; + $i++; + } + + # Autoren zusammensammeln + my $a_search_params = {}; + if ( $b_search_params ) { + my $in = "IN ( " . join( ", ", values %$buchmap ) . " )"; + $a_search_params->{'`buch_id`'} = \$in; + } + $a_search_params = undef unless keys %$a_search_params; + + $other_params = {}; + $other_params->{'order_by'} = [ 'ord_num' ]; + $other_params->{'join'} = [ 'autor' ]; + $other_params->{'select'} = [ + 'me.buch_id', + 'autor.autor_name', + ]; + $other_params->{'as'} = [ + 'buch_id', + 'autor_name', + ]; + for my $ref ( $c->model('Schema::Autor2buch')->search( $a_search_params, $other_params )->all() ) { + my $bid = $ref->get_column('buch_id'); + my $autor = $ref->get_column('autor_name'); + my $j = $buchmap->{$bid}; + next unless defined $j; + my $buch = $list->[$j]; + next unless $buch; + push @{$buch->{'autoren'}}, $autor; + } + + # Kategorien zusammensammeln + delete $other_params->{'order_by'} if exists $other_params->{'order_by'}; + $other_params->{'join'} = [ 'kategorie' ]; + $other_params->{'select'} = [ + 'me.buch_id', + 'kategorie.kategorie_name', + ]; + $other_params->{'as'} = [ + 'buch_id', + 'kategorie_name', + ]; + for my $ref ( $c->model('Schema::Buch2kategorie')->search( $a_search_params, $other_params )->all() ) { + my $bid = $ref->get_column('buch_id'); + my $name = $ref->get_column('kategorie_name'); + my $j = $buchmap->{$bid}; + next unless defined $j; + my $buch = $list->[$j]; + next unless $buch; + push @{$buch->{'kategorien'}}, $name; + } + + # Buchserien zusammensammeln + delete $other_params->{'order_by'} if exists $other_params->{'order_by'}; + $other_params->{'join'} = [ 'serie' ]; + $other_params->{'select'} = [ + 'me.buch_id', + 'serie.serien_name', + ]; + $other_params->{'as'} = [ + 'buch_id', + 'serien_name', + ]; + for my $ref ( $c->model('Schema::Buch2serie')->search( $a_search_params, $other_params )->all() ) { + my $bid = $ref->get_column('buch_id'); + my $name = $ref->get_column('serien_name'); + my $j = $buchmap->{$bid}; + next unless defined $j; + my $buch = $list->[$j]; + next unless $buch; + push @{$buch->{'serien'}}, $name; + } + + 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; diff --git a/root/src/books/list.tt2 b/root/src/books/list.tt2 index 92f909e..3c4688b 100644 --- a/root/src/books/list.tt2 +++ b/root/src/books/list.tt2 @@ -11,17 +11,18 @@ Autor (-en) Titel + Kategorie Verlag ISBN Buch-Nummer Ausgabejahr Preis -[% # Display each book in a table row %] -[% FOREACH book IN books -%][% buch_id = book.id -%] +[%- FOREACH book IN books -%][% buch_id = book.id %] - [% tt_authors = [ ]; tt_authors.push(author) FOREACH author = buch2autor.$buch_id %][% tt_authors.join(', ') %] + [% tt_authors = [ ]; tt_authors.push(autor) FOREACH autor = book.autoren %][% tt_authors.join(', ') %] [% book.title %][% IF book.title_original %] ([% book.title_original %])[% END %] + [% tt_cat = [ ]; tt_cat.push(cat) FOREACH cat = book.kategorien %][% tt_cat.join(',
') %] [% book.verlagsname_short %] [% book.isbn %] [% book.buch_nr %] -- 2.39.5