2007-11-26 19:27:24 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Doctrine::autoload('Doctrine_Pager_Range');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Pager_Layout
|
|
|
|
*
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Pager
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
2008-01-16 23:51:36 +03:00
|
|
|
* @link www.phpdoctrine.org
|
|
|
|
* @since 0.9
|
2007-11-26 19:27:24 +03:00
|
|
|
*/
|
|
|
|
class Doctrine_Pager_Layout
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Pager $_pager Doctrine_Pager object related to the pager layout
|
|
|
|
*/
|
|
|
|
private $_pager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Pager_Range $_pagerRange Doctrine_Pager_Range object related to the pager layout
|
|
|
|
*/
|
|
|
|
private $_pagerRange;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $_template Template to be applied for inactive pages
|
|
|
|
* (and also active is selected template is not defined)
|
|
|
|
*/
|
|
|
|
private $_template;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $_selectedTemplate Template to be applied for active page
|
|
|
|
*/
|
|
|
|
private $_selectedTemplate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $_separatorTemplate Separator template, applied between each page
|
|
|
|
*/
|
|
|
|
private $_separatorTemplate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $_urlMask URL to be assigned for each page. Masks are used as: {%var_name}
|
|
|
|
*/
|
|
|
|
private $_urlMask;
|
2007-12-13 06:07:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $_maskReplacements Stores references of masks and their correspondent
|
|
|
|
* (replaces defined masks with new masks or values)
|
|
|
|
*/
|
|
|
|
private $_maskReplacements = array();
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* @param Doctrine_Pager $pager Doctrine_Pager object related to the pager layout
|
|
|
|
* @param Doctrine_Pager_Range $pagerRange Doctrine_Pager_Range object related to the pager layout
|
|
|
|
* @param string $urlMask URL to be assigned for each page
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($pager, $pagerRange, $urlMask)
|
|
|
|
{
|
2007-12-13 06:07:24 +03:00
|
|
|
$this->_setPager($pager);
|
|
|
|
$this->_setPagerRange($pagerRange);
|
|
|
|
$this->_setUrlMask($urlMask);
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
$this->setTemplate('');
|
|
|
|
$this->setSelectedTemplate('');
|
|
|
|
$this->setSeparatorTemplate('');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getPager
|
|
|
|
*
|
|
|
|
* Returns the Doctrine_Pager object related to the pager layout
|
|
|
|
*
|
|
|
|
* @return Doctrine_Pager Doctrine_Pager object related to the pager range
|
|
|
|
*/
|
|
|
|
public function getPager()
|
|
|
|
{
|
|
|
|
return $this->_pager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-12-13 06:07:24 +03:00
|
|
|
* _setPager
|
2007-11-26 19:27:24 +03:00
|
|
|
*
|
|
|
|
* Defines the Doctrine_Pager object related to the pager layout
|
|
|
|
*
|
|
|
|
* @param $pager Doctrine_Pager object related to the pager range
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-12-13 06:07:24 +03:00
|
|
|
protected function _setPager($pager)
|
2007-11-26 19:27:24 +03:00
|
|
|
{
|
|
|
|
$this->_pager = $pager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-16 23:51:36 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-26 19:27:24 +03:00
|
|
|
/**
|
|
|
|
* getPagerRange
|
|
|
|
*
|
|
|
|
* Returns the Doctrine_Pager_Range subclass object related to the pager layout
|
|
|
|
*
|
|
|
|
* @return Doctrine_Pager_Range Doctrine_Pager_Range subclass object related to the pager range
|
|
|
|
*/
|
|
|
|
public function getPagerRange()
|
|
|
|
{
|
|
|
|
return $this->_pagerRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-12-13 06:07:24 +03:00
|
|
|
* _setPagerRange
|
2007-11-26 19:27:24 +03:00
|
|
|
*
|
|
|
|
* Defines the Doctrine_Pager_Range subclass object related to the pager layout
|
|
|
|
*
|
|
|
|
* @param $pagerRange Doctrine_Pager_Range subclass object related to the pager range
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-12-13 06:07:24 +03:00
|
|
|
protected function _setPagerRange($pagerRange)
|
2007-11-26 19:27:24 +03:00
|
|
|
{
|
|
|
|
$this->_pagerRange = $pagerRange;
|
|
|
|
$this->getPagerRange()->setPager($this->getPager());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getUrlMask
|
|
|
|
*
|
|
|
|
* Returns the URL to be assigned for each page
|
|
|
|
*
|
|
|
|
* @return string URL to be assigned for each page
|
|
|
|
*/
|
|
|
|
public function getUrlMask()
|
|
|
|
{
|
|
|
|
return $this->_urlMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-12-13 06:07:24 +03:00
|
|
|
* _setUrlMask
|
2007-11-26 19:27:24 +03:00
|
|
|
*
|
|
|
|
* Defines the URL to be assigned for each page
|
|
|
|
*
|
|
|
|
* @param $urlMask URL to be assigned for each page
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-12-13 06:07:24 +03:00
|
|
|
protected function _setUrlMask($urlMask)
|
2007-11-26 19:27:24 +03:00
|
|
|
{
|
|
|
|
$this->_urlMask = $urlMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTemplate
|
|
|
|
*
|
|
|
|
* Returns the Template to be applied for inactive pages
|
|
|
|
*
|
|
|
|
* @return string Template to be applied for inactive pages
|
|
|
|
*/
|
|
|
|
public function getTemplate()
|
|
|
|
{
|
|
|
|
return $this->_template;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setTemplate
|
|
|
|
*
|
|
|
|
* Defines the Template to be applied for inactive pages
|
|
|
|
* (also active page if selected template not defined)
|
|
|
|
*
|
|
|
|
* @param $template Template to be applied for inactive pages
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->_template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getSelectedTemplate
|
|
|
|
*
|
|
|
|
* Returns the Template to be applied for active page
|
|
|
|
*
|
|
|
|
* @return string Template to be applied for active page
|
|
|
|
*/
|
|
|
|
public function getSelectedTemplate()
|
|
|
|
{
|
|
|
|
return $this->_selectedTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setSelectedTemplate
|
|
|
|
*
|
|
|
|
* Defines the Template to be applied for active page
|
|
|
|
*
|
|
|
|
* @param $selectedTemplate Template to be applied for active page
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setSelectedTemplate($selectedTemplate)
|
|
|
|
{
|
|
|
|
$this->_selectedTemplate = $selectedTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getSeparatorTemplate
|
|
|
|
*
|
|
|
|
* Returns the Separator template, applied between each page
|
|
|
|
*
|
|
|
|
* @return string Separator template, applied between each page
|
|
|
|
*/
|
|
|
|
public function getSeparatorTemplate()
|
|
|
|
{
|
|
|
|
return $this->_separatorTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setSeparatorTemplate
|
|
|
|
*
|
|
|
|
* Defines the Separator template, applied between each page
|
|
|
|
*
|
|
|
|
* @param $separatorTemplate Separator template, applied between each page
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setSeparatorTemplate($separatorTemplate)
|
|
|
|
{
|
|
|
|
$this->_separatorTemplate = $separatorTemplate;
|
|
|
|
}
|
2008-01-16 23:51:36 +03:00
|
|
|
|
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
/**
|
|
|
|
* addMaskReplacement
|
|
|
|
*
|
|
|
|
* Defines a mask replacement. When parsing template, it converts replacement
|
|
|
|
* masks into new ones (or values), allowing to change masks behavior on the fly
|
|
|
|
*
|
|
|
|
* @param $oldMask Mask to be replaced
|
|
|
|
* @param $newMask Mask or Value that will be defined after replacement
|
|
|
|
* @param $asValue Optional value (default false) that if defined as true,
|
|
|
|
* changes the bahavior of replacement mask to replacement
|
|
|
|
* value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addMaskReplacement($oldMask, $newMask, $asValue = false)
|
|
|
|
{
|
2007-12-22 22:04:18 +03:00
|
|
|
if (($oldMask = trim($oldMask)) != 'page_number') {
|
|
|
|
$this->_maskReplacements[$oldMask] = array(
|
|
|
|
'newMask' => $newMask,
|
|
|
|
'asValue' => ($asValue === false) ? false : true
|
|
|
|
);
|
|
|
|
}
|
2007-12-13 06:07:24 +03:00
|
|
|
}
|
2008-01-16 23:51:36 +03:00
|
|
|
|
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
/**
|
|
|
|
* removeMaskReplacement
|
|
|
|
*
|
|
|
|
* Remove a mask replacement
|
|
|
|
*
|
|
|
|
* @param $oldMask Replacement Mask to be removed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function removeMaskReplacement($oldMask)
|
|
|
|
{
|
|
|
|
if (isset($this->_maskReplacements[$oldMask])) {
|
|
|
|
$this->_maskReplacements[$oldMask] = null;
|
|
|
|
unset($this->_maskReplacements[$oldMask]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cleanMaskReplacements
|
|
|
|
*
|
|
|
|
* Remove all mask replacements
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function cleanMaskReplacements()
|
|
|
|
{
|
|
|
|
$this->_maskReplacements = null;
|
|
|
|
$this->_maskReplacements = array();
|
|
|
|
}
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* display
|
|
|
|
*
|
|
|
|
* Displays the pager on screen based on templates and options defined
|
|
|
|
*
|
|
|
|
* @param $options Optional parameters to be applied in template and url mask
|
|
|
|
* @param $return Optional parameter if you want to capture the output of this method call
|
|
|
|
* (Default value is false), instead of printing it
|
|
|
|
* @return mixed If you would like to capture the output of Doctrine_Pager_Layout::display(),
|
|
|
|
* use the return parameter. If this parameter is set to TRUE, this method
|
|
|
|
* will return its output, instead of printing it (which it does by default)
|
|
|
|
*/
|
|
|
|
public function display($options = array(), $return = false)
|
|
|
|
{
|
|
|
|
$range = $this->getPagerRange()->rangeAroundPage();
|
|
|
|
$str = '';
|
|
|
|
|
|
|
|
// For each page in range
|
|
|
|
for ($i = 0, $l = count($range); $i < $l; $i++) {
|
|
|
|
// Define some optional mask values
|
2007-12-13 06:07:24 +03:00
|
|
|
$options['page_number'] = $range[$i];
|
2007-11-26 19:27:24 +03:00
|
|
|
|
2008-01-16 23:51:36 +03:00
|
|
|
$str .= $this->processPage($options);
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
// Apply separator between pages
|
|
|
|
if ($i < $l - 1) {
|
|
|
|
$str .= $this->getSeparatorTemplate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible wish to return value instead of print it on screen
|
|
|
|
if ($return) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $str;
|
|
|
|
}
|
|
|
|
|
2008-01-16 23:51:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-18 15:00:12 +03:00
|
|
|
/**
|
2008-01-16 23:51:36 +03:00
|
|
|
* Simply calls display, and returns the output.
|
2007-12-18 15:00:12 +03:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->display(array(), true);
|
|
|
|
}
|
|
|
|
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
/**
|
2007-12-13 06:07:24 +03:00
|
|
|
* _parseTemplate
|
2007-11-26 19:27:24 +03:00
|
|
|
*
|
|
|
|
* Process the template of a given page and return the processed template
|
|
|
|
*
|
2008-01-16 23:51:36 +03:00
|
|
|
* @param array Optional parameters to be applied in template and url mask
|
2007-11-26 19:27:24 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2007-12-13 06:07:24 +03:00
|
|
|
protected function _parseTemplate($options = array())
|
2007-11-26 19:27:24 +03:00
|
|
|
{
|
|
|
|
$str = '';
|
|
|
|
|
2008-01-16 23:51:36 +03:00
|
|
|
// If given page is the current active one
|
|
|
|
if ($options['page_number'] == $this->getPager()->getPage()) {
|
2007-12-13 06:07:24 +03:00
|
|
|
$str = $this->_parseMaskReplacements($this->getSelectedTemplate());
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Possible attempt where Selected == Template
|
|
|
|
if ($str == '') {
|
2007-12-13 06:07:24 +03:00
|
|
|
$str = $this->_parseMaskReplacements($this->getTemplate());
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
|
|
|
|
2008-01-16 23:51:36 +03:00
|
|
|
// Defining "url" options index to allow {%url} mask
|
|
|
|
$options['url'] = $this->_parseUrl($options);
|
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
$replacements = array();
|
2008-01-16 23:51:36 +03:00
|
|
|
|
2007-11-26 19:27:24 +03:00
|
|
|
foreach ($options as $k => $v) {
|
2007-12-13 06:07:24 +03:00
|
|
|
$replacements['{%'.$k.'}'] = $v;
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
return strtr($str, $replacements);
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-12-13 06:07:24 +03:00
|
|
|
* _parseUrl
|
2007-11-26 19:27:24 +03:00
|
|
|
*
|
|
|
|
* Process the url mask of a given page and return the processed url
|
|
|
|
*
|
|
|
|
* @param $options Optional parameters to be applied in template and url mask
|
2007-12-13 06:07:24 +03:00
|
|
|
* @return string
|
2007-11-26 19:27:24 +03:00
|
|
|
*/
|
2007-12-13 06:07:24 +03:00
|
|
|
protected function _parseUrl($options = array())
|
2007-11-26 19:27:24 +03:00
|
|
|
{
|
2007-12-13 06:07:24 +03:00
|
|
|
$str = $this->_parseMaskReplacements($this->getUrlMask());
|
2007-11-26 19:27:24 +03:00
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
$replacements = array();
|
2007-11-26 19:27:24 +03:00
|
|
|
|
|
|
|
foreach ($options as $k => $v) {
|
2007-12-13 06:07:24 +03:00
|
|
|
$replacements['{%'.$k.'}'] = $v;
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
return strtr($str, $replacements);
|
2007-11-26 19:27:24 +03:00
|
|
|
}
|
2007-12-13 06:07:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _parseMaskReplacements
|
|
|
|
*
|
|
|
|
* Process the mask replacements, changing from to-be replaced mask with new masks/values
|
|
|
|
*
|
|
|
|
* @param $str String to have masks replaced
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _parseMaskReplacements($str)
|
|
|
|
{
|
|
|
|
$replacements = array();
|
2007-11-26 19:27:24 +03:00
|
|
|
|
2007-12-13 06:07:24 +03:00
|
|
|
foreach ($this->_maskReplacements as $k => $v) {
|
|
|
|
$replacements['{%'.$k.'}'] = ($v['asValue'] === true) ? $v['newMask'] : '{%'.$v['newMask'].'}';
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtr($str, $replacements);
|
|
|
|
}
|
|
|
|
}
|