[PATCH] Pager - <link> tag support
| From: | Joerg Bruckmann | Date: | Sun, 11 Jan 2004 19:42:23 +0000 |
| Subject: | [PATCH] Pager - <link> tag support | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-24987@lists.php.net to get a copy of this message | ||
Hello pear dev,
I added support for the <link> tag to the Pager package (for example <link
rel="next" href="page.php" title="next page">). This is done by
four new
private methods of Pager_Common:
Pager_Common::_getFirstLinkTag(),
Pager_Common::_getNextLinkTag(),
Pager_Common::_getPrevLinkTag(),
Pager_Common::_getLastLinkTag().
The title attributes can be set in the constructor of Pager. If not set they
default to 'first page', 'next page', 'previous page' and 'last
page'
respectively.
The attachement holds the diff-file. See below for an example usage.
Best regards,
Joerg Bruckmann
<?php
require_once 'Pager/Pager.php';
//create dummy array of data
$myData = array();
for($i=0; $i<200; $i++) {
$myData[] = $i;
}
$params = array(
'itemData' => $myData,
'perPage' => 10,
'delta' => 8, // for 'Jumping'-style a lower number is
better
'append' => true,
//'separator' => ' | ',
'clearIfVoid' => false,
'urlVar' => 'entrant',
'useSessions' => true,
'closeSession' => true,
// 'mode' => 'Sliding', //try switching modes
'mode' => 'Jumping',
'firstLinkTitle' => 'my first link title',
'nextLinkTitle' => 'my next link title',
'prevLinkTitle' => 'my previous link title',
'lastLinkTitle' => 'my last link title'
);
$pager = &new Pager($params);
$page_data = $pager->getPageData();
$links = $pager->getLinks();
?>
<html>
<head>
<title>new PEAR::Pager example</title>
<?php echo $links['linktags']; ?>
</head>
<body>
<table border="1" width="500" summary="example 1">
<tr>
<td colspan="3" align="center">all:
<?php echo $links['all']; ?>
</td>
</tr>
</table>
</body>
</html>
Index: Common.php =================================================================== RCS file: /repository/pear/Pager/Common.php,v retrieving revision 1.4 diff -u -r1.4 Common.php --- Common.php 16 Dec 2003 20:41:13 -0000 1.4 +++ Common.php 11 Jan 2004 19:26:10 -0000 @@ -288,6 +288,30 @@ * @access private */ var $_pearErrorMode = null; + + /** + * @var string $_firstLinkTitle + * @access private + */ + var $_firstLinkTitle = 'first page'; + + /** + * @var string $_nextLinkTitle + * @access private + */ + var $_nextLinkTitle = 'next page'; + + /** + * @var string $_prevLinkTitle + * @access private + */ + var $_prevLinkTitle = 'previous page'; + + /** + * @var string $_lastLinkTitle + * @access private + */ + var $_lastLinkTitle = 'last page'; // }}} // {{{ public vars @@ -297,6 +321,12 @@ * @access public */ var $links = ''; + + /** + * @var string Complete set of link tags + * @access public + */ + var $linkTags = ''; /** * @var array Array with a key => value pair representing @@ -625,6 +655,26 @@ } // }}} + // {{{ _getPrevLinkTag() + + /** + * Returns previous link tag + * + * @return string the link tag + * @access private + */ + function _getPrevLinkTag() + { + if ($this->_currentPage > 1) { + $prevLinkTag = sprintf('<link rel="previous" href="%s" title="%s" />', $this->_append ? $this->_url.$this->getPreviousPageID() : $this->_url.sprintf($this->_fileName, $this->getPreviousPageID()), $this->_prevLinkTitle); + } else { + $prevLinkTag = ''; + } + + return $prevLinkTag; + } + + // }}} // {{{ _getPageLinks() /** @@ -681,6 +731,28 @@ // }}} + // {{{ _getNextLinkTag() + + /** + * Returns next link tag + * + * @return string the link tag + * @access private + */ + function _getNextLinkTag() + { + + if ($this->_currentPage < $this->_totalPages) { + + $nextLinkTag = sprintf('<link rel="next" href="%s" title="%s" />', $this->_append ? $this->_url.$this->getNextPageID() : $this->_url.sprintf($this->_fileName, $this->getNextPageID()), $this->_nextLinkTitle); + } else { + $nextLinktag = ''; + } + return $nextLinkTag; + } + + + // }}} // {{{ getPerPageSelectBox() /** @@ -772,6 +844,38 @@ } // }}} + // {{{ _getFirstLinkTag() + + /** + * @return string + * @access private + */ + function _getFirstLinkTag() + { + if ($this->isFirstPage()) { + return ''; + } else { + return sprintf('<link rel="first" href="%s" title="%s" />', ($this->_append ? $this->_url."1" : $this->_url.sprintf($this->_fileName, 1)), $this->_firstLinkTitle); + } + } + + // }}} + // {{{ _getLastLinkTag() + + /** + * @return string the link tag + * @access private + */ + function _getLastLinkTag() + { + if ($this->isLastPage()) { + return ''; + } else { + return sprintf('<link rel="last" href="%s" title="%s" />',($this->_append ? $this->_url.$this->_totalPages : $this->_url.sprintf($this->_fileName, $this->_totalPages)), $this->_lastLinkTitle); + } + } + + // }}} // {{{ _setFirstLastText() /** @@ -852,7 +956,11 @@ 'useSessions', 'closeSession', 'sessionVar', - 'pearErrorMode' + 'pearErrorMode', + 'firstLinkTitle', + 'nextLinkTitle', + 'prevLinkTitle', + 'lastLinkTitle' ); foreach ($options as $key => $value) { @@ -928,4 +1036,4 @@ // }}} } -?> \ No newline at end of file +?> Index: Jumping.php =================================================================== RCS file: /repository/pear/Pager/Jumping.php,v retrieving revision 1.3 diff -u -r1.3 Jumping.php --- Jumping.php 10 Dec 2003 20:20:42 -0000 1.3 +++ Jumping.php 11 Jan 2004 19:26:10 -0000 @@ -66,14 +66,19 @@ * @access public */ function Pager_Jumping($options = array()) - { + { $this->_setOptions($options); $this->_generatePageData(); $this->_setFirstLastText(); $this->links .= $this->_getBackLink(); $this->links .= $this->_getPageLinks(); - $this->links .= $this->_getNextLink(); + $this->links .= $this->_getNextLink(); + + $this->linkTags .= $this->_getFirstLinkTag(); + $this->linkTags .= $this->_getNextLinkTag(); + $this->linkTags .= $this->_getPrevLinkTag(); + $this->linkTags .= $this->_getLastLinkTag(); } @@ -183,6 +188,7 @@ $first = $this->_printFirstPage(); $last = $this->_printLastPage(); $all = $this->links; + $linkTags = $this->linkTags; if ($pageID != null) { $this->_currentPage = $_sav; @@ -200,7 +206,8 @@ 'next' => $next, 'first' => $first, 'last' => $last, - 'all' => $all + 'all' => $all, + 'linktags' => $linkTags ); } @@ -254,4 +261,4 @@ // }}} // }}} } -?> \ No newline at end of file +?> Index: Sliding.php =================================================================== RCS file: /repository/pear/Pager/Sliding.php,v retrieving revision 1.3 diff -u -r1.3 Sliding.php --- Sliding.php 10 Dec 2003 20:20:42 -0000 1.3 +++ Sliding.php 11 Jan 2004 19:26:11 -0000 @@ -68,6 +68,11 @@ $this->links .= $this->_getBackLink(); $this->links .= $this->_getPageLinks(); $this->links .= $this->_getNextLink(); + + $this->linkTags .= $this->_getFirstLinkTag(); + $this->linkTags .= $this->_getNextLinkTag(); + $this->linkTags .= $this->_getPrevLinkTag(); + $this->linkTags .= $this->_getLastLinkTag(); if ($this->_totalPages > (2 * $this->_delta + 1)) { $this->links .= $this->_printLastPage(); @@ -158,6 +163,7 @@ $first = $this->_printFirstPage(); $last = $this->_printLastPage(); $all = $this->links; + $linkTags = $this->linkTags; if ($pageID != null) { $this->_currentPage = $_sav; @@ -175,7 +181,8 @@ 'next' => $next, 'first' => $first, 'last' => $last, - 'all' => $all + 'all' => $all, + 'linktags' => $linkTags ); }
Index: Common.php =================================================================== RCS file: /repository/pear/Pager/Common.php,v retrieving revision 1.4 diff -u -r1.4 Common.php --- Common.php 16 Dec 2003 20:41:13 -0000 1.4 +++ Common.php 11 Jan 2004 19:26:10 -0000 @@ -288,6 +288,30 @@ * @access private */ var $_pearErrorMode = null; + + /** + * @var string $_firstLinkTitle + * @access private + */ + var $_firstLinkTitle = 'first page'; + + /** + * @var string $_nextLinkTitle + * @access private + */ + var $_nextLinkTitle = 'next page'; + + /** + * @var string $_prevLinkTitle + * @access private + */ + var $_prevLinkTitle = 'previous page'; + + /** + * @var string $_lastLinkTitle + * @access private + */ + var $_lastLinkTitle = 'last page'; // }}} // {{{ public vars @@ -297,6 +321,12 @@ * @access public */ var $links = ''; + + /** + * @var string Complete set of link tags + * @access public + */ + var $linkTags = ''; /** * @var array Array with a key => value pair representing @@ -625,6 +655,26 @@ } // }}} + // {{{ _getPrevLinkTag() + + /** + * Returns previous link tag + * + * @return string the link tag + * @access private + */ + function _getPrevLinkTag() + { + if ($this->_currentPage > 1) { + $prevLinkTag = sprintf('<link rel="previous" href="%s" title="%s" />', $this->_append ? $this->_url.$this->getPreviousPageID() : $this->_url.sprintf($this->_fileName, $this->getPreviousPageID()), $this->_prevLinkTitle); + } else { + $prevLinkTag = ''; + } + + return $prevLinkTag; + } + + // }}} // {{{ _getPageLinks() /** @@ -681,6 +731,28 @@ // }}} + // {{{ _getNextLinkTag() + + /** + * Returns next link tag + * + * @return string the link tag + * @access private + */ + function _getNextLinkTag() + { + + if ($this->_currentPage < $this->_totalPages) { + + $nextLinkTag = sprintf('<link rel="next" href="%s" title="%s" />', $this->_append ? $this->_url.$this->getNextPageID() : $this->_url.sprintf($this->_fileName, $this->getNextPageID()), $this->_nextLinkTitle); + } else { + $nextLinktag = ''; + } + return $nextLinkTag; + } + + + // }}} // {{{ getPerPageSelectBox() /** @@ -772,6 +844,38 @@ } // }}} + // {{{ _getFirstLinkTag() + + /** + * @return string + * @access private + */ + function _getFirstLinkTag() + { + if ($this->isFirstPage()) { + return ''; + } else { + return sprintf('<link rel="first" href="%s" title="%s" />', ($this->_append ? $this->_url."1" : $this->_url.sprintf($this->_fileName, 1)), $this->_firstLinkTitle); + } + } + + // }}} + // {{{ _getLastLinkTag() + + /** + * @return string the link tag + * @access private + */ + function _getLastLinkTag() + { + if ($this->isLastPage()) { + return ''; + } else { + return sprintf('<link rel="last" href="%s" title="%s" />',($this->_append ? $this->_url.$this->_totalPages : $this->_url.sprintf($this->_fileName, $this->_totalPages)), $this->_lastLinkTitle); + } + } + + // }}} // {{{ _setFirstLastText() /** @@ -852,7 +956,11 @@ 'useSessions', 'closeSession', 'sessionVar', - 'pearErrorMode' + 'pearErrorMode', + 'firstLinkTitle', + 'nextLinkTitle', + 'prevLinkTitle', + 'lastLinkTitle' ); foreach ($options as $key => $value) { @@ -928,4 +1036,4 @@ // }}} } -?> \ No newline at end of file +?> Index: Jumping.php =================================================================== RCS file: /repository/pear/Pager/Jumping.php,v retrieving revision 1.3 diff -u -r1.3 Jumping.php --- Jumping.php 10 Dec 2003 20:20:42 -0000 1.3 +++ Jumping.php 11 Jan 2004 19:26:10 -0000 @@ -66,14 +66,19 @@ * @access public */ function Pager_Jumping($options = array()) - { + { $this->_setOptions($options); $this->_generatePageData(); $this->_setFirstLastText(); $this->links .= $this->_getBackLink(); $this->links .= $this->_getPageLinks(); - $this->links .= $this->_getNextLink(); + $this->links .= $this->_getNextLink(); + + $this->linkTags .= $this->_getFirstLinkTag(); + $this->linkTags .= $this->_getNextLinkTag(); + $this->linkTags .= $this->_getPrevLinkTag(); + $this->linkTags .= $this->_getLastLinkTag(); } @@ -183,6 +188,7 @@ $first = $this->_printFirstPage(); $last = $this->_printLastPage(); $all = $this->links; + $linkTags = $this->linkTags; if ($pageID != null) { $this->_currentPage = $_sav; @@ -200,7 +206,8 @@ 'next' => $next, 'first' => $first, 'last' => $last, - 'all' => $all + 'all' => $all, + 'linktags' => $linkTags ); } @@ -254,4 +261,4 @@ // }}} // }}} } -?> \ No newline at end of file +?> Index: Sliding.php =================================================================== RCS file: /repository/pear/Pager/Sliding.php,v retrieving revision 1.3 diff -u -r1.3 Sliding.php --- Sliding.php 10 Dec 2003 20:20:42 -0000 1.3 +++ Sliding.php 11 Jan 2004 19:26:11 -0000 @@ -68,6 +68,11 @@ $this->links .= $this->_getBackLink(); $this->links .= $this->_getPageLinks(); $this->links .= $this->_getNextLink(); + + $this->linkTags .= $this->_getFirstLinkTag(); + $this->linkTags .= $this->_getNextLinkTag(); + $this->linkTags .= $this->_getPrevLinkTag(); + $this->linkTags .= $this->_getLastLinkTag(); if ($this->_totalPages > (2 * $this->_delta + 1)) { $this->links .= $this->_printLastPage(); @@ -158,6 +163,7 @@ $first = $this->_printFirstPage(); $last = $this->_printLastPage(); $all = $this->links; + $linkTags = $this->linkTags; if ($pageID != null) { $this->_currentPage = $_sav; @@ -175,7 +181,8 @@ 'next' => $next, 'first' => $first, 'last' => $last, - 'all' => $all + 'all' => $all, + 'linktags' => $linkTags ); }