Skip to content

Instantly share code, notes, and snippets.

@perlDreamer
Created October 14, 2009 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlDreamer/210266 to your computer and use it in GitHub Desktop.
Save perlDreamer/210266 to your computer and use it in GitHub Desktop.
diff --git a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm
index 71bc86f..04b9d45 100644
--- a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm
+++ b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm
@@ -207,7 +207,7 @@ sub getId {
#-------------------------------------------------------------------
-=head2 iterateAll ( $asset )
+=head2 iterateAll ( $asset, [ $options ] )
This class method returns an iterator set to iterate over all entries for a Dataform.
@@ -215,13 +215,33 @@ This class method returns an iterator set to iterate over all entries for a Data
A reference to a Dataform object.
+=head3 $options
+
+A hashreference of options.
+
+=head4 offset
+
+The record number to start the iterator at. Defaults to 0 if not set.
+
+=head4 limit
+
+The number of records for the iterator to return. Defaults to a very large number if not set.
+
=cut
sub iterateAll {
- my $class = shift;
- my $asset = shift;
- my $sth = $asset->session->dbSlave->read("SELECT `DataForm_entryId`, `userId`, `username`, `ipAddress`, `submissionDate`, `entryData` FROM `DataForm_entry` WHERE `assetId` = ? ORDER BY `submissionDate` DESC", [$asset->getId]);
- my $sub = sub {
+ my $class = shift;
+ my $asset = shift;
+ my $options = shift;
+ my $sql = "SELECT SQL_CALC_FOUND_ROWS `DataForm_entryId`, `userId`, `username`, `ipAddress`, `submissionDate`, `entryData` FROM `DataForm_entry` WHERE `assetId` = ? ORDER BY `submissionDate` DESC LIMIT ?,?";
+ my $placeHolders = [ $asset->getId ];
+ push @{ $placeHolders }, exists $options->{offset} ? $options->{offset} : 0;
+ push @{ $placeHolders }, exists $options->{limit} ? $options->{limit} : 1234567890;
+ my $slave = $asset->session->dbSlave; ##Use the same slave to calculate the number of rows
+ my $sth = $slave->read($sql, $placeHolders);
+ my $allRows = $slave->quickScalar('SELECT FOUND_ROWS()');
+ my $sub = sub {
+ return $allRows if $_[0] eq 'rowCount';
if (defined wantarray) {
my $properties = $sth->hashRef;
if ($properties) {
diff --git a/lib/WebGUI/Paginator.pm b/lib/WebGUI/Paginator.pm
index 4cbf6fa..d2f57fb 100644
--- a/lib/WebGUI/Paginator.pm
+++ b/lib/WebGUI/Paginator.pm
@@ -63,7 +63,7 @@ Private method which retrieves a data set from a database and replaces whatever
This method should only ever be called by the public setDataByQuery method and is only called in the case that dynamicPageNumberKey is set.
-The public setDataByQuery method is not capable of efficiently handling requests that dyncmically set the page number by value
+The public setDataByQuery method is not capable of efficiently handling requests that dynamically set the page number by value
due to the fact that only one page of results is ever returned. In this method, all the results are returned making this possible.
=head3 query
@@ -414,28 +414,28 @@ Defaults to the page you're currently viewing. This is mostly here as an overrid
=cut
sub getPageData {
- my $self = shift;
- my $pageNumber = shift || $self->getPageNumber;
- my $allRows = $self->{_rowRef};
-
- my $pageCount = $self->getNumberOfPages;
- return [] if ($pageNumber > $pageCount);
+ my $self = shift;
+ my $pageNumber = shift || $self->getPageNumber;
+ my $allRows = $self->{_rowRef};
+
+ my $pageCount = $self->getNumberOfPages;
+ return [] if ($pageNumber > $pageCount);
if($self->{_setByQuery}) {
#Return the cached page
return $allRows if($pageNumber == $self->getPageNumber);
return [];
- }
+ }
- #Handle setByArrayRef or the old setDataByQuery method
- my @pageRows = ();
+ #Handle setByArrayRef or the old setDataByQuery method
+ my @pageRows = ();
my $rowsPerPage = $self->{_rpp};
my $pageStartRow = ($pageNumber*$rowsPerPage)-$rowsPerPage;
my $pageEndRow = $pageNumber*$rowsPerPage;
for (my $i=$pageStartRow; $i<$pageEndRow; $i++) {
- $pageRows[$i-$pageStartRow] = $allRows->[$i] if ($i <= $#{$self->{_rowRef}});
- }
- return \@pageRows;
+ $pageRows[$i-$pageStartRow] = $allRows->[$i] if ($i <= $#{$self->{_rowRef}});
+ }
+ return \@pageRows;
}
#-------------------------------------------------------------------
@@ -458,7 +458,7 @@ Returns links to all pages in this paginator.
=head3 limit
-An integer representing the maximum number of page links to return. Defaultly all page links will be returned.
+An integer representing the maximum number of page links to return. By default, all page links will be returned.
=cut
@@ -589,13 +589,13 @@ By default the page number will be determined by looking at $self->session->form
=cut
sub new {
- my $class = shift;
- my $session = shift;
- my $currentURL = shift;
- my $rowsPerPage = shift || 25;
- my $formVar = shift || "pn";
- my $pn = shift || $session->form->process($formVar) || 1;
- bless {_session=>$session, _url => $currentURL, _rpp => $rowsPerPage, _formVar => $formVar, _pn => $pn}, $class;
+ my $class = shift;
+ my $session = shift;
+ my $currentURL = shift;
+ my $rowsPerPage = shift || 25;
+ my $formVar = shift || "pn";
+ my $pn = shift || $session->form->process($formVar) || 1;
+ bless {_session=>$session, _url => $currentURL, _rpp => $rowsPerPage, _formVar => $formVar, _pn => $pn}, $class;
}
#-------------------------------------------------------------------
@@ -653,6 +653,39 @@ sub setDataByArrayRef {
#-------------------------------------------------------------------
+=head2 setDataByCallback ( callback )
+
+Provide the paginator with data by giving it a callback.
+
+=head3 callback
+
+A callback to invoke that returns an iterator. The callback method should
+accept two optional parameters, an offset to start, and the rows per page
+to return. The iterator should return the total number of rows in
+the query, without limits, when the first argument it is passed is 'rowCount'.
+
+=cut
+
+sub setDataByCallback {
+ my $self = shift;
+ my $callback = shift;
+
+ my $pageNumber = $self->getPageNumber;
+ my $rowsPerPage = $self->{_rpp};
+ my $start = ( ($pageNumber - 1) * $rowsPerPage );
+
+ my $obj = $callback->($start, $rowsPerPage);
+
+ $self->{_callbackObj} = $obj;
+ $self->{_setByQuery} = 0;
+ $self->{_setByArrayRef} = 0;
+ $self->{_setByCallback} = 1;
+ return '';
+}
+
+
+#-------------------------------------------------------------------
+
=head2 setDataByQuery ( query [, dbh, unconditional, placeholders, dynamicPageNumberKey, dynamicPageNumberValue ] )
Retrieves a data set from a database and replaces whatever data set was passed in through the constructor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment