/upload/src/library/engine/extension/viewer/PwTemplateCompilerPage.php

https://gitlab.com/wuhang2003/phpwind · PHP · 119 lines · 50 code · 6 blank · 63 comment · 7 complexity · 573df1905d9ca8a47efbb6b92b9bf515 MD5 · raw file

  1. <?php
  2. Wind::import('WIND:viewer.AbstractWindTemplateCompiler');
  3. /**
  4. * page标签解析
  5. *
  6. * 职责:编译模板page标签
  7. * 支持参数类型:<code>
  8. * 模板名称,当前页,总条数,每页显示多少条,url
  9. * <page tpl='' current='' count='' per='' url='read.php?tid=$tid&page=' args='' />
  10. * </code>
  11. * @author Qiong Wu <papa0924@gmail.com>
  12. * @copyright ©2003-2103 phpwind.com
  13. * @license http://www.windframework.com
  14. * @version $Id: PwTemplateCompilerPage.php 3580 2012-05-25 03:34:14Z yishuo $
  15. * @package viewer
  16. * @subpackage compiler
  17. */
  18. class PwTemplateCompilerPage extends AbstractWindTemplateCompiler {
  19. /**
  20. * 分页模板
  21. *
  22. * @var string
  23. */
  24. protected $tpl = '';
  25. /**
  26. * 分页跳转url
  27. *
  28. * @var string
  29. */
  30. protected $url = '';
  31. /**
  32. * 字符型数字,总共有多少页
  33. *
  34. * @var string
  35. */
  36. protected $total = '0';
  37. /**
  38. * 字符型数字,当前page
  39. *
  40. * @var string
  41. */
  42. protected $page = '1';
  43. /**
  44. * 字符型数字,总条数
  45. *
  46. * @var string
  47. */
  48. protected $count = '0';
  49. /**
  50. * 字符型数字,每页显示的条数
  51. *
  52. * @var string
  53. */
  54. protected $per = '0';
  55. /**
  56. *
  57. * URL参数,http query
  58. *
  59. * @var array
  60. * @deprecated
  61. */
  62. protected $args = 'array()';
  63. /* (non-PHPdoc)
  64. * @see AbstractWindTemplateCompiler::compile()
  65. */
  66. public function compile($key, $content) {
  67. empty($this->total) && $this->total = '0';
  68. empty($this->page) && $this->page = '1';
  69. empty($this->count) && $this->count = '0';
  70. empty($this->per) && $this->per = '0';
  71. $_return = array();
  72. $_return[] = '<?php $__tplPageCount=(int)' . $this->count . ';';
  73. $_return[] = '$__tplPagePer=(int)' . $this->per . ';';
  74. $_return[] = '$__tplPageTotal=(int)' . $this->total . ';';
  75. $_return[] = '$__tplPageCurrent=(int)' . $this->page . ';';
  76. $_return[] = 'if($__tplPageCount > 0 && $__tplPagePer > 0){';
  77. $_return[] = '$__tmp = ceil($__tplPageCount / $__tplPagePer);';
  78. $_return[] = '($__tplPageTotal !== 0 && $__tplPageTotal < $__tmp) || $__tplPageTotal = $__tmp;}';
  79. $_return[] = '$__tplPageCurrent > $__tplPageTotal && $__tplPageCurrent = $__tplPageTotal;';
  80. $_return[] = 'if ($__tplPageTotal > 1) {?>';
  81. $_return[] = $this->getTplContent();
  82. $_return[] = '<?php } ?>';
  83. return implode("\r\n", $_return);
  84. }
  85. /**
  86. * 获得page页模板内容
  87. *
  88. * @return string|mixed
  89. */
  90. private function getTplContent() {
  91. if (!$this->tpl) return '';
  92. list($pageFile) = $this->windViewerResolver->getWindView()->getViewTemplate($this->tpl);
  93. if (!is_file($pageFile)) {
  94. throw new WindViewException($pageFile, WindViewException::VIEW_NOT_EXIST);
  95. }
  96. $content = WindFile::read($pageFile);
  97. strpos($this->url, '?') !== false || $this->url .= '?';
  98. $url = '{@url:' . $this->url . '&page=$_page_i}{@' . $this->args . ' ? \'&\' . http_build_query(' . $this->args . ') : \'\'';
  99. $content = str_ireplace(array('{@$url', '{$url'), $url, $content);
  100. $_windTemplate = Wind::getComponent('template');
  101. $content = $_windTemplate->compileStream($content, $this->windViewerResolver);
  102. $arrPageTags = array('$total', '$page', '$count');
  103. $arrPageVars = array('$__tplPageTotal', '$__tplPageCurrent', '$__tplPageCount');
  104. return str_ireplace($arrPageTags, $arrPageVars, $content);
  105. }
  106. /* (non-PHPdoc)
  107. * @see AbstractWindTemplateCompiler::getProperties()
  108. */
  109. public function getProperties() {
  110. return array('tpl', 'total', 'page', 'per', 'count', 'url', 'args');
  111. }
  112. }
  113. ?>