[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class AphrontCursorPagerView extends AphrontView { 4 5 private $afterID; 6 private $beforeID; 7 8 private $pageSize = 100; 9 10 private $nextPageID; 11 private $prevPageID; 12 private $moreResults; 13 14 private $uri; 15 16 final public function setPageSize($page_size) { 17 $this->pageSize = max(1, $page_size); 18 return $this; 19 } 20 21 final public function getPageSize() { 22 return $this->pageSize; 23 } 24 25 final public function setURI(PhutilURI $uri) { 26 $this->uri = $uri; 27 return $this; 28 } 29 30 final public function readFromRequest(AphrontRequest $request) { 31 $this->uri = $request->getRequestURI(); 32 $this->afterID = $request->getStr('after'); 33 $this->beforeID = $request->getStr('before'); 34 return $this; 35 } 36 37 final public function setAfterID($after_id) { 38 $this->afterID = $after_id; 39 return $this; 40 } 41 42 final public function getAfterID() { 43 return $this->afterID; 44 } 45 46 final public function setBeforeID($before_id) { 47 $this->beforeID = $before_id; 48 return $this; 49 } 50 51 final public function getBeforeID() { 52 return $this->beforeID; 53 } 54 55 final public function setNextPageID($next_page_id) { 56 $this->nextPageID = $next_page_id; 57 return $this; 58 } 59 60 final public function getNextPageID() { 61 return $this->nextPageID; 62 } 63 64 final public function setPrevPageID($prev_page_id) { 65 $this->prevPageID = $prev_page_id; 66 return $this; 67 } 68 69 final public function getPrevPageID() { 70 return $this->prevPageID; 71 } 72 73 final public function sliceResults(array $results) { 74 if (count($results) > $this->getPageSize()) { 75 $offset = ($this->beforeID ? count($results) - $this->getPageSize() : 0); 76 $results = array_slice($results, $offset, $this->getPageSize(), true); 77 $this->moreResults = true; 78 } 79 return $results; 80 } 81 82 public function willShowPagingControls() { 83 return $this->prevPageID || 84 $this->nextPageID || 85 $this->afterID || 86 ($this->beforeID && $this->moreResults); 87 } 88 89 public function getFirstPageURI() { 90 if (!$this->uri) { 91 throw new Exception( 92 pht('You must call setURI() before you can call getFirstPageURI().')); 93 } 94 95 if (!$this->afterID && !($this->beforeID && $this->moreResults)) { 96 return null; 97 } 98 99 return $this->uri 100 ->alter('before', null) 101 ->alter('after', null); 102 } 103 104 public function getPrevPageURI() { 105 if (!$this->uri) { 106 throw new Exception( 107 pht('You must call setURI() before you can call getPrevPageURI().')); 108 } 109 110 if (!$this->prevPageID) { 111 return null; 112 } 113 114 return $this->uri 115 ->alter('after', null) 116 ->alter('before', $this->prevPageID); 117 } 118 119 public function getNextPageURI() { 120 if (!$this->uri) { 121 throw new Exception( 122 pht('You must call setURI() before you can call getNextPageURI().')); 123 } 124 125 if (!$this->nextPageID) { 126 return null; 127 } 128 129 return $this->uri 130 ->alter('after', $this->nextPageID) 131 ->alter('before', null); 132 } 133 134 public function render() { 135 if (!$this->uri) { 136 throw new Exception( 137 pht('You must call setURI() before you can call render().')); 138 } 139 140 $links = array(); 141 142 $first_uri = $this->getFirstPageURI(); 143 if ($first_uri) { 144 $links[] = phutil_tag( 145 'a', 146 array( 147 'href' => $first_uri, 148 ), 149 "\xC2\xAB ".pht('First')); 150 } 151 152 $prev_uri = $this->getPrevPageURI(); 153 if ($prev_uri) { 154 $links[] = phutil_tag( 155 'a', 156 array( 157 'href' => $prev_uri, 158 ), 159 "\xE2\x80\xB9 ".pht('Prev')); 160 } 161 162 $next_uri = $this->getNextPageURI(); 163 if ($next_uri) { 164 $links[] = phutil_tag( 165 'a', 166 array( 167 'href' => $next_uri, 168 ), 169 pht('Next')." \xE2\x80\xBA"); 170 } 171 172 return phutil_tag( 173 'div', 174 array('class' => 'aphront-pager-view'), 175 $links); 176 } 177 178 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |