[ Index ] |
|
Code source de Drupal 5.3 |
1 <?php 2 // $Id: pager.inc,v 1.59 2006/10/15 19:57:05 dries Exp $ 3 4 /** 5 * @file 6 * Functions to aid in presenting database results as a set of pages. 7 */ 8 9 /** 10 * Perform a paged database query. 11 * 12 * Use this function when doing select queries you wish to be able to page. The 13 * pager uses LIMIT-based queries to fetch only the records required to render a 14 * certain page. However, it has to learn the total number of records returned 15 * by the query to compute the number of pages (the number of records / records 16 * per page). This is done by inserting "COUNT(*)" in the original query. For 17 * example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY 18 * sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM 19 * node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the 20 * query is accomplished using a regular expression. 21 * 22 * Unfortunately, the rewrite rule does not always work as intended for queries 23 * that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for 24 * other complex queries. In those cases, you can optionally pass a query that 25 * will be used to count the records. 26 * 27 * For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node 28 * GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT 29 * COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT 30 * COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter. 31 * 32 * @param $query 33 * The SQL query that needs paging. 34 * @param $limit 35 * The number of query results to display per page. 36 * @param $element 37 * An optional integer to distinguish between multiple pagers on one page. 38 * @param $count_query 39 * An SQL query used to count matching records. 40 * @param ... 41 * A variable number of arguments which are substituted into the query (and 42 * the count query) using printf() syntax. Instead of a variable number of 43 * query arguments, you may also pass a single array containing the query 44 * arguments. 45 * @return 46 * A database query result resource, or FALSE if the query was not executed 47 * correctly. 48 * 49 * @ingroup database 50 */ 51 function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) { 52 global $pager_page_array, $pager_total, $pager_total_items; 53 $page = isset($_GET['page']) ? $_GET['page'] : ''; 54 55 // Substitute in query arguments. 56 $args = func_get_args(); 57 $args = array_slice($args, 4); 58 // Alternative syntax for '...' 59 if (isset($args[0]) && is_array($args[0])) { 60 $args = $args[0]; 61 } 62 63 // Construct a count query if none was given. 64 if (!isset($count_query)) { 65 $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query); 66 } 67 68 // Convert comma-separated $page to an array, used by other functions. 69 $pager_page_array = explode(',', $page); 70 71 // We calculate the total of pages as ceil(items / limit). 72 $pager_total_items[$element] = db_result(db_query($count_query, $args)); 73 $pager_total[$element] = ceil($pager_total_items[$element] / $limit); 74 $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1)); 75 return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit); 76 } 77 78 /** 79 * Compose a query string to append to pager requests. 80 * 81 * @return 82 * A query string that consists of all components of the current page request 83 * except for those pertaining to paging. 84 */ 85 function pager_get_querystring() { 86 static $string = NULL; 87 if (!isset($string)) { 88 $string = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'page'), array_keys($_COOKIE))); 89 } 90 return $string; 91 } 92 93 /** 94 * Format a query pager. 95 * 96 * Menu callbacks that display paged query results should call theme('pager') to 97 * retrieve a pager control so that users can view other results. 98 * 99 * @param $tags 100 * An array of labels for the controls in the pager. 101 * @param $limit 102 * The number of query results to display per page. 103 * @param $element 104 * An optional integer to distinguish between multiple pagers on one page. 105 * @param $parameters 106 * An associative array of query string parameters to append to the pager links. 107 * @return 108 * An HTML string that generates the query pager. 109 * 110 * @ingroup themeable 111 */ 112 function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) { 113 global $pager_total; 114 $output = ''; 115 116 if ($pager_total[$element] > 1) { 117 $output .= '<div class="pager">'; 118 $output .= theme('pager_first', ($tags[0] ? $tags[0] : t('« first')), $limit, $element, $parameters); 119 $output .= theme('pager_previous', ($tags[1] ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters); 120 $output .= theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $parameters); 121 $output .= theme('pager_next', ($tags[3] ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters); 122 $output .= theme('pager_last', ($tags[4] ? $tags[4] : t('last »')), $limit, $element, $parameters); 123 $output .= '</div>'; 124 125 return $output; 126 } 127 } 128 129 /** 130 * @name Pager pieces 131 * @{ 132 * Use these pieces to construct your own custom pagers in your theme. Note that 133 * you should NOT modify this file to customize your pager. 134 */ 135 136 /** 137 * Format a "first page" link. 138 * 139 * @param $text 140 * The name (or image) of the link. 141 * @param $limit 142 * The number of query results to display per page. 143 * @param $element 144 * An optional integer to distinguish between multiple pagers on one page. 145 * @param $parameters 146 * An associative array of query string parameters to append to the pager links. 147 * @return 148 * An HTML string that generates this piece of the query pager. 149 * 150 * @ingroup themeable 151 */ 152 function theme_pager_first($text, $limit, $element = 0, $parameters = array()) { 153 global $pager_page_array; 154 $output = ''; 155 156 // If we are anywhere but the first page 157 if ($pager_page_array[$element] > 0) { 158 $output = theme('pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-first')); 159 } 160 161 return $output; 162 } 163 164 /** 165 * Format a "previous page" link. 166 * 167 * @param $text 168 * The name (or image) of the link. 169 * @param $limit 170 * The number of query results to display per page. 171 * @param $element 172 * An optional integer to distinguish between multiple pagers on one page. 173 * @param $interval 174 * The number of pages to move backward when the link is clicked. 175 * @param $parameters 176 * An associative array of query string parameters to append to the pager links. 177 * @return 178 * An HTML string that generates this piece of the query pager. 179 * 180 * @ingroup themeable 181 */ 182 function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) { 183 global $pager_page_array; 184 $output = ''; 185 186 // If we are anywhere but the first page 187 if ($pager_page_array[$element] > 0) { 188 $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array); 189 190 // If the previous page is the first page, mark the link as such. 191 if ($page_new[$element] == 0) { 192 $output = theme('pager_first', $text, $limit, $element, $parameters); 193 } 194 // The previous page is not the first page. 195 else { 196 $output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-previous')); 197 } 198 } 199 200 return $output; 201 } 202 203 /** 204 * Format a "next page" link. 205 * 206 * @param $text 207 * The name (or image) of the link. 208 * @param $limit 209 * The number of query results to display per page. 210 * @param $element 211 * An optional integer to distinguish between multiple pagers on one page. 212 * @param $interval 213 * The number of pages to move forward when the link is clicked. 214 * @param $parameters 215 * An associative array of query string parameters to append to the pager links. 216 * @return 217 * An HTML string that generates this piece of the query pager. 218 * 219 * @ingroup themeable 220 */ 221 function theme_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) { 222 global $pager_page_array, $pager_total; 223 $output = ''; 224 225 // If we are anywhere but the last page 226 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) { 227 $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array); 228 // If the next page is the last page, mark the link as such. 229 if ($page_new[$element] == ($pager_total[$element] - 1)) { 230 $output = theme('pager_last', $text, $limit, $element, $parameters); 231 } 232 // The next page is not the last page. 233 else { 234 $output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-next')); 235 } 236 } 237 238 return $output; 239 } 240 241 /** 242 * Format a "last page" link. 243 * 244 * @param $text 245 * The name (or image) of the link. 246 * @param $limit 247 * The number of query results to display per page. 248 * @param $element 249 * An optional integer to distinguish between multiple pagers on one page. 250 * @param $parameters 251 * An associative array of query string parameters to append to the pager links. 252 * @return 253 * An HTML string that generates this piece of the query pager. 254 * 255 * @ingroup themeable 256 */ 257 function theme_pager_last($text, $limit, $element = 0, $parameters = array()) { 258 global $pager_page_array, $pager_total; 259 $output = ''; 260 261 // If we are anywhere but the last page 262 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) { 263 $output = theme('pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-last')); 264 } 265 266 return $output; 267 } 268 269 /** 270 * Format a list of nearby pages with additional query results. 271 * 272 * @param $limit 273 * The number of query results to display per page. 274 * @param $element 275 * An optional integer to distinguish between multiple pagers on one page. 276 * @param $quantity 277 * The number of pages in the list. 278 * @param $text 279 * A string of text to display before the page list. 280 * @param $parameters 281 * An associative array of query string parameters to append to the pager links. 282 * @return 283 * An HTML string that generates this piece of the query pager. 284 * 285 * @ingroup themeable 286 */ 287 function theme_pager_list($limit, $element = 0, $quantity = 5, $text = '', $parameters = array()) { 288 global $pager_page_array, $pager_total; 289 290 $output = '<span class="pager-list">'; 291 // Calculate various markers within this pager piece: 292 // Middle is used to "center" pages around the current page. 293 $pager_middle = ceil($quantity / 2); 294 // current is the page we are currently paged to 295 $pager_current = $pager_page_array[$element] + 1; 296 // first is the first page listed by this pager piece (re quantity) 297 $pager_first = $pager_current - $pager_middle + 1; 298 // last is the last page listed by this pager piece (re quantity) 299 $pager_last = $pager_current + $quantity - $pager_middle; 300 // max is the maximum page number 301 $pager_max = $pager_total[$element]; 302 // End of marker calculations. 303 304 // Prepare for generation loop. 305 $i = $pager_first; 306 if ($pager_last > $pager_max) { 307 // Adjust "center" if at end of query. 308 $i = $i + ($pager_max - $pager_last); 309 $pager_last = $pager_max; 310 } 311 if ($i <= 0) { 312 // Adjust "center" if at start of query. 313 $pager_last = $pager_last + (1 - $i); 314 $i = 1; 315 } 316 // End of generation loop preparation. 317 318 // When there is more than one page, create the pager list. 319 if ($i != $pager_max) { 320 $output .= $text; 321 if ($i > 1) { 322 $output .= '<span class="pager-ellipsis">…</span>'; 323 } 324 325 // Now generate the actual pager piece. 326 for (; $i <= $pager_last && $i <= $pager_max; $i++) { 327 if ($i < $pager_current) { 328 $output .= theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters); 329 } 330 if ($i == $pager_current) { 331 $output .= '<strong class="pager-current">'. $i .'</strong>'; 332 } 333 if ($i > $pager_current) { 334 $output .= theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters); 335 } 336 } 337 338 if ($i < $pager_max) { 339 $output .= '<span class="pager-ellipsis">…</span>'; 340 } 341 } 342 $output .= '</span>'; 343 344 return $output; 345 } 346 347 /** 348 * Format a link to a specific query result page. 349 * 350 * @param $page_new 351 * The first result to display on the linked page. 352 * @param $element 353 * An optional integer to distinguish between multiple pagers on one page. 354 * @param $parameters 355 * An associative array of query string parameters to append to the pager link. 356 * @param $attributes 357 * An associative array of HTML attributes to apply to a pager anchor tag. 358 * @return 359 * An HTML string that generates the link. 360 */ 361 function theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) { 362 $page = isset($_GET['page']) ? $_GET['page'] : ''; 363 if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) { 364 $parameters['page'] = $new_page; 365 } 366 367 $query = array(); 368 if (count($parameters)) { 369 $query[] = drupal_query_string_encode($parameters, array()); 370 } 371 $querystring = pager_get_querystring(); 372 if ($querystring != '') { 373 $query[] = $querystring; 374 } 375 376 // Set each pager link title 377 if (!isset($attributes['title'])) { 378 static $titles = NULL; 379 if (!isset($titles)) { 380 $titles = array( 381 t('« first') => t('Go to first page'), 382 t('‹ previous') => t('Go to previous page'), 383 t('next ›') => t('Go to next page'), 384 t('last »') => t('Go to last page'), 385 ); 386 } 387 if (isset($titles[$text])) { 388 $attributes['title'] = $titles[$text]; 389 } 390 else if (is_numeric($text)) { 391 $attributes['title'] = t('Go to page @number', array('@number' => $text)); 392 } 393 } 394 395 return l($text, $_GET['q'], $attributes, count($query) ? implode('&', $query) : NULL); 396 } 397 398 /** 399 * @} End of "Pager pieces". 400 */ 401 402 /** 403 * Helper function 404 * 405 * Copies $old_array to $new_array and sets $new_array[$element] = $value 406 * Fills in $new_array[0 .. $element - 1] = 0 407 */ 408 function pager_load_array($value, $element, $old_array) { 409 $new_array = $old_array; 410 // Look for empty elements. 411 for ($i = 0; $i < $element; $i++) { 412 if (!$new_array[$i]) { 413 // Load found empty element with 0. 414 $new_array[$i] = 0; 415 } 416 } 417 // Update the changed element. 418 $new_array[$element] = (int)$value; 419 return $new_array; 420 } 421 422
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Nov 30 16:20:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |