=head1 NAME
-FrBr::Books::Controller::Verlag - Catalyst Controller fuer alles rund um verlage
+FrBr::Books::Controller::Verlag - Catalyst Controller fuer alles rund um Verlage
=head1 DESCRIPTION
my ( $self, $c ) = @_;
$c->stash->{'template'} = 'verlag/index.tt2';
+ $c->session->{'return_target_verlag_save'} = $c->web_path("/verlag");
+
+ delete $c->session->{'from_verlag_list'} if exists $c->session->{'from_verlag_list'};
+ delete $c->session->{'verlag_data_edit'} if exists $c->session->{'verlag_data_edit'};
}
#-------------------------------------------------------
#-------------------------------------------------------
+sub add_verlaglist_menu : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/verlag/list"),
+ 'name' => "Liste"
+ };
+
+}
+
+#-------------------------------------------------------
+
=head2 list
Sucht alle Verlage als Objekt zusammen und übergibt das verlag/list.tt2 zur Darstellung.
my $K = ( caller(0) )[3] . "(): ";
$c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
- push @{ $c->stash->{'menu_path'} }, {
- 'path' => $c->web_path("/verlag/list"),
- 'name' => "Liste"
- };
+ $self->add_verlaglist_menu($c);
+ delete $c->session->{'verlag_data_edit'} if exists $c->session->{'verlag_data_edit'};
+
+ my $page = $c->session->{'verlag_list_page'} || 1;
+ my $list_length = $c->stash->{'list_length'} || 10;
- my $liste = get_verlagsliste( $c );
+ if ( $c->request->params->{'page'} and to_int( $c->request->params->{'page'} ) ) {
+ $page = to_int( $c->request->params->{'page'} );
+ $c->session->{'verlag_list_page'} = $page;
+ }
+
+ my ( $liste, $anzahl ) = get_verlagsliste( $c, 'get_books' => undef, 'page' => $page );
$c->log->debug( get_output_string( $K, "Erhaltene Liste der Verlage: ", $liste ) ) if $c->stash->{'debug_level'} >= 2;
$c->stash->{'verlage'} = $liste;
+ my $max_page = 1;
+ if ( $anzahl ) {
+ $max_page = int( $anzahl / $list_length ) + 1;
+ if ( ( $page - 1 ) * $list_length > $anzahl ) {
+ $page = $max_page;
+ $c->session->{'verlag_list_page'} = $page;
+ }
+ }
+ else {
+ $page = 1;
+ $c->session->{'verlag_list_page'} = 1;
+ }
+
+ my $nav = {
+ 'cur' => $page,
+ 'first' => 1,
+ 'last' => $max_page,
+ 'prev' => ( ( $page > 1 ) ? ( $page - 1 ) : 1 ),
+ 'next' => ( ( ( $page + 1 ) > $max_page ) ? $max_page : ( $page + 1 ) ),
+ 'verlage' => $anzahl,
+ 'verlag_from' => ( $anzahl ? ( ( $page - 1 ) * $list_length ) + 1 : 0 ),
+ 'verlag_to' => ( ( $page * $list_length ) > $anzahl ? $anzahl : ( $page * $list_length ) ),
+ };
+ $c->stash->{'nav'} = $nav;
+ $c->log->debug( get_output_string( $K, "Seitennavigation: ", $nav ) ) if $c->stash->{'debug_level'} >= 2;
+
+ $c->session->{'return_target_view'} = $c->web_path("/verlag/list");
+ $c->session->{'return_target_verlag_save'} = $c->web_path("/verlag/list");
+ $c->session->{'from_verlag_list'} = 1;
$c->stash->{'template'} = 'verlag/list.tt2';
}
$c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
$c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'};
+ $self->add_verlaglist_menu($c) if $c->session->{'from_verlag_list'};
push @{ $c->stash->{'menu_path'} }, {
'path' => $c->web_path("/verlag/view"),
'name' => "Betrachten"
return 1;
}
- $c->stash->{'return_target'} = $c->web_path("/verlag/list");
+ $c->stash->{'return_target'} = $c->session->{'return_target_view'} or $c->web_path("/verlag/list");
$c->stash->{'verlag'} = $verlagsliste->[0];
$verlagsliste = undef;
#-------------------------------------------------------
+=head2 form_edit( )
+
+Aendern eines vorhandenen Verlags.
+
+=cut
+
+sub form_edit : Path('edit') {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ unless ( $c->user_exists() ) {
+ $c->detach('/not_allowed');
+ return;
+ }
+
+ $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'};
+ $self->add_verlaglist_menu($c) if $c->session->{'from_verlag_list'};
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/autor/edit"),
+ 'name' => "Ändern"
+ };
+
+ $c->stash->{'error_message'} = '';
+
+ # Zu bearbeitende Verlags-Id ermitteln
+ my $vid = to_int( $c->request->params->{'edit_verlags_id'} || 0 );
+
+ unless ( $vid ) {
+ if ( $c->session->{'verlag_data_edit'} and $c->session->{'verlag_data_edit'}{'id'} ) {
+ $vid = $c->session->{'verlag_data_edit'}{'id'};
+ }
+ }
+
+ # Keine Verlags-Id da?
+ unless ( $vid ) {
+ $c->stash->{'template'} = 'error.tt2';
+ $c->stash->{'error'} = 'Keine oder ungültige Verlags-Id übergeben.';
+ return 1;
+ }
+
+ # Angaben zum Verlag aus der Datenbank holen
+ my $vliste = get_verlagsliste( $c, 'verlags_id' => $vid, );
+
+ unless ( $vliste and scalar( @$vliste ) ) {
+ $c->stash->{'template'} = 'error.tt2';
+ $c->stash->{'error'} = 'Zur übergebenen Verlags-Id wurde kein Verlag gefunden.';
+ return 1;
+ }
+
+ my $verlag = $vliste->[0];
+
+ $c->log->debug( get_output_string( $K, "Verlag aus Datenbank: ", $verlag ) ) if $c->stash->{'debug_level'} >= 2;
+
+ # Ruecksprung-URL bestimmen
+ $c->stash->{'return_target'} = $c->session->{'return_target_edit'} or $c->web_path("/verlag/list");
+
+ # Sitzungsdaten vorbelegen, falls noch nicht geschehen
+ if ( $c->session->{'verlag_data_edit'} ) {
+ if ( ! $c->session->{'verlag_data_edit'}{'id'} or $c->session->{'verlag_data_edit'}{'id'} != $vid ) {
+ $c->session->{'verlag_data_edit'} = $verlag;
+ }
+ }
+ else {
+ $c->session->{'verlag_data_edit'} = $verlag;
+ }
+ $vliste = undef;
+
+ $self->verlag_cgi2session($c);
+
+ $c->stash->{'template'} = 'verlag/edit.tt2';
+ push @{$c->stash->{'cssfiles'}}, 'verlag/form.css';
+
+ $self->verlag_session2stash($c);
+
+ unless ( $c->request->params->{'verlag_form_sent'} and $c->request->params->{'do_save'} ) {
+ return 1;
+ }
+
+ $c->stash->{'return_target_action'} = $c->session->{'return_target_edit'};
+ return $self->do_save_verlag($c);
+
+}
+
+#-------------------------------------------------------
+
sub do_save_verlag : Private {
my ( $self, $c ) = @_;
$c->stash->{'verlag_edit'} = {} unless $c->stash->{'verlag_edit'};
$c->stash->{'verlag_edit'}{'id'} = $c->session->{'verlag_data_edit'}{'id'} if $c->session->{'verlag_data_edit'}{'id'};
$c->stash->{'verlag_edit'}{'name'} = $c->session->{'verlag_data_edit'}{'name'} if $c->session->{'verlag_data_edit'}{'name'};
- $c->stash->{'verlag_edit'}{'name_long'} = $c->session->{'verlag_data_edit'}{'name_long'} if $c->session->{'verlag_data_edit'}{'desc'};
+ $c->stash->{'verlag_edit'}{'name_long'} = $c->session->{'verlag_data_edit'}{'name_long'} if $c->session->{'verlag_data_edit'}{'name_long'};
$c->stash->{'return_target_verlag_save'} = $c->session->{'return_target_verlag_save'} || $c->web_path('/verlag');
@EXPORT = qw(
&get_verlagsliste
&save_verlag
+ &delete_verlag
);
#%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
$c->log->debug( get_output_string( $K, "Uebergebene Parameter: ", $params ) ) if $c->stash->{'debug_level'} >= 2;
my $list = [];
+ my $anzahl_verlage = 0;
+
my $page = to_int( $params->{'page'} ) ? to_int( $params->{'page'} ) : undef;
my $rows = undef;
$rows = $c->stash->{'list_length'} || 20 if defined $page;
};
}
+ $anzahl_verlage = $c->model('Schema::Verlage')->count( $search_params );
+ $c->log->debug( get_output_string( $K, "Anzahl gefundene Verlage: ", $anzahl_verlage ) ) if $c->stash->{'debug_level'} >= 2;
+
my $other_params = {};
$other_params->{'order_by'} = [ 'name_short' ];
$other_params->{'select'} = [
my $verlag = {};
$verlag->{'id'} = $verlag_rs->id();
$verlag->{'name'} = $verlag_rs->name_short();
- $verlag->{'descr'} = $verlag_rs->name_long();
+ $verlag->{'name_long'} = $verlag_rs->name_long();
$verlag->{'count_books'} = $verlag_rs->get_column('count_books');
push @$list, $verlag;
}
- return $list unless scalar $list;
- return $list unless $params->{'get_books'};
+ return ( wantarray ? ( $list, 0 ) : $list ) unless scalar $list;
+ return ( wantarray ? ( $list, $anzahl_verlage ) : $list ) unless $params->{'get_books'};
for my $verlag ( @$list ) {
}
- return $list;
+ return ( wantarray ? ( $list, $anzahl_verlage ) : $list );
}
#-----------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------
+=head2 delete_verlag( $c, $verlags_id )
+
+Loescht den Autor mit der uebergebenen Autor-Id
+
+=cut
+
+sub delete_verlag {
+
+ my $c = shift;
+ my $verlags_id = shift;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen fuer Verlags-Id " . ( defined $verlags_id ? $verlags_id : '<undef>' ) . "." ) if $c->stash->{'debug_level'} > 2;
+
+ return undef unless $verlags_id and to_int($verlags_id);
+ $verlags_id = to_int($verlags_id);
+
+ my $schema = $c->model('Schema')->{'schema'};
+
+ # Raus, wenn der Verlag noch Buecher hat ...
+ if ( $c->model('Schema::Buecher')->count( { 'verlags_id' => $verlags_id, } ) ) {
+ my $msg = 'Es existieren noch Buecher dieses Verlags.';
+ die $msg . "\n";
+ }
+
+ # Das eigentliche Löschen
+ my $coderef = sub {
+ $c->model('Schema::Verlage')->search( { 'id' => $verlags_id } )->delete();
+ };
+
+ my $rs;
+ eval {
+ $rs = $schema->txn_do($coderef);
+ };
+
+ if ($@) { # Transaction failed
+ my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+ die $msg . "\n";
+ return undef;
+ }
+ return 1;
+
+}
+
+#-----------------------------------------------------------------------------------
+
=head1 AUTHOR
Frank Brehm
[% META title = 'Verlagsliste' -%]
+[% tab_colspan = 4 %][% IF Catalyst.user_exists %][% tab_colspan = 6 %][% END -%]
+[% BLOCK navrow %]
+ <tr>
+ <td class="nav" colspan="[% tab_colspan %]"><table cellspacing="0" class="nav" width="100%">
+ <tr>
+ <td width="33%" style="text-align: left;"><b>[% IF nav.cur != 1 %]<a href="[% self_url %]?page=1" title="Erste Seite"><<</a>[% ELSE %]<<[% END %] [% IF nav.cur != nav.prev %]<a href="[% self_url %]?page=[% nav.prev %]" title="Vorherige Seite"><</a>[% ELSE %]<[% END %]</b></td>
+ <td width="33%" style="text-align: center;">Verlag [% nav.verlag_from %] bis [% nav.verlag_to %] von [% nav.verlage %]</td>
+ <td width="33%" style="text-align: right;"><b>[% IF nav.cur != nav.next %]<a href="[% self_url %]?page=[% nav.next %]" title="Nächste Seite">></a>[% ELSE %]>[% END %] [% IF nav.cur != nav.last %]<a href="[% self_url %]?page=[% nav.last %]" title="Letzte Seite">>></a>[% ELSE %]>>[% END %]</b></td>
+ </tr>
+ </table></td>
+ </tr>
+[% END -%]
+
<div class="center">
<table class="wrapper" cellspacing="0">
<th></th>
[% IF Catalyst.user_exists %]<th></th>
<th></th>[% END %]
- </tr>
+ </tr>[% PROCESS navrow %]
[% rowstyle = 'even' %][%- FOREACH verlag IN verlage -%]
<tr>
<td class="[% rowstyle %]">[% verlag.name %]</td>
- <td class="[% rowstyle %]">[% verlag.descr %]</td>
+ <td class="[% rowstyle %]">[% verlag.name_long %]</td>
<td class="[% rowstyle %]" style="text-align: right;">[% verlag.count_books %]</td>
<td class="button [% rowstyle %]"><a href="[% path('/verlag/view') %]?view_verlags_id=[% verlag.id %]">Ansehen</a></td>
[% IF Catalyst.user_exists %]<td class="button [% rowstyle %]"><a href="[% path('/verlag/edit') %]?edit_verlags_id=[% verlag.id %]">Ändern</a></td>
<td class="button [% rowstyle %]">[% IF verlag.count_books > 0 %]Löschen[% ELSE %]<a href="[% path('/verlag/delete') %]?delete_verlags_id=[% author.id %]">Löschen</a>[% END %]</td>[% END %]
</tr>
[% IF rowstyle == 'even' %][% rowstyle = 'odd'%][% ELSE %][% rowstyle = 'even' %][% END %][% END -%]
-</table></td></tr></table>
+[% PROCESS navrow %]</table></td></tr></table>
</div>
<br />