shopxo/extend/base/Page.php

218 lines
7.7 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace base;
/**
* 分页驱动
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class Page
{
private $page;
private $page_size;
2018-12-28 18:58:37 +08:00
private $total;
private $bt_number;
private $where;
2021-08-08 22:03:09 +08:00
private $not_fields;
2018-12-28 18:58:37 +08:00
private $page_total;
private $url;
private $html;
private $page_join;
2022-03-04 17:31:26 +08:00
private $tips_msg;
2018-12-28 18:58:37 +08:00
/**
* 构造方法
* @param [int] $params['page'] [页码]
* @param [int] $params['page_size / number'][每页数据条数]
* @param [int] $params['total'] [数据总数]
* @param [int] $params['bt_number'] [分页显示按钮个数]
* @param [array] $params['where'] [额外条件(键值对)]
* @param [array] $params['not_fields'] [不参与条件拼接的字段]
* @param [string] $params['url'] [url地址]
2018-12-28 18:58:37 +08:00
*/
2021-08-08 22:03:09 +08:00
public function __construct($params = [])
2018-12-28 18:58:37 +08:00
{
$this->page = max(1, isset($params['page']) ? intval($params['page']) : 1);
$this->page_size = empty($params['page_size']) ? (empty($params['number']) ? 10 : intval($params['number'])) : intval($params['page_size']);
2018-12-28 18:58:37 +08:00
$this->total = max(1, isset($params['total']) ? intval($params['total']) : 1);
$this->bt_number = isset($params['bt_number']) ? intval($params['bt_number']) : 2;
$this->where = (isset($params['where']) && is_array($params['where'])) ? $params['where'] : '';
2021-08-08 22:03:09 +08:00
$this->not_fields = (!empty($params['not_fields']) && is_array($params['not_fields'])) ? $params['not_fields'] : [];
2018-12-28 18:58:37 +08:00
$this->url = isset($params['url']) ? $params['url'] : '';
2022-03-04 17:31:26 +08:00
$this->tips_msg = empty($params['tips_msg']) ? '' : trim($params['tips_msg']);
2018-12-28 18:58:37 +08:00
$this->page_total = 1;
$this->html = '';
// 插件基础参数不参与条件
$this->not_fields[] = 'pluginsname';
$this->not_fields[] = 'pluginscontrol';
$this->not_fields[] = 'pluginsaction';
2018-12-28 18:58:37 +08:00
/* 参数设置 */
$this->SetParem();
}
/**
2023-01-19 17:44:03 +08:00
* 参数设置
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-01-19
* @desc description
2018-12-28 18:58:37 +08:00
*/
private function SetParem()
{
/* 防止超出最大页码数 */
$this->page_total = ceil($this->total/$this->page_size);
2018-12-28 18:58:37 +08:00
if($this->page > $this->page_total) $this->page = $this->page_total;
/* url是否包含问号 */
$state = stripos($this->url, '?');
/* 额外条件url设置 */
if(!empty($this->where) && is_array($this->where))
{
$tmp = true;
foreach($this->where as $k=>$v)
{
2021-08-08 22:03:09 +08:00
if(!in_array($k, $this->not_fields) && !is_array($v))
2018-12-28 18:58:37 +08:00
{
2022-01-25 15:54:57 +08:00
if($k == 'page')
{
continue;
}
2022-11-30 12:01:50 +08:00
$k = empty($k) ? $k : htmlspecialchars($k);
$v = empty($v) ? $v : htmlspecialchars($v);
2022-01-25 15:54:57 +08:00
2018-12-28 18:58:37 +08:00
if($tmp)
{
$this->url .= ($state === false) ? '?' : '&';
$this->url .= $k.'='.$v;
$tmp = false;
} else {
$this->url .= '&'.$k.'='.$v;
}
}
}
$this->page_join = ($tmp == false) ? '&' : (($state === false) ? '?' : '&');
2018-12-28 18:58:37 +08:00
} else {
$this->page_join = ($state === false) ? '?' : '&';
2018-12-28 18:58:37 +08:00
}
}
/**
2023-01-19 17:44:03 +08:00
* 获取生成好的分页代码
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-01-19
* @desc description
2018-12-28 18:58:37 +08:00
*/
public function GetPageHtml()
{
$before_disabled = ($this->page > 1) ? '' : ' class="am-disabled"';
$after_disabled = ($this->page > 0 && $this->page < $this->page_total) ? '' : ' class="am-disabled"';
$this->html .= '<ul class="am-pagination am-pagination-centered">';
$this->html .= '<li '.$before_disabled.'>';
$this->html .= '<a href="'.$this->url.$this->page_join.'page=1" class="am-radius am-icon-angle-double-left"></a>';
2018-12-28 18:58:37 +08:00
$this->html .= '</li>';
$this->html .= '<li '.$before_disabled.'>';
$this->html .= '<a href="'.$this->url.$this->page_join.'page='.($this->page-1).'" class="am-radius am-icon-angle-left"></a>';
2018-12-28 18:58:37 +08:00
$this->html .= '</li>';
$this->html .= $this->GetButtonNumberHtml();
$this->html .= '<li '.$after_disabled.'>';
$this->html .= '<a href="'.$this->url.$this->page_join.'page='.($this->page+1).'" class="am-radius am-icon-angle-right"></a>';
2018-12-28 18:58:37 +08:00
$this->html .= '</li>';
$this->html .= '<li '.$after_disabled.'>';
$this->html .= '<a href="'.$this->url.$this->page_join.'page='.$this->page_total.'" class="am-radius am-icon-angle-double-right"></a>';
2018-12-28 18:58:37 +08:00
$this->html .= '</li>';
2023-02-12 22:14:47 +08:00
$this->html .= '<span class="am-margin-left-sm">'.MyLang('common_extend.base.page.each_page_name').'</span>';
$this->html .= '<input type="text" min="1" data-is-clearout="0" class="am-form-field am-inline-block am-text-center am-margin-horizontal-xs am-radius pagination-input" value="'.$this->page_size.'" onchange="window.location.href=\''.str_replace(['&page_size='.$this->page_size, 'page_size='.$this->page_size.'&'], '', $this->url).$this->page_join.'page_size=\'+(isNaN(parseInt(this.value)) ? 10 : parseInt(this.value) || 10);" onclick="this.select()" />';
2023-02-18 23:25:00 +08:00
$this->html .= '<span>'.MyLang('common_extend.base.page.page_strip').'</span>';
2023-02-12 22:14:47 +08:00
$this->html .= '<span class="am-margin-left-sm">'.MyLang('common_extend.base.page.jump_to_text').'</span>';
$this->html .= '<input type="text" min="1" data-is-clearout="0" class="am-form-field am-inline-block am-text-center am-margin-horizontal-xs am-radius pagination-input" value="'.$this->page.'" onchange="window.location.href=\''.$this->url.$this->page_join.'page=\'+(isNaN(parseInt(this.value)) ? 1 : parseInt(this.value) || 1);" onclick="this.select()" />';
2023-02-12 22:14:47 +08:00
$this->html .= '<span>'.MyLang('common_extend.base.page.page_unit').'</span>';
2018-12-28 18:58:37 +08:00
$this->html .= '<div>';
2023-02-12 22:14:47 +08:00
$this->html .= '<span>'.MyLang('common_extend.base.page.data_total', ['total'=>$this->total]).'</span>';
$this->html .= '<span class="am-margin-left-sm">'.MyLang('common_extend.base.page.page_total', ['total'=>$this->page_total]).'</span>';
2022-03-04 17:31:26 +08:00
if(!empty($this->tips_msg))
{
$this->html .= '<span class="am-margin-left-sm">'.$this->tips_msg.'</span>';
2022-03-04 17:31:26 +08:00
}
2018-12-28 18:58:37 +08:00
$this->html .= '</div>';
$this->html .= '</ul>';
return $this->html;
}
/**
2023-01-19 17:44:03 +08:00
* 获取button显示个数的html
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-01-19
* @desc description
* @return 按钮个数html代码
2018-12-28 18:58:37 +08:00
*/
private function GetButtonNumberHtml()
{
$html_before = '';
$html_after = '';
$html_page = '<li class="am-active"><a class="am-radius">'.$this->page.'</a></li>';
if($this->bt_number > 0)
{
/* 前按钮 */
if($this->page > 1)
{
$total = ($this->page-$this->bt_number < 1) ? 1 : $this->page-$this->bt_number;
for($i=$this->page-1; $i>=$total; $i--)
{
$html_before = '<li><a href="'.$this->url.$this->page_join.'page='.$i.'" class="am-radius">'.$i.'</a></li>'.$html_before;
2018-12-28 18:58:37 +08:00
}
}
/* 后按钮 */
if($this->page_total > $this->page)
{
$total = ($this->page+$this->bt_number > $this->page_total) ? $this->page_total : $this->page+$this->bt_number;
for($i=$this->page+1; $i<=$total; $i++)
{
$html_after .= '<li><a href="'.$this->url.$this->page_join.'page='.$i.'" class="am-radius">'.$i.'</a></li>';
2018-12-28 18:58:37 +08:00
}
}
}
return $html_before.$html_page.$html_after;
}
/**
2023-01-19 17:44:03 +08:00
* 获取分页起始值
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-01-19
* @desc description
2018-12-28 18:58:37 +08:00
*/
public function GetPageStarNumber()
{
return intval(($this->page-1)*$this->page_size);
2018-12-28 18:58:37 +08:00
}
}
?>