Pages.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. *------------------------------------------------------
  4. * 分页样式
  5. *------------------------------------------------------
  6. *
  7. * @author qqiu@qq.com
  8. * @date 2016-09-29 13:09:16
  9. * @version V1.0
  10. *
  11. */
  12. namespace App\Widget\Pagination;
  13. use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
  14. use Illuminate\Contracts\Pagination\Presenter as PresenterContract;
  15. use Illuminate\Pagination\UrlWindow;
  16. use Illuminate\Support\HtmlString;
  17. use Illuminate\Pagination\UrlWindowPresenterTrait;
  18. class Pages implements PresenterContract
  19. {
  20. use UrlWindowPresenterTrait;
  21. protected $paginationWrapper = '<ul class="pagination">%s %s %s</ul>';
  22. protected $disabledPageWrapper = '<li class="disabled"><span>%s</span></li>';
  23. protected $activePageWrapper = '<li class="active"><span>%s</span></li>';
  24. protected $previousButtonText = '<span>&laquo;</span>';
  25. protected $nextButtonText = '<span>&raquo;</span>';
  26. protected $availablePageWrapper = '<li><a href="%s" rel="next">%s</a></li>';
  27. protected $dotsText = '...';
  28. protected $paginator;
  29. protected $window;
  30. protected $pageRule;
  31. protected $appendArr;
  32. public function init( PaginatorContract $paginator, UrlWindow $window = null )
  33. {
  34. $this->paginator = $paginator;
  35. $this->window = is_null( $window ) ? UrlWindow::make( $paginator ) : $window->get();
  36. return $this;
  37. }
  38. public function simpleCreate( PaginatorContract $paginator )
  39. {
  40. $this->paginator = $paginator;
  41. return $this;
  42. }
  43. public function hasPages()
  44. {
  45. return $this->paginator->hasPages();
  46. }
  47. public function appends(array $request = [])
  48. {
  49. $this->appendArr = $request;
  50. return $this;
  51. }
  52. public function setPage( $pageRule )
  53. {
  54. $this->pageRule = rtrim($pageRule, '/');
  55. return $this;
  56. }
  57. public function render()
  58. {
  59. if ( $this->hasPages() ) {
  60. return sprintf(
  61. $this->getPaginationWrapperHTML(),
  62. $this->getPreviousButton(),
  63. $this->getLinks(),
  64. $this->getNextButton()
  65. );
  66. }
  67. return '';
  68. }
  69. protected function getAvailablePageWrapper( $url, $page )
  70. {
  71. return sprintf( $this->getAvailablePageWrapperHTML(), $url, $page );
  72. }
  73. protected function getDisabledTextWrapper( $text )
  74. {
  75. return sprintf( $this->getDisabledPageWrapperHTML(), $text );
  76. }
  77. protected function getActivePageWrapper( $text )
  78. {
  79. return sprintf( $this->getActivePageWrapperHTML(), $text );
  80. }
  81. protected function getDots()
  82. {
  83. return $this->getDisabledTextWrapper( $this->getDotsText() );
  84. }
  85. protected function currentPage()
  86. {
  87. return $this->paginator->currentPage();
  88. }
  89. protected function lastPage()
  90. {
  91. return $this->paginator->lastPage();
  92. }
  93. protected function getPageLinkWrapper( $url, $page )
  94. {
  95. if($this->pageRule){
  96. $url = $this->formatUrl($url, $this->pageRule);
  97. }
  98. if ( $page == $this->paginator->currentPage() ) {
  99. return $this->getActivePageWrapper( $page );
  100. }
  101. return $this->getAvailablePageWrapper( $url, $page );
  102. }
  103. protected function getPreviousButton()
  104. {
  105. if ( $this->paginator->currentPage() <= 1 ) {
  106. return $this->getDisabledTextWrapper( $this->getPreviousButtonText() );
  107. }
  108. $url = $this->paginator->url(
  109. $this->paginator->currentPage() - 1
  110. );
  111. return $this->getPageLinkWrapper( $url, $this->getPreviousButtonText() );
  112. }
  113. protected function getNextButton()
  114. {
  115. if ( !$this->paginator->hasMorePages() ) {
  116. return $this->getDisabledTextWrapper( $this->getNextButtonText() );
  117. }
  118. $url = $this->paginator->url( $this->paginator->currentPage() + 1 );
  119. return $this->getPageLinkWrapper( $url, $this->getNextButtonText() );
  120. }
  121. protected function getAvailablePageWrapperHTML()
  122. {
  123. return $this->availablePageWrapper;
  124. }
  125. protected function getActivePageWrapperHTML()
  126. {
  127. return $this->activePageWrapper;
  128. }
  129. protected function getDisabledPageWrapperHTML()
  130. {
  131. return $this->disabledPageWrapper;
  132. }
  133. protected function getPreviousButtonText()
  134. {
  135. return $this->previousButtonText;
  136. }
  137. protected function getNextButtonText()
  138. {
  139. return $this->nextButtonText;
  140. }
  141. protected function getDotsText()
  142. {
  143. return $this->dotsText;
  144. }
  145. protected function getPaginationWrapperHTML()
  146. {
  147. return $this->paginationWrapper;
  148. }
  149. /**
  150. * @param $url
  151. * @param string $pageRule
  152. * @return string
  153. */
  154. protected function formatUrl($url, $pageRule)
  155. {
  156. $pagePreg = '|' . str_replace(['/', '-', '{page}'], ['\/', '\-', '([0-9]+)'], $pageRule) . '|is';
  157. $urlArr = parse_url($url);
  158. $newUrl = isset($urlArr['scheme']) ? ($urlArr['scheme'] . "://") : '';
  159. $newUrl .= isset($urlArr['host']) ? $urlArr['host'] : '';
  160. $newUrl .= isset($urlArr['path']) ? $urlArr['path'] : '';
  161. $arrQuery = $this->convertUrlQuery($urlArr['query']);
  162. if($this->appendArr){
  163. $arrQuery = array_merge($this->appendArr, $arrQuery);
  164. }
  165. if(isset($arrQuery['page'])) {
  166. $pageRule = str_replace('{page}', $arrQuery['page'], $pageRule);
  167. unset($arrQuery['page']);
  168. }
  169. if(isset($urlArr['path'])){
  170. $newUrl = preg_replace($pagePreg, '', $newUrl) . $pageRule . (empty($arrQuery) ? '' : ('?' . http_build_query($arrQuery)));
  171. }else{
  172. $newUrl = rtrim($newUrl, '/') . $pageRule . (empty($arrQuery) ? '' : ('?' . http_build_query($arrQuery)));
  173. }
  174. return $newUrl;
  175. }
  176. /**
  177. * Render the actual link slider.
  178. *
  179. * @return string
  180. */
  181. protected function getLinks()
  182. {
  183. $html = '';
  184. if (is_array($this->window['first'])) {
  185. $html .= $this->getUrlLinks($this->window['first']);
  186. }
  187. if (is_array($this->window['slider'])) {
  188. $html .= $this->getDots();
  189. $html .= $this->getUrlLinks($this->window['slider']);
  190. }
  191. if (is_array($this->window['last'])) {
  192. $html .= $this->getDots();
  193. $html .= $this->getUrlLinks($this->window['last']);
  194. }
  195. return $html;
  196. }
  197. /**
  198. * Get the links for the URLs in the given array.
  199. *
  200. * @param array $urls
  201. * @return string
  202. */
  203. protected function getUrlLinks(array $urls)
  204. {
  205. $html = '';
  206. foreach ($urls as $page => $url) {
  207. $html .= $this->getPageLinkWrapper($url, $page);
  208. }
  209. return $html;
  210. }
  211. /**
  212. * Returns the url query as associative array
  213. *
  214. * @param string query
  215. * @return array params
  216. */
  217. protected function convertUrlQuery($query)
  218. {
  219. $queryParts = explode('&', $query);
  220. $params = array();
  221. foreach ($queryParts as $param)
  222. {
  223. $item = explode('=', $param);
  224. $params[$item[0]] = $item[1];
  225. }
  226. return $params;
  227. }
  228. }