예제 #1
1
 public function __construct(Pagerfanta $paginator, Request $request, $base_url)
 {
     $this->data = $paginator->getCurrentPageResults();
     $this->total_count = $paginator->getNbResults();
     $this->count = count($this->data);
     $query = array('q' => $request->get('q'), 'limit' => $request->get('limit'), 'page' => $request->get('page'));
     $this->query = $query;
     $this->urls['current'] = $base_url . '?' . http_build_query($query);
     if ($paginator->hasPreviousPage()) {
         $query['page'] = $paginator->getPreviousPage();
         $this->urls['previous'] = $base_url . '?' . http_build_query($query);
         if ($paginator->getCurrentPage() > 2) {
             $query['page'] = 1;
             $this->urls['start'] = $base_url . '?' . http_build_query($query);
         }
     }
     if ($paginator->hasNextPage()) {
         $query['page'] = $paginator->getNextPage();
         $this->urls['next'] = $base_url . '?' . http_build_query($query);
         if ($paginator->getCurrentPage() < $paginator->getNbPages() - 1) {
             $query['page'] = $paginator->getNbPages();
             $this->urls['end'] = $base_url . '?' . http_build_query($query);
         }
     }
 }
 public function createRepresentationFromPagerfanta(Pagerfanta $pager)
 {
     $this->pager = empty($pager) ? $this->pager : $pager;
     if (empty($this->pager)) {
         return array();
     }
     $representation = array('hasToPaginate' => $this->pager->haveToPaginate(), 'hasNextPage' => $this->pager->hasNextPage(), 'hasPreviousPage' => $this->pager->hasPreviousPage(), 'totalItems' => $this->pager->getNbResults(), 'itemsPerPage' => $this->pager->getMaxPerPage(), 'currentPage' => $this->pager->getCurrentPage(), 'data' => $this->pager->getCurrentPageResults());
     return $representation;
 }
예제 #3
0
 public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePagesWithDeprecatedBooleansArguments()
 {
     $this->setAdapterNbResultsAny(100);
     $this->pagerfanta->setMaxPerPage(10);
     $this->pagerfanta->setCurrentPage(11, false, true);
     $this->assertSame(10, $this->pagerfanta->getCurrentPage());
 }
 /**
  * @param Pagerfanta $pager  The pager
  * @param Route      $route  The collection's route
  * @param mixed      $inline Most of the time, a custom `CollectionRepresentation` instance
  *
  * @return PaginatedRepresentation
  */
 public function createRepresentation(Pagerfanta $pager, Route $route, $inline = null)
 {
     if (null === $inline) {
         $inline = new CollectionRepresentation($pager->getCurrentPageResults());
     }
     return new PaginatedRepresentation($inline, $route->getName(), $route->getParameters(), $pager->getCurrentPage(), $pager->getMaxPerPage(), $pager->getNbPages(), $this->getPageParameterName(), $this->getLimitParameterName(), $route->isAbsolute(), $pager->getNbResults());
 }
예제 #5
0
 public function __construct(Pagerfanta $pagerfanta)
 {
     $photos = $pagerfanta->getCurrentPageResults();
     $this->photos = $photos instanceof \Traversable ? iterator_to_array($photos) : $photos;
     $this->page = $pagerfanta->getCurrentPage();
     $this->pagesCount = $pagerfanta->getNbPages();
     $this->totalCount = $pagerfanta->getNbResults();
 }
