<?php

/*
 * ----------------------------------------------------
 * 程序名称: 牛叉广告管理系统(NiuXams)
 * 程序功能: 实现专业的广告投放、管理和统计等管理系统！
 * 程序开发: 牛叉软件(NiuXSoft.com)
 * 版权所有: [NiuXams] (C)2013-2099 NiuXSoft.com
 * 官方网站: www.niuxsoft.com  Email: niuxsoft@163.com
 * ----------------------------------------------------
 * 使用条款:
 * 1.为构建创新型经济社会,请尊重保护知识产权！
 * 2.该软件个人非商业用途免费使用.
 * 3.免费使用禁止修改版权信息和官方推广链接.
 * 4.禁止任何衍生版本.禁止任何非法修改源代码贩卖.
 * ----------------------------------------------------
 */
class Fenye {

    public $total;
    public $prePageListNums;
    public $currentPage;
    public $subPageNums;
    public $subPageLink;
    public $styleType;
    public $pageNums;
    public $pageArray;
    public $result;

    function __construct($total, $prePageListNums, $currentPage, $subPageNums, $subPageLink, $styleType = 1) {
        $this->total = intval($total);
        $this->prePageListNums = intval($prePageListNums);
        $this->pageNums = ceil($this->total / $this->prePageListNums);
        $this->currentPage = (is_numeric($currentPage) && (intval($currentPage) > 1)) ? intval($currentPage) : 1;
        $this->currentPage = ($this->currentPage > $this->pageNums) ? $this->pageNums : $this->currentPage;
        $this->subPageNums = intval($subPageNums);
        $this->subPageLink = $subPageLink;
        $this->styleType = intval($styleType);
        $this->pageArray = array();
        switch ($this->styleType) {
            case 1:
                $this->outputStyle1();
                break;
            case 2:
                $this->outputStyle2();
                break;
            case 3:
                $this->outputStyle3();
                break;
            case 4:
                $this->outputStyle4();
                break;
            case 5:
                $this->outputStyle5();
                break;
            case 6:
                $this->outputStyle6();
                break;
            default :
                $this->createFenye(0);
                break;
        }
    }

    function __destruct() {
        unset($this->total);
        unset($this->prePageListNums);
        unset($this->currentPage);
        unset($this->subPageNums);
        unset($this->subPageLink);
        unset($this->styleType);
        unset($this->pageNums);
        unset($this->pageArray);
        unset($this->result);
    }

    function createPageArray() {
        if ($this->pageNums < $this->subPageNums) {
            for ($i = 0; $i < $this->pageNums; $i++) {
                $this->pageArray[$i] = $i + 1;
            }
        } elseif ($this->currentPage <= ceil($this->subPageNums / 2)) {
            for ($i = 0; $i < $this->subPageNums; $i++) {
                $this->pageArray[$i] = $i + 1;
            }
        } elseif ($this->currentPage > $this->pageNums - $this->subPageNums + 1) {
            for ($i = 0; $i < $this->subPageNums; $i++) {
                $this->pageArray[$i] = (($this->pageNums - $this->subPageNums) + 1) + $i;
            }
        } else {
            for ($i = 0; $i < $this->subPageNums; $i++) {
                $this->pageArray[$i] = ($this->currentPage - ceil($this->subPageNums / 2) + 1) + $i;
            }
        }
        return $this->pageArray;
    }

