00001 <?php
00002
00003
00051 function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
00052 global $pager_page_array, $pager_total, $pager_total_items;
00053 $page = isset($_GET['page']) ? $_GET['page'] : '';
00054
00055
00056 $args = func_get_args();
00057 $args = array_slice($args, 4);
00058
00059 if (isset($args[0]) && is_array($args[0])) {
00060 $args = $args[0];
00061 }
00062
00063
00064 if (!isset($count_query)) {
00065 $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
00066 }
00067
00068
00069 $pager_page_array = explode(',', $page);
00070
00071
00072 $pager_total_items[$element] = db_result(db_query($count_query, $args));
00073 $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
00074 $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
00075 return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
00076 }
00077
00085 function pager_get_querystring() {
00086 static $string = NULL;
00087 if (!isset($string)) {
00088 $string = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'page'), array_keys($_COOKIE)));
00089 }
00090 return $string;
00091 }
00092
00115 function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
00116 global $pager_page_array, $pager_total;
00117
00118
00119
00120 $pager_middle = ceil($quantity / 2);
00121
00122 $pager_current = $pager_page_array[$element] + 1;
00123
00124 $pager_first = $pager_current - $pager_middle + 1;
00125
00126 $pager_last = $pager_current + $quantity - $pager_middle;
00127
00128 $pager_max = $pager_total[$element];
00129
00130
00131
00132 $i = $pager_first;
00133 if ($pager_last > $pager_max) {
00134
00135 $i = $i + ($pager_max - $pager_last);
00136 $pager_last = $pager_max;
00137 }
00138 if ($i <= 0) {
00139
00140 $pager_last = $pager_last + (1 - $i);
00141 $i = 1;
00142 }
00143
00144
00145 $li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('« first')), $limit, $element, $parameters);
00146 $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
00147 $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
00148 $li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('last »')), $limit, $element, $parameters);
00149
00150 if ($pager_total[$element] > 1) {
00151 if ($li_first) {
00152 $items[] = array(
00153 'class' => 'pager-first',
00154 'data' => $li_first,
00155 );
00156 }
00157 if ($li_previous) {
00158 $items[] = array(
00159 'class' => 'pager-previous',
00160 'data' => $li_previous,
00161 );
00162 }
00163
00164
00165 if ($i != $pager_max) {
00166 if ($i > 1) {
00167 $items[] = array(
00168 'class' => 'pager-ellipsis',
00169 'data' => '…',
00170 );
00171 }
00172
00173 for (; $i <= $pager_last && $i <= $pager_max; $i++) {
00174 if ($i < $pager_current) {
00175 $items[] = array(
00176 'class' => 'pager-item',
00177 'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters),
00178 );
00179 }
00180 if ($i == $pager_current) {
00181 $items[] = array(
00182 'class' => 'pager-current',
00183 'data' => $i,
00184 );
00185 }
00186 if ($i > $pager_current) {
00187 $items[] = array(
00188 'class' => 'pager-item',
00189 'data' => theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
00190 );
00191 }
00192 }
00193 if ($i < $pager_max) {
00194 $items[] = array(
00195 'class' => 'pager-ellipsis',
00196 'data' => '…',
00197 );
00198 }
00199 }
00200
00201 if ($li_next) {
00202 $items[] = array(
00203 'class' => 'pager-next',
00204 'data' => $li_next,
00205 );
00206 }
00207 if ($li_last) {
00208 $items[] = array(
00209 'class' => 'pager-last',
00210 'data' => $li_last,
00211 );
00212 }
00213 return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
00214 }
00215 }
00216
00217
00241 function theme_pager_first($text, $limit, $element = 0, $parameters = array()) {
00242 global $pager_page_array;
00243 $output = '';
00244
00245
00246 if ($pager_page_array[$element] > 0) {
00247 $output = theme('pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters);
00248 }
00249
00250 return $output;
00251 }
00252
00271 function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
00272 global $pager_page_array;
00273 $output = '';
00274
00275
00276 if ($pager_page_array[$element] > 0) {
00277 $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
00278
00279
00280 if ($page_new[$element] == 0) {
00281 $output = theme('pager_first', $text, $limit, $element, $parameters);
00282 }
00283
00284 else {
00285 $output = theme('pager_link', $text, $page_new, $element, $parameters);
00286 }
00287 }
00288
00289 return $output;
00290 }
00291
00310 function theme_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
00311 global $pager_page_array, $pager_total;
00312 $output = '';
00313
00314
00315 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
00316 $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
00317
00318 if ($page_new[$element] == ($pager_total[$element] - 1)) {
00319 $output = theme('pager_last', $text, $limit, $element, $parameters);
00320 }
00321
00322 else {
00323 $output = theme('pager_link', $text, $page_new, $element, $parameters);
00324 }
00325 }
00326
00327 return $output;
00328 }
00329
00346 function theme_pager_last($text, $limit, $element = 0, $parameters = array()) {
00347 global $pager_page_array, $pager_total;
00348 $output = '';
00349
00350
00351 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
00352 $output = theme('pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters);
00353 }
00354
00355 return $output;
00356 }
00357
00358
00375 function theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
00376 $page = isset($_GET['page']) ? $_GET['page'] : '';
00377 if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
00378 $parameters['page'] = $new_page;
00379 }
00380
00381 $query = array();
00382 if (count($parameters)) {
00383 $query[] = drupal_query_string_encode($parameters, array());
00384 }
00385 $querystring = pager_get_querystring();
00386 if ($querystring != '') {
00387 $query[] = $querystring;
00388 }
00389
00390
00391 if (!isset($attributes['title'])) {
00392 static $titles = NULL;
00393 if (!isset($titles)) {
00394 $titles = array(
00395 t('« first') => t('Go to first page'),
00396 t('‹ previous') => t('Go to previous page'),
00397 t('next ›') => t('Go to next page'),
00398 t('last »') => t('Go to last page'),
00399 );
00400 }
00401 if (isset($titles[$text])) {
00402 $attributes['title'] = $titles[$text];
00403 }
00404 else if (is_numeric($text)) {
00405 $attributes['title'] = t('Go to page @number', array('@number' => $text));
00406 }
00407 }
00408
00409 return l($text, $_GET['q'], array('attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL));
00410 }
00411
00422 function pager_load_array($value, $element, $old_array) {
00423 $new_array = $old_array;
00424
00425 for ($i = 0; $i < $element; $i++) {
00426 if (!$new_array[$i]) {
00427
00428 $new_array[$i] = 0;
00429 }
00430 }
00431
00432 $new_array[$element] = (int)$value;
00433 return $new_array;
00434 }