예제 #6
0
 /**
  * @param Request          $request
  * @param AdapterInterface $adapter
  * @param string|null      $route
  *
  * @return PaginatedRepresentation
  */
 protected function paginate(Request $request, AdapterInterface $adapter, $route = null, $routeParameters = null)
 {
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage($request->query->get(self::LIMIT_PARAMETER, 5));
     $pagerfanta->setCurrentPage($request->query->get(self::PAGE_PARAMETER, 1));
     $collection = new CollectionRepresentation($pagerfanta->getCurrentPageResults());
     $paginated = new PaginatedRepresentation($collection, $route ?? $request->get('_route'), $routeParameters ?? $request->attributes->get('_route_params'), $pagerfanta->getCurrentPage(), $pagerfanta->getMaxPerPage(), $pagerfanta->getNbPages(), self::PAGE_PARAMETER, self::LIMIT_PARAMETER, false, $pagerfanta->count());
     return $paginated;
 }
 private function getNavigationLinks(Pagerfanta $pager, array $params = array())
 {
     $page = $pager->getCurrentPage();
     $limit = $pager->getMaxPerPage();
     $links = [];
     if ($pager->getCurrentPage() > 1) {
         $links['first'] = $this->generateUrl('app_api_categories', array_merge($params, ['offset' => $this->getOffset(1, $limit)]));
     }
     if ($pager->hasPreviousPage()) {
         $links['previous'] = $this->generateUrl('app_api_categories', array_merge($params, ['offset' => $this->getOffset($pager->getPreviousPage(), $limit)]));
     }
     if ($pager->hasNextPage()) {
         $links['next'] = $this->generateUrl('app_api_categories', array_merge($params, ['offset' => $this->getOffset($pager->getNextPage(), $limit)]));
     }
     if ($pager->getNbPages() != $page) {
         $links['last'] = $this->generateUrl('app_api_categories', array_merge($params, ['offset' => $this->getOffset($pager->getNbPages(), $limit)]));
     }
     return $links;
 }
 /**
  * @param Pagerfanta $object
  *
  * @return PaginatedRepresentation
  */
 protected function createPaginatedRepresentation($object)
 {
     if (!$object instanceof Pagerfanta) {
         return;
     }
     $items = $object->getCurrentPageResults();
     if ($items instanceof \ArrayIterator) {
         $items = $items->getArrayCopy();
     }
     return new PaginatedRepresentation($items, $object->getCurrentPage(), $object->getMaxPerPage(), $object->getNbPages(), $object->getNbResults());
 }
예제 #9
0
 /**
  * @param JsonApiSerializationVisitor $visitor
  * @param Pagerfanta                  $pagerfanta
  * @param array                       $type
  * @param Context                     $context
  * @return Pagerfanta
  */
 public function serializePagerfanta(JsonApiSerializationVisitor $visitor, Pagerfanta $pagerfanta, array $type, Context $context)
 {
     $request = $this->requestStack->getCurrentRequest();
     $pagerfanta->setNormalizeOutOfRangePages(true);
     $pagerfanta->setAllowOutOfRangePages(true);
     $pagerfanta->setMaxPerPage($request->get('page[limit]', $this->paginationOptions['limit'], true));
     $pagerfanta->setCurrentPage($request->get('page[number]', 1, true));
     $results = $pagerfanta->getCurrentPageResults();
     if ($results instanceof \ArrayIterator) {
         $results = $results->getArrayCopy();
     }
     $data = $context->accept($results);
     $root = $visitor->getRoot();
     $root['meta'] = array('page' => $pagerfanta->getCurrentPage(), 'limit' => $pagerfanta->getMaxPerPage(), 'pages' => $pagerfanta->getNbPages(), 'total' => $pagerfanta->getNbResults());
     $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($pagerfanta->getNbPages()), 'prev' => $pagerfanta->hasPreviousPage() ? $this->getUriForPage($pagerfanta->getPreviousPage()) : null, 'next' => $pagerfanta->hasNextPage() ? $this->getUriForPage($pagerfanta->getNextPage()) : null);
     $visitor->setRoot($root);
     return $data;
 }
