1
0
mirror of synced 2025-01-30 20:11:49 +03:00

Fixed count bug in Doctrine_Pager that was wrong counting the total of results found. Added 3 new methods: Doctrine_Pager::getExecuted (checks if the Pager was already executed), Doctrine_Pager_Layout::execute (handy access to execute Pager query without having to access Doctrine_Pager instance) and Doctrine_Pager_Layout::processPage (processes the template of a given page and returns the parsed string)

This commit is contained in:
guilhermeblanco 2008-01-16 20:51:36 +00:00
parent e4381a6a94
commit b68eba074b
6 changed files with 217 additions and 96 deletions

View File

@ -28,8 +28,8 @@
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
class Doctrine_Pager class Doctrine_Pager
{ {
@ -58,6 +58,11 @@ class Doctrine_Pager
*/ */
protected $_lastPage; protected $_lastPage;
/**
* @var boolean $_executed Pager was initialized (called "execute" at least once)
*/
protected $_executed;
/** /**
@ -71,12 +76,12 @@ class Doctrine_Pager
*/ */
public function __construct($query, $page, $maxPerPage = 0) public function __construct($query, $page, $maxPerPage = 0)
{ {
$this->_setQuery($query); $this->_setExecuted(false);
$this->_setMaxPerPage($maxPerPage); $this->_setQuery($query);
$this->_setPage($page); $this->_setPage($page);
$this->_initialize(); $this->setMaxPerPage($maxPerPage);
} }
@ -85,15 +90,18 @@ class Doctrine_Pager
* *
* Initialize Pager object calculating number of results * Initialize Pager object calculating number of results
* *
* @param $params Optional parameters to Doctrine_Query::execute
* @return void * @return void
*/ */
protected function _initialize() protected function _initialize($params = array())
{ {
// retrieve the number of items found // retrieve the number of items found
$count = $this->getQuery()->count(); $count = $this->getQuery()->count($params);
$this->_setNumResults($count); $this->_setNumResults($count);
$this->_adjustOffset(); $this->_adjustOffset();
$this->_setExecuted(true);
} }
@ -106,6 +114,7 @@ class Doctrine_Pager
*/ */
protected function _adjustOffset() protected function _adjustOffset()
{ {
if (!$this->getExecuted()) {
// Define new total of pages // Define new total of pages
$this->_setLastPage( $this->_setLastPage(
max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) max(1, ceil($this->getNumResults() / $this->getMaxPerPage()))
@ -117,6 +126,34 @@ class Doctrine_Pager
$p->offset($offset); $p->offset($offset);
$p->limit($this->getMaxPerPage()); $p->limit($this->getMaxPerPage());
} }
}
/**
* getExecuted
*
* Returns the check if Pager was already executed at least once
*
* @return boolen Pager was executed
*/
public function getExecuted()
{
return $this->_executed;
}
/**
* _setExecuted
*
* Defines if Pager was already executed
*
* @param $executed Pager was executed
* @return void
*/
protected function _setExecuted($executed)
{
$this->_executed = $executed;
}
/** /**
@ -128,9 +165,15 @@ class Doctrine_Pager
*/ */
public function getNumResults() public function getNumResults()
{ {
if ($this->getExecuted()) {
return $this->_numResults; return $this->_numResults;
} }
throw new Doctrine_Pager_Exception(
'Cannot retrieve the number of results of a not yet executed Pager query'
);
}
/** /**
* _setNumResults * _setNumResults
@ -168,9 +211,15 @@ class Doctrine_Pager
*/ */
public function getLastPage() public function getLastPage()
{ {
if ($this->getExecuted()) {
return $this->_lastPage; return $this->_lastPage;
} }
throw new Doctrine_Pager_Exception(
'Cannot retrieve the last page number of a not yet executed Pager query'
);
}
/** /**
* _setLastPage * _setLastPage
@ -212,7 +261,13 @@ class Doctrine_Pager
*/ */
public function getNextPage() public function getNextPage()
{ {
return min($this->getPage() + 1, $this->getLastPage()); if ($this->getExecuted()) {
return $this->_lastPage;
}
throw new Doctrine_Pager_Exception(
'Cannot retrieve the last page number of a not yet executed Pager query'
);return min($this->getPage() + 1, $this->getLastPage());
} }
@ -225,9 +280,15 @@ class Doctrine_Pager
*/ */
public function getPreviousPage() public function getPreviousPage()
{ {
if ($this->getExecuted()) {
return max($this->getPage() - 1, $this->getFirstPage()); return max($this->getPage() - 1, $this->getFirstPage());
} }
throw new Doctrine_Pager_Exception(
'Cannot retrieve the previous page number of a not yet executed Pager query'
);
}
/** /**
* haveToPaginate * haveToPaginate
@ -238,9 +299,15 @@ class Doctrine_Pager
*/ */
public function haveToPaginate() public function haveToPaginate()
{ {
if ($this->getExecuted()) {
return $this->getNumResults() > $this->getMaxPerPage(); return $this->getNumResults() > $this->getMaxPerPage();
} }
throw new Doctrine_Pager_Exception(
'Cannot know if it is necessary to paginate a not yet executed Pager query'
);
}
/** /**
* setPage * setPage
@ -253,7 +320,7 @@ class Doctrine_Pager
public function setPage($page) public function setPage($page)
{ {
$this->_setPage($page); $this->_setPage($page);
$this->_adjustOffset(); $this->_setExecuted(false);
} }
@ -294,21 +361,6 @@ class Doctrine_Pager
* @return void * @return void
*/ */
public function setMaxPerPage($max) public function setMaxPerPage($max)
{
$this->_setMaxPerPage($max);
$this->_adjustOffset();
}
/**
* _setMaxPerPage
*
* Defines the maximum number of itens per page
*
* @param $max maximum number of itens per page
* @return void
*/
private function _setMaxPerPage($max)
{ {
if ($max > 0) { if ($max > 0) {
$this->_maxPerPage = $max; $this->_maxPerPage = $max;
@ -317,6 +369,8 @@ class Doctrine_Pager
} else { } else {
$this->_maxPerPage = abs($max); $this->_maxPerPage = abs($max);
} }
$this->_setExecuted(false);
} }
@ -354,15 +408,18 @@ class Doctrine_Pager
/** /**
* execute * execute
* executes the query and populates the data set *
* Executes the query, populates the collection and then return it
* *
* @param $params Optional parameters to Doctrine_Query::execute * @param $params Optional parameters to Doctrine_Query::execute
* @param $hydrationMode Hyddration Mode of Doctrine_Query::execute * @param $hydrationMode Hydration Mode of Doctrine_Query::execute
* returned ResultSet. Doctrine::Default is FETCH_RECORD * returned ResultSet. Doctrine::Default is FETCH_RECORD
* @return Doctrine_Collection the root collection * @return Doctrine_Collection The root collection
*/ */
public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD)
{ {
$this->_initialize($params);
return $this->getQuery()->execute($params, $hydrationMode); return $this->getQuery()->execute($params, $hydrationMode);
} }
} }

