use base 'Catalyst::Controller';
use FrBr::Common;
+use FrBr::Books::Util::Book;
=head1 NAME
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;
$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';
+
}
--- /dev/null
+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;