From 9b8ce0d783485d5ff7cc0a984c01aa00eda66da1 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Wed, 7 Aug 2013 15:59:52 +0200 Subject: [PATCH] Align billing fees to new DT mechanism. Finally remove the ajax_process_resultset function from Root controller. --- lib/NGCP/Panel/Controller/Billing.pm | 23 +++----- lib/NGCP/Panel/Controller/Root.pm | 87 ---------------------------- share/templates/billing/fees.tt | 24 ++++---- 3 files changed, 21 insertions(+), 113 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index e7d6698019..276ebf2af4 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -201,7 +201,13 @@ sub create_without_reseller :Chained('profile_list') :PathPart('create/noreselle sub fees_list :Chained('base') :PathPart('fees') :CaptureArgs(0) { my ($self, $c) = @_; - + $c->stash->{fee_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [ + { name => 'id', search => 1, title => '#' }, + { name => 'source', search => 1, title => 'Source Pattern' }, + { name => 'destination', search => 1, title => 'Destination Pattern' }, + { name => 'direction', search => 1, title => 'Match Direction' }, + { name => 'billing_zone.detail', search => 1, title => 'Billing Zone' }, + ]); $c->stash(template => 'billing/fees.tt'); } @@ -236,19 +242,8 @@ sub fees_base :Chained('fees_list') :PathPart('') :CaptureArgs(1) { sub fees_ajax :Chained('fees_list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - my $resultset = $c->stash->{'profile_result'}->billing_fees - ->search(undef, { - join => 'billing_zone', - columns => [ - {'zone' => 'billing_zone.zone'}, - 'id','source','destination','direction' - ] - }); - - $c->forward( "/ajax_process_resultset", [$resultset, - ["id", "source", "destination", "direction", "zone"], - ["source", "destination", "direction"]]); - + my $resultset = $c->stash->{'profile_result'}->billing_fees; + NGCP::Panel::Utils::Datatables::process($c, $resultset, $c->stash->{fee_dt_columns}); $c->detach( $c->view("JSON") ); } diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index acc8a1383a..d86a40a9fd 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -108,79 +108,6 @@ sub default :Path { sub end : ActionClass('RenderView') {} -sub ajax_process_resultset :Private { - my ($self, $c, $rs, $columns, $searchable) = @_; - - #Process Arguments - my $sEcho = int($c->request->params->{sEcho} // 1); #/ - # http://datatables.net/usage/server-side#sEcho - my $sSearch = $c->request->params->{sSearch} // ""; #/ - my $iDisplayStart = $c->request->params->{iDisplayStart}; - my $iDisplayLength = $c->request->params->{iDisplayLength}; - my $iSortCol_0 = $c->request->params->{iSortCol_0}; - my $sSortDir_0 = $c->request->params->{sSortDir_0}; - - if (defined $sSortDir_0) { - if ('desc' eq lc $sSortDir_0) { - $sSortDir_0 = 'desc'; - } else { - $sSortDir_0 = 'asc'; - } - } - - my $iIdOnTop = $c->request->params->{iIdOnTop}; - - #will contain final data to be sent - my $aaData = []; - - my $totalRecords = $rs->count; - - if ($sSearch) { - $rs = $rs->search([ map{ +{ $_ => { like => '%'.$sSearch.'%' } } } @$searchable ]); - } - - my $totalDisplayRecords = $rs->count; - - #potentially selected Id as first element - if (defined $iIdOnTop) { - if (defined(my $row = $rs->find($iIdOnTop))) { - push @{ $aaData }, _prune_row($columns, $row->get_inflated_columns); - $rs = $rs->search({ 'me.id' => { '!=', $iIdOnTop} }); - } else { - $c->log->error("iIdOnTop $iIdOnTop not found in resultset " . ref $rs); - }; - } - - #Sorting - if (defined $iSortCol_0 && defined $sSortDir_0) { - $rs = $rs->search(undef, { - order_by => { - "-$sSortDir_0" => $columns->[$iSortCol_0], - } - }); - } - - #Pagination - # $iDisplayLength will be -1 if bPaginate is false - if (defined $iDisplayStart && $iDisplayLength && $iDisplayLength > 0) { - $rs = $rs->search(undef, { - offset => $iDisplayStart, - rows => $iDisplayLength, - }); - } - - for my $row ($rs->all) { - push @{ $aaData }, _prune_row($columns, $row->get_inflated_columns); - } - - $c->stash( - aaData => $aaData, - iTotalRecords => $totalRecords, - iTotalDisplayRecords => $totalDisplayRecords, - sEcho => $sEcho, - ); -} - sub _prune_row { my ($columns, %row) = @_; while (my ($k,$v) = each %row) { @@ -244,20 +171,6 @@ Standard 404 error page Attempt to render a view, if needed. -=head2 ajax_process_resultset - -Processes a L and prepares data from other controllers to be used -with the JSON view. The items exposed to stash are namely: - -* sEcho -* aaData -* iTotalRecords -* iTotalDisplayRecords - -They are intended for use with datatables. - -Arguments: $resultset, \@columns, \@searchable - =head2 error_page should be called if the intended page could not be found (404). diff --git a/share/templates/billing/fees.tt b/share/templates/billing/fees.tt index a6751616e9..047e41b987 100644 --- a/share/templates/billing/fees.tt +++ b/share/templates/billing/fees.tt @@ -2,10 +2,8 @@ [% helper.name = 'Billing Fees'; - helper.show_create_button = 1; helper.messages = messages; - helper.column_titles = [ '#', 'Source', 'Destination', 'Direction', 'Zone' ]; - helper.column_fields = [ 'id', 'source', 'destination', 'direction', 'zone' ]; + helper.dt_columns = fee_dt_columns; helper.close_target = close_target; helper.create_flag = create_flag; @@ -14,15 +12,17 @@ helper.ajax_uri = c.uri_for_action('/billing/fees_ajax', [c.req.captures.0]); helper.tmpuri = c.uri_for( profile.id, 'fees'); - helper.dt_buttons = [ - { name = 'Edit', uri = helper.tmpuri _ "/'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, - { name = 'Delete', uri = helper.tmpuri _ "/'+full[\"id\"]+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' }, - ]; - helper.top_buttons = [ - { name = 'Create Fee Entry', uri = c.uri_for( profile.id, 'fees') _ "/create", icon = 'icon-star' }, - { name = 'Upload Fees as CSV', uri = c.uri_for_action('/billing/fees_upload',[c.req.captures.0]), icon = 'icon-star' }, - { name = 'Edit Zones', uri = c.uri_for_action('/billing/zones',[c.req.captures.0]), icon = 'icon-star' }, - ]; + UNLESS c.user.read_only; + helper.dt_buttons = [ + { name = 'Edit', uri = helper.tmpuri _ "/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, + { name = 'Delete', uri = helper.tmpuri _ "/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' }, + ]; + helper.top_buttons = [ + { name = 'Create Fee Entry', uri = c.uri_for( profile.id, 'fees') _ "/create", icon = 'icon-star' }, + { name = 'Upload Fees as CSV', uri = c.uri_for_action('/billing/fees_upload',[c.req.captures.0]), icon = 'icon-star' }, + { name = 'Edit Zones', uri = c.uri_for_action('/billing/zones',[c.req.captures.0]), icon = 'icon-star' }, + ]; + END; PROCESS 'helpers/datatables.tt'; -%]