PDA

View Full Version : vBulletin like pager in vbDrupal


elmuerte
02-19-2006, 02:39 AM
I've changed my pager to be more like the vBulletin pager.
Here's how I did it.

I added the following templates (note, do not place the templates under the drupal product or you'll lose them in an upgrade):

drupal_pagenav:

<div class="pagenav" align="$stylevar[right]">
<table class="tborder" cellpadding="3" cellspacing="$stylevar[cellspacing]" border="0">
<tr>
<td class="vbmenu_control" style="font-weight:normal"><phrase 1="$pagenumber" 2="$totalpages">$vbphrase[page_x_of_y]</phrase></td>
<if condition="$show['first']"><td class="alt1"><a class="smallfont" href="$firstpage" title="$vbphrase[first_page] - <phrase 1="$firstnumbers[first]" 2="$firstnumbers[last]" 3="$total">$vbphrase[results_x_to_y_of_z]</phrase>"><strong>&laquo;</strong> $vbphrase[first]</a></td></if>
<if condition="$show['prev']"><td class="alt1"><a class="smallfont" href="$prevpage" title="$vbphrase[prev_page] - <phrase 1="$prevnumbers[first]" 2="$prevnumbers[last]" 3="$total">$vbphrase[results_x_to_y_of_z]</phrase>">&lt;</a></td></if>
$pagenav
<if condition="$show['next']"><td class="alt1"><a class="smallfont" href="$nextpage" title="$vbphrase[next_page] - <phrase 1="$nextnumbers[first]" 2="$nextnumbers[last]" 3="$total">$vbphrase[results_x_to_y_of_z]</phrase>">&gt;</a></td></if>
<if condition="$show['last']"><td class="alt1"><a class="smallfont" href="$lastpage" title="$vbphrase[last_page] - <phrase 1="$lastnumbers[first]" 2="$lastnumbers[last]" 3="$total">$vbphrase[results_x_to_y_of_z]</phrase>">$vbphrase[last] <strong>&raquo;</strong></a></td></if>
<if condition="$show['popups']"><td class="vbmenu_control" title="$address$address2"><a name="PageNav"></a></td></if>
</tr>
</table>
</div>


drupal_pagenav_curpage

<td class="alt2"><span class="smallfont" title="<phrase 1="$numbers[first]" 2="$numbers[last]" 3="$total">$vbphrase[showing_results_x_to_y_of_z]</phrase>"><strong>$curpage</strong></span></td>


drupal_pagenav_pagelink

<td class="alt1"><a class="smallfont" href="$curpagelink" title="<phrase 1="$pagenumbers[first]" 2="$pagenumbers[last]" 3="$total">$vbphrase[show_results_x_to_y_of_z]</phrase>">$curpage</a></td>


Then I added the following code to my .theme

function mbnvb_pager($tags = array(), $limit = 10, $element = 0, $attributes = array()) {
global $pager_total, $pager_from_array, $stylevar, $vbphrase;
$output = '';

if ($pager_total[$element] > $limit) {
if (($pagenumber = (ceil(($pager_from_array[$element] + 1) / $limit))) < 1) {
$pagenumber = 1;
}
$totalpages = ceil($pager_total[$element] / $limit);
$total = $pager_total[$element];

$firstpage = theme('pager_first', ($tags[0] ? $tags[0] : t('first page')), $limit, $element, $attributes);
$prevpage = theme('pager_previous', ($tags[1] ? $tags[1] : t('previous page')), $limit, $element, 1, $attributes);
$pagenav = theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $attributes);
$nextpage = theme('pager_next', ($tags[3] ? $tags[3] : t('next page')), $limit, $element, 1, $attributes);
$lastpage = theme('pager_last', ($tags[4] ? $tags[4] : t('last page')), $limit, $element, $attributes);


$show['popups'] = false;
$show['first'] = !empty($firstpage);
$show['prev'] = !empty($prevpage);
$show['next'] = !empty($nextpage);
$show['last'] = !empty($lastpage);

eval('$output = "' . fetch_template('drupal_pagenav') . '";');

return $output;
}
}

