From 4ef08be450afeca5b2eeffc62ed84446a554e312 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 21 Jun 2013 10:52:48 +0200 Subject: [PATCH] restructure parameters of NGCP::Panel::Utils::check_form_buttons to allow multiple redirect_uris to be given to it the old style (arrayref) is still supported though +fix typo --- lib/NGCP/Panel/Controller/Billing.pm | 14 ++++--- lib/NGCP/Panel/Controller/Contract.pm | 54 ++++++++++++++++++++++---- lib/NGCP/Panel/Controller/Peering.pm | 8 ++-- lib/NGCP/Panel/Field/BillingProfile.pm | 2 +- lib/NGCP/Panel/Utils.pm | 28 +++++++------ 5 files changed, 75 insertions(+), 31 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index 674d478366..3ea17d5bcc 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -170,16 +170,17 @@ sub fees_ajax :Chained('fees_list') :PathPart('ajax') :Args(0) { sub fees_create :Chained('fees_list') :PathPart('create') :Args(0) { my ($self, $c) = @_; + my $profile_id = $c->stash->{profile}->{id}; my $form = NGCP::Panel::Form::BillingFee->new; $form->process( posted => ($c->request->method eq 'POST'), params => $c->request->params, - action => $c->uri_for($c->stash->{profile}->{id}, 'fees', 'create'), + action => $c->uri_for($profile_id, 'fees', 'create'), ); return if NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/billing_zone.create/], + c => $c, form => $form, + fields => {'billing_zone.create' => $c->uri_for("$profile_id/zones/create")}, back_uri => $c->req->uri, - redir_uri => $c->uri_for($c->stash->{profile}->{id}, 'zones', 'create'), ); if($form->validated) { $c->stash->{'profile_result'} @@ -242,18 +243,19 @@ sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) { sub fees_edit :Chained('fees_base') :PathPart('edit') :Args(0) { my ($self, $c) = @_; + my $profile_id = $c->stash->{profile}->{id}; my $posted = ($c->request->method eq 'POST'); my $form = NGCP::Panel::Form::BillingFee->new; $form->field('billing_zone')->field('id')->ajax_src('../../zones/ajax'); $form->process( posted => 1, params => $posted ? $c->request->params : $c->stash->{fee}, - action => $c->uri_for($c->stash->{profile}->{id},'fees',$c->stash->{fee}->{id}, 'edit'), + action => $c->uri_for($profile_id,'fees',$c->stash->{fee}->{id}, 'edit'), ); return if NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/billing_zone.create/], + c => $c, form => $form, + fields => {'billing_zone.create' => $c->uri_for("$profile_id/zones/create")}, back_uri => $c->req->uri, - redir_uri => $c->uri_for($c->stash->{profile}->{id}, 'zones', 'create'), ); if($posted && $form->validated) { $c->stash->{'fee_result'} diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index 92377e75dd..2eaadb85f7 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -55,11 +55,6 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) { my ($self, $c) = @_; my $item = $c->model('billing')->resultset('billing_mappings')->new_result({}); - if($c->session->{create_peering_contract}){ - $item->product( - $c->model('billing')->resultset('products')->find({class => 'sippeering'}) - ); - } my $form = NGCP::Panel::Form::Contract->new; if($form->process( @@ -123,12 +118,13 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) { # TODO: catch insert error and roll back billing_mapping and contracts entry } return if NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/contact.create/], + c => $c, form => $form, + fields => {'contract.contact.create' => $c->uri_for('/contact/create'), + 'billing_profile.create' => $c->uri_for('/billing/create')}, back_uri => $c->uri_for('create') ); if($form->validated) { $c->flash(messages => [{type => 'success', text => 'Contract successfully created!'}]); - $c->session(create_peering_contract => 0); if($c->stash->{close_target}) { $c->response->redirect($c->stash->{close_target}); @@ -194,7 +190,9 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { action => $c->uri_for($c->stash->{contract}->{id}, 'edit'), ); return if NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/contact.create/], + c => $c, form => $form, + fields => {'contract.contact.create' => $c->uri_for('/contact/create'), + 'billing_profile.create' => $c->uri_for('/billing/create')}, back_uri => $c->uri_for($c->stash->{contract}->{id}, 'edit') ); if($posted && $form->validated) { @@ -259,6 +257,41 @@ sub peering_ajax :Chained('peering_list') :PathPart('ajax') :Args(0) { $c->detach( $c->view("JSON") ); } +sub peering_create :Chained('peering_list') :PathPart('create') :Args(0) { + my ($self, $c) = @_; + + my $item = $c->model('billing')->resultset('billing_mappings')->new_result({}); + $item->product( + $c->model('billing')->resultset('products')->find({class => 'sippeering'}) + ); + + my $form = NGCP::Panel::Form::Contract->new; + $form->process( + posted => ($c->request->method eq 'POST'), + params => $c->request->params, + item => $item, + ); + return if NGCP::Panel::Utils::check_form_buttons( + c => $c, form => $form, + fields => {'contract.contact.create' => $c->uri_for('/contact/create'), + 'billing_profile.create' => $c->uri_for('/billing/create')}, + back_uri => $c->uri_for('create') + ); + if($form->validated) { + $c->flash(messages => [{type => 'success', text => 'Contract successfully created!'}]); + + if($c->stash->{close_target}) { + $c->response->redirect($c->stash->{close_target}); + return; + } + $c->response->redirect($c->uri_for_action('/contract/root')); + return; + } + + $c->stash(create_flag => 1); + $c->stash(form => $form); +} + __PACKAGE__->meta->make_immutable; 1; @@ -316,6 +349,11 @@ Display contracts through F template. Use L as Similar to L. Only select contracts, where billing.product is of class "sippeering". +=head2 peering_create + +Similar to L but sets product_id of billing_mapping to match the +product of class "sippeering". + =head1 AUTHOR Andreas Granig,,, diff --git a/lib/NGCP/Panel/Controller/Peering.pm b/lib/NGCP/Panel/Controller/Peering.pm index 94ea52c275..3edc735856 100644 --- a/lib/NGCP/Panel/Controller/Peering.pm +++ b/lib/NGCP/Panel/Controller/Peering.pm @@ -74,10 +74,10 @@ sub edit :Chained('base') :PathPart('edit') { action => $c->uri_for_action('/peering/edit', [$c->req->captures->[0]]) ); if (NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/contract.create/], + c => $c, form => $form, + fields => {'contract.create' => $c->uri_for('/contract/peering/create')}, back_uri => $c->req->uri, )) { - $c->session(create_peering_contract => 1); return; } if($posted && $form->validated) { @@ -119,10 +119,10 @@ sub create :Chained('group_list') :PathPart('create') :Args(0) { action => $c->uri_for('create'), ); if (NGCP::Panel::Utils::check_form_buttons( - c => $c, form => $form, fields => [qw/contract.create/], + c => $c, form => $form, + fields => {'contract.create' => $c->uri_for('/contract/peering/create')}, back_uri => $c->req->uri, )) { - $c->session(create_peering_contract => 1); return; } if($form->validated) { diff --git a/lib/NGCP/Panel/Field/BillingProfile.pm b/lib/NGCP/Panel/Field/BillingProfile.pm index b4e7b28102..216ac93812 100644 --- a/lib/NGCP/Panel/Field/BillingProfile.pm +++ b/lib/NGCP/Panel/Field/BillingProfile.pm @@ -17,7 +17,7 @@ has_field 'id' => ( has_field 'create' => ( type => 'Button', label => 'or', - value => 'Create Contract', + value => 'Create Billing Profile', element_class => [qw/btn btn-tertiary/], ); diff --git a/lib/NGCP/Panel/Utils.pm b/lib/NGCP/Panel/Utils.pm index 1f49c255b8..624c150094 100644 --- a/lib/NGCP/Panel/Utils.pm +++ b/lib/NGCP/Panel/Utils.pm @@ -29,26 +29,30 @@ sub check_form_buttons { my $fields = $params{fields}; my $form = $params{form}; my $back_uri = $params{back_uri}; - my $redir_uri = $params{redir_uri}; + + $fields = { map {($_, undef)} @$fields } + if (ref($fields) eq "ARRAY"); my $posted = ($c->request->method eq 'POST'); if($posted && $form->field('submitid')) { my $val = $form->field('submitid')->value; - - if(defined $val and grep {/^$val$/} @{ $fields }) { - my $target = '/'.$val; - $target =~ s/\./\//g; + + if(defined $val and exists($fields->{$val}) ) { + my $target; + if (defined $fields->{$val}) { + $target = $fields->{$val}; + } else { + $target = '/'.$val; + $target =~ s/\./\//g; + $target = $c->uri_for($target); + } if($c->session->{redirect_targets}) { unshift @{ $c->session->{redirect_targets} }, $back_uri; } else { $c->session->{redirect_targets} = [ $back_uri ]; } - if (defined $redir_uri) { - $c->response->redirect($redir_uri); - } else { - $c->response->redirect($c->uri_for($target)); - } + $c->response->redirect($target); return 1; } } @@ -204,10 +208,10 @@ Puts close_target to stash, which will be read by the templates. Parameters: c - fields + fields - either an arrayref of fieldnames or a hashref with fieldnames + key and redirect target as value (where it should redirect to) form back_uri - the uri we come from - redir_uri (optional) - the uri we go to, used for any button Checks the hidden field "submitid" and redirects to its "value" when it matches a field.