--- /dev/null
+package FrBr::Books::Controller::Ort;
+
+# $Id$
+# $URL$
+
+use strict;
+use warnings;
+use base 'Catalyst::Controller';
+
+use FrBr::Common;
+use FrBr::Books::Util::Ort;
+
+=head1 NAME
+
+FrBr::Books::Controller::Ort - Catalyst Controller fuer alles rund um Aufbewahrungsorte
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+#-------------------------------------------------------
+
+=head2 auto
+
+=cut
+
+sub auto : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'};
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/ort"),
+ 'name' => "Aufbewahrungsort"
+ };
+
+ $c->stash->{'cssfiles'} = [] unless $c->stash->{'cssfiles'};
+ push @{$c->stash->{'cssfiles'}}, 'ort/styles.css';
+
+ 1;
+
+} ## end sub auto :
+
+#-------------------------------------------------------
+
+=head2 index
+
+=cut
+
+sub index : Private {
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->stash->{'template'} = 'ort/index.tt2';
+}
+
+#-------------------------------------------------------
+
+=head2 default
+
+=cut
+
+sub default : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->stash->{'template'} = 'not_implemented.tt2';
+
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/ort/default"),
+ 'name' => "Nicht Implementiert"
+ };
+
+} ## end sub default :
+
+#-------------------------------------------------------
+
+=head2 form_new( )
+
+Erstellen eines neuen Aufbewahrungsortes.
+
+=cut
+
+sub form_new : Path('new') {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'};
+ push @{ $c->stash->{'menu_path'} }, {
+ 'path' => $c->web_path("/ort/new"),
+ 'name' => "Neu"
+ };
+
+ $c->stash->{'template'} = 'ort/new.tt2';
+ push @{$c->stash->{'cssfiles'}}, 'ort/form.css';
+
+ $c->stash->{'error_message'} = '';
+
+# $self->prepare_data_structures($c);
+
+ $self->ort_cgi2session($c);
+
+ $c->stash->{'ort_edit'} = {} unless $c->stash->{'ort_edit'};
+ $self->ort_session2stash($c);
+ $c->stash->{'ort_edit'}{'name'} = "Neuer Aufbewahrungsort" unless $c->stash->{'ort_edit'}{'name'};
+ $c->stash->{'ort_edit'}{'descr'} = "Beschreibung des Aufbewahrungsortes" unless defined $c->stash->{'ort_edit'}{'descr'};
+
+ return 1 unless $c->request->params->{'ort_form_sent'} and $c->request->params->{'do_save'};
+
+ return $self->do_save_ort($c);
+
+}
+
+#-------------------------------------------------------
+
+sub do_save_ort : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ return 1 unless $self->check_formparams($c);
+
+ eval {
+ die "Speichern des Aufbewahrungsortes mißlungen." unless save_ort( $c, $c->stash->{'ort_edit'} );
+ };
+ if ( $@ ) {
+ $c->stash->{'error_message'} = $@;
+ return undef;
+ }
+
+ $c->stash->{'template'} = 'ort/save_success.tt2';
+ delete $c->session->{'ort_data_edit'} if exists $c->session->{'ort_data_edit'};
+ delete $c->session->{'return_target_ort_save'} if exists $c->session->{'return_target_ort_save'};
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub check_formparams : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ unless ( $c->stash->{'ort_edit'} ) {
+ $c->stash->{'error_message'} = "Interner Fehler";
+ return undef;
+ }
+
+ unless ( $c->stash->{'ort_edit'}{'name'} ) {
+ $c->stash->{'error_message'} = "Kein Name des Aufbewahrungsortes angegeben.";
+ return undef;
+ }
+
+ $c->stash->{'ort_edit'}{'statisch'} = to_bool( $c->stash->{'ort_edit'}{'statisch'} ) ? 'y' : 'n';
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub ort_cgi2session : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->session->{'return_target_ort_save'} = $c->request->params->{'return_target_form'} if $c->request->params->{'return_target_form'};
+
+ return 1 unless $c->request->params->{'ort_form_sent'};
+
+ # Basis anlegen, wenn notwendig
+ $c->session->{'ort_data_edit'} = {} unless $c->session->{'ort_data_edit'};
+
+ # Orts-Id eintragen, wenn notwendig
+ $c->session->{'ort_data_edit'}{'id'} = $c->request->params->{'orts_id'} if $c->request->params->{'orts_id'};
+
+ # Name der Serie
+ if ( defined $c->request->params->{'orts_name'} ) {
+ my $name = $c->request->params->{'orts_name'};
+ $name =~ s/^\s+//;
+ $name =~ s/\s+$//;
+ $c->session->{'ort_data_edit'}{'name'} = $name;
+ }
+
+ # Beschreibung der Serie
+ if ( defined $c->request->params->{'orts_descr'} ) {
+ my $descr = $c->request->params->{'orts_descr'};
+ $descr =~ s/^\s+//;
+ $descr =~ s/\s+$//;
+ $c->session->{'ort_data_edit'}{'descr'} = $descr;
+ }
+ else {
+ $c->session->{'ort_data_edit'}{'descr'} = '';
+ }
+
+ # ist es statischer (nicht änderbarer) Aufbewahrungsort ?
+ $c->session->{'ort_data_edit'}{'statisch'} = to_bool( $c->request->params->{'ort_statisch'} ) ? 'y' : 'n';
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub ort_session2stash : Private {
+
+ my ( $self, $c ) = @_;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( get_output_string( $K . "Aufbewahrungsort in der Session:", $c->session->{'ort_data_edit'} ) ) if $c->stash->{'debug_level'} > 2;
+
+ $c->stash->{'ort_edit'} = {} unless $c->stash->{'ort_edit'};
+ $c->stash->{'ort_edit'}{'id'} = $c->session->{'ort_data_edit'}{'id'} if $c->session->{'ort_data_edit'}{'id'};
+ $c->stash->{'ort_edit'}{'name'} = $c->session->{'ort_data_edit'}{'name'} if $c->session->{'ort_data_edit'}{'name'};
+ $c->stash->{'ort_edit'}{'descr'} = $c->session->{'ort_data_edit'}{'descr'};
+ $c->stash->{'ort_edit'}{'statisch'} = $c->session->{'ort_data_edit'}{'statisch'};
+
+ $c->stash->{'return_target_ort_save'} = $c->session->{'return_target_ort_save'} || $c->web_path('/ort');
+
+ return 1;
+
+}
+
+#-------------------------------------------------------
+
+=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;
+
+__END__
+
+# vim: noai : ts=4 fenc=utf-8 filetype=perl expandtab :
@ISA = qw(Exporter);
@EXPORT = qw(
&get_ortsliste
+ &save_ort
);
#%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
#-----------------------------------------------------------------------------------
+=head2 save_ort( $c, $ort )
+
+=cut
+
+sub save_ort {
+
+ my $c = shift;
+ my $ort = shift;
+ my $K = ( caller(0) )[3] . "(): ";
+
+ $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+ my $storage = $c->stash->{'storage'};
+
+ my $save_func = sub {
+ my ( $storage, $dbh, $orts_id, $orts_name, $orts_desc, $statisch ) = @_;
+ my $sql = <<ENDE;
+INSERT INTO `orte` (
+ `id`, `name`, `descr`, `statisch` )
+ VALUES (
+ ?, ?, ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `id` = LAST_INSERT_ID(`id`), `name` = ?, `descr` = ?, `statisch` = ?
+ENDE
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } ( $orts_id, $orts_name, $orts_desc, $statisch, $orts_name, $orts_desc, $statisch ) ) . "\n";
+ warn $text;
+ }
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute( $orts_id, $orts_name, $orts_desc, $statisch, $orts_name, $orts_desc, $statisch );
+ };
+
+ my $static = to_bool( $ort->{'statisch'} ) ? 'y' : 'n';
+
+ $storage->dbh_do( $save_func, $ort->{'id'}, $ort->{'name'}, $ort->{'descr'}, $static );
+
+ return $storage->last_insert_id();
+
+}
+
+#-----------------------------------------------------------------------------------
+
=head1 AUTHOR
Frank Brehm