国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

312 lignes
12KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 系统列表分页
  5. *
  6. * @version $id:datalistcp.class.php 3 17:02 2010年7月9日 tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  10. * @link https://www.dedebiz.com
  11. */
  12. require_once(DEDEINC.'/dedetemplate.class.php');
  13. //分页说明
  14. $lang_pre_page = '上页';
  15. $lang_next_page = '下页';
  16. $lang_index_page = '首页';
  17. $lang_end_page = '末页';
  18. $lang_record_number = '条';
  19. $lang_page = '页';
  20. /**
  21. * DataListCP
  22. *
  23. * @package DedeBIZ.Libraries
  24. */
  25. class DataListCP
  26. {
  27. var $dsql;
  28. var $tpl;
  29. var $pageNO;
  30. var $totalPage;
  31. var $totalResult;
  32. var $pagesize;
  33. var $getValues;
  34. var $sourceSql;
  35. var $isQuery;
  36. var $queryTime;
  37. /**
  38. * 用指定的文档id进行初始化
  39. *
  40. * @access public
  41. * @param string $tplfile 模板文件
  42. * @return string
  43. */
  44. function __construct($tplfile = '')
  45. {
  46. global $dsql;
  47. $this->sourceSql = '';
  48. $this->pagesize = 30;
  49. $this->queryTime = 0;
  50. $this->getValues = array();
  51. $this->isQuery = false;
  52. $this->totalResult = 0;
  53. $this->totalPage = 0;
  54. $this->pageNO = 0;
  55. $this->dsql = $dsql;
  56. $this->SetVar('ParseEnv', 'datalist');
  57. $this->tpl = new DedeTemplate();
  58. if ($GLOBALS['cfg_tplcache'] == 'N') {
  59. $this->tpl->isCache = false;
  60. }
  61. if ($tplfile != '') {
  62. $this->tpl->LoadTemplate($tplfile);
  63. }
  64. }
  65. /**
  66. * 兼容PHP4版本
  67. *
  68. * @access private
  69. * @param string $tplfile 模板文件
  70. * @return void
  71. */
  72. function DataListCP($tplfile = '')
  73. {
  74. $this->__construct($tplfile);
  75. }
  76. //设置SQL语句
  77. function SetSource($sql)
  78. {
  79. $this->sourceSql = $sql;
  80. }
  81. //设置模板
  82. //如果想要使用模板中指定的pagesize,必须在调用模板后才调用 SetSource($sql)
  83. function SetTemplate($tplfile)
  84. {
  85. $this->tpl->LoadTemplate($tplfile);
  86. }
  87. function SetTemplet($tplfile)
  88. {
  89. $this->tpl->LoadTemplate($tplfile);
  90. }
  91. /**
  92. * 对config参数及get参数等进行预处理
  93. *
  94. * @access public
  95. * @return void
  96. */
  97. function PreLoad()
  98. {
  99. global $totalresult, $pageno;
  100. if (empty($pageno) || preg_match("#[^0-9]#", $pageno)) {
  101. $pageno = 1;
  102. }
  103. if (empty($totalresult) || preg_match("#[^0-9]#", $totalresult)) {
  104. $totalresult = 0;
  105. }
  106. $this->pageNO = $pageno;
  107. $this->totalResult = $totalresult;
  108. if (isset($GLOBALS['pagesize'])) {
  109. $this->pagesize = $GLOBALS['pagesize'];
  110. $this->SetParameter('pagesize', $this->pagesize);
  111. }
  112. if (isset($this->tpl->tpCfgs['pagesize'])) {
  113. $this->pagesize = $this->tpl->tpCfgs['pagesize'];
  114. }
  115. $this->totalPage = ceil($this->totalResult / $this->pagesize);
  116. if ($this->totalResult == 0) {
  117. $countQuery = preg_replace("#SELECT[ \r\n\t](.*)[ \r\n\t]FROM#is", 'SELECT COUNT(*) AS dd FROM', $this->sourceSql);
  118. $countQuery = preg_replace("#ORDER[ \r\n\t]{1,}BY(.*)#is", '', $countQuery);
  119. $row = $this->dsql->GetOne($countQuery);
  120. if (!is_array($row)) $row = array("dd" => 0);
  121. $this->totalResult = isset($row['dd']) ? $row['dd'] : 0;
  122. $this->sourceSql .= " LIMIT 0,".$this->pagesize;
  123. } else {
  124. $this->sourceSql .= " LIMIT ".(($this->pageNO - 1) * $this->pagesize).",".$this->pagesize;
  125. }
  126. }
  127. //设置网址的Get参数键值
  128. function SetParameter($key, $value)
  129. {
  130. $this->getValues[$key] = $value;
  131. }
  132. //设置/获取文档相关的各种变量
  133. function SetVar($k, $v)
  134. {
  135. global $_vars;
  136. if (!isset($_vars[$k])) {
  137. $_vars[$k] = $v;
  138. }
  139. }
  140. function GetVar($k)
  141. {
  142. global $_vars;
  143. return isset($_vars[$k]) ? $_vars[$k] : '';
  144. }
  145. function XSSClean($val)
  146. {
  147. if (is_array($val)) {
  148. foreach ($val as $key => $v) {
  149. $val[$key] = $this->XSSClean($v);
  150. }
  151. return $val;
  152. }
  153. return RemoveXss($val);
  154. }
  155. //获取当前页数据列表
  156. function GetArcList($atts, $refObj = '', $fields = array())
  157. {
  158. $rsArray = array();
  159. $t1 = Exectime();
  160. if (!$this->isQuery) $this->dsql->Execute('dlist', $this->sourceSql);
  161. $i = 0;
  162. while ($arr = $this->dsql->GetArray('dlist')) {
  163. $i++;
  164. $rsArray[$i] = $this->XSSClean($arr);
  165. $rsArray[$i]['__safe'] = $arr;
  166. if ($i >= $this->pagesize) {
  167. break;
  168. }
  169. }
  170. $this->dsql->FreeResult('dlist');
  171. $this->queryTime = (Exectime() - $t1);
  172. return $rsArray;
  173. }
  174. //获取分页导航列表
  175. function GetPageList($atts, $refObj = '', $fields = array())
  176. {
  177. global $lang_pre_page, $lang_next_page, $lang_index_page, $lang_end_page, $lang_record_number, $lang_page;
  178. $prepage = $nextpage = $geturl = $hidenform = '';
  179. $purl = $this->GetCurUrl();
  180. $prepagenum = $this->pageNO - 1;
  181. $nextpagenum = $this->pageNO + 1;
  182. if (!isset($atts['listsize']) || preg_match("#[^0-9]#", $atts['listsize'])) {
  183. $atts['listsize'] = 2;
  184. }
  185. if (!isset($atts['listitem'])) {
  186. $atts['listitem'] = "pagesize,info,index,end,pre,next,pageno,form";
  187. }
  188. $totalpage = ceil($this->totalResult / $this->pagesize);
  189. //echo " {$totalpage}=={$this->totalResult}=={$this->pagesize}";
  190. //无结果或只有一页的情况
  191. if ($totalpage <= 1 && $this->totalResult > 0) {
  192. return "<ul class='pagination justify-content-end'><li class='page-item disabled'><span class='page-link'>1{$lang_page}".$this->totalResult.$lang_record_number."</span></li></ul>";
  193. }
  194. if ($this->totalResult == 0) {
  195. return "<ul class='pagination justify-content-end'><li class='page-item disabled'><span class='page-link'>0{$lang_page}".$this->totalResult.$lang_record_number."</span></li></ul>";
  196. }
  197. $infos = "<li class='page-item disabled'><span class='page-link'>{$totalpage}{$lang_page}/{$this->totalResult}{$lang_record_number}</span></li>";
  198. if ($this->totalResult != 0) {
  199. $this->getValues['totalresult'] = $this->totalResult;
  200. }
  201. if (count($this->getValues) > 0) {
  202. foreach ($this->getValues as $key => $value) {
  203. $value = urlencode($value);
  204. $geturl .= "$key=$value"."&";
  205. $hidenform .= "<input type='hidden' name='$key' value='$value'>\r\n";
  206. }
  207. }
  208. $purl .= "?".$geturl;
  209. //获得上页和下页的链接
  210. if ($this->pageNO != 1) {
  211. $prepage .= "<li class='page-item'><a class='page-link' href='".$purl."pageno=$prepagenum'>$lang_pre_page</a></li>\r\n";
  212. $indexpage = "<li class='page-item'><a class='page-link' href='".$purl."pageno=1'>$lang_index_page</a></li>\r\n";
  213. } else {
  214. $indexpage = "<li class='page-item disabled'><span class='page-link'>"."$lang_index_page \n"."</span></li>";
  215. }
  216. if ($this->pageNO != $totalpage && $totalpage > 1) {
  217. $nextpage .= "<li class='page-item'><a class='page-link' href='".$purl."pageno=$nextpagenum'>$lang_next_page</a></li>\r\n";
  218. $endpage = "<li class='page-item'><a class='page-link' href='".$purl."pageno=$totalpage'>$lang_end_page</a></li>\r\n";
  219. } else {
  220. $endpage = " <li class='page-item disabled'><span class='page-link'>$lang_end_page</span></li>\r\n";
  221. }
  222. //获得数字链接
  223. $listdd = '';
  224. $total_list = $atts['listsize'] * 2 + 1;
  225. if ($this->pageNO >= $total_list) {
  226. $j = $this->pageNO - $atts['listsize'];
  227. $total_list = $this->pageNO + $atts['listsize'];
  228. if ($total_list > $totalpage) {
  229. $total_list = $totalpage;
  230. }
  231. } else {
  232. $j = 1;
  233. if ($total_list > $totalpage) {
  234. $total_list = $totalpage;
  235. }
  236. }
  237. for ($j; $j <= $total_list; $j++) {
  238. $listdd .= $j == $this->pageNO ? "<li class='page-item'><span class='page-link'>$j</span></li>\r\n" : "<li class='page-item'><a class='page-link' href='".$purl."pageno=$j'>".$j."</a></li>\r\n";
  239. }
  240. $plist = "<div class='d-flex justify-content-end'>\r\n";
  241. $sizesel = "<select name='pagesize' id='dedepagesize' class='form-control mr-2' style='width:120px'>\r\n";
  242. $sizesel .= "<option value='30' ".($this->pagesize == 30 ? "selected='selected'" : "").">30条/页</option>\r\n";
  243. $sizesel .= "<option value='50' ".($this->pagesize == 50 ? "selected='selected'" : "").">50条/页</option>\r\n";
  244. $sizesel .= "<option value='100' ".($this->pagesize == 100 ? "selected='selected'" : "").">100条/页</option>\r\n";
  245. $sizesel .= "<option value='500' ".($this->pagesize == 500 ? "selected='selected'" : "").">500条/页</option>\r\n";
  246. $sizesel .= "<option value='1000' ".($this->pagesize == 1000 ? "selected='selected'" : "").">1000条/页</option>\r\n";
  247. $sizesel .= "</select><script>document.addEventListener('DOMContentLoaded', function() {var selectElement = document.getElementById('dedepagesize');selectElement.addEventListener('change', function() {var selectedSize = this.value;var url = new URL(window.location.href);url.searchParams.set('pagesize', selectedSize);window.location.href = url.toString();});});</script><ul class='pagination'>\r\n";
  248. if (preg_match("#pagesize#i", $atts['listitem'])) {
  249. $plist .= $sizesel;
  250. }
  251. if (preg_match("#info#i", $atts['listitem'])) {
  252. $plist .= $infos;
  253. }
  254. if (preg_match("#index#i", $atts['listitem'])) {
  255. $plist .= $indexpage;
  256. }
  257. if (preg_match("#pre#i", $atts['listitem'])) {
  258. $plist .= $prepage;
  259. }
  260. if (preg_match("#pageno#i", $atts['listitem'])) {
  261. $plist .= $listdd;
  262. }
  263. if (preg_match("#next#i", $atts['listitem'])) {
  264. $plist .= $nextpage;
  265. }
  266. if (preg_match("#end#i", $atts['listitem'])) {
  267. $plist .= $endpage;
  268. }
  269. if (preg_match("#form#i", $atts['listitem'])) {
  270. $plist .= "</ul><form name='pagelist' action='".$this->GetCurUrl()."'>$hidenform";
  271. if ($totalpage > $total_list) {
  272. $plist .= "<input type='text' name='pageno' class='form-control admin-input-xs ml-2' placeholder='页数'>\r\n";
  273. $plist .= "<button type='submit' name='plistgo' class='btn btn-success btn-sm'>跳转</button>\r\n";
  274. }
  275. $plist .= "</form>\r\n";
  276. }
  277. $plist .= "</div>\r\n";
  278. return $plist;
  279. }
  280. //获得当前网址
  281. function GetCurUrl()
  282. {
  283. if (!empty($_SERVER["REQUEST_URI"])) {
  284. $nowurl = $_SERVER["REQUEST_URI"];
  285. $nowurls = explode("?", $nowurl);
  286. $nowurl = $nowurls[0];
  287. } else {
  288. $nowurl = $_SERVER["PHP_SELF"];
  289. }
  290. return $nowurl;
  291. }
  292. //关闭
  293. function Close()
  294. {
  295. }
  296. //显示数据
  297. function Display()
  298. {
  299. $this->PreLoad();
  300. //在PHP4中,对象引用必须放在display之前,放在其它位置中无效
  301. $this->tpl->SetObject($this);
  302. $this->tpl->Display();
  303. }
  304. //保存为HTML
  305. function SaveTo($filename)
  306. {
  307. $this->tpl->SaveTo($filename);
  308. }
  309. }
  310. ?>