예제 #10
0
 protected function addPagination(Request $request, Pagerfanta $pager, $resource)
 {
     $route = $request->attributes->get('_route');
     $params = $request->attributes->get('_route_params');
     $params = array_merge($params, $request->query->all());
     $resource->setMetaValue('page', $pager->getCurrentPage());
     $resource->setMetaValue('count', $pager->getNbResults());
     $resource->setMetaValue('nextPage', null);
     $resource->setMetaValue('previousPage', null);
     $resource->setMetaValue('next', null);
     $resource->setMetaValue('previous', null);
     if ($pager->hasNextPage()) {
         $resource->setMetaValue('next', $this->generateUrl($route, array_replace($params, ['page' => $pager->getNextPage(), 'limit' => $pager->getMaxPerPage()]), true));
         $resource->setMetaValue('nextPage', $pager->getNextPage());
     }
     if ($pager->hasPreviousPage()) {
         $resource->setMetaValue('previous', $this->generateUrl($route, array_replace($params, ['page' => $pager->getPreviousPage(), 'limit' => $pager->getMaxPerPage()]), true));
         $resource->setMetaValue('previousPage', $pager->getPreviousPage());
     }
 }
예제 #11
0
 public function create(Pagerfanta $pager, $route, array $routeParameters = array())
 {
     return new PaginatedCollection($pager->getCurrentPageResults(), $route, $routeParameters, $pager->getCurrentPage(), $pager->getMaxPerPage(), $pager->getNbPages(), $this->pageParameterName, $this->limitParameterName);
 }
예제 #12
0
파일: Table.php 프로젝트: ekyna/table
 /**
  * Creates and returns the table view.
  *
  * @param Request $request
  * @throws RuntimeException
  * @return TableView
  */
 public function createView(Request $request = null)
 {
     if (null !== $request) {
         $this->requestHelper->setRequest($request);
     }
     $view = new TableView();
     $view->name = $this->getName();
     $view->options = ['selector' => $this->config->getSelector()];
     $view->selection_form = $this->config->getSelector();
     if (!$this->config->hasColumns()) {
         throw new RuntimeException('Table has no columns and cannot be generated.');
     }
     $class = $this->config->getDataClass();
     $alias = strtolower(substr($class, strrpos($class, '\\') + 1, 1));
     // TODO What if data is already set ?
     $queryBuilder = $this->entityManager->createQueryBuilder()->select($alias)->from($this->config->getDataClass(), $alias);
     if (null !== ($customizeQb = $this->config->getCustomizeQb())) {
         $customizeQb($queryBuilder, $alias);
     }
     $this->generateFilters($queryBuilder, $view);
     $this->generateColumns($queryBuilder, $view);
     $currentPage = $this->requestHelper->getVar($this->getName() . '_page', 1);
     /*$dql = $queryBuilder->getQuery()->getDQL();
       var_dump($dql);
       exit();*/
     $adapter = new DoctrineORMAdapter($queryBuilder);
     $pager = new Pagerfanta($adapter);
     $pager->setNormalizeOutOfRangePages(true)->setMaxPerPage($this->config->getMaxPerPage())->setCurrentPage($currentPage);
     $this->setData($pager->getCurrentPageResults());
     if ($currentPage != $pager->getCurrentPage()) {
         $this->requestHelper->setVar($this->getName() . '_page', $pager->getCurrentPage());
     }
     $view->pager = $pager;
     $this->generateCells($view);
     return $view;
 }
 /**
  * {@inheritdoc}
  */
 public function key()
 {
     return $this->pager->getCurrentPage();
 }
