/** * Create and initialize pagination. * * @param Collection $collection */ public function __construct(Collection $collection) { require_once __DIR__ . '/paginationpage.php'; /** @var Uri $uri */ $uri = self::getGrav()['uri']; $config = self::getGrav()['config']; $this->current = $uri->currentPage(); // get params $url_params = explode('/', ltrim($uri->params(), '/')); foreach ($url_params as $key => $value) { if (strpos($value, 'page' . $config->get('system.param_sep')) !== false) { unset($url_params[$key]); } } $this->url_params = '/' . implode('/', $url_params); // check for empty params if ($this->url_params == '/') { $this->url_params = ''; } $params = $collection->params(); $this->items_per_page = $params['limit']; $this->page_count = ceil($collection->count() / $this->items_per_page); for ($x = 1; $x <= $this->page_count; $x++) { $this->items[$x] = new PaginationPage($x, '/page' . $config->get('system.param_sep') . $x); } }
/** * Get a collection of pages in the current context. * * @param string|array $params * @param boolean $pagination * @return Collection * @throws \InvalidArgumentException */ public function collection($params = 'content', $pagination = true) { if (is_string($params)) { $params = (array) $this->value('header.' . $params); } elseif (!is_array($params)) { throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters'); } if (!isset($params['items'])) { return array(); } $collection = $this->evaluate($params['items']); if (!$collection instanceof Collection) { $collection = new Collection(); } $collection->setParams($params); /** @var Uri $uri */ $uri = self::getGrav()['uri']; /** @var Config $config */ $config = self::getGrav()['config']; $process_taxonomy = isset($params['url_taxonomy_filters']) ? $params['url_taxonomy_filters'] : $config->get('system.pages.url_taxonomy_filters'); if ($process_taxonomy) { foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if ($uri->param($taxonomy)) { $items = explode(',', $uri->param($taxonomy)); $collection->setParams(['taxonomies' => [$taxonomy => $items]]); foreach ($collection as $page) { // Don't filter modular pages if ($page->modular()) { continue; } foreach ($items as $item) { if (empty($page->taxonomy[$taxonomy]) || !in_array(htmlspecialchars_decode($item, ENT_QUOTES), $page->taxonomy[$taxonomy])) { $collection->remove(); } } } } } } if (isset($params['dateRange'])) { $start = isset($params['dateRange']['start']) ? $params['dateRange']['start'] : 0; $end = isset($params['dateRange']['end']) ? $params['dateRange']['end'] : false; $field = isset($params['dateRange']['field']) ? $params['dateRange']['field'] : false; $collection->dateRange($start, $end, $field); } if (isset($params['order'])) { $by = isset($params['order']['by']) ? $params['order']['by'] : 'default'; $dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc'; $custom = isset($params['order']['custom']) ? $params['order']['custom'] : null; $collection->order($by, $dir, $custom); } /** @var Grav $grav */ $grav = self::getGrav()['grav']; // New Custom event to handle things like pagination. $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection])); // Slice and dice the collection if pagination is required if ($pagination) { $params = $collection->params(); $limit = isset($params['limit']) ? $params['limit'] : 0; $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0; if ($limit && $collection->count() > $limit) { $collection->slice($start, $limit); } } return $collection; }
/** * Get a collection of pages in the current context. * * @param string|array $params * @return Collection * @throws \InvalidArgumentException */ public function collection($params = 'content') { if (is_string($params)) { $params = (array) $this->value('header.' . $params); } elseif (!is_array($params)) { throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters'); } if (!isset($params['items'])) { return array(); } $collection = $this->evaluate($params['items']); if (!$collection instanceof Collection) { $collection = new Collection(); } $collection->setParams($params); // TODO: MOVE THIS INTO SOMEWHERE ELSE? /** @var Uri $uri */ $uri = self::$grav['uri']; /** @var Config $config */ $config = self::$grav['config']; foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if ($uri->param($taxonomy)) { $items = explode(',', $uri->param($taxonomy)); $collection->setParams(['taxonomies' => [$taxonomy => $items]]); foreach ($collection as $page) { if ($page->modular()) { continue; } foreach ($items as $item) { if (empty($page->taxonomy[$taxonomy]) || !in_array($item, $page->taxonomy[$taxonomy])) { $collection->remove(); } } } } } // TODO: END OF MOVE if (isset($params['order'])) { $by = isset($params['order']['by']) ? $params['order']['by'] : 'default'; $dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc'; $custom = isset($params['order']['custom']) ? $params['order']['custom'] : null; $collection->order($by, $dir, $custom); } /** @var Grav $grav */ $grav = self::$grav['grav']; // New Custom event to handle things like pagination. $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection])); $params = $collection->params(); $limit = isset($params['limit']) ? $params['limit'] : 0; $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0; if ($limit && $collection->count() > $limit) { $collection->slice($start, $limit); } return $collection; }