/**
 * [JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename wap_global.func.php $
 *
 * @Author http://www.jishigou.net $
 *
 * @Date 2014-01-03 17:38:36 2110477426 1492392824 6427 $
 */
function wap_page($total_record, $per_page_num, $url = '', $_config = array(), $per_page_nums = "")
{
    $SystemConfig = jconf::get();
    $result = array();
    $url = wap_iconv($url);
    $total_record = intval($total_record);
    $per_page_num = intval($per_page_num);
    if ($per_page_num < 1) {
        $per_page_num = 10;
    }
    $config['total_page'] = max(0, (int) (isset($_config['total_page']) ? $_config['total_page'] : $SystemConfig['total_page_default']));
    $config['page_display'] = isset($_config['page_display']) ? (int) $_config['page_display'] : 3;
    $config['char'] = isset($_config['char']) ? (string) $_config['char'] : ' ';
    $config['url_postfix'] = isset($_config['url_postfix']) ? (string) $_config['url_postfix'] : '';
    $config['extra'] = isset($_config['extra']) ? (string) $_config['extra'] : '';
    $config['idencode'] = (bool) $_config['idencode'];
    $config['var'] = isset($_config['var']) ? (string) $_config['var'] : 'page';
    $config['return'] = isset($_config['return']) ? (string) $_config['return'] : 'html';
    extract($config);
    $total_page = ceil($total_record / $per_page_num);
    if ($config['total_page'] > 1 && $total_page > $config['total_page']) {
        $total_page = $config['total_page'];
    }
    $result['total_page'] = $total_page;
    $current_page = $_GET[$var] ? $_GET[$var] : $_POST[$var];
    $current_page = max(1, (int) (true == $idencode ? iddecode($current_page) : $current_page));
    $current_page = $total_page > 0 && $current_page > $total_page ? $total_page : $current_page;
    $result['current_page'] = $current_page;
    $result['title_postfix'] = $current_page > 1 ? "_第{$current_page}页" : "";
    $result['offset'] = (int) (($current_page - 1) * $per_page_num);
    $result['limit'] = " LIMIT " . $result['offset'] . ",{$per_page_num} ";
    if (isset($result[$return])) {
        return $result[$return];
    }
    if ('' == $url) {
        $request = count($_POST) ? array_merge($_GET, $_POST) : $_GET;
        $query_string = '';
        foreach ($request as $_var => $_val) {
            if (is_string($_val) && $var !== $_var) {
                $query_string .= "&{$_var}=" . urlencode($_val);
            }
        }
        $url = '?' . ($result['query_string'] = trim($query_string, '&'));
    }
    $p_val = "V01001page10010V";
    if ('/#' != $url) {
        $url = '' == $url ? "?{$var}={$p_val}" : ($url_no_page = false !== strpos($url, "&{$var}=") ? preg_replace("/\\&?{$var}\\=[^\\&]*/i", '', $url) : $url) . "&amp;{$var}={$p_val}";
        if ($jishigou_rewrite) {
            $url_no_page = $jishigou_rewrite->formatURL($url_no_page, false);
            $url = $jishigou_rewrite->formatURL($url, false);
        }
    } else {
        $url_no_page = $url;
    }
    $result['url'] = $url;
    if (isset($result[$return])) {
        return $result[$return];
    }
    $html = '';
    if ($total_record > $per_page_num) {
        $halfper = (int) ($config['page_display'] / 2);
        $html = $current_page - 1 >= 1 ? "\n<a href='{$url_no_page}{$url_postfix}'  {$extra}>首页</a>{$char}\n<a href='" . (1 == ($previous_page = $current_page - 1) ? $url_no_page : str_replace($p_val, true === $idencode ? idencode($previous_page) : $previous_page, $url)) . "{$url_postfix}' {$extra}>上一页</a>{$char}" : "首页{$char}上一页{$char}";
        for ($i = $current_page - $halfper, $i > 0 or $i = 1, $j = $current_page + $halfper, $j < $total_page or $j = $total_page; $i <= $j; $i++) {
            $html .= $i == $current_page ? "\n<B>" . $i . "</B>{$char}" : "\n<a href='" . (1 == $i ? $url_no_page : str_replace($p_val, true === $idencode ? idencode($i) : $i, $url)) . "{$url_postfix}'  {$extra}>" . $i . "</a>{$char}";
        }
        $html .= ($next_page = $current_page + 1) > $total_page ? "下一页{$char}尾页" : "\n<a href='" . str_replace($p_val, true === $idencode ? idencode($next_page) : $next_page, $url) . "{$url_postfix}'  {$extra}>下一页</a>{$char}\n<a href='" . str_replace($p_val, true === $idencode ? idencode($total_page) : $total_page, $url) . "{$url_postfix}'  {$extra}>尾页</a>";
        if (!empty($per_page_nums)) {
            $per_page_num_list = is_array($per_page_nums) ? $per_page_nums : explode(" ", $per_page_nums);
            $current_url = str_replace($p_val, true === $idencode ? idencode($current_page) : $current_page, $url) . $url_postfix;
            $pn_postfix = $jishigou_rewrite ? $jishigou_rewrite->argSeparator . "pn" . $jishigou_rewrite->varSeparator : "&pn=";
            $per_page_num_select = "<select name='per_page_num' onchange=\"window.location='{$current_url}{$pn_postfix}'+this.value\">";
            foreach ($per_page_num_list as $_per_page_num) {
                $selected = $_per_page_num == $per_page_num ? "selected" : "";
                $per_page_num_select .= "<option value={$_per_page_num} {$selected}>{$_per_page_num}";
            }
            $per_page_num_select .= "</select>";
        } else {
            $per_page_num_select = "<B>{$per_page_num}</B>";
        }
        $html = "<div id='page'>{$html}</div>";
    }
    $result['html'] = $html;
    if (isset($result[$return])) {
        return $result[$return];
    }
    return $result;
}
function page($total_record, $per_page_num, $url = '', $_config = array(), $per_page_nums = "")
{
    if (true === IN_JISHIGOU_INDEX || true === IN_JISHIGOU_AJAX) {
        global $jishigou_rewrite;
    }
    $sys_config = jconf::get();
    if (true === IN_JISHIGOU_ADMIN && isset($sys_config['total_page_default'])) {
        unset($sys_config['total_page_default']);
    }
    $result = array();
    $total_record = intval($total_record);
    $per_page_num = intval($per_page_num);
    if ($per_page_num < 1) {
        $per_page_num = 10;
    }
    $config['total_page'] = max(0, (int) (isset($_config['total_page']) ? $_config['total_page'] : $sys_config['total_page_default']));
    $config['page_display'] = isset($_config['page_display']) ? (int) $_config['page_display'] : 5;
    $config['char'] = isset($_config['char']) ? (string) $_config['char'] : ' ';
    $config['url_postfix'] = isset($_config['url_postfix']) ? (string) $_config['url_postfix'] : '';
    $config['extra'] = isset($_config['extra']) ? (string) $_config['extra'] : '';
    $config['idencode'] = (bool) $_config['idencode'];
    $config['var'] = isset($_config['var']) ? (string) $_config['var'] : 'page';
    $config['return'] = isset($_config['return']) ? (string) $_config['return'] : 'html';
    if (!$url) {
        $config['url'] = $_config['page_url'] ? $_config['page_url'] : ($_config['page_link'] ? $_config['page_link'] : $_config['url']);
    }
    $config['per_page_nums'] = $per_page_nums ? $per_page_nums : $_config['per_page_nums'];
    extract($config);
    $total_page = ceil($total_record / $per_page_num);
    if ($config['total_page'] > 1 && $total_page > $config['total_page']) {
        $total_page = $config['total_page'];
    }
    $result['total_record'] = $total_record;
    $result['total_page'] = $total_page;
    $current_page = $_GET[$var] ? $_GET[$var] : $_POST[$var];
    $current_page = max(1, (int) (true == $idencode ? iddecode($current_page) : $current_page));
    $current_page = $total_page > 0 && $current_page > $total_page ? $total_page : $current_page;
    $result['current_page'] = $current_page;
    $result['title_postfix'] = $current_page > 1 ? "_第{$current_page}页" : "";
    $result['offset'] = (int) (($current_page - 1) * $per_page_num);
    $result['limit'] = " LIMIT " . $result['offset'] . ",{$per_page_num} ";
    if (isset($result[$return])) {
        return $result[$return];
    }
    if ('' == $url) {
        $request = count($_POST) ? array_merge($_GET, $_POST) : $_GET;
        $query_string = '';
        foreach ($request as $_var => $_val) {
            if (is_string($_val) && $var !== $_var) {
                $query_string .= "&{$_var}=" . urlencode($_val);
            }
        }
        $url = '?' . ($result['query_string'] = trim($query_string, '&'));
    }
    $p_val = "V01001page10010V";
    if ('/#' != $url) {
        $url = '' == $url ? "?{$var}={$p_val}" : ($url_no_page = false !== strpos($url, "&{$var}=") ? preg_replace("/\\&?{$var}\\=[^\\&]*/i", '', $url) : $url) . "&{$var}={$p_val}";
        if ($jishigou_rewrite) {
            $url_no_page = $jishigou_rewrite->formatURL($url_no_page, false);
            $url = $jishigou_rewrite->formatURL($url, false);
        }
    } else {
        $url_no_page = $url;
    }
    $result['url'] = $url;
    if (isset($result[$return])) {
        return $result[$return];
    }
    $html = '';
    if ($total_record > $per_page_num) {
        $halfper = (int) ($config['page_display'] / 2);
        $html = $current_page - 1 >= 1 ? "\n<a href='{$url_no_page}{$url_postfix}' title=1 {$extra}>首页</a>{$char}\n<a href='" . (1 == ($previous_page = $current_page - 1) ? $url_no_page : str_replace($p_val, true === $idencode ? idencode($previous_page) : $previous_page, $url)) . "{$url_postfix}' title={$previous_page} {$extra}>上一页</a>{$char}" : "首页{$char}上一页{$char}";
        for ($i = $current_page - $halfper, $i > 0 or $i = 1, $j = $current_page + $halfper, $j < $total_page or $j = $total_page; $i <= $j; $i++) {
            $html .= $i == $current_page ? "\n<B>" . $i . "</B>{$char}" : "\n<a href='" . (1 == $i ? $url_no_page : str_replace($p_val, true === $idencode ? idencode($i) : $i, $url)) . "{$url_postfix}' title={$i} {$extra}>" . $i . "</a>{$char}";
        }
        $html .= ($next_page = $current_page + 1) > $total_page ? "下一页{$char}尾页" : "\n<a href='" . str_replace($p_val, true === $idencode ? idencode($next_page) : $next_page, $url) . "{$url_postfix}' title={$next_page} {$extra}>下一页</a>{$char}\n<a href='" . str_replace($p_val, true === $idencode ? idencode($total_page) : $total_page, $url) . "{$url_postfix}' title={$total_page} {$extra}>尾页</a>";
        $html .= "<input type='text' id='htmlpagenum' value='" . $current_page . "' style='width:25px;margin:0 2px 0 5px;' onKeyDown=\"if(event.keyCode==13) window.location='" . str_replace($p_val, '', $url) . "'+this.value\"><input type='button' value='确定' class='u-btn button' onclick=\"window.location='" . str_replace($p_val, '', $url) . "'+document.getElementById('htmlpagenum').value\">";
        if (!empty($per_page_nums)) {
            $per_page_num_list = is_array($per_page_nums) ? $per_page_nums : explode(" ", $per_page_nums);
            $current_url = str_replace($p_val, true === $idencode ? idencode($current_page) : $current_page, $url) . $url_postfix;
            $pn_postfix = $jishigou_rewrite ? $jishigou_rewrite->argSeparator . "pn" . $jishigou_rewrite->varSeparator : "&pn=";
            $per_page_num_select = "<select name='per_page_num' onchange=\"window.location='{$current_url}{$pn_postfix}'+this.value\">";
            foreach ($per_page_num_list as $_per_page_num) {
                $selected = $_per_page_num == $per_page_num ? "selected" : "";
                $per_page_num_select .= "<option value={$_per_page_num} {$selected}>{$_per_page_num}";
            }
            $per_page_num_select .= "</select>";
        } else {
            $per_page_num_select = "<i>{$per_page_num}</i>";
        }
        $html = "<div id='page'> {$html} &nbsp;每页{$per_page_num_select}条/共<i>{$total_record}</i>条</div>";
    }
    $result['html'] = $html;
    if (isset($result[$return])) {
        return $result[$return];
    }
    return $result;
}