예제 #14
0
 protected function configureCollectionRepresentation(CollectionRepresentation $collectionRepresentation, Pagerfanta $pager, $entity = null, $collectionRel = null)
 {
     // Properties
     $collectionRepresentation->total = $pager->getNbResults();
     $collectionRepresentation->page = $pager->getCurrentPage();
     $collectionRepresentation->limit = $pager->getMaxPerPage();
     // Links between pages
     $createRoute = function ($page, $limit) use($entity, $collectionRel) {
         $parameters = array('search' => array('page' => $page, 'limit' => $limit));
         return null !== $entity && null !== $collectionRel ? $this->getUrlGenerator()->generateEntityCollectionUrl($entity, $collectionRel, $parameters) : $this->getUrlGenerator()->generateCollectionUrl($parameters);
     };
     $collectionRepresentation->addLink($this->atomLinkFactory->create('self', $createRoute($pager->getCurrentPage(), $pager->getMaxPerPage())));
     if ($pager->hasNextPage()) {
         $collectionRepresentation->addLink($this->atomLinkFactory->create('next', $createRoute($pager->getNextPage(), $pager->getMaxPerPage())));
     }
     if ($pager->hasPreviousPage()) {
         $collectionRepresentation->addLink($this->atomLinkFactory->create('previous', $createRoute($pager->getPreviousPage(), $pager->getMaxPerPage())));
     }
     $collectionRepresentation->addLink($this->atomLinkFactory->create('first', $createRoute(1, $pager->getMaxPerPage())));
     $collectionRepresentation->addLink($this->atomLinkFactory->create('last', $createRoute($pager->getNbPages(), $pager->getMaxPerPage())));
 }
 public function serializeToArray(GenericSerializationVisitor $visitor, Pagerfanta $pager, array $type, Context $context)
 {
     $resultsType = array('name' => 'array');
     if (isset($type['params'])) {
         $resultsType['params'] = $type['params'];
     }
     $shouldSetRoot = null === $visitor->getRoot();
     $data = array('page' => $pager->getCurrentPage(), 'limit' => $pager->getMaxPerPage(), 'total' => $pager->getNbResults(), 'results' => $visitor->getNavigator()->accept($pager->getCurrentPageResults(), $resultsType, $context));
     if (null !== ($links = $this->linkEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type)))) {
         $data[$this->linksJsonKey] = $links;
     }
     if (null !== ($relations = $this->embedderEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type)))) {
         $data[$this->relationsJsonKey] = $relations;
     }
     if ($shouldSetRoot) {
         $visitor->setRoot($data);
     }
     return $data;
 }
 /**
  * @return int
  * @see \ComPHPPuebla\Paginator\Paginator::getCurrentPage()
  */
 public function getCurrentPage()
 {
     return $this->pagerfanta->getCurrentPage();
 }
 /**
  * @param Pagerfanta $pager
  *
  * @return array
  */
 protected function getPagerMeta(Pagerfanta $pager)
 {
     if ($pager instanceof OutOfRangePager) {
         return array('currentPage' => $pager->getOriginalPage(), 'maxPerPage' => $pager->getMaxPerPage(), 'hasNextPage' => false, 'outOfRange' => true);
     }
     $meta = array('currentPage' => $pager->getCurrentPage(), 'maxPerPage' => $pager->getMaxPerPage(), 'pagesCount' => $pager->getNbPages(), 'hasNextPage' => $pager->hasNextPage(), 'resultsCount' => $pager->getNbResults(), 'hasPreviousPage' => $pager->hasPreviousPage());
     if ($pager->hasNextPage()) {
         $meta['nextPage'] = $pager->getNextPage();
     }
     if ($pager->hasPreviousPage()) {
         $meta['previousPage'] = $pager->getPreviousPage();
     }
     return $meta;
 }
