]> Frank Brehm's Git Trees - books.git/commitdiff
Buchliste sauber
authorFrank Brehm <frank@brehm-online.com>
Tue, 18 Mar 2008 13:20:32 +0000 (13:20 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 18 Mar 2008 13:20:32 +0000 (13:20 +0000)
lib/FrBr/Books/Controller/Books.pm
lib/FrBr/Books/Db/Autor2buch.pm
lib/FrBr/Books/Db/Buch2kategorie.pm
lib/FrBr/Books/Db/Buch2serie.pm
lib/FrBr/Books/Util/Book.pm [new file with mode: 0644]
root/src/books/list.tt2

index bd114fbb46c20843d3c043a0f3a539c50d469f19..a9feebeac272bfcb58c860ea05ff1bff8c2ee127 100644 (file)
@@ -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';
+
 }
 
 
index 84d83c49460224a5aa63aee872fe0f1078fbbaee..7c43f8f60b8891cc3e2b3cf8300de14313d649e7 100644 (file)
@@ -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
index e61f62b45304e5ec3946ed9ff23ea93691a1d787..0f40976e4ba1adef591ebe4ad77a539e99ab5601 100644 (file)
@@ -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
index 0220903eaed9f3ddbaebb6d90de0e852df489541..e04c1e656d47db3a7d8580a6ea61c5fc5953b02a 100644 (file)
@@ -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 (file)
index 0000000..ec0f277
--- /dev/null
@@ -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;
index 92f909eeae9fc6fa1b184926806074ee10e79fc7..3c4688bf072b00aa2336c6a3822e44f988c3d8f4 100644 (file)
   <tr>
     <th>Autor (-en)</th>
     <th>Titel</th>
+    <th>Kategorie</th>
     <th>Verlag</th>
     <th>ISBN</th>
     <th>Buch-Nummer</th>
     <th>Ausgabejahr</th>
     <th>Preis</th>
   </tr>
-[% # Display each book in a table row %]
-[% FOREACH book IN books -%][% buch_id = book.id -%]
+[%- FOREACH book IN books -%][% buch_id = book.id %]
   <tr>
-    <td>[% tt_authors = [ ]; tt_authors.push(author) FOREACH author = buch2autor.$buch_id %][% tt_authors.join(', ') %]</td>
+    <td>[% tt_authors = [ ]; tt_authors.push(autor) FOREACH autor = book.autoren %][% tt_authors.join(', ') %]</td>
     <td>[% book.title %][% IF book.title_original %] ([% book.title_original %])[% END %]</td>
+    <td>[% tt_cat = [ ]; tt_cat.push(cat) FOREACH cat = book.kategorien %][% tt_cat.join(',<br />') %]</td>
     <td>[% book.verlagsname_short %]</td>
     <td>[% book.isbn %]</td>
     <td>[% book.buch_nr %]</td>