PHP Classes

File: plugins/block.p_iterator.php

Recommend this page to a friend!
  Classes of Anderson A. Meggiolaro   Paginator Iterator   plugins/block.p_iterator.php   Download  
File: plugins/block.p_iterator.php
Role: Auxiliary script
Content type: text/plain
Description: Main Block
Class: Paginator Iterator
Iterate over listings split in page using Smarty
Author: By
Last change:
Date: 15 years ago
Size: 5,265 bytes
 

Contents

Class file image Download
<?php

/**
 * Smarty {p_iterator}{/p_iterator} block plugin
 *
 * Paginator Iterator Plugin
 *
 * Simple paginator plugin with iterator features
 *
 *
 * Simple usage:
 -------------------------------------------------------------------------------------------------------
 | index.php
 |
 | include('PaginatorIterator.php');
 | PaginatorIterator::setConnection(new PDO('mysql:dbname=j15;host=127.0.0.1', 'root', '123456'));
 |
 | include('Smarty-2.6.19/Smarty.class.php');
 | $smarty=new Smarty;
 |
 | $smarty->assign('ds', "select * from jos_menu limit :LIMIT offset :OFFSET");
 | $smarty->assign('numrows', "select count(*) from jos_menu");
 |
 | $smarty->display("index.tpl");
 -------------------------------------------------------------------------------------------------------
 | index.tpl
 |
 | {p_iterator id='rec' ds=$ds numrows=$numrows pageno=$smarty.get.pageno numlinks=10 rowsperpage=10}
 |
 | {p_header}
 | <a href="index.php?pageno={$rec.prev}">Prev</a>
 | <a href="index.php?pageno={$rec.next}">Next</a>
 |
 | <table border="1" width="100%">
 | {/p_header}
 |
 | {p_item}
 | <tr>
 | <td>{$rec.ds.name}</td>
 | <td>{$rec.ds.link}</td>
 | </tr>
 | {/p_item}
 |
 | {p_footer}
 | </table>
 | {/p_footer}
 |
 | {/p_iterator}
 -------------------------------------------------------------------------------------------------------
 *
 * @type block
 * @name paginator
 * @version 0.1
 * @see
 *
 * @author Anderson A. Meggiolaro <ameggiolaro@gmail.com>
 * @license LGPL
 *
 * @param id REQUIRED
 * @param ds REQUIRED
 * @param numrows REQUIRED
 * @param pageno REQUIRED default: 1
 * @param numlinks default: 10
 * @param rowsperpage default: 10
 * @param strip default: true
 */

function smarty_block_p_iterator($params, $content, &$smarty, &$repeat)
{
    static
$flag;

    if(
is_null($content)) {
        if(!
$smarty->_paginator)
           
$smarty->_paginator=array();
        return;
    }
   
    if(!
$params['id']) {
       
$smarty->trigger_error("paginator: missing 'id' attribute.");
    }

    if(!
$params['ds']) {
       
$smarty->trigger_error("paginator: missing 'ds' attribute.");
    }

    if(!
$params['numrows']) {
       
$smarty->trigger_error("paginator: missing 'numrows' attribute.");
    }

    if(!
array_key_exists('pageno', $params)) {
       
$smarty->trigger_error("paginator: missing 'pageno' attribute.");
    }

    if(!
$flag[$params['id']])
    {
       
$smarty->_paginator[$params['id']]['counter']=0;

       
$paginator=new PaginatorIterator;
       
$paginator->ds=$params['ds'];

        if(
$params['rowsperpage'])
           
$paginator->rowsperpage=$params['rowsperpage'];
   
       
$paginator->numrows=$params['numrows'];

        if(
$params['numlinks'])
           
$paginator->numlinks=$params['numlinks'];
       
       
$paginator->pageno=$params['pageno'];
       
$smarty->assign($params['id'], $paginator->paginate());

       
$smarty->_tpl_vars[$params['id']]['data']=$smarty->_tpl_vars[$params['id']]['ds'][$smarty->_paginator[$params['id']]['counter']];
       
$smarty->_paginator[$params['id']]['is_alternating']=($smarty->_paginator[$params['id']]['counter']%2==0?false:true);

       
$flag[$params['id']]=1;
       
$repeat=true;
        return;
    }
    else
       
$repeat=false;

   
$has_more_rows=$smarty->_paginator[$params['id']]['counter']<count($smarty->_tpl_vars[$params['id']]['ds'])-1;
    if(
$has_more_rows)
    {
       
$smarty->_paginator[$params['id']]['counter']++;
       
$smarty->_tpl_vars[$params['id']]['data']=$smarty->_tpl_vars[$params['id']]['ds'][$smarty->_paginator[$params['id']]['counter']];
       
$smarty->_paginator[$params['id']]['is_alternating']=($smarty->_paginator[$params['id']]['counter']%2==0?false:true);
       
$repeat=true;
        return;
    }
    else
       
$repeat=false;

    if(!@
$params['strip'])
    {
       
$_return=$smarty->_paginator[$params['id']]['header'].
            (
$smarty->_tpl_vars[$params['id']]['numrows']>0?$smarty->_paginator[$params['id']]['content']:$smarty->_paginator[$params['id']]['empty']).
           
$smarty->_paginator[$params['id']]['footer'];

        if(!
$has_more_rows)
           
$smarty->_paginator[$params['id']]=null;

        return
preg_replace('!\s+!', ' ', $_return);
    }
    else
    {
       
$_return=$smarty->_paginator[$params['id']]['header'].
            (
$smarty->_tpl_vars[$params['id']]['numrows']>0?$smarty->_paginator[$params['id']]['content']:$smarty->_paginator[$params['id']]['empty']).
           
$smarty->_paginator[$params['id']]['footer'];
       
        if(!
$has_more_rows)
           
$smarty->_paginator[$params['id']]=null;

        return
$_return;
    }
}

// from 'Banded Report Generator Framework' plugin [at]-> http://smarty.incutio.com/?page=BandedReportGenerator
if(!function_exists('smarty_get_parent_plugin_params'))
{
    function &
smarty_get_parent_plugin_params(&$smarty, $parent_plugin_name)
    {
        for (
$i=count($smarty->_tag_stack)-1; $i>=0; $i--) {
           
$tag_name = $smarty->_tag_stack[$i][0];
            if (
$tag_name == $parent_plugin_name) break;
        }

        if (
$i<0) {
           
/* $parent_plugin_name not found */
           
list($plugin_name, $plugin_params) = $smarty->_tag_stack[count($smarty->_tag_stack)-1];
           
$smarty->trigger_error("{".$plugin_name."}: not inside {".$parent_plugin_name."}-context", E_USER_ERROR);
            return;
        } else {
            return
$smarty->_tag_stack[$i][1];
        }
    }
}

?>