<?php   if(!defined('DEDEINC')) exit('Request Error!');
/**
 
 *
 * @version        $Id: arcpagelist.lib.php 1 9:29 2010年7月6日Z tianya $
 * @package        DedeCMS.Taglib
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
 
function lib_arcpagelist(&$ctag, &$refObj)
{
    global $dsql;
    $attlist = "tagid|,style|1";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);
    
    $row = $dsql->GetOne("SELECT * FROM #@__arcmulti WHERE tagid='$tagid'");
    if(is_array($row))
    {
      $ids = explode(',', $row['arcids']);
    
      $totalnum = count($ids);
      $pagestr = '<div id="page_'.$tagid.'">';
      if($row['pagesize'] < $totalnum)
      {
        $pagestr .= multipage($totalnum, 1, $row['pagesize'], $tagid);
      } else {
          $pagestr .= '共1頁';
      }
      $pagestr .= '</div>';
      return $pagestr;
    } else {
      $pagestr = '<div id="page_'.$tagid.'">';
      $pagestr .= '沒有檢索到對應分頁';
      $pagestr .= '</div>';
        return $pagestr;
    }
}

/**
 *  分頁函數
 *
 * @access    public
 * @param     string  $allItemTotal  所有記錄
 * @param     string  $currPageNum  當前頁面數
 * @param     string  $pageSize  顯示條數
 * @param     string  $tagid  標籤ID
 * @return    string
 */
function multipage($allItemTotal, $currPageNum, $pageSize, $tagid='')
{
    if ($allItemTotal == 0) return "";

    //計算總頁數
    $pagesNum = ceil($allItemTotal/$pageSize);

    //第一頁顯示
    $firstPage = ($currPageNum <= 1) ? $currPageNum ."</b>&lt;&lt;" : "<a href='javascript:multi(1,\"{$tagid}\")' title='第1頁'>1&lt;&lt;</a>";

    //最後一頁顯示
    $lastPage = ($currPageNum >= $pagesNum)? "&gt;&gt;". $currPageNum : "<a href='javascript:multi(". $pagesNum . ",\"{$tagid}\")' title='第". $pagesNum ."頁'>&gt;&gt;". $pagesNum ."</a>";

    //上一頁顯示
    $prePage  = ($currPageNum <= 1) ? "上頁" : "<a href='javascript:multi(". ($currPageNum-1) . ",\"{$tagid}\")'  accesskey='p'  title='上一頁'>[上一頁]</a>";

    //下一頁顯示
    $nextPage = ($currPageNum >= $pagesNum) ? "下頁" : "<a href='javascript:multi(". ($currPageNum+1) .",\"{$tagid}\")' title='下一頁'>[下一頁]</a>";

    //按頁顯示
    $listNums = "";
    for ($i=($currPageNum-4); $i<($currPageNum+9); $i++) {
        if ($i < 1 || $i > $pagesNum) continue;
        if ($i == $currPageNum) $listNums.= "<a href='javascript:void(0)' class='thislink'>".$i."</a>";
        else $listNums.= " <a href='javascript:multi(". $i .",\"{$tagid}\")' title='". $i ."'>". $i ."</a> ";
    }

    $returnUrl = $listNums;
    return $returnUrl;
}