[ Index ] |
PHP Cross Reference of Eventum |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */ 3 // +----------------------------------------------------------------------+ 4 // | Eventum - Issue Tracking System | 5 // +----------------------------------------------------------------------+ 6 // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB | 7 // | | 8 // | This program is free software; you can redistribute it and/or modify | 9 // | it under the terms of the GNU General Public License as published by | 10 // | the Free Software Foundation; either version 2 of the License, or | 11 // | (at your option) any later version. | 12 // | | 13 // | This program is distributed in the hope that it will be useful, | 14 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 // | GNU General Public License for more details. | 17 // | | 18 // | You should have received a copy of the GNU General Public License | 19 // | along with this program; if not, write to: | 20 // | | 21 // | Free Software Foundation, Inc. | 22 // | 59 Temple Place - Suite 330 | 23 // | Boston, MA 02111-1307, USA. | 24 // +----------------------------------------------------------------------+ 25 // | Authors: João Prado Maia <jpm@mysql.com> | 26 // +----------------------------------------------------------------------+ 27 // 28 // @(#) $Id: class.pager.php 3246 2007-02-09 09:10:12Z glen $ 29 // 30 31 require_once (APP_INC_PATH . "class.error_handler.php"); 32 33 /** 34 * Class to manage paginated links on the frontend pages. 35 * 36 * @version 1.0 37 * @author João Prado Maia <jpm@mysql.com> 38 */ 39 40 class Pager 41 { 42 /** 43 * Returns the total number of rows for a specific query. It is used to 44 * calculate the total number of pages of data. 45 * 46 * @access public 47 * @param string $stmt The SQL statement 48 * @return int The total number of rows 49 */ 50 function getTotalRows($stmt) 51 { 52 $stmt = str_replace("\n", "", $stmt); 53 $stmt = str_replace("\r", "", $stmt); 54 if (stristr($stmt, 'GROUP BY')) { 55 // go the extra mile and try to use the grouped by column in the count() call 56 preg_match("/.*\s+GROUP BY\s+(\w*)\s+.*/i", $stmt, $matches); 57 if (!empty($matches[1])) { 58 $stmt = preg_replace("/SELECT (.*?) FROM /sei", "'SELECT COUNT(DISTINCT " . $matches[1] . ") AS total_rows FROM '", $stmt); 59 } 60 } else { 61 $stmt = preg_replace("/SELECT (.*?) FROM /sei", "'SELECT COUNT(*) AS total_rows FROM '", $stmt); 62 } 63 // remove any order by clauses 64 $stmt = preg_replace("/(.*)(ORDER BY\s+\w+\s+\w+)[,\s+\w+\s+\w+]*(.*)/sei", "'\\1\\3'", $stmt); 65 $rows = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC); 66 if (PEAR::isError($rows)) { 67 Error_Handler::logError(array($rows->getMessage(), $rows->getDebugInfo()), __FILE__, __LINE__); 68 return 0; 69 } elseif (empty($rows)) { 70 return 0; 71 } else { 72 // the query above works only if there is no left join or any other complex queries 73 if (count($rows) == 1) { 74 return $rows[0]["total_rows"]; 75 } else { 76 return count($rows); 77 } 78 } 79 } 80 81 82 /** 83 * Returns the query string to be used on the paginated links 84 * 85 * @access private 86 * @return string The query string 87 */ 88 function _buildQueryString() 89 { 90 $query_str = ""; 91 // gotta check manually here 92 $params = $_GET; 93 while (list($key, $value) = each($params)) { 94 if ($key != "pagerRow") { 95 $query_str .= "&" . $key . "=" . urlencode($value); 96 } 97 } 98 return $query_str; 99 } 100 101 102 /** 103 * Returns an array with the paginated links, one in each item. 104 * 105 * @access public 106 * @param int $row Current page number (starts from zero) 107 * @param int $total_rows Total number of rows, as returned by Pager::getTotalRows() 108 * @param int $per_page Maximum number of rows per page 109 * @param string $show_links An option to show 'Next'/'Previous' links, page numbering links or both ('sides', 'pages' or 'all') 110 * @param string $show_blank An option to show 'Next'/'Previous' strings even if there are no appropriate next or previous pages 111 * @param array $link_str The strings to be used instead of the default 'Next >>' and '<< Previous' 112 * @return array The list of paginated links 113 * @see getTotalRows() 114 */ 115 function getLinks($row, $total_rows, $per_page, $show_links = "all", $show_blank = "off", $link_str = -1) 116 { 117 // check for emptyness 118 if ((empty($total_rows)) || (empty($per_page))) { 119 return array(); 120 } 121 if ($link_str == -1) { 122 $link_str = array( 123 "previous" => "<< " . ev_gettext("Previous"), 124 "next" => ev_gettext("Next") . " >>" 125 ); 126 } 127 $extra_vars = Pager::_buildQueryString(); 128 $file = $_SERVER["SCRIPT_NAME"]; 129 $number_of_pages = ceil($total_rows / $per_page); 130 $subscript = 0; 131 for ($current = 0; $current < $number_of_pages; $current++) { 132 // if we need to show all links, or the 'side' links, 133 // let's add the 'Previous' link as the first item of the array 134 if ((($show_links == "all") || ($show_links == "sides")) && ($current == 0)) { 135 if ($row != 0) { 136 $array[0] = '<A HREF="' . $file . '?pagerRow=' . ($row - 1) . $extra_vars . '">' . $link_str["previous"] . '</A>'; 137 } elseif (($row == 0) && ($show_blank == "on")) { 138 $array[0] = $link_str["previous"]; 139 } 140 } 141 142 // check to show page numbering links or not 143 if (($show_links == "all") || ($show_links == "pages")) { 144 if ($row == $current) { 145 // if we only have one page worth of rows, we should show the '1' page number 146 if (($current == ($number_of_pages - 1)) && ($number_of_pages == 1) && ($show_blank == "off")) { 147 $array[0] = "<b>" . ($current > 0 ? ($current + 1) : 1) . "</b>"; 148 } else { 149 $array[++$subscript] = "<b>" . ($current > 0 ? ($current + 1) : 1) . "</b>"; 150 } 151 } else { 152 $array[++$subscript] = '<A HREF="' . $file . '?pagerRow=' . $current . $extra_vars . '">' . ($current + 1) . '</A>'; 153 } 154 } 155 156 // only add the 'Next' link to the array if we are on the last iteration of this loop 157 if ((($show_links == "all") || ($show_links == "sides")) && ($current == ($number_of_pages - 1))) { 158 if ($row != ($number_of_pages - 1)) { 159 $array[++$subscript] = '<A HREF="' . $file . '?pagerRow=' . ($row + 1) . $extra_vars . '">' . $link_str["next"] . '</A>'; 160 } elseif (($row == ($number_of_pages - 1)) && ($show_blank == "on")) { 161 $array[++$subscript] = $link_str["next"]; 162 } 163 } 164 } 165 return $array; 166 } 167 168 169 /** 170 * Returns a portion of an array of links, as returned by the Pager::getLinks() 171 * function. This is especially useful for preventing a huge list of links 172 * on the paginated list. 173 * 174 * @access public 175 * @param array $array The full list of paginated links 176 * @param int $current The current page number 177 * @param int $target_size The maximum number of paginated links 178 * @return array The list of paginated links 179 * @see getLinks() 180 */ 181 function getPortion($array, $current, $target_size = 20) 182 { 183 $size = count($array); 184 if (($size <= 2) || ($size < $target_size)) { 185 $temp = $array; 186 } else { 187 $temp = array(); 188 if (($current + $target_size) > $size) { 189 $temp = array_slice($array, $size - $target_size); 190 } else { 191 $temp = array_slice($array, $current, $target_size); 192 if ($size >= $target_size) { 193 array_push($temp, $array[$size-1]); 194 } 195 } 196 if ($current > 0) { 197 array_unshift($temp, $array[0]); 198 } 199 } 200 // extra check to make sure 201 if (count($temp) == 0) { 202 return ""; 203 } else { 204 return $temp; 205 } 206 } 207 } 208 209 // benchmarking the included file (aka setup time) 210 if (APP_BENCHMARK) { 211 $GLOBALS['bench']->setMarker('Included Pager Class'); 212 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 19 21:21:33 2007 | Cross-referenced by PHPXref 0.7 |