<?php
/**
 * Copyright (c) 2007 Mats Lindh, http://e-mats.org/
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
function smarty_function_paginator($params, &$smarty)
{
    
$separator ' &middot; &middot; &middot; ';
    
$page_separator ' &nbsp; ';
    
$offset 0;
    
$hits 20;
    
$url '';
    
$current_page 1;
    
$pages 1;
    
$title_prefix 'Page ';
    
$class_active 'paginator_active';
    
$class_inactive 'paginator_inactive';
    
$url_argument_separator '?';
    
$hits_parameter_name 'hits';
    
$offset_parameter_name 'offset';
    
$next_page_title 'Next page';
    
$previous_page_title 'Previous page';

    if (isset(
$params['offset']))
    {
        
$offset = (int) $params['offset'];
    }

    if (isset(
$params['hits']))
    {
        
$hits = (int) $params['hits'];
    }

    if (isset(
$params['url']))
    {
        
$url $params['url'];
    }

    if (!empty(
$params['total_hits']))
    {
        
$pages ceil($params['total_hits'] / $hits);
        
$current_page floor($offset $hits) + 1;
    }

    if (!empty(
$params['pages']))
    {
        
$pages max(1, (int) $params['pages']);
    }

    if (!empty(
$params['current']))
    {
        
$current_page max(1, (int) $params['current']);
    }

    if (!empty(
$params['title']))
    {
        
$title_prefix htmlspecialchars($params['title'], ENT_QUOTES);
    }

    if (!empty(
$params['hits_parameter_name']))
    {
        
$hits_parameter_name $params['hits_parameter_name'];
    }

    if (!empty(
$params['offset_parameter_name']))
    {
        
$offset_parameter_name $params['offset_parameter_name'];
    }

    if (isset(
$params['title_prefix']))
    {
        
$title_prefix $params['title_prefix'];
    }

    if (isset(
$params['next_page_title']))
    {
        
$next_page_title $params['next_page_title'];
    }

    if (isset(
$params['previous_page_title']))
    {
        
$previous_page_title $params['previous_page_title'];
    }

    if (isset(
$params['class_inactive']))
    {
        
$class_inactive $params['class_inactive'];
    }

    if (isset(
$params['class_active']))
    {
        
$class_active $params['class_active'];
    }

    if (
strpos($url'?') !== false)
    {
        
$url_argument_separator '&';
    }

    
$str '';

    
$printed_start_middle_separator false;
    
$printed_middle_end_separator false;

    for(
$i 0$i $pages$i++)
    {
        
$middle false;

        
// if we're somewhere in the middle..
        
if (($i 2) && ($current_page 2))
        {
            
// if we've not printed the start, do it now..
            
if (!$printed_start_middle_separator && ($current_page 5))
            {
                
$printed_start_middle_separator true;
                
$str .= $page_separator $separator;
            }

            
// check if we're printing an ending here...
            
if (!$printed_middle_end_separator && ($current_page > ($pages 5)))
            {
                
$printed_middle_end_separator true;
                
//$str .= $page_separator . $separator;
            
}

            
// jump to the middle position if we've not been there already..
            
$i max($i$current_page 2);

            if (
$i < ($current_page 1))
            {
                
// just so we can handle it below if we're in the middle of our middle-thingie..
                
$middle true;
            }
        }

        if ((
$i 2) && !$middle)
        {
            
// if we've come to the end without printing the separator...
            
if (!$printed_middle_end_separator)
            {
                
$printed_middle_end_separator true;
                
$str .= $page_separator $separator;
            }

            
// jump to the last three if we've not already done so..
            
$i max($i$pages-3);
        }

        if (
$i 0)
        {
            
$str .= $page_separator;
        }

        
// the offset for this page..
        
$this_start $hits $i;

        
// initialize the links..
        
$link_preface '';
        
$link_postface '';

        
// check if we're linking this page..
        
if (($i+1) != $current_page)
        {
            
$link_preface "<a href='" $url $url_argument_separator $offset_parameter_name "=" $this_start "&" $hits_parameter_name "=" $hits "' title='" $title_prefix " " . ($i 1) . "' class='" $class_inactive "'>";
            
$link_postface "</a>";
        }
        else
        {
            
$link_preface "<span class='" $class_active "'>";
            
$link_postface "</span>";
        }

        
$str .= $link_preface . ($i 1) . $link_postface;
    }

    if (
$current_page 1)
    {
        
$str "<a href='" $url $url_argument_separator $offset_parameter_name "=" . (($current_page-2)*$hits) . "&" $hits_parameter_name "=" $hits "' title='" $previous_page_title "'>&laquo;</a>" $page_separator $str;
    }

    if (
$current_page $pages)
    {
        
$str .= $page_separator "<a href='" $url $url_argument_separator $offset_parameter_name "=" . ($current_page*$hits) . "&" $hits_parameter_name "=" $hits "' title='" $next_page_title "'>&raquo;</a>";
    }

    return 
$str;
}
?>