function mbnvb_pager_first($text, $limit, $element = 0, $attributes = array()) {
global $pager_from_array;
if ($pager_from_array[$element]) {
return pager_link(pager_load_array(0, $element, $pager_from_array), $element, $attributes);
}
else {
return '';
}
}

function mbnvb_pager_previous($text, $limit, $element = 0, $interval = 1, $attributes = array()) {
global $pager_from_array;
$from_new = pager_load_array(((int)$pager_from_array[$element] - ((int)$limit * (int)$interval)), $element, $pager_from_array);
if ($from_new[$element] < 1) {
return theme('pager_first', $text, $limit, $element, $attributes);
}
else {
return pager_link($from_new, $element, $attributes);
}
}

function mbnvb_pager_next($text, $limit, $element = 0, $interval = 1, $attributes = array()) {
global $pager_from_array, $pager_total;
$from_new = pager_load_array(((int)$pager_from_array[$element] + ((int)$limit * (int)$interval)), $element, $pager_from_array);
if ($from_new[$element] < $pager_total[$element]) {
return pager_link($from_new, $element, $attributes);
}
else {
return '';
}
}

function mbnvb_pager_last($text, $limit, $element = 0, $attributes = array()) {
global $pager_from_array, $pager_total;

$last_num = (($pager_total[$element] % $limit) ? ($pager_total[$element] % $limit) : $limit);
$from_new = pager_load_array(($pager_total[$element] - $last_num), $element, $pager_from_array);
if ($from_new[$element] < ($pager_from_array[$element] + $limit)) {
return theme('pager_next', $text, $limit, $element, 1, $attributes);
}
else if (($from_new[$element] > $pager_from_array[$element]) && ($from_new[$element] > 0) && ($from_new[$element] < $pager_total[$element])) {
return pager_link($from_new, $element, $attributes);
}
else {
return '';
}
}

function mbnvb_pager_list($limit, $element = 0, $quantity = 5, $text = '', $attributes = array()) {
global $pager_from_array, $pager_total, $vbphrase;

$output = '';
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil((int)$quantity / 2);
// offset adds "offset" second page
$pager_offset = (int)$pager_from_array[$element] % (int)$limit;
// current is the page we are currently paged to
if (($pager_current = (ceil(($pager_from_array[$element] + 1) / $limit))) < 1) {
$pager_current = 1;
}
// first is the first page listed by this pager piece (re quantity)
$pager_first = (int)$pager_current - (int)$pager_middle + 1;
// last is the last page listed by this pager piece (re quantity)
$pager_last = (int)$pager_current + (int)$quantity - (int)$pager_middle;
// max is the maximum number of pages content can is divided into
if (!$pager_max = (ceil($pager_total[$element] / $limit))) {
$pager_max = 1;
}
if ((int)$pager_offset) {
// adjust for offset second page
$pager_max++;
$pager_current++;
}
// End of marker calculations.

// Prepare for generation loop.
$i = (int)$pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + (int)($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
// End of generation loop preparation.

// When there is more than one page, create the pager list.
$total = $pager_total[$element];
if ($i != $pager_max) {
$output .= $text;

// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
$curpage = $i;
$n = pager_load_array((((int)$limit * (int)($i-1))), $element, $pager_from_array);
$curpagelink = pager_link($n, $element, $attributes);
$pagenumbers['first'] = $limit * ($i-1) + 1;
$pagenumbers['last'] = $limit * ($i);
if ($i == $pager_current) {
eval('$output .= "' . fetch_template('drupal_pagenav_curpage') . '";');
}
else {
eval('$output .= "' . fetch_template('drupal_pagenav_pagelink') . '";');
}
}

}

return $output;
}

Note: make sure to change the function names to YourTheme_FunctionName

The result: 1

live example (http://www.magicball.net/search/node/the)

Mark
02-24-2006, 09:39 AM
very nice :) I'll be using this in the near future