예제 #18
0
 public function serializePagerfantaToJson(JsonSerializationVisitor $visitor, Pagerfanta $pagerfanta, array $type, Context $context)
 {
     $type['name'] = 'array';
     return ['items' => $visitor->visitArray((array) $pagerfanta->getCurrentPageResults(), $type, $context), 'pages_count' => $pagerfanta->getNbPages(), 'current_page' => $pagerfanta->getCurrentPage(), 'max_per_page' => $pagerfanta->getMaxPerPage(), 'items_count' => $pagerfanta->count()];
 }
 /**
  * @param Pagerfanta $paginator
  * @param $route
  * @param $limit
  */
 protected function addLinksToMetadata(Pagerfanta $paginator, $route, $limit)
 {
     $data = $this->get('bite_codes_rest_api_generator.services.response_data');
     $router = $this->get('router');
     $first = $router->generate($route, ['page' => 1, 'limit' => $limit], UrlGeneratorInterface::ABSOLUTE_URL);
     $prev = $paginator->hasPreviousPage() ? $router->generate($route, ['page' => $paginator->getPreviousPage(), 'limit' => $limit], UrlGeneratorInterface::ABSOLUTE_URL) : null;
     $current = $router->generate($route, ['page' => $paginator->getCurrentPage(), 'limit' => $limit], UrlGeneratorInterface::ABSOLUTE_URL);
     $next = $paginator->hasNextPage() ? $router->generate($route, ['page' => $paginator->getNextPage(), 'limit' => $limit], UrlGeneratorInterface::ABSOLUTE_URL) : null;
     $last = $router->generate($route, ['page' => $paginator->getNbPages(), 'limit' => $limit], UrlGeneratorInterface::ABSOLUTE_URL);
     $data->addLink('first', $first);
     $data->addLink('prev', $prev);
     $data->addLink('current', $current);
     $data->addLink('next', $next);
     $data->addLink('last', $last);
     $data->addMeta('total', $paginator->getNbResults());
 }
 /**
  * Return list of children node
  * @param \eZ\Publish\Core\Repository\Values\Content\Location $location
  * @param type $maxPerPage
  * @param type $currentPage
  * @return type
  */
 public function getFolderChildrens(\eZ\Publish\Core\Repository\Values\Content\Location $location, $currentUser, $maxPerPage, $currentPage = 1, $category)
 {
     $criteria = array(new Criterion\ParentLocationId($location->id), new Criterion\ContentTypeIdentifier(array('service_link')), new Criterion\Visibility(Criterion\Visibility::VISIBLE));
     if (isset($category) && $category != "all") {
         $criteria[] = new Criterion\Field('category', Criterion\Operator::CONTAINS, $category);
     }
     $query = new Query();
     $query->filter = new Criterion\LogicalAnd($criteria);
     $query->sortClauses = array($this->sortClauseAuto($location));
     $searchResult = $this->repository->getSearchService()->findContent($query);
     $subscritions = $this->fetchByUserId($currentUser->id);
     //$this->debug($subscritions);
     $content = array();
     $contentId = null;
     foreach ($searchResult->searchHits as $serviceLink) {
         if (!$contentId) {
             $contentId = $serviceLink->valueObject->getVersionInfo()->getContentInfo()->id;
         }
         $content[] = array('serviceLink' => $serviceLink->valueObject->contentInfo->mainLocationId, 'subscrition' => $this->hasSubscription($subscritions, $serviceLink->valueObject->getVersionInfo()->getContentInfo()->id));
     }
     $result['offset'] = ($currentPage - 1) * $maxPerPage;
     $adapter = new ArrayAdapter($content);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage($maxPerPage);
     $pagerfanta->setCurrentPage($currentPage);
     $httpReferer = $_SERVER['SCRIPT_URL'];
     if (isset($category)) {
         $httpReferer .= "?category=" . $category;
     } else {
         $httpReferer .= "?";
     }
     $result['offset'] = ($currentPage - 1) * $maxPerPage;
     $result['prev_page'] = $pagerfanta->hasPreviousPage() ? $pagerfanta->getPreviousPage() : 0;
     $result['next_page'] = $pagerfanta->hasNextPage() ? $pagerfanta->getNextPage() : 0;
     $result['nb_pages'] = $pagerfanta->getNbPages();
     $result['items'] = $pagerfanta->getCurrentPageResults();
     $result['base_href'] = $httpReferer;
     $result['current_page'] = $pagerfanta->getCurrentPage();
     $result['options'] = isset($contentId) ? $this->getCategorie($contentId, "ezselection") : array();
     return $result;
 }
    /**
     * Return list of event sorted 
     * @param Location $location
     * @param type $maxPerPage
     * @param type $currentPage
     * @return type
     */
    public function getFolderChildrens( Location $location, $maxPerPage, $currentPage = 1 )
    {

        $criteria = array(
            new Criterion\ParentLocationId( $location->parentLocationId ),
            new Criterion\ContentTypeIdentifier( array( 'agenda_event' ) ),
            new Criterion\Visibility( Criterion\Visibility::VISIBLE ),
            new Criterion\Field( 'publish_start', Criterion\Operator::LT, time() ),
            new Criterion\LogicalOr( array(
                new Criterion\Field( 'publish_end', Criterion\Operator::GT, time() ), new Criterion\Field( 'publish_end', Criterion\Operator::EQ, 0 )
                    ) )
        );
        $query = new Query();
        $query->filter = new Criterion\LogicalAnd( $criteria );
        $query->sortClauses = array(
            $this->sortClauseAuto( $location )
        );

        $searchResult = $this->repository->getSearchService()->findContent( $query );

        $content = array();
        foreach( $searchResult->searchHits as $agendaEvent )
        {
            $listDates = $this->getChildren( $agendaEvent );
            foreach( $listDates->searchHits as $agendaSchedule )
            {
                $content[] = array(
                    'AgendaEvent' => $agendaEvent->valueObject->contentInfo->mainLocationId,
                    'AgendaSchedule' => $agendaSchedule->valueObject->contentInfo->mainLocationId,
                    'start' => $this->getFormattedDate( $agendaSchedule, 'order' )
                );
            }
        }

        usort( $content, array( $this, 'agendaSortMethod' ) );

        $result['offset'] = ($currentPage - 1) * $maxPerPage;
        $adapter = new ArrayAdapter( $content );
        $pagerfanta = new Pagerfanta( $adapter );

        $pagerfanta->setMaxPerPage( $maxPerPage );
        $pagerfanta->setCurrentPage( $currentPage );

        $result['prev_page'] = $pagerfanta->hasPreviousPage() ? $pagerfanta->getPreviousPage() : 0;
        $result['next_page'] = $pagerfanta->hasNextPage() ? $pagerfanta->getNextPage() : 0;
        $result['nb_pages'] = $pagerfanta->getNbPages();
        $result['items'] = $pagerfanta->getCurrentPageResults();
        $result['base_href'] = "?";
        $result['current_page'] = $pagerfanta->getCurrentPage();
        return $result;
    }
 /**
  * Get the current page.
  *
  * @return int
  */
 public function getCurrentPage()
 {
     return $this->paginator->getCurrentPage();
 }
