getPage() public method

Returns the zero-based current page number.
public getPage ( boolean $recalculate = false ) : integer
$recalculate boolean whether to recalculate the current page based on the page size and item count.
return integer the zero-based current page number.
示例#1
0
 /**
  * Creates an url for the specified page size.
  * @param \yii\data\Pagination $pagination
  * @param integer $pageSize page size
  * @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
  *
  * @return string the created URL
  */
 public static function createSizeUrl($pagination, $pageSize, $absolute = false)
 {
     if (($params = $pagination->params) === null) {
         $request = Yii::$app->getRequest();
         $params = $request instanceof Request ? $request->getQueryParams() : [];
     }
     $currentPageSize = $pagination->getPageSize();
     $currentPage = $pagination->getPage();
     $target = $currentPage * $currentPageSize;
     $page = (int) ($target / $pageSize);
     if ($page > 0 || $page >= 0 && $pagination->forcePageParam) {
         $params[$pagination->pageParam] = $page + 1;
     } else {
         unset($params[$pagination->pageParam]);
     }
     if ($pageSize != $pagination->defaultPageSize) {
         $params[$pagination->pageSizeParam] = $pageSize;
     } else {
         unset($params[$pagination->pageSizeParam]);
     }
     $params[0] = $pagination->route === null ? Yii::$app->controller->getRoute() : $pagination->route;
     $urlManager = $pagination->urlManager === null ? Yii::$app->getUrlManager() : $pagination->urlManager;
     if ($absolute) {
         return $urlManager->createAbsoluteUrl($params);
     } else {
         return $urlManager->createUrl($params);
     }
 }
示例#2
0
 /**
  * @param Pagination $pagination
  * @return string
  */
 protected function getPage(Pagination $pagination)
 {
     $page = $pagination->getPage() + 1;
     if ($page == 1) {
         return '';
     }
     return ' (' . Yii::t('forum', 'Page number', ['page' => $page]) . ')';
 }
示例#3
0
 /**
  * Returns the zero-based current page number.
  * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
  * @return integer the zero-based current page number.
  */
 public function getPage($recalculate = false)
 {
     if ($this->_page === null || $recalculate) {
         $page = (int) $this->getQueryParam($this->pageParam, 1) - 1;
         $this->setPage($page, true);
     }
     $page = isset($page) ? $page : $this->getQueryParam($this->pageParam, 1);
     if ($page) {
         $pagesCount = ceil($this->totalCount / $this->pageSize);
         if ($pagesCount < (int) $page + 1) {
             throw new NotFoundHttpException();
         }
     }
     return parent::getPage();
 }
示例#4
0
 public function actionGetTopics()
 {
     $query = Topic::find()->where(['active' => 1])->with('tags');
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 3]);
     $cacheTopics = 'Topics' . $pages->getPage();
     if (Yii::$app->request->get('cache')) {
         Yii::$app->cache->delete($cacheTopics);
     }
     if (false === ($topics = Yii::$app->cache->get($cacheTopics))) {
         if (null === ($topics = $query->offset($pages->offset)->limit($pages->limit)->all())) {
             throw new NotFoundHttpException();
         }
         Yii::$app->cache->set($cacheTopics, $topics, 86400, new TagDependency(['tags' => []]));
     }
     $this->renderJSON($topics);
 }
示例#5
0
 /**
  * Adds HTTP headers about the pagination to the response.
  * @param Pagination $pagination
  */
 protected function addPaginationHeaders($pagination)
 {
     $links = [];
     foreach ($pagination->getLinks(true) as $rel => $url) {
         $links[] = "<{$url}>; rel={$rel}";
     }
     $this->response->getHeaders()->set($this->totalCountHeader, $pagination->totalCount)->set($this->pageCountHeader, $pagination->getPageCount())->set($this->currentPageHeader, $pagination->getPage() + 1)->set($this->perPageHeader, $pagination->pageSize)->set('Link', implode(', ', $links));
 }
示例#6
0
 protected function cacheKeyPaginate(Pagination $paginate)
 {
     $keys = ['page' => $paginate->getPage(), 'per_page' => $paginate->getPageSize()];
     return md5(serialize($keys));
 }
示例#7
0
 /**
  * Serializes a pagination into an array.
  * @param \yii\data\Pagination $pagination
  * @return array the array representation of the pagination
  * @see addPaginationHeaders()
  */
 protected function serializePagination($pagination)
 {
     if ($this->serializedPagination !== null) {
         return $this->serializedPagination;
     }
     /** @var \yii\rest\Serializer $serializer */
     $serializer = $this->serializer;
     return $this->serializedPagination = [$serializer->linksEnvelope => \yii\web\Link::serialize($pagination->getLinks(true)), $serializer->metaEnvelope => ['totalCount' => $pagination->totalCount, 'pageCount' => $pagination->getPageCount(), 'currentPage' => $pagination->getPage() + 1, 'perPage' => $pagination->getPageSize()]];
 }
示例#8
0
 /**
  * @inheritdoc
  * @see yii\data\Pagination::setPage()
  */
 public function setPage($value, $validatePage = false)
 {
     parent::setPage($value);
     $this->_page = parent::getPage();
 }
示例#9
0
 /**
  * Serializes a pagination into an array.
  * @param Pagination $pagination
  * @return array the array representation of the pagination
  * @see addPaginationHeaders()
  */
 protected function serializePagination($pagination)
 {
     return [$this->linksEnvelope => Link::serialize($pagination->getLinks(true)), $this->metaEnvelope => ['total-count' => $pagination->totalCount, 'page-count' => $pagination->getPageCount(), 'current-page' => $pagination->getPage() + 1, 'per-page' => $pagination->getPageSize()]];
 }
示例#10
-1
 public function testValidatePage()
 {
     $pagination = new Pagination();
     $pagination->validatePage = true;
     $pagination->pageSize = 10;
     $pagination->totalCount = 100;
     $pagination->setPage(999, true);
     $this->assertEquals(9, $pagination->getPage());
     $pagination->setPage(999, false);
     $this->assertEquals(999, $pagination->getPage());
 }