From 6b64c732acbd64a287480e549d7b0966c2b94a6e Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 8 Mar 2013 13:21:00 +0100 Subject: [PATCH] outsource ajax processing into private action /ajax_process in Root controller --- lib/NGCP/Panel/Controller/Contact.pm | 44 +++------------------------ lib/NGCP/Panel/Controller/Reseller.pm | 44 ++------------------------- lib/NGCP/Panel/Controller/Root.pm | 36 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 81 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Contact.pm b/lib/NGCP/Panel/Controller/Contact.pm index 7bc467e882..d5fdee6527 100644 --- a/lib/NGCP/Panel/Controller/Contact.pm +++ b/lib/NGCP/Panel/Controller/Contact.pm @@ -122,51 +122,15 @@ sub ajax :Chained('list') :PathPart('ajax') :Args(0) { #TODO: when user is not logged in, this gets forwarded to login page - #Process Arguments - my $sEcho = $c->request->params->{sEcho}; - my $sSearch = $c->request->params->{sSearch}; - my $iDisplayStart = $c->request->params->{iDisplayStart}; - my $iDisplayLength = $c->request->params->{iDisplayLength}; + my $contacts = $c->stash->{contacts}; - if(! $sEcho ) { - $sEcho = "1"; - } - if(! $sSearch ) { - $sSearch = ""; - } - - $c->stash(sEcho => $sEcho); - - #Parse contacts into aaData (for datatables) - my $data = $c->stash->{contacts}; - my $aaData = []; - - for my $row (@$data) { - if (index($row->{firstname}, $sSearch) >= 0 || - index($row->{lastname}, $sSearch) >= 0 || - index($row->{email}, $sSearch) >= 0) { - push @$aaData, [$row->{id}, - $row->{firstname}, - $row->{lastname}, - $row->{email}]; - } - } - my $totalRecords = scalar(@$aaData); - #Pagination - if($iDisplayStart || $iDisplayLength ) { - my $endIndex = $iDisplayLength+$iDisplayStart-1; - $endIndex = $#$aaData if $endIndex > $#$aaData; - @$aaData = @$aaData[$iDisplayStart .. $endIndex]; - } - - $c->stash(aaData => $aaData, - iTotalRecords => $totalRecords, - iTotalDisplayRecords => $totalRecords); + $c->forward( "/ajax_process", [$contacts, + ["id","firstname","lastname","email"], + [1,2,3]]); $c->detach( $c->view("JSON") ); } - =head1 AUTHOR Andreas Granig,,, diff --git a/lib/NGCP/Panel/Controller/Reseller.pm b/lib/NGCP/Panel/Controller/Reseller.pm index 3c089eda77..3eaa310b96 100644 --- a/lib/NGCP/Panel/Controller/Reseller.pm +++ b/lib/NGCP/Panel/Controller/Reseller.pm @@ -133,53 +133,15 @@ sub ajax :Chained('list') :PathPart('ajax') :Args(0) { #TODO: when user is not logged in, this gets forwarded to login page - #Process Arguments - my $sEcho = $c->request->params->{sEcho}; - my $sSearch = $c->request->params->{sSearch}; - my $iDisplayStart = $c->request->params->{iDisplayStart}; - my $iDisplayLength = $c->request->params->{iDisplayLength}; - - if(! $sEcho ) { - $sEcho = "1"; - } - if(! $sSearch ) { - $sSearch = ""; - } - - $c->stash(sEcho => $sEcho); - - #Parse resellers into aaData (for datatables) my $resellers = $c->stash->{resellers}; - my $aaData = []; - - for my $row (@$resellers) { - if (index($row->{name}, $sSearch) >= 0) { - push @$aaData, [$row->{id}, - $row->{name}, - $row->{"contract.id"}, - $row->{status}]; - } - } - my $totalRecords = scalar(@$aaData); - #Pagination - if($iDisplayStart || $iDisplayLength ) { - my $endIndex = $iDisplayLength+$iDisplayStart-1; - $endIndex = $#$aaData if $endIndex > $#$aaData; - @$aaData = @$aaData[$iDisplayStart .. $endIndex]; - } - $c->stash(aaData => $aaData, - iTotalRecords => $totalRecords, - iTotalDisplayRecords => $totalRecords); + $c->forward( "/ajax_process", [$resellers, + ["id","name","contract.id","status"], + [1,3]]); $c->detach( $c->view("JSON") ); } -sub listdt :Chained('list') :PathPart('listdt') :Args(0) { - my ($self, $c) = @_; - - $c->stash(template => 'reseller/listdt.tt'); -} =head1 AUTHOR diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index 3f73e8a761..ab430e39da 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -91,6 +91,42 @@ Attempt to render a view, if needed. sub end : ActionClass('RenderView') {} +sub ajax_process :Private { + my ($self,$c,@arguments) = @_; + + my ($data,$columns,$searchable) = @arguments; + + #Process Arguments + my $sEcho = $c->request->params->{sEcho} // "1"; #/ + my $sSearch = $c->request->params->{sSearch} // ""; #/ + my $iDisplayStart = $c->request->params->{iDisplayStart}; + my $iDisplayLength = $c->request->params->{iDisplayLength}; + + #parse $data into $aaData + my $aaData = []; + + for my $row (@$data) { + my @aaRow = @$row{@$columns}; + if (grep /$sSearch/, @aaRow[@$searchable]) { + push @$aaData, \@aaRow; + } + } + my $totalRecords = scalar(@$aaData); + #Pagination + if($iDisplayStart || $iDisplayLength ) { + my $endIndex = $iDisplayLength+$iDisplayStart-1; + $endIndex = $#$aaData if $endIndex > $#$aaData; + @$aaData = @$aaData[$iDisplayStart .. $endIndex]; + } + + $c->stash(aaData => $aaData, + iTotalRecords => $totalRecords, + iTotalDisplayRecords => $totalRecords); + + + $c->stash(sEcho => $sEcho); +} + =head1 AUTHOR Andreas Granig,,,