View File

@ -30,8 +30,8 @@ Doctrine::autoload('Doctrine_Exception');
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
class Doctrine_Pager_Exception extends Doctrine_Exception class Doctrine_Pager_Exception extends Doctrine_Exception
{ } { }

View File

@ -30,8 +30,8 @@ Doctrine::autoload('Doctrine_Pager_Range');
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
class Doctrine_Pager_Layout class Doctrine_Pager_Layout
{ {
@ -120,6 +120,22 @@ class Doctrine_Pager_Layout
} }
/**
* execute
*
* Handy method to execute the query without need to retrieve the Pager instance
*
* @param $params Optional parameters to Doctrine_Query::execute
* @param $hydrationMode Hydration Mode of Doctrine_Query::execute
* returned ResultSet. Doctrine::Default is FETCH_RECORD
* @return Doctrine_Collection The root collection
*/
public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD)
{
return $this->getPager()->execute($params, $hydrationMode);
}
/** /**
* getPagerRange * getPagerRange
* *
@ -333,10 +349,8 @@ class Doctrine_Pager_Layout
for ($i = 0, $l = count($range); $i < $l; $i++) { for ($i = 0, $l = count($range); $i < $l; $i++) {
// Define some optional mask values // Define some optional mask values
$options['page_number'] = $range[$i]; $options['page_number'] = $range[$i];
$options['page'] = $range[$i]; // Handy assignment for URLs
$options['url'] = $this->_parseUrl($options);
$str .= $this->_parseTemplate($options); $str .= $this->processPage($options);
// Apply separator between pages // Apply separator between pages
if ($i < $l - 1) { if ($i < $l - 1) {
@ -352,8 +366,39 @@ class Doctrine_Pager_Layout
echo $str; echo $str;
} }
/** /**
* simply calls display, and returns the output. * processPage
*
* Parses the template and returns the string of a processed page
*
* @param array Optional parameters to be applied in template and url mask
* @return string Processed template for the given page
*/
public function processPage($options = array())
{
// Check if at least basic options are defined
if (!isset($options['page_number'])) {
throw new Doctrine_Pager_Exception(
'Cannot process template of the given page. ' .
'Missing at least one of needed parameters: \'page\' or \'page_number\''
);
// Should never reach here
return '';
}
// Assign "page" options index if not defined yet
if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) {
$options['page'] = $options['page_number'];
}
return $this->_parseTemplate($options);
}
/**
* Simply calls display, and returns the output.
*/ */
public function __toString() public function __toString()
{ {
@ -366,14 +411,15 @@ class Doctrine_Pager_Layout
* *
* Process the template of a given page and return the processed template * Process the template of a given page and return the processed template
* *
* @param $options Optional parameters to be applied in template and url mask * @param array Optional parameters to be applied in template and url mask
* @return string * @return string
*/ */
protected function _parseTemplate($options = array()) protected function _parseTemplate($options = array())
{ {
$str = ''; $str = '';
if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { // If given page is the current active one
if ($options['page_number'] == $this->getPager()->getPage()) {
$str = $this->_parseMaskReplacements($this->getSelectedTemplate()); $str = $this->_parseMaskReplacements($this->getSelectedTemplate());
} }
@ -382,6 +428,9 @@ class Doctrine_Pager_Layout
$str = $this->_parseMaskReplacements($this->getTemplate()); $str = $this->_parseMaskReplacements($this->getTemplate());
} }
// Defining "url" options index to allow {%url} mask
$options['url'] = $this->_parseUrl($options);
$replacements = array(); $replacements = array();
foreach ($options as $k => $v) { foreach ($options as $k => $v) {

View File

@ -28,8 +28,8 @@
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
abstract class Doctrine_Pager_Range abstract class Doctrine_Pager_Range
{ {

View File

@ -30,8 +30,8 @@ Doctrine::autoload('Doctrine_Pager_Range');
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range
{ {
@ -95,6 +95,8 @@ class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range
public function rangeAroundPage() public function rangeAroundPage()
{ {
$pager = $this->getPager(); $pager = $this->getPager();
if ($pager->getExecuted()) {
$page = $pager->getPage(); $page = $pager->getPage();
// Define initial assignments for StartPage and EndPage // Define initial assignments for StartPage and EndPage
@ -110,4 +112,9 @@ class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range
return range($startPage, $endPage); return range($startPage, $endPage);
} }
throw new Doctrine_Pager_Exception(
'Cannot retrieve the range around the page of a not yet executed Pager query'
);
}
} }

View File

@ -30,8 +30,8 @@ Doctrine::autoload('Doctrine_Pager_Range');
* @subpackage Pager * @subpackage Pager
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @link www.phpdoctrine.com * @link www.phpdoctrine.org
* @since 1.0 * @since 0.9
*/ */
class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range
{ {
@ -100,10 +100,13 @@ class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range
public function rangeAroundPage() public function rangeAroundPage()
{ {
$pager = $this->getPager(); $pager = $this->getPager();
if ($pager->getExecuted()) {
$page = $pager->getPage(); $page = $pager->getPage();
$pages = $pager->getLastPage(); $pages = $pager->getLastPage();
$chunk = $this->getChunkLength(); $chunk = $this->getChunkLength();
if ($chunk > $pages) { if ($chunk > $pages) {
$chunk = $pages; $chunk = $pages;
} }
@ -116,6 +119,7 @@ class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range
$chunkStart = 1; $chunkStart = 1;
$chunkEnd = $chunkEnd + $adjust; $chunkEnd = $chunkEnd + $adjust;
} }
if ($chunkEnd > $pages) { if ($chunkEnd > $pages) {
$adjust = $chunkEnd - $pages; $adjust = $chunkEnd - $pages;
$chunkStart = $chunkStart - $adjust; $chunkStart = $chunkStart - $adjust;
@ -123,6 +127,10 @@ class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range
} }
return range($chunkStart, $chunkEnd); return range($chunkStart, $chunkEnd);
}
throw new Doctrine_Pager_Exception(
'Cannot retrieve the range around the page of a not yet executed Pager query'
);
} }
} }