From 90a39f217577f30a1eafde0ef4cb2a1d4b476e5b Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 27 Oct 2009 22:24:14 +0000 Subject: [PATCH] =?utf8?q?Kategorien=20+=20Verlage=20vervollst=C3=A4ndigt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/FrBr/Books/Controller/Kategorie.pm | 169 ++++++++++++++++++++++++- lib/FrBr/Books/Controller/Verlag.pm | 81 +++++++++++- lib/FrBr/Books/Util/Category.pm | 47 +++++++ root/src/category/cat_form.tt2 | 4 +- root/src/category/delete.tt2 | 48 +++++++ root/src/category/delete_success.tt2 | 30 +++++ root/src/category/edit.tt2 | 16 +++ root/src/category/form.css | 5 +- root/src/category/new.tt2 | 6 +- root/src/verlag/delete.tt2 | 52 ++++++++ root/src/verlag/delete_success.tt2 | 31 +++++ 11 files changed, 480 insertions(+), 9 deletions(-) create mode 100644 root/src/category/delete.tt2 create mode 100644 root/src/category/delete_success.tt2 create mode 100644 root/src/category/edit.tt2 create mode 100644 root/src/verlag/delete.tt2 create mode 100644 root/src/verlag/delete_success.tt2 diff --git a/lib/FrBr/Books/Controller/Kategorie.pm b/lib/FrBr/Books/Controller/Kategorie.pm index 240689d..3105b13 100644 --- a/lib/FrBr/Books/Controller/Kategorie.pm +++ b/lib/FrBr/Books/Controller/Kategorie.pm @@ -245,7 +245,7 @@ sub form_new : Path('new') { $self->category_cgi2session($c); - $c->stash->{'verlag_edit'} = {} unless $c->stash->{'verlag_edit'}; + $c->stash->{'category_edit'} = {} unless $c->stash->{'category_edit'}; $self->category_session2stash($c); $c->stash->{'category_edit'}{'name'} = "Neue Kategorie" unless $c->stash->{'category_edit'}{'name'}; @@ -257,6 +257,173 @@ sub form_new : Path('new') { #------------------------------------------------------- +=head2 form_edit( ) + +Aendern einer vorhandenen Kategorie. + +=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("/kategorie/edit"), + 'name' => "Ändern" + }; + + $c->stash->{'error_message'} = ''; + + # Zu bearbeitende Kategorie-Id ermitteln + my $cid = to_int( $c->request->params->{'edit_category_id'} || 0 ); + + unless ( $cid ) { + if ( $c->session->{'category_data_edit'} and $c->session->{'category_data_edit'}{'id'} ) { + $cid = $c->session->{'category_data_edit'}{'id'}; + } + } + + # Keine Kategorie-Id da? + unless ( $cid ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Keine oder ungültige Kategorie-Id übergeben.'; + return 1; + } + + # Angaben zur Kategorie aus der Datenbank holen + my $cliste = get_category_list( $c, 'category_id' => $cid, ); + + unless ( $cliste and scalar( @$cliste ) ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Zur übergebenen Kategorie-Id wurde keine Kategorie gefunden.'; + return 1; + } + + my $category = $cliste->[0]; + + $c->log->debug( get_output_string( $K, "Kategorie aus Datenbank: ", $category ) ) if $c->stash->{'debug_level'} >= 2; + + # Ruecksprung-URL bestimmen + $c->stash->{'return_target'} = $c->session->{'return_target_category_save'} or $c->web_path("/kategorie/list"); + + # Sitzungsdaten vorbelegen, falls noch nicht geschehen + if ( $c->session->{'category_data_edit'} ) { + if ( ! $c->session->{'category_data_edit'}{'id'} or $c->session->{'category_data_edit'}{'id'} != $cid ) { + $c->session->{'category_data_edit'} = $category; + } + } + else { + $c->session->{'category_data_edit'} = $category; + } + $cliste = undef; + + $self->category_cgi2session($c); + + $c->stash->{'template'} = 'category/edit.tt2'; + push @{$c->stash->{'cssfiles'}}, 'category/form.css'; + + $self->category_session2stash($c); + + unless ( $c->request->params->{'category_form_sent'} and $c->request->params->{'do_save'} ) { + return 1; + } + + $c->stash->{'return_target_action'} = $c->session->{'return_target_edit'}; + return $self->do_save_category($c); + +} + +#------------------------------------------------------- + +=head2 delete_category_form( ) + +Loescht eine vorhandene Kategorie. + +=cut + +sub delete_category_form : Path('delete') { + + 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("/kategorie/delete"), + 'name' => "Löschen" + }; + + $c->stash->{'error_message'} = ''; + + my $cid = to_int( $c->request->params->{'delete_category_id'} || $c->session->{'delete_category_id'} || 0 ); + unless ( $cid ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Keine oder ungültige Kategorie-Id übergeben.'; + return 1; + } + $c->session->{'delete_category_id'} = $cid; + + # Angaben zur Kategorie aus der Datenbank holen + my $cliste = get_category_list( $c, 'category_id' => $cid ); + + unless ( $cliste and scalar( @$cliste ) ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Zur übergebenen Kategorie-Id wurde keine Kategorie gefunden.'; + return 1; + } + + my $category = $cliste->[0]; + $c->log->debug( get_output_string( $K, "Kategorie aus Datenbank: ", $category ) ) if $c->stash->{'debug_level'} >= 2; + + my $anzahl_buecher = $c->model('Schema::Buch2kategorie')->count( { 'kategorie_id' => $cid, } ); + + if ( $anzahl_buecher ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Der übergebenen Kategorie sind noch Bücher zugeordnet.'; + return 1; + } + + $c->stash->{'return_target'} = $c->session->{'return_target_category_save'} or $c->web_path("/kategorie/list"); + + $c->stash->{'kategorie'} = $category; + $cliste = undef; + + $c->stash->{'template'} = 'category/delete.tt2'; + push @{$c->stash->{'cssfiles'}}, 'category/view.css'; + + if ( $c->request->params->{'really_delete_category'} and $c->request->params->{'really_delete_category'} eq 'yes' and $c->request->params->{'do_delete'} ) { + eval { + delete_category( $c, $cid ); + }; + if ( $@ ) { + $c->stash->{'error_message'} = $@; + $c->log->warn( "Fehler beim Löschen der Kategorie: " . $@ ); + return undef; + } + $c->stash->{'template'} = 'category/delete_success.tt2'; + } + +} + +#------------------------------------------------------- + sub do_save_category : Private { my ( $self, $c ) = @_; diff --git a/lib/FrBr/Books/Controller/Verlag.pm b/lib/FrBr/Books/Controller/Verlag.pm index db48ee1..b22a663 100644 --- a/lib/FrBr/Books/Controller/Verlag.pm +++ b/lib/FrBr/Books/Controller/Verlag.pm @@ -278,7 +278,7 @@ sub form_edit : Path('edit') { $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"), + 'path' => $c->web_path("/verlag/edit"), 'name' => "Ändern" }; @@ -345,6 +345,85 @@ sub form_edit : Path('edit') { #------------------------------------------------------- +=head2 delete_verlag_form( ) + +Loescht einen vorhandenen Verlag. + +=cut + +sub delete_verlag_form : Path('delete') { + + 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("/verlag/delete"), + 'name' => "Löschen" + }; + + $c->stash->{'error_message'} = ''; + + my $vid = to_int( $c->request->params->{'delete_verlags_id'} || $c->session->{'delete_verlags_id'} || 0 ); + unless ( $vid ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Keine oder ungültige Verlags-Id übergeben.'; + return 1; + } + $c->session->{'delete_verlags_id'} = $vid; + + # 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; + + my $anzahl_buecher = $c->model('Schema::Buecher')->count( { 'verlags_id' => $vid, } ); + + if ( $anzahl_buecher ) { + $c->stash->{'template'} = 'error.tt2'; + $c->stash->{'error'} = 'Dem übergebenen Verlag sind noch Bücher zugeordnet.'; + return 1; + } + + $c->stash->{'return_target'} = $c->session->{'return_target_verlag_save'} or $c->web_path("/verlag/list"); + + $c->stash->{'verlag'} = $verlag; + $vliste = undef; + + $c->stash->{'template'} = 'verlag/delete.tt2'; + push @{$c->stash->{'cssfiles'}}, 'verlag/view.css'; + + if ( $c->request->params->{'really_delete_verlag'} and $c->request->params->{'really_delete_verlag'} eq 'yes' and $c->request->params->{'do_delete'} ) { + eval { + delete_verlag( $c, $vid ); + }; + if ( $@ ) { + $c->stash->{'error_message'} = $@; + $c->log->warn( "Fehler beim Löschen des Verlags: " . $@ ); + return undef; + } + $c->stash->{'template'} = 'verlag/delete_success.tt2'; + } + +} + +#------------------------------------------------------- + sub do_save_verlag : Private { my ( $self, $c ) = @_; diff --git a/lib/FrBr/Books/Util/Category.pm b/lib/FrBr/Books/Util/Category.pm index fdca38a..d2fefa2 100644 --- a/lib/FrBr/Books/Util/Category.pm +++ b/lib/FrBr/Books/Util/Category.pm @@ -24,6 +24,7 @@ BEGIN { @EXPORT = qw( &get_category_list &save_category + &delete_category ); #%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], @@ -330,6 +331,52 @@ ENDE #----------------------------------------------------------------------------------- +=head2 delete_category( $c, $category_id ) + +Loescht die Kategorie mit der uebergebenen Kategorie-Id + +=cut + +sub delete_category { + + my $c = shift; + my $cat_id = shift; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( $K . "aufgerufen fuer Kategorie-Id " . ( defined $cat_id ? $cat_id : '' ) . "." ) if $c->stash->{'debug_level'} > 2; + + return undef unless $cat_id and to_int($cat_id); + $cat_id = to_int($cat_id); + + my $schema = $c->model('Schema')->{'schema'}; + + # Raus, wenn die Kategorie noch Buecher hat ... + if ( $c->model('Schema::Buch2kategorie')->count( { 'kategorie_id' => $cat_id, } ) ) { + my $msg = 'Es existieren noch Buecher dieser Kategorie.'; + die $msg . "\n"; + } + + # Das eigentliche Löschen + my $coderef = sub { + $c->model('Schema::Kategorien')->search( { 'id' => $cat_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 diff --git a/root/src/category/cat_form.tt2 b/root/src/category/cat_form.tt2 index 1901121..06874c3 100644 --- a/root/src/category/cat_form.tt2 +++ b/root/src/category/cat_form.tt2 @@ -1,4 +1,4 @@ -[%# + +Kategorie-Formular -->
diff --git a/root/src/category/delete.tt2 b/root/src/category/delete.tt2 new file mode 100644 index 0000000..2fd187f --- /dev/null +++ b/root/src/category/delete.tt2 @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + +
Möchten Sie die Kategorie '[% kategorie.name | html %]' wirklich löschen?
Name der Kategorie:[% kategorie.name | html %]
+ +[%- IF error_message %] +
+Fehler: [% error_message %] +
+[% END -%] + + + diff --git a/root/src/category/delete_success.tt2 b/root/src/category/delete_success.tt2 new file mode 100644 index 0000000..18eb62c --- /dev/null +++ b/root/src/category/delete_success.tt2 @@ -0,0 +1,30 @@ + + +
+ +Die Kategorie "[% kategorie.name | html %]" wurde erfolgreich gelöscht. + +
+ + + diff --git a/root/src/category/edit.tt2 b/root/src/category/edit.tt2 new file mode 100644 index 0000000..6a0f1bc --- /dev/null +++ b/root/src/category/edit.tt2 @@ -0,0 +1,16 @@ + +[% PROCESS category/cat_form.tt2 %] + diff --git a/root/src/category/form.css b/root/src/category/form.css index ae5c535..4849a54 100644 --- a/root/src/category/form.css +++ b/root/src/category/form.css @@ -1,10 +1,11 @@ -[%# +/* [%# # Template fuer Stylesheets Kategorieformulare # # $Id$ # $URL$ # -%] -/* Stylesheets Kategorie-Formulare */ +Stylesheets Kategorie-Formulare */ +[% PROCESS ftable.css %] diff --git a/root/src/category/new.tt2 b/root/src/category/new.tt2 index 3fd7d2c..0e39f70 100644 --- a/root/src/category/new.tt2 +++ b/root/src/category/new.tt2 @@ -1,4 +1,4 @@ -[%# + [% PROCESS category/cat_form.tt2 %] - diff --git a/root/src/verlag/delete.tt2 b/root/src/verlag/delete.tt2 new file mode 100644 index 0000000..f706286 --- /dev/null +++ b/root/src/verlag/delete.tt2 @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + +
Möchten Sie den Verlag '[% verlag.name | html %]' wirklich löschen?
Name des Verlags:[% verlag.name | html %]
Ausführlicher Verlagsname:[% verlag.name_long | html %]
+ +[%- IF error_message %] +
+Fehler: [% error_message %] +
+[% END -%] + + + diff --git a/root/src/verlag/delete_success.tt2 b/root/src/verlag/delete_success.tt2 new file mode 100644 index 0000000..f6a0a84 --- /dev/null +++ b/root/src/verlag/delete_success.tt2 @@ -0,0 +1,31 @@ + + +
+ +Der Verlag "[% verlag.name | html %]" wurde erfolgreich gelöscht. + +
+ + + -- 2.39.5