예제 #23
0
 /**
  * Construct a pagerfanta representation from the current request
  *
  * @param AdapterInterface $adapter - The adapter to use
  * @return QueryablePaginatedRepresentation
  */
 protected function getPagerfantaRepresentation(Request $request, AdapterInterface $adapter)
 {
     $pagerfanta = new Pagerfanta($adapter);
     $limit = $request->query->get('limit');
     $zeroLimit = false;
     if (!$limit && ($limit === 0 || $limit === '0')) {
         $limit = $pagerfanta->count();
         $zeroLimit = true;
     }
     if (!$limit) {
         $limit = 10;
     }
     $pagerfanta->setMaxPerPage($limit);
     $page = $request->query->get('page');
     $nbPages = $pagerfanta->getNbPages();
     if (!$page) {
         $page = 1;
     }
     // Avoid errors: redirect to max page
     if ($page > $nbPages) {
         $page = $nbPages;
     }
     $pagerfanta->setCurrentPage($page);
     $route = new Route($request->get('_route'), $request->attributes->get('_route_params'), false);
     return new QueryablePaginatedRepresentation(new CollectionRepresentation($pagerfanta->getCurrentPageResults()), $route->getName(), $route->getParameters(), $pagerfanta->getCurrentPage(), $zeroLimit ? 0 : $pagerfanta->getMaxPerPage(), $nbPages, $pagerfanta->count(), null, null, $route->isAbsolute(), $request->query->get('where'), $request->query->get('search'), $request->query->get('order'), null, null, null);
 }