24class XCube_PageNavigator {
41 public $mTotalItems = 0;
47 public $mPerpage = XCUBE_PAGENAVI_DEFAULT_PERPAGE;
54 public $mPerpageFreeze =
false;
94 public $_mIsSpecifedTotalItems =
false;
103 public $mGetTotalItems;
107 public $_mIsSpecifiedTotal;
123 public function __construct( $url, $flags = XCUBE_PAGENAVI_START ) {
125 $this->mFlags = $flags;
128 $this->mFetch->add( [ &$this,
'fetchNaviControl' ] );
142 $this->mFetch->call(
new XCube_Ref( $this ) );
145 public function fetchNaviControl( &$navi ) {
146 $root =& XCube_Root::getSingleton();
148 $startKey = $navi->getStartKey();
149 $perpageKey = $navi->getPerpageKey();
151 if ( $navi->mFlags & XCUBE_PAGENAVI_START ) {
152 $t_start = $root->mContext->mRequest->getRequest( $navi->getStartKey() );
153 if (
null !== $t_start && (
int) $t_start >= 0 ) {
154 $navi->mStart = (int) $t_start;
158 if ( $navi->mFlags & XCUBE_PAGENAVI_PERPAGE && ! $navi->mPerpageFreeze ) {
159 $t_perpage = $root->mContext->mRequest->getRequest( $navi->getPerpageKey() );
160 if (
null !== $t_perpage && (
int) $t_perpage > 0 ) {
161 $navi->mPerpage = (int) $t_perpage;
166 public function addExtra( $key, $value ) {
167 $this->mExtra[ $key ] = $value;
170 public function removeExtra( $key ) {
171 if ( $this->mExtra[ $key ] ) {
172 unset( $this->mExtra[ $key ] );
178 if ( ! is_array( $extra ) ) {
179 $query[] = $key .
'=' . urlencode( $extra );
181 foreach ( $extra as $k => $value ) {
182 $this->
_renderExtra( $key .
'[' . $k .
']', $value, $query );
187 public function getRenderBaseUrl( $mask =
null ) {
188 if (
null === $mask ) {
191 if ( ! is_array( $mask ) ) {
195 if ( count( $this->mExtra ) > 0 ) {
198 foreach ( $this->mExtra as $key => $value ) {
199 if ( is_array( $mask ) && ! in_array( $key, $mask,
true ) ) {
204 if ( 0 === (is_countable($tarr) ? count( $tarr ) : 0) ) {
208 if (
false !== strpos( $this->mUrl,
'?' ) ) {
209 return $this->mUrl .
'&' . implode(
'&', $tarr );
212 return $this->mUrl .
'?' . implode(
'&', $tarr );
225 if (
null !== $mask && ! is_array( $mask ) ) {
230 $url = $this->getRenderBaseUrl( $mask );
232 if (
false !== strpos( $url,
'?' ) ) {
233 $delimiter =
'&';
236 return $url . $delimiter . $this->getStartKey() .
'=';
239 public function renderUrlForSort() {
240 if ( count( $this->mExtra ) > 0 ) {
243 foreach ( $this->mExtra as $key => $value ) {
248 $tarr[] = $this->getPerpageKey() .
'=' . $this->mPerpage;
250 if (
false !== strpos( $this->mUrl,
'?' ) ) {
251 return $this->mUrl .
'&' . implode(
'&', $tarr );
254 return $this->mUrl .
'?' . implode(
'&', $tarr );
260 public function renderUrlForPage( $page =
null ) {
263 foreach ( $this->mExtra as $key => $value ) {
268 foreach ( $this->mSort as $key => $value ) {
269 $tarr[] = $key .
'=' . urlencode( $value );
272 $tarr[] = $this->getPerpageKey() .
'=' . $this->getPerpage();
274 if (
null !== $page ) {
275 $tarr[] = $this->getStartKey() .
'=' . (int) $page;
278 if (
false !== strpos( $this->mUrl,
'?' ) ) {
279 return $this->mUrl .
'&' . implode(
'&', $tarr );
282 return $this->mUrl .
'?' . implode(
'&', $tarr );
294 return $this->renderUrlForSort();
297 public function setStart( $start ) {
298 $this->mStart = (int) $start;
301 public function getStart() {
302 return $this->mStart;
305 public function setTotalItems( $total ) {
306 $this->mTotal = (int) $total;
307 $this->_mIsSpecifiedTotal =
true;
310 public function getTotalItems() {
311 if (
false === $this->_mIsSpecifedTotalItems ) {
312 $this->mGetTotalItems->call(
new XCube_Ref( $this->mTotal ) );
313 $this->_mIsSpecifedTotalItems =
true;
316 return $this->mTotal;
319 public function getTotalPages() {
320 if ( $this->getPerpage() > 0 ) {
321 return ceil( $this->getTotalItems() / $this->getPerpage() );
327 public function setPerpage( $perpage ) {
328 $this->mPerpage = (int) $perpage;
331 public function freezePerpage() {
332 $this->mPerpageFreeze =
true;
335 public function getPerpage() {
336 return $this->mPerpage;
339 public function setPrefix( $prefix ) {
340 $this->mPrefix = $prefix;
343 public function getPrefix() {
347 public function getStartKey() {
348 return $this->mPrefix .
'start';
351 public function getPerpageKey() {
352 return $this->mPrefix .
'perpage';
355 public function getCurrentPage() {
356 return (
int) floor( ( $this->getStart() + $this->getPerpage() ) / $this->getPerpage() );
359 public function hasPrivPage() {
360 return ( $this->getStart() - $this->getPerpage() ) >= 0;
363 public function getPrivStart() {
364 $prev = $this->getStart() - $this->getPerpage();
366 return ( $prev > 0 ) ? $prev : 0;
369 public function hasNextPage() {
370 return $this->getTotalItems() > ( $this->getStart() + $this->getPerpage() );
373 public function getNextStart() {
374 $next = $this->getStart() + $this->getPerpage();
376 return ( $this->getTotalItems() > $next ) ? $next : 0;
_renderExtra($key, $extra, &$query)
__construct( $url, $flags=XCUBE_PAGENAVI_START)