/** * @param \Sonata\PageBundle\Model\PageInterface $page * * @return void */ public function fixUrl(PageInterface $page) { if ($page->isInternal()) { $page->setUrl(null); // internal routes do not have any url ... return; } // hybrid page cannot be altered if (!$page->isHybrid()) { // make sure Page has a valid url if ($page->getParent()) { if (!$page->getSlug()) { $page->setSlug(Page::slugify($page->getName())); } if ($page->getParent()->getUrl() == '/') { $base = '/'; } elseif (substr($page->getParent()->getUrl(), -1) != '/') { $base = $page->getParent()->getUrl() . '/'; } else { $base = $page->getParent()->getUrl(); } $page->setUrl($base . $page->getSlug()); } else { // a parent page does not have any slug - can have a custom url ... $page->setSlug(null); $page->setUrl('/' . $page->getSlug()); } } foreach ($page->getChildren() as $child) { $this->fixUrl($child); } }
/** * @param PageInterface $page * @param PageInterface $currentPage * @param array $choices * @param int $level */ private function childWalker(PageInterface $page, PageInterface $currentPage = null, &$choices, $level = 1) { if (!($currentPage && $currentPage->getId() == $page->getId())) { $choices[$page->getId()] = $page->getLongName(); foreach ($page->getChildren() as $child) { $this->childWalker($child, $currentPage, $choices, $level + 1); } } }
/** * @param PageInterface $page * @param PageInterface $currentPage * @param array $choices * @param int $level */ private function childWalker(PageInterface $page, PageInterface $currentPage = null, &$choices, $level = 1) { foreach ($page->getChildren() as $child) { if ($currentPage && $currentPage->getId() == $child->getId()) { continue; } if ($child->isDynamic()) { continue; } $choices[$child->getId()] = $child; $this->childWalker($child, $currentPage, $choices, $level + 1); } }
public function iterate($menu, PageInterface $page) { foreach ($page->getChildren() as $page) { if ($page->isHybrid() || !$page->getUrl()) { continue; } $menu->addChild($page->getName(), array('route' => $page->getRouteName(), 'routeParameters' => array('path' => $page->getUrl()))); if ($page->getChildren()) { $this->iterate($menu[$page->getName()], $page); } } }
/** * Generates an URL from a Page object * * @param PageInterface $page Page object * @param array $parameters An array of parameters * @param bool|string $referenceType The type of reference to be generated (one of the constants) * * @return string * * @throws \RuntimeException */ protected function generateFromPage(PageInterface $page, array $parameters = array(), $referenceType = self::ABSOLUTE_PATH) { if (isset($parameters['path'])) { unset($parameters['path']); } // hybrid pages use, by definition, the default routing mechanism if ($page->isHybrid()) { //return $this->router->generate($page->getRouteName(), $parameters, $referenceType); } $url = $this->getUrlFromPage($page); if ($url === false) { throw new \RuntimeException(sprintf('Page "%d" has no url or customUrl.', $page->getId())); } $url = $this->customDecorateUrl($page->getSite(), $url, $parameters, $referenceType); if ($page->getSite() !== $this->siteSelector->retrieve()) { $url = str_replace($this->siteSelector->retrieve()->getRelativePath(), $page->getSite()->getRelativePath(), $url); } return $url; }
/** * {@inheritdoc} */ public function findContainer($code, PageInterface $page, BlockInterface $parentContainer = null) { $container = null; if ($parentContainer) { // parent container is set, nothing to find, don't need to loop across the // name to find the correct container (main template level) $container = $parentContainer; } // first level blocks are containers if (!$container && $page->getBlocks()) { foreach ($page->getBlocks() as $block) { if ($block->getSetting('code') == $code) { $container = $block; break; } } } if (!$container) { $container = $this->blockInteractor->createNewContainer(array('enabled' => true, 'page' => $page, 'code' => $code, 'position' => 1, 'parent' => $parentContainer)); } return $container; }
/** * @param \Sonata\PageBundle\Model\PageInterface $page * * @return void */ public function fixUrl(PageInterface $page) { if ($page->isInternal()) { $page->setUrl(null); // internal routes do not have any url ... return; } // hybrid page cannot be altered if (!$page->isHybrid()) { // make sure Page has a valid url if ($page->getParent()) { foreach ($page->getTranslations() as $trans) { $locale = $trans->getLocale(); if (!$trans->getSlug()) { $trans->setSlug(ModelPage::slugify($trans->getName())); } $parent = $page->getParent(); foreach ($parent->getTranslations() as $ptrans) { if ($ptrans->getLocale() === $locale) { $url = $ptrans->getUrl(); if ($url == '/') { $base = '/'; } elseif (substr($url, -1) != '/') { $base = $url . '/'; } else { $base = $url; } $trans->setUrl($base . $trans->getSlug()); } } } } else { foreach ($page->getTranslations() as $trans) { // a parent page does not have any slug - can have a custom url ... $trans->setUrl('/' . $trans->getSlug()); } } } foreach ($page->getChildren() as $child) { $this->fixUrl($child); } }
/** * Updates the SEO page values for given page instance * * @param PageInterface $page */ protected function updateSeoPage(PageInterface $page) { if (!$this->seoPage) { return; } if ($page->getTitle()) { $this->seoPage->setTitle($page->getTitle() ?: $page->getName()); } if ($page->getMetaDescription()) { $this->seoPage->addMeta('name', 'description', $page->getMetaDescription()); } if ($page->getMetaKeyword()) { $this->seoPage->addMeta('name', 'keywords', $page->getMetaKeyword()); } $this->seoPage->addMeta('property', 'og:type', 'article'); $this->seoPage->addHtmlAttributes('prefix', 'og: http://ogp.me/ns#'); }
/** * Creates a base response for given page. * * @param PageInterface $page * * @return Response */ protected function createResponse(PageInterface $page) { if ($page->getTarget()) { $page->addHeader('Location', $this->router->generate($page->getTarget())); $response = new Response('', 302, $page->getHeaders() ?: array()); } else { $response = new Response('', 200, $page->getHeaders() ?: array()); } return $response; }
/** * Add children * * @param \Sonata\PageBundle\Model\PageInterface $children */ public function addChildren(PageInterface $children) { $this->children[] = $children; $children->setParent($this); }
/** * @param \Sonata\PageBundle\Model\PageInterface $page * @param array $params * @param null|\Symfony\Component\HttpFoundation\Response $response * @return null|\Symfony\Component\HttpFoundation\Response */ public function renderPage(PageInterface $page, array $params = array(), Response $response = null) { if (!$response) { if ($page->getTarget()) { $page->addHeader('Location', sprintf('%s%s', $this->getRouter()->getContext()->getBaseUrl(), $page->getTarget()->getUrl())); return new Response('', 302, $page->getHeaders()); } if ($page->getHeaders()) { $response = new Response('', 200, $page->getHeaders()); } } $template = false; if ($this->getCurrentPage()) { $template = $this->getPageManager()->getTemplate($this->getCurrentPage()->getTemplateCode())->getPath(); } if (!$template) { $template = $this->defaultTemplatePath; } $params = array_merge($params, $this->getRenderPageParams($page)); $response = $this->templating->renderResponse($template, $params, $response); $response->setTtl($page->getTtl()); return $response; }
/** * Returns the Url from a Page object. * * @param PageInterface $page * * @return string */ protected function getUrlFromPage(PageInterface $page) { return $page->getCustomUrl() ?: $page->getUrl(); }
public function save(PageInterface $page) { if (!$page->isHybrid() || $page->getRouteName() == 'homepage') { $this->fixUrl($page); } $this->entityManager->persist($page); $this->entityManager->flush(); return $page; }
/** * {@inheritdoc} */ public function getChildren(PageInterface $parent) { if (!isset($this->children[$parent->getId()])) { $date = new \Datetime(); $parameters = array('publicationDateStart' => $date, 'publicationDateEnd' => $date, 'parentId' => $parent->getId()); $manager = $this->registry->getManagerForClass($this->snapshotManager->getClass()); if (!$manager instanceof EntityManagerInterface) { throw new \RuntimeException('Invalid entity manager type'); } $snapshots_query = $manager->createQueryBuilder()->select('s')->from($this->snapshotManager->getClass(), 's')->where('s.parentId = :parentId and s.enabled = 1')->andWhere('s.publicationDateStart <= :publicationDateStart AND ( s.publicationDateEnd IS NULL OR s.publicationDateEnd >= :publicationDateEnd )')->orderBy('s.position')->setParameters($parameters)->getQuery(); $snapshots_query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); $snapshots_query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $parent->getSite()->getLocale()); $snapshots = $snapshots_query->execute(); $pages = array(); foreach ($snapshots as $snapshot) { $page = new SnapshotPageProxy($this->snapshotManager, $this, $snapshot); $pages[$page->getId()] = $page; } $this->children[$parent->getId()] = new \Doctrine\Common\Collections\ArrayCollection($pages); } return $this->children[$parent->getId()]; }
/** * {@inheritdoc} */ public function getBlocksById(PageInterface $page) { $blocks = $this->getEntityManager()->createQuery(sprintf('SELECT b FROM %s b INDEX BY b.id WHERE b.page = :page ORDER BY b.position ASC', $this->blockManager->getClass()))->setParameters(array('page' => $page->getId()))->useResultCache(true, 300)->execute(); return $blocks; }
/** * Get children * * @param \Sonata\PageBundle\Model\PageInterface $parent * @return Collection */ public function getChildren(PageInterface $parent) { if (!isset($this->children[$parent->getId()])) { $date = new \Datetime(); $parameters = array('publicationDateStart' => $date, 'publicationDateEnd' => $date, 'parentId' => $parent->getId()); $snapshots = $this->entityManager->createQueryBuilder()->select('s')->from('Application\\Sonata\\PageBundle\\Entity\\Snapshot', 's')->where('s.parentId = :parentId and s.enabled = 1')->andWhere('s.publicationDateStart <= :publicationDateStart AND ( s.publicationDateEnd IS NULL OR s.publicationDateEnd >= :publicationDateEnd )')->orderBy('s.position')->setParameters($parameters)->getQuery()->execute(); $pages = array(); foreach ($snapshots as $snapshot) { $page = new SnapshotPageProxy($this, $snapshot); $pages[$page->getId()] = $page; } $this->children[$parent->getId()] = new \Doctrine\Common\Collections\ArrayCollection($pages); } return $this->children[$parent->getId()]; }
/** * @param PageInterface $page * @param PageInterface $currentPage * @param array $choices * @param int $level */ private function childWalker(PageInterface $page, PageInterface $currentPage = null, &$choices, $level = 1, $addNotGrantedPage = false) { if (!($currentPage && $currentPage->getId() == $page->getId())) { if ($level > 1 || $this->isEditor || $this->securityContext->isGranted('EDIT', $page) || $addNotGrantedPage) { $choices[$page->getId()] = $page->getLongName(); } foreach ($page->getChildren() as $child) { if ($this->isEditor || $this->securityContext->isGranted('EDIT', $child)) { $this->childWalker($child, $currentPage, $choices, $level + 1); } } } }
/** * load the blocks of the $page * * @param \Sonata\PageBundle\Model\PageInterface $page */ public function loadBlocks(PageInterface $page) { $i = new RecursiveBlockIterator($page->getBlocks()); foreach ($i as $block) { $this->blocks[$block->getId()] = $block; } }
/** * load the blocks of the $page * * @param \Sonata\PageBundle\Model\PageInterface $page */ private function loadBlocks(PageInterface $page) { $i = new \RecursiveIteratorIterator(new RecursiveBlockIterator($page->getBlocks()), \RecursiveIteratorIterator::SELF_FIRST); foreach ($i as $block) { $this->blocks[$block->getId()] = $block; } }
/** * {@inheritdoc} */ public function getChildren(PageInterface $parent) { if (!isset($this->children[$parent->getId()])) { $date = new \Datetime(); $parameters = array('publicationDateStart' => $date, 'publicationDateEnd' => $date, 'parentId' => $parent->getId()); $manager = $this->registry->getManagerForClass($this->snapshotManager->getClass()); if (!$manager instanceof EntityManagerInterface) { throw new \RuntimeException('Invalid entity manager type'); } $snapshots = $manager->createQueryBuilder()->select('s')->from($this->snapshotManager->getClass(), 's')->where('s.parentId = :parentId and s.enabled = 1')->andWhere('s.publicationDateStart <= :publicationDateStart AND ( s.publicationDateEnd IS NULL OR s.publicationDateEnd >= :publicationDateEnd )')->orderBy('s.position')->setParameters($parameters)->getQuery()->execute(); $pages = array(); foreach ($snapshots as $snapshot) { $page = new SnapshotPageProxy($this->snapshotManager, $this, $snapshot); $pages[$page->getId()] = $page; } $this->children[$parent->getId()] = new \Doctrine\Common\Collections\ArrayCollection($pages); } return $this->children[$parent->getId()]; }
/** * Updates the SEO page values for given page instance * * @param PageInterface $page */ protected function updateSeoPage(PageInterface $page, $locale) { /** * @var LanguageVersion $languageVersion */ $languageVersion = $page->getSite()->getLanguageVersion($locale); $siteTitle = $languageVersion->getTitle() ?: $page->getSite()->getName(); if (!$page->getParent()) { $title = $siteTitle; } else { if ($page->getTitle()) { $title = $page->getTitle() . ' - ' . $siteTitle; } elseif ($page->getName()) { $title = $page->getName() . ' - ' . $siteTitle; } } $this->seoPage->setTitle($title); $this->seoPage->addMeta('property', 'og:title', $title); if ($page->getMetaDescription()) { $this->seoPage->addMeta('name', 'description', $page->getMetaDescription()); $this->seoPage->addMeta('property', 'og:description', $page->getMetaDescription()); } elseif ($languageVersion->getMetaDescription()) { $this->seoPage->addMeta('name', 'description', $languageVersion->getMetaDescription()); $this->seoPage->addMeta('property', 'og:description', $languageVersion->getMetaDescription()); } if ($page->getMetaKeyword()) { $this->seoPage->addMeta('name', 'keywords', $page->getMetaKeyword()); } elseif ($languageVersion->getMetaKeywords()) { $this->seoPage->addMeta('name', 'keywords', $languageVersion->getMetaKeywords()); } $this->seoPage->addMeta('property', 'og:site_name', $languageVersion->getTitle()); $this->seoPage->addMeta('property', 'og:url', 'http://' . $languageVersion->getHost() . $languageVersion->getRelativePath()); $this->seoPage->addMeta('property', 'og:type', 'website'); $this->seoPage->addHtmlAttributes('prefix', 'og: http://ogp.me/ns#'); }
/** * @throws \RunTimeException * @param null|\Sonata\PageBundle\Model\PageInterface|string $page * @param bool $absolute * @return string */ public function url($page = null, $absolute = false) { if (!$page) { return ''; } $context = $this->router->getContext(); if ($page instanceof PageInterface) { if ($page->isDynamic()) { if ($this->environment->isDebug()) { throw new \RunTimeException('Unable to generate path for dynamic page'); } return ''; } $url = $page->getCustomUrl() ?: $page->getUrl(); } else { $url = $page; } $url = sprintf('%s%s', $context->getBaseUrl(), $url); if ($absolute && $context->getHost()) { $scheme = $context->getScheme(); $port = ''; if ('http' === $scheme && 80 != $context->getHttpPort()) { $port = ':' . $context->getHttpPort(); } elseif ('https' === $scheme && 443 != $context->getHttpsPort()) { $port = ':' . $context->getHttpsPort(); } $url = $scheme . '://' . $context->getHost() . $port . $url; } return $url; }
/** * {@inheritDoc} */ public function cleanup(PageInterface $page, $keep) { if (!is_numeric($keep)) { throw new \RuntimeException(sprintf('Please provide an integer value, %s given', gettype($keep))); } $tableName = $this->getTableName(); $platform = $this->getConnection()->getDatabasePlatform()->getName(); if ('mysql' === $platform) { return $this->getConnection()->exec(sprintf('DELETE FROM %s WHERE page_id = %d AND id NOT IN ( SELECT id FROM ( SELECT id, publication_date_end FROM %s WHERE page_id = %d ORDER BY publication_date_end IS NULL DESC, publication_date_end DESC LIMIT %d ) AS table_alias )', $tableName, $page->getId(), $tableName, $page->getId(), $keep)); } if ('oracle' === $platform) { return $this->getConnection()->exec(sprintf('DELETE FROM %s WHERE page_id = %d AND id NOT IN ( SELECT id FROM ( SELECT id, publication_date_end FROM %s WHERE page_id = %d AND rownum <= %d ORDER BY publication_date_end DESC ) AS table_alias )', $tableName, $page->getId(), $tableName, $page->getId(), $keep)); } throw new \RuntimeException(sprintf('The %s database platform has not been tested yet. Please report us if it works and feel free to create a pull request to handle it ;-)', $platform)); }
/** * {@inheritdoc} */ public function loadPageBlocks(PageInterface $page) { if (isset($this->pageBlocksLoaded[$page->getId()])) { return array(); } $blocks = $this->getBlocksById($page); $page->disableBlockLazyLoading(); foreach ($blocks as $block) { $parent = $block->getParent(); $block->disableChildrenLazyLoading(); if (!$parent) { $page->addBlocks($block); continue; } $blocks[$block->getParent()->getId()]->disableChildrenLazyLoading(); $blocks[$block->getParent()->getId()]->addChildren($block); } $this->pageBlocksLoaded[$page->getId()] = true; return $blocks; }
/** * Updates the SEO page values for given page instance * * @param PageInterface $page */ protected function updateSeoPage(PageInterface $page, Request $request) { /** * @var LanguageVersion $languageVersion */ $languageVersion = $page->getSite()->getLanguageVersion($request->getLocale()); $siteTitle = $languageVersion->getTitle() ?: $page->getSite()->getName(); if (!$page->getParent()) { $title = $siteTitle; } else { if ($page->getTitle()) { $title = $page->getTitle() . ' - ' . $siteTitle; } elseif ($page->getName()) { $title = $page->getName() . ' - ' . $siteTitle; } else { $title = $siteTitle; } } $this->seoPage->setTitle($title); $this->seoPage->addMeta('property', 'og:title', $title); if ($page->getMetaDescription()) { $this->seoPage->addMeta('name', 'description', $page->getMetaDescription()); $this->seoPage->addMeta('property', 'og:description', $page->getMetaDescription()); } elseif ($languageVersion->getMetaDescription()) { $this->seoPage->addMeta('name', 'description', $languageVersion->getMetaDescription()); $this->seoPage->addMeta('property', 'og:description', $languageVersion->getMetaDescription()); } if ($page->getMetaKeyword()) { $this->seoPage->addMeta('name', 'keywords', $page->getMetaKeyword()); } elseif ($languageVersion->getMetaKeywords()) { $this->seoPage->addMeta('name', 'keywords', $languageVersion->getMetaKeywords()); } if ($page->getOgImage()) { //$ogImageUrl = $this->get('sonata.media.twig.extension')->path($ogImage, 'og_image'); TODO $this->seoPage->addMeta('property', 'og:image', strpos($page->getOgImage(), '://') !== false ? $page->getOgImage() : sprintf('%s://%s%s', isset($_SERVER['HTTPS']) ? 'https' : 'http', $_SERVER['HTTP_HOST'], $page->getOgImage())); } $this->seoPage->addMeta('property', 'og:site_name', $languageVersion->getTitle()); $this->seoPage->addMeta('property', 'og:url', $request->getUri()); $this->seoPage->addMeta('property', 'og:type', 'website'); $this->seoPage->addHtmlAttributes('prefix', 'og: http://ogp.me/ns#'); }