    function createFenye($fkh = 1, $border = 0, $corner = 0) {
        $str = '';
        $qfkh = $fkh ? '[' : '';
        $hfkh = $fkh ? ']' : '';
        $hc = $fkh ? ' style="color:red;font-weight:bold;"' : '';
        $defaultStyle = $border ? ' ui-state-default' : '';
        $currentStyle = $border ? ' ui-state-active' : '';
        $cornerStyle = $corner ? ' ui-corner-all' : '';
        if ($this->currentPage > 1) {
            $firstPageUrl = $this->subPageLink . '1';
            $prewPageUrl = $this->subPageLink . ($this->currentPage - 1);
            $qslh = '';
            if ($this->pageNums > $this->subPageNums && $this->currentPage > ceil($this->subPageNums / 2)) {
                $qslh = '...';
            }
            $str .= $qfkh . '<a class="link' . $defaultStyle . $cornerStyle . '" href="' . $firstPageUrl . '">首页</a>' . $hfkh;
            $str .= $qfkh . '<a class="link' . $defaultStyle . $cornerStyle . '" href="' . $prewPageUrl . '">上页</a>' . $hfkh . $qslh;
        } else {
            $str .= $qfkh . '<span class="current' . $currentStyle . $cornerStyle . '">首页</span>' . $hfkh;
            $str .= $qfkh . '<span class="current' . $currentStyle . $cornerStyle . '">上页</span>' . $hfkh;
        }
        $this->createPageArray();
        for ($i = 0, $len = count($this->pageArray); $i < $len; $i++) {
            $s = $this->pageArray[$i];
            if ($s == $this->currentPage) {
                $str .= $qfkh . '<span class="current' . $currentStyle . $cornerStyle . '"' . $hc . '>' . $s . '</span>' . $hfkh;
            } else {
                $url = $this->subPageLink . $s;
                $str .= $qfkh . '<a class="link' . $defaultStyle . $cornerStyle . '" href="' . $url . '">' . $s . '</a>' . $hfkh;
            }
        }
        if ($this->currentPage < $this->pageNums) {
            $lastPageUrl = $this->subPageLink . $this->pageNums;
            $nextPageUrl = $this->subPageLink . ($this->currentPage + 1);
            $hslh = '';
            if ($this->pageNums > $this->subPageNums && ($this->currentPage <= ceil($this->subPageNums / 2) || $this->currentPage <= ($this->pageNums - $this->subPageNums) + 1)) {
                $hslh = '...';
            }
            $str .= $hslh . $qfkh . '<a class="link' . $defaultStyle . $cornerStyle . '" href="' . $nextPageUrl . '">下页</a>' . $hfkh;
            $str .= $qfkh . '<a class="link' . $defaultStyle . $cornerStyle . '" href="' . $lastPageUrl . '">尾页</a>' . $hfkh;
        } else {
            $str .= $qfkh . '<span class="current' . $currentStyle . $cornerStyle . '">下页</span>' . $hfkh;
            $str .= $qfkh . '<span class="current' . $currentStyle . $cornerStyle . '">尾页</span>' . $hfkh;
        }
        $this->result = $str;
    }

    function outputStyle1() {
        $this->createFenye();
    }

    function outputStyle2() {
        $str = ' 共<span class="total">' . $this->total . '</span>条记录 ';
        $str .= '每页显示<input class="limit ui-widget-content" name="limit" type="text" title="输入每页显示数 按回车或失去焦点" value="' . $this->prePageListNums . '">条 ';
        $str .= '当前第<input class="page ui-widget-content" name="page" type="text" title="输入页码 按回车或失去焦点快速跳转" value="' . $this->currentPage . '">/' . $this->pageNums . '页 ';
        $this->createFenye();
        $this->result = $str . $this->result;
    }

    function outputStyle3() {
        $this->createFenye(0, 1, 0);
    }

    function outputStyle4() {
        $str = ' 共<span class="total ui-state-active">' . $this->total . '</span>条记录 ';
        $str .= '每页显示<input class="limit ui-widget-content" name="limit" title="输入每页显示数 按回车或失去焦点" value="' . $this->prePageListNums . '">条 ';
        $str .= '当前第<input class="page ui-widget-content" name="page" title="输入页码 按回车或失去焦点快速跳转" value="' . $this->currentPage . '">/<span class="pageNums ui-state-active">' . $this->pageNums . '</span>页 ';
        $this->createFenye(0, 1, 0);
        $this->result = $str . $this->result;
    }

    function outputStyle5() {
        $this->createFenye(0, 1, 1);
    }

    function outputStyle6() {
        $str = ' 共<span class="total ui-state-active ui-corner-all">' . $this->total . '</span>条记录 ';
        $str .= '每页显示<input class="limit ui-widget-content ui-corner-all" name="limit" title="输入每页显示数 按回车或失去焦点" value="' . $this->prePageListNums . '">条 ';
        $str .= '当前第<input class="page ui-widget-content ui-corner-all" name="page" title="输入页码 按回车或失去焦点快速跳转" value="' . $this->currentPage . '">/<span class="pageNums ui-state-active ui-corner-all">' . $this->pageNums . '</span>页 ';
        $this->createFenye(0, 1, 1);
        $this->result = $str . $this->result;
    }

}
