Changeset 3529
- Timestamp:
- 01/16/08 20:51:36 (18 months ago)
- Files:
-
- 18 modified
-
branches/0.10/lib/Doctrine/Pager.php (modified) (15 diffs)
-
branches/0.10/lib/Doctrine/Pager/Exception.php (modified) (1 diff)
-
branches/0.10/lib/Doctrine/Pager/Layout.php (modified) (9 diffs)
-
branches/0.10/lib/Doctrine/Pager/Range.php (modified) (1 diff)
-
branches/0.10/lib/Doctrine/Pager/Range/Jumping.php (modified) (2 diffs)
-
branches/0.10/lib/Doctrine/Pager/Range/Sliding.php (modified) (2 diffs)
-
branches/0.9/lib/Doctrine/Pager.php (modified) (15 diffs)
-
branches/0.9/lib/Doctrine/Pager/Exception.php (modified) (1 diff)
-
branches/0.9/lib/Doctrine/Pager/Layout.php (modified) (9 diffs)
-
branches/0.9/lib/Doctrine/Pager/Range.php (modified) (1 diff)
-
branches/0.9/lib/Doctrine/Pager/Range/Jumping.php (modified) (2 diffs)
-
branches/0.9/lib/Doctrine/Pager/Range/Sliding.php (modified) (2 diffs)
-
trunk/lib/Doctrine/Pager.php (modified) (15 diffs)
-
trunk/lib/Doctrine/Pager/Exception.php (modified) (1 diff)
-
trunk/lib/Doctrine/Pager/Layout.php (modified) (9 diffs)
-
trunk/lib/Doctrine/Pager/Range.php (modified) (1 diff)
-
trunk/lib/Doctrine/Pager/Range/Jumping.php (modified) (2 diffs)
-
trunk/lib/Doctrine/Pager/Range/Sliding.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.10/lib/Doctrine/Pager.php
r3395 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 class Doctrine_Pager … … 59 59 protected $_lastPage; 60 60 61 /** 62 * @var boolean $_executed Pager was initialized (called "execute" at least once) 63 */ 64 protected $_executed; 65 61 66 62 67 … … 65 70 * 66 71 * @param mixed $query Accepts either a Doctrine_Query object or a string 67 * (which does the Doctrine_Query class creation).72 * (which does the Doctrine_Query class creation). 68 73 * @param int $page Current page 69 74 * @param int $maxPerPage Maximum itens per page … … 72 77 public function __construct($query, $page, $maxPerPage = 0) 73 78 { 79 $this->_setExecuted(false); 80 74 81 $this->_setQuery($query); 75 76 $this->_setMaxPerPage($maxPerPage);77 82 $this->_setPage($page); 78 83 79 $this-> _initialize();84 $this->setMaxPerPage($maxPerPage); 80 85 } 81 86 … … 86 91 * Initialize Pager object calculating number of results 87 92 * 88 * @return void 89 */ 90 protected function _initialize() 93 * @param $params Optional parameters to Doctrine_Query::execute 94 * @return void 95 */ 96 protected function _initialize($params = array()) 91 97 { 92 98 // retrieve the number of items found 93 $count = $this->getQuery()->count();99 $count = $this->getQuery()->count($params); 94 100 $this->_setNumResults($count); 95 101 96 102 $this->_adjustOffset(); 103 104 $this->_setExecuted(true); 97 105 } 98 106 … … 107 115 protected function _adjustOffset() 108 116 { 109 // Define new total of pages 110 $this->_setLastPage( 111 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 117 if (!$this->getExecuted()) { 118 // Define new total of pages 119 $this->_setLastPage( 120 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 121 ); 122 $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 123 124 // Assign new offset and limit to Doctrine_Query object 125 $p = $this->getQuery(); 126 $p->offset($offset); 127 $p->limit($this->getMaxPerPage()); 128 } 129 } 130 131 132 /** 133 * getExecuted 134 * 135 * Returns the check if Pager was already executed at least once 136 * 137 * @return boolen Pager was executed 138 */ 139 public function getExecuted() 140 { 141 return $this->_executed; 142 } 143 144 145 /** 146 * _setExecuted 147 * 148 * Defines if Pager was already executed 149 * 150 * @param $executed Pager was executed 151 * @return void 152 */ 153 protected function _setExecuted($executed) 154 { 155 $this->_executed = $executed; 156 } 157 158 159 /** 160 * getNumResults 161 * 162 * Returns the number of results found 163 * 164 * @return int the number of results found 165 */ 166 public function getNumResults() 167 { 168 if ($this->getExecuted()) { 169 return $this->_numResults; 170 } 171 172 throw new Doctrine_Pager_Exception( 173 'Cannot retrieve the number of results of a not yet executed Pager query' 112 174 ); 113 $offset = ($this->getPage() - 1) * $this->getMaxPerPage();114 115 // Assign new offset and limit to Doctrine_Query object116 $p = $this->getQuery();117 $p->offset($offset);118 $p->limit($this->getMaxPerPage());119 }120 121 122 /**123 * getNumResults124 *125 * Returns the number of results found126 *127 * @return int the number of results found128 */129 public function getNumResults()130 {131 return $this->_numResults;132 175 } 133 176 … … 169 212 public function getLastPage() 170 213 { 171 return $this->_lastPage; 214 if ($this->getExecuted()) { 215 return $this->_lastPage; 216 } 217 218 throw new Doctrine_Pager_Exception( 219 'Cannot retrieve the last page number of a not yet executed Pager query' 220 ); 172 221 } 173 222 … … 213 262 public function getNextPage() 214 263 { 215 return min($this->getPage() + 1, $this->getLastPage()); 264 if ($this->getExecuted()) { 265 return $this->_lastPage; 266 } 267 268 throw new Doctrine_Pager_Exception( 269 'Cannot retrieve the last page number of a not yet executed Pager query' 270 );return min($this->getPage() + 1, $this->getLastPage()); 216 271 } 217 272 … … 226 281 public function getPreviousPage() 227 282 { 228 return max($this->getPage() - 1, $this->getFirstPage()); 283 if ($this->getExecuted()) { 284 return max($this->getPage() - 1, $this->getFirstPage()); 285 } 286 287 throw new Doctrine_Pager_Exception( 288 'Cannot retrieve the previous page number of a not yet executed Pager query' 289 ); 229 290 } 230 291 … … 239 300 public function haveToPaginate() 240 301 { 241 return $this->getNumResults() > $this->getMaxPerPage(); 302 if ($this->getExecuted()) { 303 return $this->getNumResults() > $this->getMaxPerPage(); 304 } 305 306 throw new Doctrine_Pager_Exception( 307 'Cannot know if it is necessary to paginate a not yet executed Pager query' 308 ); 242 309 } 243 310 … … 254 321 { 255 322 $this->_setPage($page); 256 $this->_ adjustOffset();323 $this->_setExecuted(false); 257 324 } 258 325 … … 295 362 */ 296 363 public function setMaxPerPage($max) 297 {298 $this->_setMaxPerPage($max);299 $this->_adjustOffset();300 }301 302 303 /**304 * _setMaxPerPage305 *306 * Defines the maximum number of itens per page307 *308 * @param $max maximum number of itens per page309 * @return void310 */311 private function _setMaxPerPage($max)312 364 { 313 365 if ($max > 0) { … … 318 370 $this->_maxPerPage = abs($max); 319 371 } 372 373 $this->_setExecuted(false); 320 374 } 321 375 … … 340 394 * 341 395 * @param $query Accepts either a Doctrine_Query object or a string 342 * (which does the Doctrine_Query class creation).396 * (which does the Doctrine_Query class creation). 343 397 * @return void 344 398 */ … … 355 409 /** 356 410 * execute 357 * executes the query and populates the data set 358 * 359 * @param $params Optional parameters to Doctrine_Query::execute 360 * @param $hydrationMode Hyddration Mode of Doctrine_Query::execute 361 * returned ResultSet. Doctrine::Default is FETCH_RECORD 362 * @return Doctrine_Collection the root collection 411 * 412 * Executes the query, populates the collection and then return it 413 * 414 * @param $params Optional parameters to Doctrine_Query::execute 415 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 416 * returned ResultSet. Doctrine::Default is FETCH_RECORD 417 * @return Doctrine_Collection The root collection 363 418 */ 364 419 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 365 420 { 421 $this->_initialize($params); 422 366 423 return $this->getQuery()->execute($params, $hydrationMode); 367 424 } -
branches/0.10/lib/Doctrine/Pager/Exception.php
r3222 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Exception extends Doctrine_Exception -
branches/0.10/lib/Doctrine/Pager/Layout.php
r3425 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Layout … … 122 122 123 123 /** 124 * execute 125 * 126 * Handy method to execute the query without need to retrieve the Pager instance 127 * 128 * @param $params Optional parameters to Doctrine_Query::execute 129 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 130 * returned ResultSet. Doctrine::Default is FETCH_RECORD 131 * @return Doctrine_Collection The root collection 132 */ 133 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 134 { 135 return $this->getPager()->execute($params, $hydrationMode); 136 } 137 138 139 /** 124 140 * getPagerRange 125 141 * … … 256 272 $this->_separatorTemplate = $separatorTemplate; 257 273 } 258 259 274 275 260 276 /** 261 277 * addMaskReplacement … … 280 296 } 281 297 } 282 283 298 299 284 300 /** 285 301 * removeMaskReplacement … … 334 350 // Define some optional mask values 335 351 $options['page_number'] = $range[$i]; 336 $options['page'] = $range[$i]; // Handy assignment for URLs 337 $options['url'] = $this->_parseUrl($options); 338 339 $str .= $this->_parseTemplate($options); 352 353 $str .= $this->processPage($options); 340 354 341 355 // Apply separator between pages … … 353 367 } 354 368 355 /** 356 * simply calls display, and returns the output. 369 370 /** 371 * processPage 372 * 373 * Parses the template and returns the string of a processed page 374 * 375 * @param array Optional parameters to be applied in template and url mask 376 * @return string Processed template for the given page 377 */ 378 public function processPage($options = array()) 379 { 380 // Check if at least basic options are defined 381 if (!isset($options['page_number'])) { 382 throw new Doctrine_Pager_Exception( 383 'Cannot process template of the given page. ' . 384 'Missing at least one of needed parameters: \'page\' or \'page_number\'' 385 ); 386 387 // Should never reach here 388 return ''; 389 } 390 391 // Assign "page" options index if not defined yet 392 if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 393 $options['page'] = $options['page_number']; 394 } 395 396 return $this->_parseTemplate($options); 397 } 398 399 400 /** 401 * Simply calls display, and returns the output. 357 402 */ 358 403 public function __toString() … … 367 412 * Process the template of a given page and return the processed template 368 413 * 369 * @param $optionsOptional parameters to be applied in template and url mask414 * @param array Optional parameters to be applied in template and url mask 370 415 * @return string 371 416 */ … … 374 419 $str = ''; 375 420 376 if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 421 // If given page is the current active one 422 if ($options['page_number'] == $this->getPager()->getPage()) { 377 423 $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 378 424 } … … 383 429 } 384 430 431 // Defining "url" options index to allow {%url} mask 432 $options['url'] = $this->_parseUrl($options); 433 385 434 $replacements = array(); 386 435 387 436 foreach ($options as $k => $v) { 388 437 $replacements['{%'.$k.'}'] = $v; -
branches/0.10/lib/Doctrine/Pager/Range.php
r3395 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 abstract class Doctrine_Pager_Range -
branches/0.10/lib/Doctrine/Pager/Range/Jumping.php
r3414 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range … … 96 96 { 97 97 $pager = $this->getPager(); 98 $page = $pager->getPage();99 98 100 // Define initial assignments for StartPage and EndPage 101 $startPage = $page - ($page - 1) % $this->getChunkLength(); 102 $endPage = ($startPage + $this->getChunkLength()) - 1; 99 if ($pager->getExecuted()) { 100 $page = $pager->getPage(); 103 101 104 // Check for EndPage out-range 105 if ($endPage > $pager->getLastPage()) { 106 $endPage = $pager->getLastPage(); 102 // Define initial assignments for StartPage and EndPage 103 $startPage = $page - ($page - 1) % $this->getChunkLength(); 104 $endPage = ($startPage + $this->getChunkLength()) - 1; 105 106 // Check for EndPage out-range 107 if ($endPage > $pager->getLastPage()) { 108 $endPage = $pager->getLastPage(); 109 } 110 111 // No need to check for out-range in start, it will never happens 112 113 return range($startPage, $endPage); 107 114 } 108 115 109 // No need to check for out-range in start, it will never happens110 111 return range($startPage, $endPage);116 throw new Doctrine_Pager_Exception( 117 'Cannot retrieve the range around the page of a not yet executed Pager query' 118 ); 112 119 } 113 120 } -
branches/0.10/lib/Doctrine/Pager/Range/Sliding.php
r3512 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range … … 101 101 { 102 102 $pager = $this->getPager(); 103 $page = $pager->getPage();104 $pages = $pager->getLastPage();105 103 106 $chunk = $this->getChunkLength(); 107 if ($chunk > $pages) { 108 $chunk = $pages; 104 if ($pager->getExecuted()) { 105 $page = $pager->getPage(); 106 $pages = $pager->getLastPage(); 107 108 $chunk = $this->getChunkLength(); 109 110 if ($chunk > $pages) { 111 $chunk = $pages; 112 } 113 114 $chunkStart = $page - (floor($chunk / 2)); 115 $chunkEnd = $page + (ceil($chunk / 2)-1); 116 117 if ($chunkStart < 1) { 118 $adjust = 1 - $chunkStart; 119 $chunkStart = 1; 120 $chunkEnd = $chunkEnd + $adjust; 121 } 122 123 if ($chunkEnd > $pages) { 124 $adjust = $chunkEnd - $pages; 125 $chunkStart = $chunkStart - $adjust; 126 $chunkEnd = $pages; 127 } 128 129 return range($chunkStart, $chunkEnd); 109 130 } 110 131 111 $chunkStart = $page - (floor($chunk / 2)); 112 $chunkEnd = $page + (ceil($chunk / 2)-1); 113 114 if ($chunkStart < 1) { 115 $adjust = 1 - $chunkStart; 116 $chunkStart = 1; 117 $chunkEnd = $chunkEnd + $adjust; 118 } 119 if ($chunkEnd > $pages) { 120 $adjust = $chunkEnd - $pages; 121 $chunkStart = $chunkStart - $adjust; 122 $chunkEnd = $pages; 123 } 124 125 return range($chunkStart, $chunkEnd); 126 132 throw new Doctrine_Pager_Exception( 133 'Cannot retrieve the range around the page of a not yet executed Pager query' 134 ); 127 135 } 128 136 } -
branches/0.9/lib/Doctrine/Pager.php
r3512 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 class Doctrine_Pager … … 59 59 protected $_lastPage; 60 60 61 /** 62 * @var boolean $_executed Pager was initialized (called "execute" at least once) 63 */ 64 protected $_executed; 65 61 66 62 67 … … 65 70 * 66 71 * @param mixed $query Accepts either a Doctrine_Query object or a string 67 * (which does the Doctrine_Query class creation).72 * (which does the Doctrine_Query class creation). 68 73 * @param int $page Current page 69 74 * @param int $maxPerPage Maximum itens per page … … 72 77 public function __construct($query, $page, $maxPerPage = 0) 73 78 { 79 $this->_setExecuted(false); 80 74 81 $this->_setQuery($query); 75 76 $this->_setMaxPerPage($maxPerPage);77 82 $this->_setPage($page); 78 83 79 $this-> _initialize();84 $this->setMaxPerPage($maxPerPage); 80 85 } 81 86 … … 86 91 * Initialize Pager object calculating number of results 87 92 * 88 * @return void 89 */ 90 protected function _initialize() 93 * @param $params Optional parameters to Doctrine_Query::execute 94 * @return void 95 */ 96 protected function _initialize($params = array()) 91 97 { 92 98 // retrieve the number of items found 93 $count = $this->getQuery()->count();99 $count = $this->getQuery()->count($params); 94 100 $this->_setNumResults($count); 95 101 96 102 $this->_adjustOffset(); 103 104 $this->_setExecuted(true); 97 105 } 98 106 … … 107 115 protected function _adjustOffset() 108 116 { 109 // Define new total of pages 110 $this->_setLastPage( 111 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 117 if (!$this->getExecuted()) { 118 // Define new total of pages 119 $this->_setLastPage( 120 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 121 ); 122 $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 123 124 // Assign new offset and limit to Doctrine_Query object 125 $p = $this->getQuery(); 126 $p->offset($offset); 127 $p->limit($this->getMaxPerPage()); 128 } 129 } 130 131 132 /** 133 * getExecuted 134 * 135 * Returns the check if Pager was already executed at least once 136 * 137 * @return boolen Pager was executed 138 */ 139 public function getExecuted() 140 { 141 return $this->_executed; 142 } 143 144 145 /** 146 * _setExecuted 147 * 148 * Defines if Pager was already executed 149 * 150 * @param $executed Pager was executed 151 * @return void 152 */ 153 protected function _setExecuted($executed) 154 { 155 $this->_executed = $executed; 156 } 157 158 159 /** 160 * getNumResults 161 * 162 * Returns the number of results found 163 * 164 * @return int the number of results found 165 */ 166 public function getNumResults() 167 { 168 if ($this->getExecuted()) { 169 return $this->_numResults; 170 } 171 172 throw new Doctrine_Pager_Exception( 173 'Cannot retrieve the number of results of a not yet executed Pager query' 112 174 ); 113 $offset = ($this->getPage() - 1) * $this->getMaxPerPage();114 115 // Assign new offset and limit to Doctrine_Query object116 $p = $this->getQuery();117 $p->offset($offset);118 $p->limit($this->getMaxPerPage());119 }120 121 122 /**123 * getNumResults124 *125 * Returns the number of results found126 *127 * @return int the number of results found128 */129 public function getNumResults()130 {131 return $this->_numResults;132 175 } 133 176 … … 169 212 public function getLastPage() 170 213 { 171 return $this->_lastPage; 214 if ($this->getExecuted()) { 215 return $this->_lastPage; 216 } 217 218 throw new Doctrine_Pager_Exception( 219 'Cannot retrieve the last page number of a not yet executed Pager query' 220 ); 172 221 } 173 222 … … 213 262 public function getNextPage() 214 263 { 215 return min($this->getPage() + 1, $this->getLastPage()); 264 if ($this->getExecuted()) { 265 return $this->_lastPage; 266 } 267 268 throw new Doctrine_Pager_Exception( 269 'Cannot retrieve the last page number of a not yet executed Pager query' 270 );return min($this->getPage() + 1, $this->getLastPage()); 216 271 } 217 272 … … 226 281 public function getPreviousPage() 227 282 { 228 return max($this->getPage() - 1, $this->getFirstPage()); 283 if ($this->getExecuted()) { 284 return max($this->getPage() - 1, $this->getFirstPage()); 285 } 286 287 throw new Doctrine_Pager_Exception( 288 'Cannot retrieve the previous page number of a not yet executed Pager query' 289 ); 229 290 } 230 291 … … 239 300 public function haveToPaginate() 240 301 { 241 return $this->getNumResults() > $this->getMaxPerPage(); 302 if ($this->getExecuted()) { 303 return $this->getNumResults() > $this->getMaxPerPage(); 304 } 305 306 throw new Doctrine_Pager_Exception( 307 'Cannot know if it is necessary to paginate a not yet executed Pager query' 308 ); 242 309 } 243 310 … … 254 321 { 255 322 $this->_setPage($page); 256 $this->_ adjustOffset();323 $this->_setExecuted(false); 257 324 } 258 325 … … 295 362 */ 296 363 public function setMaxPerPage($max) 297 {298 $this->_setMaxPerPage($max);299 $this->_adjustOffset();300 }301 302 303 /**304 * _setMaxPerPage305 *306 * Defines the maximum number of itens per page307 *308 * @param $max maximum number of itens per page309 * @return void310 */311 private function _setMaxPerPage($max)312 364 { 313 365 if ($max > 0) { … … 318 370 $this->_maxPerPage = abs($max); 319 371 } 372 373 $this->_setExecuted(false); 320 374 } 321 375 … … 340 394 * 341 395 * @param $query Accepts either a Doctrine_Query object or a string 342 * (which does the Doctrine_Query class creation).396 * (which does the Doctrine_Query class creation). 343 397 * @return void 344 398 */ … … 355 409 /** 356 410 * execute 357 * executes the query and populates the data set 358 * 359 * @param $params Optional parameters to Doctrine_Query::execute 360 * @param $hydrationMode Hyddration Mode of Doctrine_Query::execute 361 * returned ResultSet. Doctrine::Default is FETCH_RECORD 362 * @return Doctrine_Collection the root collection 411 * 412 * Executes the query, populates the collection and then return it 413 * 414 * @param $params Optional parameters to Doctrine_Query::execute 415 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 416 * returned ResultSet. Doctrine::Default is FETCH_RECORD 417 * @return Doctrine_Collection The root collection 363 418 */ 364 419 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 365 420 { 421 $this->_initialize($params); 422 366 423 return $this->getQuery()->execute($params, $hydrationMode); 367 424 } -
branches/0.9/lib/Doctrine/Pager/Exception.php
r3512 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Exception extends Doctrine_Exception -
branches/0.9/lib/Doctrine/Pager/Layout.php
r3512 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Layout … … 122 122 123 123 /** 124 * execute 125 * 126 * Handy method to execute the query without need to retrieve the Pager instance 127 * 128 * @param $params Optional parameters to Doctrine_Query::execute 129 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 130 * returned ResultSet. Doctrine::Default is FETCH_RECORD 131 * @return Doctrine_Collection The root collection 132 */ 133 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 134 { 135 return $this->getPager()->execute($params, $hydrationMode); 136 } 137 138 139 /** 124 140 * getPagerRange 125 141 * … … 256 272 $this->_separatorTemplate = $separatorTemplate; 257 273 } 258 259 274 275 260 276 /** 261 277 * addMaskReplacement … … 280 296 } 281 297 } 282 283 298 299 284 300 /** 285 301 * removeMaskReplacement … … 334 350 // Define some optional mask values 335 351 $options['page_number'] = $range[$i]; 336 $options['page'] = $range[$i]; // Handy assignment for URLs 337 $options['url'] = $this->_parseUrl($options); 338 339 $str .= $this->_parseTemplate($options); 352 353 $str .= $this->processPage($options); 340 354 341 355 // Apply separator between pages … … 353 367 } 354 368 355 /** 356 * simply calls display, and returns the output. 369 370 /** 371 * processPage 372 * 373 * Parses the template and returns the string of a processed page 374 * 375 * @param array Optional parameters to be applied in template and url mask 376 * @return string Processed template for the given page 377 */ 378 public function processPage($options = array()) 379 { 380 // Check if at least basic options are defined 381 if (!isset($options['page_number'])) { 382 throw new Doctrine_Pager_Exception( 383 'Cannot process template of the given page. ' . 384 'Missing at least one of needed parameters: \'page\' or \'page_number\'' 385 ); 386 387 // Should never reach here 388 return ''; 389 } 390 391 // Assign "page" options index if not defined yet 392 if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 393 $options['page'] = $options['page_number']; 394 } 395 396 return $this->_parseTemplate($options); 397 } 398 399 400 /** 401 * Simply calls display, and returns the output. 357 402 */ 358 403 public function __toString() … … 367 412 * Process the template of a given page and return the processed template 368 413 * 369 * @param $optionsOptional parameters to be applied in template and url mask414 * @param array Optional parameters to be applied in template and url mask 370 415 * @return string 371 416 */ … … 374 419 $str = ''; 375 420 376 if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 421 // If given page is the current active one 422 if ($options['page_number'] == $this->getPager()->getPage()) { 377 423 $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 378 424 } … … 383 429 } 384 430 431 // Defining "url" options index to allow {%url} mask 432 $options['url'] = $this->_parseUrl($options); 433 385 434 $replacements = array(); 386 435 387 436 foreach ($options as $k => $v) { 388 437 $replacements['{%'.$k.'}'] = $v; -
branches/0.9/lib/Doctrine/Pager/Range.php
r3512 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 abstract class Doctrine_Pager_Range -
branches/0.9/lib/Doctrine/Pager/Range/Jumping.php
r3512 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range … … 96 96 { 97 97 $pager = $this->getPager(); 98 $page = $pager->getPage();99 98 100 // Define initial assignments for StartPage and EndPage 101 $startPage = $page - ($page - 1) % $this->getChunkLength(); 102 $endPage = ($startPage + $this->getChunkLength()) - 1; 99 if ($pager->getExecuted()) { 100 $page = $pager->getPage(); 103 101 104 // Check for EndPage out-range 105 if ($endPage > $pager->getLastPage()) { 106 $endPage = $pager->getLastPage(); 102 // Define initial assignments for StartPage and EndPage 103 $startPage = $page - ($page - 1) % $this->getChunkLength(); 104 $endPage = ($startPage + $this->getChunkLength()) - 1; 105 106 // Check for EndPage out-range 107 if ($endPage > $pager->getLastPage()) { 108 $endPage = $pager->getLastPage(); 109 } 110 111 // No need to check for out-range in start, it will never happens 112 113 return range($startPage, $endPage); 107 114 } 108 115 109 // No need to check for out-range in start, it will never happens110 111 return range($startPage, $endPage);116 throw new Doctrine_Pager_Exception( 117 'Cannot retrieve the range around the page of a not yet executed Pager query' 118 ); 112 119 } 113 120 } -
branches/0.9/lib/Doctrine/Pager/Range/Sliding.php
r3512 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range … … 101 101 { 102 102 $pager = $this->getPager(); 103 $page = $pager->getPage();104 $pages = $pager->getLastPage();105 103 106 $chunk = $this->getChunkLength(); 107 if ($chunk > $pages) { 108 $chunk = $pages; 104 if ($pager->getExecuted()) { 105 $page = $pager->getPage(); 106 $pages = $pager->getLastPage(); 107 108 $chunk = $this->getChunkLength(); 109 110 if ($chunk > $pages) { 111 $chunk = $pages; 112 } 113 114 $chunkStart = $page - (floor($chunk / 2)); 115 $chunkEnd = $page + (ceil($chunk / 2)-1); 116 117 if ($chunkStart < 1) { 118 $adjust = 1 - $chunkStart; 119 $chunkStart = 1; 120 $chunkEnd = $chunkEnd + $adjust; 121 } 122 123 if ($chunkEnd > $pages) { 124 $adjust = $chunkEnd - $pages; 125 $chunkStart = $chunkStart - $adjust; 126 $chunkEnd = $pages; 127 } 128 129 return range($chunkStart, $chunkEnd); 109 130 } 110 131 111 $chunkStart = $page - (floor($chunk / 2)); 112 $chunkEnd = $page + (ceil($chunk / 2)-1); 113 114 if ($chunkStart < 1) { 115 $adjust = 1 - $chunkStart; 116 $chunkStart = 1; 117 $chunkEnd = $chunkEnd + $adjust; 118 } 119 if ($chunkEnd > $pages) { 120 $adjust = $chunkEnd - $pages; 121 $chunkStart = $chunkStart - $adjust; 122 $chunkEnd = $pages; 123 } 124 125 return range($chunkStart, $chunkEnd); 126 132 throw new Doctrine_Pager_Exception( 133 'Cannot retrieve the range around the page of a not yet executed Pager query' 134 ); 127 135 } 128 136 } -
trunk/lib/Doctrine/Pager.php
r3458 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 class Doctrine_Pager … … 59 59 protected $_lastPage; 60 60 61 /** 62 * @var boolean $_executed Pager was initialized (called "execute" at least once) 63 */ 64 protected $_executed; 65 61 66 62 67 … … 65 70 * 66 71 * @param mixed $query Accepts either a Doctrine_Query object or a string 67 * (which does the Doctrine_Query class creation).72 * (which does the Doctrine_Query class creation). 68 73 * @param int $page Current page 69 74 * @param int $maxPerPage Maximum itens per page … … 72 77 public function __construct($query, $page, $maxPerPage = 0) 73 78 { 79 $this->_setExecuted(false); 80 74 81 $this->_setQuery($query); 75 76 $this->_setMaxPerPage($maxPerPage);77 82 $this->_setPage($page); 78 83 79 $this-> _initialize();84 $this->setMaxPerPage($maxPerPage); 80 85 } 81 86 … … 86 91 * Initialize Pager object calculating number of results 87 92 * 88 * @return void 89 */ 90 protected function _initialize() 93 * @param $params Optional parameters to Doctrine_Query::execute 94 * @return void 95 */ 96 protected function _initialize($params = array()) 91 97 { 92 98 // retrieve the number of items found 93 $count = $this->getQuery()->count();99 $count = $this->getQuery()->count($params); 94 100 $this->_setNumResults($count); 95 101 96 102 $this->_adjustOffset(); 103 104 $this->_setExecuted(true); 97 105 } 98 106 … … 107 115 protected function _adjustOffset() 108 116 { 109 // Define new total of pages 110 $this->_setLastPage( 111 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 117 if (!$this->getExecuted()) { 118 // Define new total of pages 119 $this->_setLastPage( 120 max(1, ceil($this->getNumResults() / $this->getMaxPerPage())) 121 ); 122 $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 123 124 // Assign new offset and limit to Doctrine_Query object 125 $p = $this->getQuery(); 126 $p->offset($offset); 127 $p->limit($this->getMaxPerPage()); 128 } 129 } 130 131 132 /** 133 * getExecuted 134 * 135 * Returns the check if Pager was already executed at least once 136 * 137 * @return boolen Pager was executed 138 */ 139 public function getExecuted() 140 { 141 return $this->_executed; 142 } 143 144 145 /** 146 * _setExecuted 147 * 148 * Defines if Pager was already executed 149 * 150 * @param $executed Pager was executed 151 * @return void 152 */ 153 protected function _setExecuted($executed) 154 { 155 $this->_executed = $executed; 156 } 157 158 159 /** 160 * getNumResults 161 * 162 * Returns the number of results found 163 * 164 * @return int the number of results found 165 */ 166 public function getNumResults() 167 { 168 if ($this->getExecuted()) { 169 return $this->_numResults; 170 } 171 172 throw new Doctrine_Pager_Exception( 173 'Cannot retrieve the number of results of a not yet executed Pager query' 112 174 ); 113 $offset = ($this->getPage() - 1) * $this->getMaxPerPage();114 115 // Assign new offset and limit to Doctrine_Query object116 $p = $this->getQuery();117 $p->offset($offset);118 $p->limit($this->getMaxPerPage());119 }120 121 122 /**123 * getNumResults124 *125 * Returns the number of results found126 *127 * @return int the number of results found128 */129 public function getNumResults()130 {131 return $this->_numResults;132 175 } 133 176 … … 169 212 public function getLastPage() 170 213 { 171 return $this->_lastPage; 214 if ($this->getExecuted()) { 215 return $this->_lastPage; 216 } 217 218 throw new Doctrine_Pager_Exception( 219 'Cannot retrieve the last page number of a not yet executed Pager query' 220 ); 172 221 } 173 222 … … 213 262 public function getNextPage() 214 263 { 215 return min($this->getPage() + 1, $this->getLastPage()); 264 if ($this->getExecuted()) { 265 return $this->_lastPage; 266 } 267 268 throw new Doctrine_Pager_Exception( 269 'Cannot retrieve the last page number of a not yet executed Pager query' 270 );return min($this->getPage() + 1, $this->getLastPage()); 216 271 } 217 272 … … 226 281 public function getPreviousPage() 227 282 { 228 return max($this->getPage() - 1, $this->getFirstPage()); 283 if ($this->getExecuted()) { 284 return max($this->getPage() - 1, $this->getFirstPage()); 285 } 286 287 throw new Doctrine_Pager_Exception( 288 'Cannot retrieve the previous page number of a not yet executed Pager query' 289 ); 229 290 } 230 291 … … 239 300 public function haveToPaginate() 240 301 { 241 return $this->getNumResults() > $this->getMaxPerPage(); 302 if ($this->getExecuted()) { 303 return $this->getNumResults() > $this->getMaxPerPage(); 304 } 305 306 throw new Doctrine_Pager_Exception( 307 'Cannot know if it is necessary to paginate a not yet executed Pager query' 308 ); 242 309 } 243 310 … … 254 321 { 255 322 $this->_setPage($page); 256 $this->_ adjustOffset();323 $this->_setExecuted(false); 257 324 } 258 325 … … 295 362 */ 296 363 public function setMaxPerPage($max) 297 {298 $this->_setMaxPerPage($max);299 $this->_adjustOffset();300 }301 302 303 /**304 * _setMaxPerPage305 *306 * Defines the maximum number of itens per page307 *308 * @param $max maximum number of itens per page309 * @return void310 */311 private function _setMaxPerPage($max)312 364 { 313 365 if ($max > 0) { … … 318 370 $this->_maxPerPage = abs($max); 319 371 } 372 373 $this->_setExecuted(false); 320 374 } 321 375 … … 340 394 * 341 395 * @param $query Accepts either a Doctrine_Query object or a string 342 * (which does the Doctrine_Query class creation).396 * (which does the Doctrine_Query class creation). 343 397 * @return void 344 398 */ … … 355 409 /** 356 410 * execute 357 * executes the query and populates the data set 358 * 359 * @param $params Optional parameters to Doctrine_Query::execute 360 * @param $hydrationMode Hyddration Mode of Doctrine_Query::execute 361 * returned ResultSet. Doctrine::Default is FETCH_RECORD 362 * @return Doctrine_Collection the root collection 411 * 412 * Executes the query, populates the collection and then return it 413 * 414 * @param $params Optional parameters to Doctrine_Query::execute 415 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 416 * returned ResultSet. Doctrine::Default is FETCH_RECORD 417 * @return Doctrine_Collection The root collection 363 418 */ 364 419 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 365 420 { 421 $this->_initialize($params); 422 366 423 return $this->getQuery()->execute($params, $hydrationMode); 367 424 } -
trunk/lib/Doctrine/Pager/Exception.php
r3458 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Exception extends Doctrine_Exception -
trunk/lib/Doctrine/Pager/Layout.php
r3458 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Layout … … 122 122 123 123 /** 124 * execute 125 * 126 * Handy method to execute the query without need to retrieve the Pager instance 127 * 128 * @param $params Optional parameters to Doctrine_Query::execute 129 * @param $hydrationMode Hydration Mode of Doctrine_Query::execute 130 * returned ResultSet. Doctrine::Default is FETCH_RECORD 131 * @return Doctrine_Collection The root collection 132 */ 133 public function execute($params = array(), $hydrationMode = Doctrine::FETCH_RECORD) 134 { 135 return $this->getPager()->execute($params, $hydrationMode); 136 } 137 138 139 /** 124 140 * getPagerRange 125 141 * … … 256 272 $this->_separatorTemplate = $separatorTemplate; 257 273 } 258 259 274 275 260 276 /** 261 277 * addMaskReplacement … … 280 296 } 281 297 } 282 283 298 299 284 300 /** 285 301 * removeMaskReplacement … … 334 350 // Define some optional mask values 335 351 $options['page_number'] = $range[$i]; 336 $options['page'] = $range[$i]; // Handy assignment for URLs 337 $options['url'] = $this->_parseUrl($options); 338 339 $str .= $this->_parseTemplate($options); 352 353 $str .= $this->processPage($options); 340 354 341 355 // Apply separator between pages … … 353 367 } 354 368 355 /** 356 * simply calls display, and returns the output. 369 370 /** 371 * processPage 372 * 373 * Parses the template and returns the string of a processed page 374 * 375 * @param array Optional parameters to be applied in template and url mask 376 * @return string Processed template for the given page 377 */ 378 public function processPage($options = array()) 379 { 380 // Check if at least basic options are defined 381 if (!isset($options['page_number'])) { 382 throw new Doctrine_Pager_Exception( 383 'Cannot process template of the given page. ' . 384 'Missing at least one of needed parameters: \'page\' or \'page_number\'' 385 ); 386 387 // Should never reach here 388 return ''; 389 } 390 391 // Assign "page" options index if not defined yet 392 if (!isset($this->_maskReplacements['page']) && !isset($options['page'])) { 393 $options['page'] = $options['page_number']; 394 } 395 396 return $this->_parseTemplate($options); 397 } 398 399 400 /** 401 * Simply calls display, and returns the output. 357 402 */ 358 403 public function __toString() … … 367 412 * Process the template of a given page and return the processed template 368 413 * 369 * @param $optionsOptional parameters to be applied in template and url mask414 * @param array Optional parameters to be applied in template and url mask 370 415 * @return string 371 416 */ … … 374 419 $str = ''; 375 420 376 if (isset($options['page_number']) && $options['page_number'] == $this->getPager()->getPage()) { 421 // If given page is the current active one 422 if ($options['page_number'] == $this->getPager()->getPage()) { 377 423 $str = $this->_parseMaskReplacements($this->getSelectedTemplate()); 378 424 } … … 383 429 } 384 430 431 // Defining "url" options index to allow {%url} mask 432 $options['url'] = $this->_parseUrl($options); 433 385 434 $replacements = array(); 386 435 387 436 foreach ($options as $k => $v) { 388 437 $replacements['{%'.$k.'}'] = $v; -
trunk/lib/Doctrine/Pager/Range.php
r3458 r3529 29 29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 30 30 * @version $Revision$ 31 * @link www.phpdoctrine. com32 * @since 1.031 * @link www.phpdoctrine.org 32 * @since 0.9 33 33 */ 34 34 abstract class Doctrine_Pager_Range -
trunk/lib/Doctrine/Pager/Range/Jumping.php
r3458 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range … … 96 96 { 97 97 $pager = $this->getPager(); 98 $page = $pager->getPage();99 98 100 // Define initial assignments for StartPage and EndPage 101 $startPage = $page - ($page - 1) % $this->getChunkLength(); 102 $endPage = ($startPage + $this->getChunkLength()) - 1; 99 if ($pager->getExecuted()) { 100 $page = $pager->getPage(); 103 101 104 // Check for EndPage out-range 105 if ($endPage > $pager->getLastPage()) { 106 $endPage = $pager->getLastPage(); 102 // Define initial assignments for StartPage and EndPage 103 $startPage = $page - ($page - 1) % $this->getChunkLength(); 104 $endPage = ($startPage + $this->getChunkLength()) - 1; 105 106 // Check for EndPage out-range 107 if ($endPage > $pager->getLastPage()) { 108 $endPage = $pager->getLastPage(); 109 } 110 111 // No need to check for out-range in start, it will never happens 112 113 return range($startPage, $endPage); 107 114 } 108 115 109 // No need to check for out-range in start, it will never happens110 111 return range($startPage, $endPage);116 throw new Doctrine_Pager_Exception( 117 'Cannot retrieve the range around the page of a not yet executed Pager query' 118 ); 112 119 } 113 120 } -
trunk/lib/Doctrine/Pager/Range/Sliding.php
r3510 r3529 31 31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 32 32 * @version $Revision$ 33 * @link www.phpdoctrine. com34 * @since 1.033 * @link www.phpdoctrine.org 34 * @since 0.9 35 35 */ 36 36 class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range … … 101 101 { 102 102 $pager = $this->getPager(); 103 $page = $pager->getPage();104 $pages = $pager->getLastPage();105 103 106 $chunk = $this->getChunkLength(); 107 if ($chunk > $pages) { 108 $chunk = $pages; 104 if ($pager->getExecuted()) { 105 $page = $pager->getPage(); 106 $pages = $pager->getLastPage(); 107 108 $chunk = $this->getChunkLength(); 109 110 if ($chunk > $pages) { 111 $chunk = $pages; 112 } 113 114 $chunkStart = $page - (floor($chunk / 2)); 115 $chunkEnd = $page + (ceil($chunk / 2)-1); 116 117 if ($chunkStart < 1) { 118 $adjust = 1 - $chunkStart; 119 $chunkStart = 1; 120 $chunkEnd = $chunkEnd + $adjust; 121 } 122 123 if ($chunkEnd > $pages) { 124 $adjust = $chunkEnd - $pages; 125 $chunkStart = $chunkStart - $adjust; 126 $chunkEnd = $pages; 127 } 128 129 return range($chunkStart, $chunkEnd); 109 130 } 110 131 111 $chunkStart = $page - (floor($chunk / 2)); 112 $chunkEnd = $page + (ceil($chunk / 2)-1); 113 114 if ($chunkStart < 1) { 115 $adjust = 1 - $chunkStart; 116 $chunkStart = 1; 117 $chunkEnd = $chunkEnd + $adjust; 118 } 119 if ($chunkEnd > $pages) { 120 $adjust = $chunkEnd - $pages; 121 $chunkStart = $chunkStart - $adjust; 122 $chunkEnd = $pages; 123 } 124 125 return range($chunkStart, $chunkEnd); 126 132 throw new Doctrine_Pager_Exception( 133 'Cannot retrieve the range around the page of a not yet executed Pager query' 134 ); 127 135 } 128 136 }