RIGHT JOIN `buecher` b ON ab.`buch_id` = b.`id`
ORDER BY b.`title`, ab.`buch_id`, ab.`ord_num`;
+SELECT `buch_id`, `title`, `title_original`, `untertitel`, `verlag`, `isbn`, `buch_nr`,
+ `ausgabejahr`, `druckjahr`, `seiten`, `preis`, `waehrungs_kuerzel`, `waehrung`, `ort`, `bindungsart`,
+ GROUP_CONCAT( DISTINCT `autor_name` ORDER BY `autor_ord_num` SEPARATOR ', ' ) AS `autoren`,
+ GROUP_CONCAT( DISTINCT `kategorie` ORDER BY `kategorie` SEPARATOR ', ' ) AS `kategorien`,
+ GROUP_CONCAT( DISTINCT `serie` ORDER BY `serie` SEPARATOR ', ' ) AS `serien`
+ FROM ( SELECT b.`id` AS `buch_id`,
+ b.`title` AS `title`,
+ b.`title_original` AS `title_original`,
+ b.`untertitel` AS `untertitel`,
+ b.`isbn` AS `isbn`,
+ b.`buch_nr` AS `buch_nr`,
+ b.`ausgabejahr` AS `ausgabejahr`,
+ b.`druckjahr` AS `druckjahr`,
+ b.`seiten` AS `seiten`,
+ b.`preis` AS `preis`,
+ w.`waehrungs_kuerzel` AS `waehrungs_kuerzel`,
+ w.`waehrungs_name` AS `waehrung`,
+ CONCAT( '`', a.`nachname`,
+ IF( ISNULL(a.`vorname`), '', ' ' ), IFNULL( a.`vorname`, '' ),
+ IF( ISNULL(a.`mittelname`), '', ' ' ), IFNULL( a.`mittelname`, '' ),
+ IF( ISNULL(a.`name_suffix`), '', ' ' ), IFNULL( a.`name_suffix`, '' ),
+ '`'
+ ) AS `autor_name`,
+ ab.`ord_num` AS `autor_ord_num`,
+ a.`titel` AS `autor_titel`,
+ a.`vorname` AS `autor_vorname`,
+ a.`mittelname` AS `autor_mittelname`,
+ a.`nachname` AS `autor_nachname`,
+ a.`name_suffix` AS `autor_name_suffix`,
+ v.`name_short` AS `verlag`,
+ o.`name` AS `ort`,
+ CONCAT( '`', k.`kategorie_name`, '`' ) AS `kategorie`,
+ CONCAT( '`', s.`serien_name`, '`' ) AS `serie`,
+ bi.`art_name` AS `bindungsart`
+ FROM autor2buch AS ab
+ LEFT JOIN `autoren` a ON ab.`autor_id` = a.`id`
+ RIGHT JOIN `buecher` b ON ab.`buch_id` = b.`id`
+ LEFT JOIN `bindungsarten` bi ON b.`bindungsart_id` = bi.`id`
+ LEFT JOIN `verlage` v ON b.`verlags_id` = v.`id`
+ LEFT JOIN `orte` o ON b.`orts_id` = o.`id`
+ LEFT JOIN `waehrungen` w ON b.`waehrungs_id` = w.`id`
+ LEFT JOIN `buch2kategorie` bk ON b.`id` = bk.`buch_id`
+ LEFT JOIN `kategorien` k ON k.`id` = bk.`kategorie_id`
+ LEFT JOIN `buch2serie` bs ON b.`id` = bs.`buch_id`
+ LEFT JOIN `serien` s ON s.`id` = bs.`serien_id`
+ ) AS `a`
+ GROUP BY `buch_id`
+ ORDER BY `autoren`, `title`;
+
-- vim: noai : ts=4 fenc=utf-8 filetype=sql expandtab :
$length = 500 if $length > 500;
$c->session->{'list_length'} = $length;
$c->stash->{'list_length'} = $length;
+ delete $c->session->{'book_list_page'} if exists $c->session->{'book_list_page'};
}
save_user_prefs($c) if $c->user_exists();
__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
-
-
-# You can replace this text with custom content, and it will be preserved on regeneration
1;
__END__
__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
-
-
-# You can replace this text with custom content, and it will be preserved on regeneration
1;
__END__
__PACKAGE__->has_many( 'autor_buch' => 'FrBr::Books::Db::Autor2Buch', { 'foreign.buch_id' => 'self.id' } );
__PACKAGE__->many_to_many( 'autoren' => 'autor_buch', 'autor' );
+__PACKAGE__->has_many( 'buch_kategorie' => 'FrBr::Books::Db::Buch2kategorie', { 'foreign.buch_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'kategorien' => 'buch_kategorie', 'kategorie' );
+
+__PACKAGE__->has_many( 'buch_serie' => 'FrBr::Books::Db::Buch2serie', { 'foreign.buch_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'serien' => 'buch_serie', 'serie' );
+
+# Make a new ResultSource based on the buecher class
+my $source = __PACKAGE__->result_source_instance();
+my $list_source = $source->new( $source );
+$list_source->source_name( 'BuecherListe' );
+
+# Hand in your query as a scalar reference
+# It will be added as a sub-select after FROM,
+# so pay attention to the surrounding brackets!
+$list_source->name( \<<SQL );
+( SELECT `buch_id`,
+ `title`,
+ `title_original`,
+ `untertitel`,
+ `verlag`,
+ `isbn`,
+ `buch_nr`,
+ `ausgabejahr`,
+ `druckjahr`,
+ `seiten`,
+ `preis`,
+ `waehrungs_kuerzel`,
+ `waehrung`,
+ `ort`,
+ `bindungsart`,
+ GROUP_CONCAT( DISTINCT `autor_name` ORDER BY `autor_ord_num` SEPARATOR ', ' ) AS `autoren`,
+ GROUP_CONCAT( DISTINCT `kategorie` ORDER BY `kategorie` SEPARATOR ', ' ) AS `kategorien`,
+ GROUP_CONCAT( DISTINCT `serie` ORDER BY `serie` SEPARATOR ', ' ) AS `serien`
+ FROM ( SELECT b.`id` AS `buch_id`,
+ b.`title` AS `title`,
+ b.`title_original` AS `title_original`,
+ b.`untertitel` AS `untertitel`,
+ b.`isbn` AS `isbn`,
+ b.`buch_nr` AS `buch_nr`,
+ b.`ausgabejahr` AS `ausgabejahr`,
+ b.`druckjahr` AS `druckjahr`,
+ b.`seiten` AS `seiten`,
+ b.`preis` AS `preis`,
+ w.`waehrungs_kuerzel` AS `waehrungs_kuerzel`,
+ w.`waehrungs_name` AS `waehrung`,
+ CONCAT( '`', a.`nachname`,
+ IF( ISNULL(a.`vorname`), '', ' ' ), IFNULL( a.`vorname`, '' ),
+ IF( ISNULL(a.`mittelname`), '', ' ' ), IFNULL( a.`mittelname`, '' ),
+ IF( ISNULL(a.`name_suffix`), '', ' ' ), IFNULL( a.`name_suffix`, '' ),
+ '`'
+ ) AS `autor_name`,
+ ab.`ord_num` AS `autor_ord_num`,
+ a.`titel` AS `autor_titel`,
+ a.`vorname` AS `autor_vorname`,
+ a.`mittelname` AS `autor_mittelname`,
+ a.`nachname` AS `autor_nachname`,
+ a.`name_suffix` AS `autor_name_suffix`,
+ v.`name_short` AS `verlag`,
+ o.`name` AS `ort`,
+ CONCAT( '`', k.`kategorie_name`, '`' ) AS `kategorie`,
+ CONCAT( '`', s.`serien_name`, '`' ) AS `serie`,
+ bi.`art_name` AS `bindungsart`
+ FROM autor2buch AS ab
+ LEFT JOIN `autoren` a ON ab.`autor_id` = a.`id`
+ RIGHT JOIN `buecher` b ON ab.`buch_id` = b.`id`
+ LEFT JOIN `bindungsarten` bi ON b.`bindungsart_id` = bi.`id`
+ LEFT JOIN `verlage` v ON b.`verlags_id` = v.`id`
+ LEFT JOIN `orte` o ON b.`orts_id` = o.`id`
+ LEFT JOIN `waehrungen` w ON b.`waehrungs_id` = w.`id`
+ LEFT JOIN `buch2kategorie` bk ON b.`id` = bk.`buch_id`
+ LEFT JOIN `kategorien` k ON k.`id` = bk.`kategorie_id`
+ LEFT JOIN `buch2serie` bs ON b.`id` = bs.`buch_id`
+ LEFT JOIN `serien` s ON s.`id` = bs.`serien_id`
+ ) AS `a`
+ GROUP BY `buch_id`
+)
+SQL
+
+# Finally, register your new ResultSource with your Schema
+FrBr::Books::Db->register_source( 'BuecherListe' => $list_source );
+
+
1;
__END__
__PACKAGE__->add_unique_constraint("kategorie_name", ["kategorie_name"]);
-# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-03-17 17:53:52
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZEMEahjPuJByMUE/OvebIg
+__PACKAGE__->has_many( 'buch_kategorie' => 'FrBr::Books::Db::Buch2kategorie', { 'foreign.kategorie_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'buecher' => 'buch_kategorie', 'buch' );
-
-# You can replace this text with custom content, and it will be preserved on regeneration
1;
__END__
# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-03-17 17:53:52
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:/IzPoWKb7jK+P2HCTO+bCQ
+__PACKAGE__->has_many( 'buch_serie' => 'FrBr::Books::Db::Buch2serie', { 'foreign.serien_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'buecher' => 'buch_serie', 'buch' );
+
# You can replace this text with custom content, and it will be preserved on regeneration
1;
$c->log->debug( get_output_string( $K, "Uebergebene Parameter: ", $params ) ) if $c->stash->{'debug_level'} >= 2;
my $list = [];
- my $buchmap = {},
my $anzahl_buecher = 0;
+ my $storage = $c->stash->{'storage'};
+
# Suchparameter zusammensammeln
my $b_search_params = {};
+ my $order = undef;
if ( $params->{'buch_id'} ) {
- $b_search_params->{'me.id'} = $params->{'buch_id'};
+ $b_search_params->{'me.buch_id'} = $params->{'buch_id'};
+ }
+ else {
+ $order = collect_sort_criteria($c)
}
# Suchparameter auf undef setzen, falls es keine gibt.
$b_search_params = undef unless scalar keys %$b_search_params;
my $other_params = {};
- $other_params->{'order_by'} = [ 'title' ];
- $other_params->{'join'} = [ 'waehrung', 'verlag', 'ort', 'bindungsart' ];
+ $other_params->{'order_by'} = $order;
+ $other_params->{'select'} = [ 'me.buch_id' ];
+ $other_params->{'as'} = [ 'buch_id' ];
+ $other_params->{'rows'} = $c->stash->{'list_length'} || 20;
+ $other_params->{'page'} = $params->{'page'} || $c->stash->{'page'} || 1;
+
+ my $book_ids = [];
- $anzahl_buecher = $c->model('Schema::Buecher')->count( $b_search_params, $other_params );
+ for my $book ( $c->model('Schema::BuecherListe')->search( $b_search_params, $other_params )->all() ) {
+ push @$book_ids, $book->get_column('buch_id');
+ }
+
+ return ( wantarray ? ( $book_ids, 0 ) : $book_ids ) unless scalar( @$book_ids );
+
+ my $in = "IN ( " . join( ", ", @$book_ids ) . " )";
+ $anzahl_buecher = scalar( @$book_ids );
$c->log->debug( get_output_string( $K, "Anzahl gefundene Buecher: ", $anzahl_buecher ) ) if $c->stash->{'debug_level'} >= 2;
- $other_params->{'rows'} = $c->stash->{'list_length'} || 10;
- $other_params->{'page'} = $params->{'page'} || $c->stash->{'page'} || 1;
+ $b_search_params = { 'me.id' => \$in };
+ $other_params = {};
+ $other_params->{'join'} = [ 'waehrung', 'verlag', 'ort', 'bindungsart' ];
$other_params->{'select'} = [
'me.id',
'me.title',
'ort_ist_statisch',
];
- my $i = 0;
+ my $buchmap = {};
for my $book ( $c->model('Schema::Buecher')->search( $b_search_params, $other_params )->all() ) {
my $buch = {};
my $id = $book->id();
$buch->{'kategorie_ids'} = [];
$buch->{'serien'} = [];
$buch->{'serien_ids'} = [];
- push @$list, $buch;
- $buchmap->{$id} = $i;
- $i++;
+ $buchmap->{$id} = $buch;
}
- return ( wantarray ? ( $list, 0 ) : $list ) unless scalar( @$list );
-
# Autoren zusammensammeln
my $a_search_params = {};
- if ( $b_search_params ) {
- my $in = "IN ( " . join( ", ", keys %$buchmap ) . " )";
- $a_search_params->{'buch_id'} = \$in;
- }
- $a_search_params = undef unless keys %$a_search_params;
+ $a_search_params->{'buch_id'} = \$in;
$other_params = {};
$other_params->{'order_by'} = [ 'ord_num' ];
push @N, $ref->get_column('nachname');
push @N, $ref->get_column('name_suffix') if $ref->get_column('name_suffix');
my $autor = join( " ", @N );
- my $j = $buchmap->{$bid};
- next unless defined $j;
- my $buch = $list->[$j];
- next unless $buch;
- push @{$buch->{'autoren'}}, $autor;
- push @{$buch->{'autor_ids'}}, $aid;
+ push @{ $buchmap->{$bid}{'autoren'} }, $autor;
+ push @{ $buchmap->{$bid}{'autor_ids'} }, $aid;
}
# Kategorien zusammensammeln
my $bid = $ref->get_column('buch_id');
my $kid = $ref->get_column('kategorie_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;
- push @{$buch->{'kategorie_ids'}}, $kid;
+ push @{ $buchmap->{$bid}{'kategorien'} }, $name;
+ push @{ $buchmap->{$bid}{'kategorie_ids'} }, $kid;
}
# Buchserien zusammensammeln
my $bid = $ref->get_column('buch_id');
my $sid = $ref->get_column('serien_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;
- push @{$buch->{'serien_ids'}}, $sid;
+ push @{ $buchmap->{$bid}{'serien'} }, $name;
+ push @{ $buchmap->{$bid}{'serien_ids'} }, $sid;
+ }
+
+ # HashRef %$buchmap sortiert in ArrayRef $list ummappen
+
+ for my $id ( @$book_ids ) {
+ push @$list, $buchmap->{$id} if $buchmap->{$id};
}
return ( wantarray ? ( $list, $anzahl_buecher ) : $list );
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 collect_sort_criteria( $c )
+
+Sammelt aus der Sitzung die Sortierkriterien der Buchliste zusammen und gibt diese verwendbar
+für get_booklist() zurück.
+
+Die Sortierkriterien müssen in der Sitzung unter dem Schlüssel 'book_order_criteria'
+als Array-Ref abgelegt sein. Die Elemente der Array-Ref sind entweder Scalare mit den
+Spaltennamen oder Hashrefs der Form [ { 'autoren' => 'desc' }, { 'kategorien' => 'asc' }, ...].
+
+Folgende Spaltennamen dürfen verwendet werden:
+
+ - autoren
+ - title
+ - title_original
+ - bindungsart
+ - verlag
+ - ort
+ - preis
+ - waehrung
+ - ausgabejahr
+ - druckjahr
+ - seiten
+ - kategorien
+ - serien
+
+Falls in $c->session->{'book_order_criteria'} nichts liegt, wird folgendes
+als Voreinstellung verwendet:
+
+ $criteria = [
+ 'title',
+ ];
+
+=cut
+
+sub collect_sort_criteria {
+
+ my $c = shift;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ my $order = [];
+
+ my $valid_column = {
+ 'autoren' => 1,
+ 'title' => 1,
+ 'title_original' => 1,
+ 'bindungsart' => 1,
+ 'verlag' => 1,
+ 'ort' => 1,
+ 'preis' => 1,
+ 'waehrung' => 1,
+ 'ausgabejahr' => 1,
+ 'druckjahr' => 1,
+ 'seiten' => 1,
+ 'kategorien' => 1,
+ 'serien' => 1,
+ };
+
+ my $used_column = {};
+
+ my $criteria = $c->session->{'book_order_criteria'};
+ unless ( $criteria and ref($criteria) and ref($criteria) eq 'ARRAY' and scalar(@$criteria) ) {
+ $criteria = [
+ 'autoren',
+ 'title',
+ ];
+ }
+
+ my $col;
+ for my $crit ( @$criteria ) {
+ if ( ref($crit) and ref($crit) eq 'HASH' ) {
+ for my $key ( keys %$crit ) {
+ $col = lc($key);
+ if ( $valid_column->{$col} and not exists $used_column->{$col} ) {
+ my $dir = $crit->{$key};
+ $dir = ( $dir =~ /desc/i ) ? 'DESC' : 'ASC';
+ $used_column->{$col} = 1;
+ push @$order, ( $dir eq 'ASC' ? $col : \"`$col` DESC" );
+ }
+ }
+ }
+ else {
+ $col = lc($crit);
+ if ( $valid_column->{$col} and not exists $used_column->{$col} ) {
+ push @$order, $col;
+ $used_column->{$col} = 1;
+ }
+ }
+ }
+
+ $order = [ 'title' ] unless scalar( @$order );
+
+ return $order;
+
}
#-----------------------------------------------------------------------------------
$c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
my $storage = $c->stash->{'storage'};
- #$c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
my $save_func = sub {
return unless $prefs;
if ( $prefs->{'list_length'} ) {
+ delete $c->session->{'book_list_page'} if exists $c->session->{'book_list_page'};
$c->stash->{'list_length'} = $prefs->{'list_length'};
$c->session->{'list_length'} = $prefs->{'list_length'};
}