From f996d4a15e3edcee8dba0eaaa2b245a569a7beba Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Tue, 16 Jul 2013 13:55:36 +0200 Subject: [PATCH] Move naviation to NGCP::Panel::Utils::Navigation --- lib/NGCP/Panel/Controller/Administrator.pm | 4 +- lib/NGCP/Panel/Controller/Billing.pm | 8 +- lib/NGCP/Panel/Controller/Customer.pm | 4 +- lib/NGCP/Panel/Controller/Peering.pm | 8 +- lib/NGCP/Panel/Controller/Reseller.pm | 8 +- lib/NGCP/Panel/Controller/Subscriber.pm | 9 +- lib/NGCP/Panel/Utils.pm | 70 -------------- lib/NGCP/Panel/Utils/Navigation.pm | 101 +++++++++++++++++++++ lib/NGCP/Panel/Utils/Subscriber.pm | 2 +- 9 files changed, 123 insertions(+), 91 deletions(-) create mode 100644 lib/NGCP/Panel/Utils/Navigation.pm diff --git a/lib/NGCP/Panel/Controller/Administrator.pm b/lib/NGCP/Panel/Controller/Administrator.pm index 26511ff10d..2c9c31eaae 100644 --- a/lib/NGCP/Panel/Controller/Administrator.pm +++ b/lib/NGCP/Panel/Controller/Administrator.pm @@ -3,7 +3,7 @@ use Sipwise::Base; use namespace::sweep; BEGIN { extends 'Catalyst::Controller'; } use NGCP::Panel::Form::Administrator qw(); -use NGCP::Panel::Utils qw(); +use NGCP::Panel::Utils::Navigation; sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) { my ($self, $c) = @_; @@ -52,7 +52,7 @@ sub create :Chained('list_admin') :PathPart('create') :Args(0) { params => $c->request->params, action => $c->uri_for('create'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => [qw(administrator.create)], diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index b4b020460f..4633c35140 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -12,9 +12,9 @@ use NGCP::Panel::Form::BillingProfile_reseller; use NGCP::Panel::Form::BillingFee; use NGCP::Panel::Form::BillingZone; use NGCP::Panel::Form::BillingPeaktimeWeekdays; -use NGCP::Panel::Utils; use NGCP::Panel::Form::BillingPeaktimeSpecial; use NGCP::Panel::Form::BillingFeeUpload; +use NGCP::Panel::Utils::Navigation; my @WEEKDAYS = map { langinfo($_) } (DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1); #Monday Tuesday Wednesday Thursday Friday Saturday Sunday @@ -28,7 +28,7 @@ sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRol sub profile_list :Chained('/') :PathPart('billing') :CaptureArgs(0) { my ( $self, $c ) = @_; - NGCP::Panel::Utils::check_redirect_chain(c => $c); + NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); my $dispatch_to = '_profile_resultset_' . $c->user->auth_realm; my $profiles_rs = $self->$dispatch_to($c); @@ -193,7 +193,7 @@ sub fees_create :Chained('fees_list') :PathPart('create') :Args(0) { params => $c->request->params, action => $c->uri_for($profile_id, 'fees', 'create'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => {'billing_zone.create' => $c->uri_for("$profile_id/zones/create")}, back_uri => $c->req->uri, @@ -278,7 +278,7 @@ sub fees_edit :Chained('fees_base') :PathPart('edit') :Args(0) { params => $posted ? $c->request->params : $c->stash->{fee}, action => $c->uri_for($profile_id,'fees',$c->stash->{fee}->{id}, 'edit'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => {'billing_zone.create' => $c->uri_for("$profile_id/zones/create")}, back_uri => $c->req->uri, diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index fcb8e00ce1..ddab2b7c66 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -6,7 +6,7 @@ use NGCP::Panel::Utils::Contract; use NGCP::Panel::Form::CustomerMonthlyFraud; use NGCP::Panel::Form::CustomerDailyFraud; use NGCP::Panel::Form::CustomerBalance; -use NGCP::Panel::Utils; +use NGCP::Panel::Utils::Navigation; =head1 NAME @@ -32,7 +32,7 @@ sub list_customer :Chained('/') :PathPart('customer') :CaptureArgs(0) { $c->stash( template => 'customer/list.tt' ); - NGCP::Panel::Utils::check_redirect_chain(c => $c); + NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); } sub root :Chained('list_customer') :PathPart('') :Args(0) { diff --git a/lib/NGCP/Panel/Controller/Peering.pm b/lib/NGCP/Panel/Controller/Peering.pm index 8e4aa4ca5a..9ad1cf11fe 100644 --- a/lib/NGCP/Panel/Controller/Peering.pm +++ b/lib/NGCP/Panel/Controller/Peering.pm @@ -4,7 +4,7 @@ use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } -use NGCP::Panel::Utils; +use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Form::PeeringGroup; use NGCP::Panel::Form::PeeringRule; use NGCP::Panel::Form::PeeringServer; @@ -19,7 +19,7 @@ sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) { sub group_list :Chained('/') :PathPart('peering') :CaptureArgs(0) { my ( $self, $c ) = @_; - NGCP::Panel::Utils::check_redirect_chain(c => $c); + NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); $c->stash(template => 'peering/list.tt'); } @@ -72,7 +72,7 @@ sub edit :Chained('base') :PathPart('edit') { params => $posted ? $c->request->params : $c->stash->{group}, action => $c->uri_for_action('/peering/edit', [$c->req->captures->[0]]) ); - if (NGCP::Panel::Utils::check_form_buttons( + if (NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => {'contract.create' => $c->uri_for('/contract/peering/create')}, back_uri => $c->req->uri, @@ -119,7 +119,7 @@ sub create :Chained('group_list') :PathPart('create') :Args(0) { params => $c->request->params, action => $c->uri_for('create'), ); - if (NGCP::Panel::Utils::check_form_buttons( + if (NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => {'contract.create' => $c->uri_for('/contract/peering/create')}, back_uri => $c->req->uri, diff --git a/lib/NGCP/Panel/Controller/Reseller.pm b/lib/NGCP/Panel/Controller/Reseller.pm index 8a60fdc9af..bf1f528044 100644 --- a/lib/NGCP/Panel/Controller/Reseller.pm +++ b/lib/NGCP/Panel/Controller/Reseller.pm @@ -5,7 +5,7 @@ BEGIN { extends 'Catalyst::Controller'; } use DateTime qw(); use HTTP::Status qw(HTTP_SEE_OTHER); use NGCP::Panel::Form::Reseller; -use NGCP::Panel::Utils; +use NGCP::Panel::Utils::Navigation; sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) { my ($self, $c) = @_; @@ -21,7 +21,7 @@ sub list_reseller :Chained('/') :PathPart('reseller') :CaptureArgs(0) { ->resultset('resellers')->search_rs({}), template => 'reseller/list.tt' ); - NGCP::Panel::Utils::check_redirect_chain(c => $c); + NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); } sub root :Chained('list_reseller') :PathPart('') :Args(0) { @@ -55,7 +55,7 @@ sub create :Chained('list_reseller') :PathPart('create') :Args(0) { params => $c->request->params, action => $c->uri_for('create'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => [qw/contract.create/], @@ -176,7 +176,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { params => $posted ? $c->request->params : {$c->stash->{reseller}->get_inflated_columns}, action => $c->uri_for($c->stash->{reseller}->get_column('id'), 'edit'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => [qw/contract.create/], back_uri => $c->uri_for($c->stash->{reseller}->get_column('id'), 'edit') ); diff --git a/lib/NGCP/Panel/Controller/Subscriber.pm b/lib/NGCP/Panel/Controller/Subscriber.pm index bba1401299..886c96a7e2 100644 --- a/lib/NGCP/Panel/Controller/Subscriber.pm +++ b/lib/NGCP/Panel/Controller/Subscriber.pm @@ -2,6 +2,7 @@ package NGCP::Panel::Controller::Subscriber; use Sipwise::Base; use namespace::sweep; BEGIN { extends 'Catalyst::Controller'; } +use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::Contract; use NGCP::Panel::Utils::Subscriber; use NGCP::Panel::Form::Subscriber; @@ -39,7 +40,7 @@ sub sub_list :Chained('/') :PathPart('subscriber') :CaptureArgs(0) { $c->stash( template => 'subscriber/list.tt', ); - #NGCP::Panel::Utils::check_redirect_chain(c => $c); + #NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); } @@ -57,7 +58,7 @@ sub create_list :Chained('sub_list') :PathPart('create') :Args(0) { params => $c->request->params, action => $c->uri_for('/subscriber/create'), ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $form, fields => [qw/domain.create/], @@ -397,7 +398,7 @@ sub preferences_callforward :Chained('base') :PathPart('preferences/callforward' params => $params, ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $cf_form, fields => { 'cf_actions.advanced' => @@ -546,7 +547,7 @@ sub preferences_callforward_advanced :Chained('base') :PathPart('preferences/cal ); - return if NGCP::Panel::Utils::check_form_buttons( + return if NGCP::Panel::Utils::Navigation::check_form_buttons( c => $c, form => $cf_form, fields => { 'cf_actions.simple' => diff --git a/lib/NGCP/Panel/Utils.pm b/lib/NGCP/Panel/Utils.pm index b133e858a2..9ca84ce133 100644 --- a/lib/NGCP/Panel/Utils.pm +++ b/lib/NGCP/Panel/Utils.pm @@ -4,59 +4,6 @@ use warnings; use NGCP::Panel::Form::Preferences; -sub check_redirect_chain { - my %params = @_; - - # TODO: check for missing fields - my $c = $params{c}; - - if($c->session->{redirect_targets} && @{ $c->session->{redirect_targets} }) { - my $target = ${ $c->session->{redirect_targets} }[0]; - if('/'.$c->request->path eq $target->path) { - shift @{$c->session->{redirect_targets}}; - $c->stash(close_target => ${ $c->session->{redirect_targets} }[0]); - } else { - $c->stash(close_target => $target); - } - } -} - -sub check_form_buttons { - my %params = @_; - - # TODO: check for missing fields - my $c = $params{c}; - my $fields = $params{fields}; - my $form = $params{form}; - my $back_uri = $params{back_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 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 ]; - } - $c->response->redirect($target); - return 1; - } - } - return; -} sub load_preference_list { my %params = @_; @@ -333,23 +280,6 @@ Various utils to outsource common tasks in the controllers. =head1 METHODS -=head2 check_redirect_chain - -Sets close_target to the next uri in our redirect_chain if it exists. -Puts close_target to stash, which will be read by the templates. - -=head2 check_form_buttons - -Parameters: - c - 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 - -Checks the hidden field "submitid" and redirects to its "value" when it -matches a field. - =head2 load_preference_list Parameters: diff --git a/lib/NGCP/Panel/Utils/Navigation.pm b/lib/NGCP/Panel/Utils/Navigation.pm new file mode 100644 index 0000000000..b782b8d8a9 --- /dev/null +++ b/lib/NGCP/Panel/Utils/Navigation.pm @@ -0,0 +1,101 @@ +package NGCP::Panel::Utils::Navigation; +use strict; +use warnings; + +use Sipwise::Base; +use DBIx::Class::Exception; + +sub check_redirect_chain { + my %params = @_; + + # TODO: check for missing fields + my $c = $params{c}; + + if($c->session->{redirect_targets} && @{ $c->session->{redirect_targets} }) { + my $target = ${ $c->session->{redirect_targets} }[0]; + if('/'.$c->request->path eq $target->path) { + shift @{$c->session->{redirect_targets}}; + $c->stash(close_target => ${ $c->session->{redirect_targets} }[0]); + } else { + $c->stash(close_target => $target); + } + } +} + +sub check_form_buttons { + my %params = @_; + + # TODO: check for missing fields + my $c = $params{c}; + my $fields = $params{fields}; + my $form = $params{form}; + my $back_uri = $params{back_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 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 ]; + } + $c->response->redirect($target); + return 1; + } + } + return; +} + +1; + +=head1 NAME + +NGCP::Panel::Utils::Navigation + +=head1 DESCRIPTION + +A temporary helper to manipulate subscriber data + +=head1 METHODS + +=head2 check_redirect_chain + +Sets close_target to the next uri in our redirect_chain if it exists. +Puts close_target to stash, which will be read by the templates. + +=head2 check_form_buttons + +Parameters: + c + 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 + +Checks the hidden field "submitid" and redirects to its "value" when it +matches a field. + +=head1 AUTHOR + +Andreas Granig, + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Utils/Subscriber.pm b/lib/NGCP/Panel/Utils/Subscriber.pm index 138cd554d2..3e087028b7 100644 --- a/lib/NGCP/Panel/Utils/Subscriber.pm +++ b/lib/NGCP/Panel/Utils/Subscriber.pm @@ -24,7 +24,7 @@ sub get_usr_preference_rs { =head1 NAME -NGCP::Panel::Utils::Contract +NGCP::Panel::Utils::Subscriber =head1 DESCRIPTION