/** * Gets the page's data for page rendering. * * It's better to call IPage::getData, which calls this function, because it * will also cache the results. It's useful for example when pagination * results needs to be re-used. */ public static function getPageData(IPage $page) { $pieCrust = $page->getApp(); $paginator = new Paginator($page); $assetor = new Assetor($page); $linker = new Linker($page); if ($page->getPaginationDataSource() != null) { $paginator->setPaginationDataSource($page->getPaginationDataSource()); } $data = array('page' => $page->getConfig()->get(), 'asset' => $assetor, 'pagination' => $paginator, 'link' => $linker); $data['page']['url'] = PieCrustHelper::formatUri($pieCrust, $page->getUri()); $data['page']['slug'] = $page->getUri(); $data['page']['timestamp'] = $page->getDate(); $dateFormat = PageHelper::getConfigValueUnchecked($page, 'date_format', $page->getConfig()->getValueUnchecked('blog')); $data['page']['date'] = date($dateFormat, $page->getDate()); switch ($page->getPageType()) { case IPage::TYPE_TAG: if (is_array($page->getPageKey())) { $data['tag'] = implode(' + ', $page->getPageKey()); } else { $data['tag'] = $page->getPageKey(); } break; case IPage::TYPE_CATEGORY: $data['category'] = $page->getPageKey(); break; } $extraData = $page->getExtraPageData(); if ($extraData) { if (is_array($extraData)) { $data = Configuration::mergeArrays($data, $extraData); } else { $data['extra'] = $extraData; } } return $data; }
/** * Gets the page's data for page rendering. * * It's better to call IPage::getData, which calls this function, because it * will also cache the results. It's useful for example when pagination * results needs to be re-used. */ public static function getPageData(IPage $page) { $pieCrust = $page->getApp(); $paginator = new Paginator($page); $assetor = new Assetor($page); $linker = new Linker($page); $recursiveLinker = new RecursiveLinkerIterator($linker); if ($page->getPaginationDataSource() != null) { $paginator->setPaginationDataSource($page->getPaginationDataSource()); } $data = array('page' => $page->getConfig()->get(), 'assets' => $assetor, 'pagination' => $paginator, 'siblings' => $linker, 'family' => $recursiveLinker); $data['page']['url'] = PieCrustHelper::formatUri($pieCrust, $page->getUri()); $data['page']['slug'] = $page->getUri(); $data['page']['timestamp'] = $page->getDate(true); $dateFormat = PageHelper::getConfigValueUnchecked($page, 'date_format', $page->getConfig()->getValueUnchecked('blog')); $data['page']['date'] = date($dateFormat, $page->getDate(true)); switch ($page->getPageType()) { case IPage::TYPE_TAG: if (is_array($page->getPageKey())) { $data['tag'] = implode(' + ', $page->getPageKey()); } else { $data['tag'] = $page->getPageKey(); } if (strpos($data['tag'], '-') >= 0) { // The tag may have been slugified. Let's cheat a bit by looking at // the first tag that matches in the first pagination post, and // using that instead. $paginationPosts = $paginator->posts(); if (count($paginationPosts) > 0) { $firstPost = $paginationPosts[0]; $firstPostTags = $firstPost['tags']; if (!is_array($firstPostTags)) { $firstPostTags = array($firstPostTags); } $flags = $pieCrust->getConfig()->getValue('site/slugify_flags'); if (is_array($page->getPageKey())) { $pageKey = $page->getPageKey(); foreach ($firstPostTags as $t) { $st = UriBuilder::slugify($t, $flags); foreach ($pageKey as &$pk) { if ($st == $pk) { $pk = $t; break; } } } if ($page->getPageKey() == null) { $page->setPageKey($pageKey); } $data['tag'] = implode(' + ', $pageKey); } else { foreach ($firstPostTags as $t) { if (UriBuilder::slugify($t, $flags) == $data['tag']) { if ($page->getPageKey() == null) { $page->setPageKey($t); } $data['tag'] = $t; break; } } } } } break; case IPage::TYPE_CATEGORY: $data['category'] = $page->getPageKey(); if (strpos($page->getPageKey(), '-') >= 0) { // Same remark as for tags. $paginationPosts = $paginator->posts(); if (count($paginationPosts) > 0) { $firstPost = $paginationPosts[0]; if ($page->getPageKey() == null) { $page->setPageKey($firstPost['category']); } $data['category'] = $firstPost['category']; } } break; } $extraData = $page->getExtraPageData(); if ($extraData) { if (is_array($extraData)) { $data = Configuration::mergeArrays($data, $extraData); } else { $data['extra'] = $extraData; } } return $data; }
/** * @dataProvider categoryPageDataProvider */ public function testCategoryPage($pageConfig, $categorize, $tagging, $expectedIndices) { $pc = new MockPieCrust(); $page = new MockPage($pc); $page->pageType = IPage::TYPE_CATEGORY; $page->pageKey = 'foo'; foreach ($pageConfig as $key => $value) { $page->getConfig()->setValue($key, $value); } $posts = $this->buildPaginationDataSource($pc, 10); // The pagination data source is ordered in reverse // chronological order. Let's reverse it to be able // to index posts easily. // (the Paginator will reorder them internally) $posts = array_reverse($posts); foreach ($categorize as $i => $category) { $posts[$i]->getConfig()->setValue('category', $category); } foreach ($tagging as $i => $tags) { $posts[$i]->getConfig()->setValue('tags', $tags); } $paginator = new Paginator($page); $paginator->setPaginationDataSource($posts); $this->assertExpectedPostsData($expectedIndices, $paginator->posts()); }
/** * @dataProvider previousAndNextPostsDataProvider */ public function testPreviousAndNextPosts($postCount, $currentPostIndex) { $pc = new MockPieCrust(); $pc->getConfig()->setValue('blog/posts_per_page', 20); $pc->getConfig()->setValue('blog/date_format', 'F j, Y'); $posts = $this->buildPaginationDataSource($pc, $postCount); // The pagination data source is ordered in reverse // chronological order. Let's reverse it to be able // to index next/current/previous posts easily. // (the Paginator will reorder them internally) $posts = array_reverse($posts); $page = $posts[$currentPostIndex]; $paginator = new Paginator($page); $paginator->setPaginationDataSource($posts); $nextPost = $paginator->next_post(); $prevPost = $paginator->prev_post(); if ($currentPostIndex > 0) { $this->assertEquals($posts[$currentPostIndex - 1]->getUri(), $prevPost['slug']); } else { $this->assertNull($prevPost); } if ($currentPostIndex < $postCount - 1) { $this->assertEquals($posts[$currentPostIndex + 1]->getUri(), $nextPost['slug']); } else { $this->assertNull($nextPost); } }