2002-11-09 13:59:36 +00:00
|
|
|
|
<?php
|
2002-11-26 19:24:20 +00:00
|
|
|
|
// $Id$
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2003-11-24 22:46:03 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @file
|
|
|
|
|
* Functions to aid in presenting database results as a set of pages.
|
2003-12-08 06:32:19 +00:00
|
|
|
|
*/
|
2003-11-24 22:46:03 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Perform a paged database query.
|
|
|
|
|
*
|
2003-12-08 06:32:19 +00:00
|
|
|
|
* Use this function when doing select queries you wish to be able to page. The
|
|
|
|
|
* pager uses LIMIT-based queries to fetch only the records required to render a
|
|
|
|
|
* certain page. However, it has to learn the total number of records returned
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* by the query to compute the number of pages (the number of records / records
|
|
|
|
|
* per page). This is done by inserting "COUNT(*)" in the original query. For
|
|
|
|
|
* example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY
|
|
|
|
|
* sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM
|
|
|
|
|
* node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the
|
|
|
|
|
* query is accomplished using a regular expression.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*
|
2003-12-08 06:32:19 +00:00
|
|
|
|
* Unfortunately, the rewrite rule does not always work as intended for queries
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for
|
2003-12-08 06:32:19 +00:00
|
|
|
|
* other complex queries. In those cases, you can optionally pass a query that
|
|
|
|
|
* will be used to count the records.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node
|
|
|
|
|
* GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT
|
2003-12-08 06:32:19 +00:00
|
|
|
|
* COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT
|
|
|
|
|
* COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $query
|
|
|
|
|
* The SQL query that needs paging.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
|
|
|
|
* @param $count_query
|
|
|
|
|
* An SQL query used to count matching records.
|
2004-07-25 14:25:42 +00:00
|
|
|
|
* @param ...
|
|
|
|
|
* A variable number of arguments which are substituted into the query (and
|
- Patch #13581 by Steven: Db_query() allows a variable amount of parameters so you can pass the query arguments in. There is however an alternative syntax: instead of passing the query arguments as function arguments, you can also pass a single array with the query arguments in it. For example the following two statements are equivalent:
db_query($query, $a, $b, $c);
db_query($query, array($a, $b, $c));
This usage is particularly interesting when the query is constructed dynamically, and the amount of arguments to pass varies. In that case we use the second method to avoid using call_user_func_array(). This behaviour is not documented explicitly, but it is used in several places.
However, db_query_range() and pager_query() do not support this syntax properly, which means there are several pieces of code which still revert to the ugly call_user_func_array() call.
This patch updates db_query_range() and pager_query() so they support the array-passing method. I also added documentation about this method to each of the db functions.
I also cleaned up the code for db_query (it was weird and hard to understand) and moved db_query() and db_queryd() from database.xxxxx.inc to database.inc: it was the same between both mysql and pgsql, as it doesn't do anything database specific. It just prefixes the tables and inserts the arguments. The actual db query is performed in _db_query(), which is still in database.xxxxx.inc.
Finally, I updated several places with the new syntax, and the code is a lot cleaner. For example:
- array_unshift($params, "SELECT u.* FROM {users} u WHERE $query u.status < 3");
- $params[] = 0;
- $params[] = 1;
- $result = call_user_func_array('db_query_range', $params);
+ $result = db_query_range("SELECT u.* FROM {users} u WHERE $query u.status < 3", $params, 0, 1);
and
- return call_user_func_array('db_query_range', array_merge(array($query), $args, array((int)$pager_from_array[$element], (int)$limit)));
+ return db_query_range($query, $args, (int)$pager_from_array[$element], (int)$limit);
I've tested it on mysql. I didn't alter the actual db behaviour, so pgsql should be okay too.
This patch is important because many people avoid the call_user_func_array() method and put data directly into the db query. This is very, very bad because the database prefix will be applied to it, and strip out braces. It's also generally bad form as you have to call check_query() yourself. With the new, documented syntax, there is no more excuse to put data directly in the query.
2004-11-29 13:13:29 +00:00
|
|
|
|
* the count query) using printf() syntax. Instead of a variable number of
|
|
|
|
|
* query arguments, you may also pass a single array containing the query
|
|
|
|
|
* arguments.
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @return
|
|
|
|
|
* A database query result resource, or FALSE if the query was not executed
|
|
|
|
|
* correctly.
|
2004-09-09 05:51:08 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup database
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*/
|
2004-07-25 14:25:42 +00:00
|
|
|
|
function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array, $pager_total, $pager_total_items;
|
2005-10-22 15:14:46 +00:00
|
|
|
|
$page = isset($_GET['page']) ? $_GET['page'] : '';
|
2003-11-24 22:46:03 +00:00
|
|
|
|
|
2004-07-25 14:25:42 +00:00
|
|
|
|
// Substitute in query arguments.
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$args = array_slice($args, 4);
|
2004-12-02 07:06:33 +00:00
|
|
|
|
// Alternative syntax for '...'
|
2005-10-22 15:14:46 +00:00
|
|
|
|
if (isset($args[0]) && is_array($args[0])) {
|
2004-12-02 07:06:33 +00:00
|
|
|
|
$args = $args[0];
|
|
|
|
|
}
|
2004-07-25 14:25:42 +00:00
|
|
|
|
|
2004-12-06 11:57:04 +00:00
|
|
|
|
// Construct a count query if none was given.
|
2004-07-25 14:25:42 +00:00
|
|
|
|
if (!isset($count_query)) {
|
2006-08-24 08:49:04 +00:00
|
|
|
|
$count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
|
2003-11-24 22:46:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// Convert comma-separated $page to an array, used by other functions.
|
|
|
|
|
$pager_page_array = explode(',', $page);
|
2003-11-24 22:46:03 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// We calculate the total of pages as ceil(items / limit).
|
2006-08-16 19:52:37 +00:00
|
|
|
|
$pager_total_items[$element] = db_result(db_query($count_query, $args));
|
|
|
|
|
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
|
|
|
|
|
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
|
|
|
|
|
return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
|
2003-11-24 22:46:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-13 08:25:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* Compose a query string to append to pager requests.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* A query string that consists of all components of the current page request
|
|
|
|
|
* except for those pertaining to paging.
|
|
|
|
|
*/
|
|
|
|
|
function pager_get_querystring() {
|
|
|
|
|
static $string = NULL;
|
|
|
|
|
if (!isset($string)) {
|
|
|
|
|
$string = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'page'), array_keys($_COOKIE)));
|
|
|
|
|
}
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a query pager.
|
|
|
|
|
*
|
|
|
|
|
* Menu callbacks that display paged query results should call theme('pager') to
|
|
|
|
|
* retrieve a pager control so that users can view other results.
|
|
|
|
|
*
|
|
|
|
|
* @param $tags
|
|
|
|
|
* An array of labels for the controls in the pager.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates the query pager.
|
2004-09-09 05:51:08 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) {
|
2003-03-12 20:45:24 +00:00
|
|
|
|
global $pager_total;
|
2004-07-02 18:46:42 +00:00
|
|
|
|
$output = '';
|
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
if ($pager_total[$element] > 1) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= '<div id="pager">';
|
2006-01-15 00:04:20 +00:00
|
|
|
|
$output .= theme('pager_first', ($tags[0] ? $tags[0] : t('« first')), $limit, $element, $parameters);
|
|
|
|
|
$output .= theme('pager_previous', ($tags[1] ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $parameters);
|
2006-01-15 00:04:20 +00:00
|
|
|
|
$output .= theme('pager_next', ($tags[3] ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
|
|
|
|
|
$output .= theme('pager_last', ($tags[4] ? $tags[4] : t('last »')), $limit, $element, $parameters);
|
2004-07-22 16:06:54 +00:00
|
|
|
|
$output .= '</div>';
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2003-03-12 20:45:24 +00:00
|
|
|
|
return $output;
|
|
|
|
|
}
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2004-07-22 16:06:54 +00:00
|
|
|
|
|
2003-11-24 22:46:03 +00:00
|
|
|
|
/**
|
2004-01-06 19:52:14 +00:00
|
|
|
|
* @name Pager pieces
|
2003-11-24 22:46:03 +00:00
|
|
|
|
* @{
|
2004-09-09 05:51:08 +00:00
|
|
|
|
* Use these pieces to construct your own custom pagers in your theme. Note that
|
|
|
|
|
* you should NOT modify this file to customize your pager.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*/
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a "first page" link.
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $text
|
|
|
|
|
* The name (or image) of the link.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates this piece of the query pager.
|
2005-01-27 14:30:33 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_first($text, $limit, $element = 0, $parameters = array()) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array;
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = '';
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// If we are anywhere but the first page
|
|
|
|
|
if ($pager_page_array[$element] > 0) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-first'));
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
|
2005-01-27 14:30:33 +00:00
|
|
|
|
return $output;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a "previous page" link.
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $text
|
|
|
|
|
* The name (or image) of the link.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
|
|
|
|
* @param $interval
|
|
|
|
|
* The number of pages to move backward when the link is clicked.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates this piece of the query pager.
|
2005-01-27 14:30:33 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array;
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = '';
|
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// If we are anywhere but the first page
|
|
|
|
|
if ($pager_page_array[$element] > 0) {
|
|
|
|
|
$page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
|
2006-04-13 08:25:27 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// If the previous page is the first page, mark the link as such.
|
|
|
|
|
if ($page_new[$element] == 0) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_first', $text, $limit, $element, $parameters);
|
2005-05-25 03:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
// The previous page is not the first page.
|
|
|
|
|
else {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-previous'));
|
2005-05-25 03:29:06 +00:00
|
|
|
|
}
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
|
2005-01-27 14:30:33 +00:00
|
|
|
|
return $output;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a "next page" link.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $text
|
|
|
|
|
* The name (or image) of the link.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
|
|
|
|
* @param $interval
|
|
|
|
|
* The number of pages to move forward when the link is clicked.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates this piece of the query pager.
|
2005-01-27 14:30:33 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array, $pager_total;
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = '';
|
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// If we are anywhere but the last page
|
|
|
|
|
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
|
|
|
|
|
$page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
|
|
|
|
|
// If the next page is the last page, mark the link as such.
|
|
|
|
|
if ($page_new[$element] == ($pager_total[$element] - 1)) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_last', $text, $limit, $element, $parameters);
|
2005-05-25 03:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
// The next page is not the last page.
|
|
|
|
|
else {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-next'));
|
2005-05-25 03:29:06 +00:00
|
|
|
|
}
|
2005-01-27 14:30:33 +00:00
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
|
2005-01-27 14:30:33 +00:00
|
|
|
|
return $output;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a "last page" link.
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $text
|
|
|
|
|
* The name (or image) of the link.
|
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates this piece of the query pager.
|
2005-01-27 14:30:33 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_last($text, $limit, $element = 0, $parameters = array()) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array, $pager_total;
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = '';
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
// If we are anywhere but the last page
|
|
|
|
|
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = theme('pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-last'));
|
2005-01-27 14:30:33 +00:00
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
|
2005-01-27 14:30:33 +00:00
|
|
|
|
return $output;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-26 19:24:20 +00:00
|
|
|
|
/**
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* Format a list of nearby pages with additional query results.
|
2003-11-24 22:46:03 +00:00
|
|
|
|
*
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @param $limit
|
|
|
|
|
* The number of query results to display per page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
|
|
|
|
* @param $quantity
|
|
|
|
|
* The number of pages in the list.
|
|
|
|
|
* @param $text
|
|
|
|
|
* A string of text to display before the page list.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager links.
|
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates this piece of the query pager.
|
2005-01-27 14:30:33 +00:00
|
|
|
|
*
|
|
|
|
|
* @ingroup themeable
|
2002-11-26 19:24:20 +00:00
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_list($limit, $element = 0, $quantity = 5, $text = '', $parameters = array()) {
|
2005-05-25 03:29:06 +00:00
|
|
|
|
global $pager_page_array, $pager_total;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output = '<span class="pager-list">';
|
2004-07-22 16:06:54 +00:00
|
|
|
|
// Calculate various markers within this pager piece:
|
|
|
|
|
// Middle is used to "center" pages around the current page.
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$pager_middle = ceil($quantity / 2);
|
2002-11-09 13:59:36 +00:00
|
|
|
|
// current is the page we are currently paged to
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$pager_current = $pager_page_array[$element] + 1;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
// first is the first page listed by this pager piece (re quantity)
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$pager_first = $pager_current - $pager_middle + 1;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
// last is the last page listed by this pager piece (re quantity)
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$pager_last = $pager_current + $quantity - $pager_middle;
|
|
|
|
|
// max is the maximum page number
|
|
|
|
|
$pager_max = $pager_total[$element];
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// End of marker calculations.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// Prepare for generation loop.
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$i = $pager_first;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
if ($pager_last > $pager_max) {
|
2004-07-22 16:06:54 +00:00
|
|
|
|
// Adjust "center" if at end of query.
|
2005-05-25 03:29:06 +00:00
|
|
|
|
$i = $i + ($pager_max - $pager_last);
|
2002-11-09 13:59:36 +00:00
|
|
|
|
$pager_last = $pager_max;
|
|
|
|
|
}
|
|
|
|
|
if ($i <= 0) {
|
2004-07-22 16:06:54 +00:00
|
|
|
|
// Adjust "center" if at start of query.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
$pager_last = $pager_last + (1 - $i);
|
|
|
|
|
$i = 1;
|
|
|
|
|
}
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// End of generation loop preparation.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2004-07-22 16:06:54 +00:00
|
|
|
|
// When there is more than one page, create the pager list.
|
2002-11-18 19:21:09 +00:00
|
|
|
|
if ($i != $pager_max) {
|
2005-01-27 14:30:33 +00:00
|
|
|
|
$output .= $text;
|
2002-11-18 19:21:09 +00:00
|
|
|
|
if ($i > 1) {
|
2006-01-15 00:04:20 +00:00
|
|
|
|
$output .= '<span class="pager-ellipsis">…</span>';
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2002-11-18 19:21:09 +00:00
|
|
|
|
|
2004-07-22 16:06:54 +00:00
|
|
|
|
// Now generate the actual pager piece.
|
2002-11-18 19:21:09 +00:00
|
|
|
|
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
|
|
|
|
|
if ($i < $pager_current) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters);
|
2002-11-18 19:21:09 +00:00
|
|
|
|
}
|
|
|
|
|
if ($i == $pager_current) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= '<strong class="pager-current">'. $i .'</strong>';
|
2002-11-18 19:21:09 +00:00
|
|
|
|
}
|
|
|
|
|
if ($i > $pager_current) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters);
|
2002-11-18 19:21:09 +00:00
|
|
|
|
}
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-18 19:21:09 +00:00
|
|
|
|
if ($i < $pager_max) {
|
2006-01-15 00:04:20 +00:00
|
|
|
|
$output .= '<span class="pager-ellipsis">…</span>';
|
2002-11-18 19:21:09 +00:00
|
|
|
|
}
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$output .= '</span>';
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-22 16:06:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* Format a link to a specific query result page.
|
|
|
|
|
*
|
2005-05-25 03:29:06 +00:00
|
|
|
|
* @param $page_new
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* The first result to display on the linked page.
|
|
|
|
|
* @param $element
|
|
|
|
|
* An optional integer to distinguish between multiple pagers on one page.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $parameters
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* An associative array of query string parameters to append to the pager link.
|
2006-01-14 09:40:22 +00:00
|
|
|
|
* @param $attributes
|
|
|
|
|
* An associative array of HTML attributes to apply to a pager anchor tag.
|
2004-07-22 16:06:54 +00:00
|
|
|
|
* @return
|
|
|
|
|
* An HTML string that generates the link.
|
|
|
|
|
*/
|
2006-01-14 09:40:22 +00:00
|
|
|
|
function theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
|
2005-10-21 10:58:15 +00:00
|
|
|
|
$page = isset($_GET['page']) ? $_GET['page'] : '';
|
|
|
|
|
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
|
2006-01-14 09:40:22 +00:00
|
|
|
|
$parameters['page'] = $new_page;
|
2005-10-21 10:58:15 +00:00
|
|
|
|
}
|
2002-12-14 11:55:54 +00:00
|
|
|
|
|
2005-10-21 10:58:15 +00:00
|
|
|
|
$query = array();
|
2006-04-13 08:25:27 +00:00
|
|
|
|
if (count($parameters)) {
|
|
|
|
|
$query[] = drupal_query_string_encode($parameters, array());
|
|
|
|
|
}
|
|
|
|
|
$querystring = pager_get_querystring();
|
|
|
|
|
if ($querystring != '') {
|
|
|
|
|
$query[] = $querystring;
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2002-12-14 11:55:54 +00:00
|
|
|
|
|
2006-01-15 16:55:35 +00:00
|
|
|
|
// Set each pager link title
|
2006-01-15 00:04:20 +00:00
|
|
|
|
if (!isset($attributes['title'])) {
|
2006-07-05 11:45:51 +00:00
|
|
|
|
static $titles = NULL;
|
2006-01-15 00:04:20 +00:00
|
|
|
|
if (!isset($titles)) {
|
|
|
|
|
$titles = array(
|
|
|
|
|
t('« first') => t('Go to first page'),
|
|
|
|
|
t('‹ previous') => t('Go to previous page'),
|
|
|
|
|
t('next ›') => t('Go to next page'),
|
|
|
|
|
t('last »') => t('Go to last page'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (isset($titles[$text])) {
|
|
|
|
|
$attributes['title'] = $titles[$text];
|
|
|
|
|
}
|
|
|
|
|
else if (is_numeric($text)) {
|
|
|
|
|
$attributes['title'] = t('Go to page %number', array('%number' => $text));
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-14 09:40:22 +00:00
|
|
|
|
|
2006-01-15 00:04:20 +00:00
|
|
|
|
return l($text, $_GET['q'], $attributes, count($query) ? implode('&', $query) : NULL);
|
2002-11-09 13:59:36 +00:00
|
|
|
|
}
|
2005-10-21 10:58:15 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* @} End of "Pager pieces".
|
|
|
|
|
*/
|
2002-11-09 13:59:36 +00:00
|
|
|
|
|
2005-05-25 03:29:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Helper function
|
|
|
|
|
*
|
|
|
|
|
* Copies $old_array to $new_array and sets $new_array[$element] = $value
|
|
|
|
|
* Fills in $new_array[0 .. $element - 1] = 0
|
|
|
|
|
*/
|
2002-11-09 13:59:36 +00:00
|
|
|
|
function pager_load_array($value, $element, $old_array) {
|
|
|
|
|
$new_array = $old_array;
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// Look for empty elements.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
for ($i = 0; $i < $element; $i++) {
|
|
|
|
|
if (!$new_array[$i]) {
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// Load found empty element with 0.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
$new_array[$i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-09-09 05:51:08 +00:00
|
|
|
|
// Update the changed element.
|
2002-11-09 13:59:36 +00:00
|
|
|
|
$new_array[$element] = (int)$value;
|
|
|
|
|
return $new_array;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-25 21:14:17 +00:00
|
|
|
|
|