<?php
$pwd = getcwd();
$path .= "$pwd/class:";
$path .= "$pwd/class/pear:";
ini_set("include_path",$path);
require_once 'MySmarty.class.php';
require_once 'AppPager.class.php';

define('_LIMIT_LIST_', 20);  // 1ページの表示数

function main(){
    $Smarty = new MySmarty();
    
    /**
     * csvファイルの読込み処理
     */
    if(!$handle = fopen("dummy_data.csv", "r")){
	echo "Cannot open file (dummy_data.csv)";
	exit;
    }
    while (($tmp = fgetcsv($handle, 1000, ",")) !== FALSE){
	$data[] = $tmp;
    }
    fclose($handle);
    $list[] = array_shift($data); // タイトルデータは除外する。
    
    /**
     * Pagerの処理
     */
    /**  Pager optionの設定 {@see http://www.pear.php.net/manual/ja/package.html.pager.factory.php} */
    $pager_option = array(
			  "totalItems" => count($data),  //データ総数
			  "perPage"    => _LIMIT_LIST_,  //1ページの表示数
			  "path"       => "",
			  'fileName'   => "javascript:commonAjax.update('pager2.php','contents','pageID=%d');",
			  "httpMethod" => "GET",
			  "append"     => false,
			  );
    $Pager = new AppPager($pager_option);
    // 表示データのオフセット値を取得
    $offset = $Pager->GetDataOffset();
    // Pagerオブジェクトをアサイン
    $Smarty->assign_by_ref("Pager",$Pager);

    // 表示するデータをセット
    for($i=0;$i<_LIMIT_LIST_;$i++){
	$list[] = $data[$offset++];
    }
    $Smarty->assign('list',$list);
    
    if($_REQUEST['ajax'] == 1){
	$Smarty->display('pager2_table.tpl');
    }
    else{
	$Smarty->assign('title',"ajaxなPagerサンプル");
	$Smarty->display('pager2.tpl');
    }
}

main();

?>