/** * @param Options $options * * @return array */ public function getChoices(Options $options) { if (!$options['site'] instanceof SiteInterface) { return array(); } $filter_choice = array_merge(array('current_page' => false, 'request_method' => 'GET', 'dynamic' => true, 'hierarchy' => 'all'), $options['filter_choice']); $pages = $this->manager->loadPages($options['site']); $choices = array(); foreach ($pages as $page) { // internal cannot be selected if ($page->isInternal()) { continue; } if (!$filter_choice['current_page'] && $options['page'] && $options['page']->getId() == $page->getId()) { continue; } if ('all' != $filter_choice['hierarchy'] && (('root' != $filter_choice['hierarchy'] || $page->getParent()) && ('children' != $filter_choice['hierarchy'] || !$page->getParent()))) { continue; } if ('all' !== $filter_choice['dynamic'] && ($filter_choice['dynamic'] && $page->isDynamic() || !$filter_choice['dynamic'] && !$page->isDynamic())) { continue; } if ('all' != $filter_choice['request_method'] && !$page->hasRequestMethod($filter_choice['request_method'])) { continue; } $choices[$page->getId()] = $page; $this->childWalker($page, $options['page'], $choices); } return $choices; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$value instanceof PageInterface) { $this->context->addViolation('The page is not valid, expected a PageInterface'); return; } if (!$value->getSite() instanceof SiteInterface) { $this->context->addViolation('The page is not linked to a Site'); return; } // do not validated error page if ($value->isError()) { return; } $this->manager->fixUrl($value); $pages = $this->manager->findBy(array('site' => $value->getSite(), 'url' => $value->getUrl())); foreach ($pages as $page) { if ($page->isError() || $page->isInternal()) { continue; } if ($page->getUrl() == $value->getUrl() && $page != $value) { $this->context->addViolation('error.uniq_url', array('%url%' => $value->getUrl())); } } }
/** * {@inheritdoc} */ public function process(ConsumerEvent $event) { $pages = $this->pageManager->findBy(array('site' => $event->getMessage()->getValue('siteId'))); $backend = $event->getMessage()->getValue('mode') === 'async' ? $this->asyncBackend : $this->runtimeBackend; $keepSnapshots = $event->getMessage()->getValue('keepSnapshots'); foreach ($pages as $page) { $backend->createAndPublish('sonata.page.cleanup_snapshot', array('pageId' => $page->getId(), 'keepSnapshots' => $keepSnapshots)); } }
public function generatePageMenu($menu) { $site = $this->site_selector->retrieve(); $page = $this->page_manager->getPageByUrl($site, '/'); $menu->addChild($page->getName(), array('route' => $page->getRouteName(), 'routeParameters' => array('path' => $page->getUrl()))); if (!$page) { return false; } $this->iterate($menu[$page->getName()], $page); }
/** * {@inheritdoc} */ public function process(ConsumerEvent $event) { $page = $this->pageManager->findOneBy(array('id' => $event->getMessage()->getValue('pageId'))); if (!$page) { return; } // start a transaction $this->snapshotManager->getConnection()->beginTransaction(); // cleanup snapshots $this->snapshotManager->cleanup($page, $event->getMessage()->getValue('keepSnapshots')); // commit the changes $this->snapshotManager->getConnection()->commit(); }
/** * {@inheritdoc} */ public function getNewInstance() { $instance = parent::getNewInstance(); if (!$this->hasRequest()) { return $instance; } if ($site = $this->getSite()) { $instance->setSite($site); } if ($site && $this->getRequest()->get('url')) { $slugs = explode('/', $this->getRequest()->get('url')); $slug = array_pop($slugs); try { $parent = $this->pageManager->getPageByUrl($site, implode('/', $slugs)); } catch (PageNotFoundException $e) { try { $parent = $this->pageManager->getPageByUrl($site, '/'); } catch (PageNotFoundException $e) { throw new InternalErrorException('Unable to find the root url, please create a route with url = /'); } } $instance->setSlug(urldecode($slug)); $instance->setParent($parent ?: null); $instance->setName(urldecode($slug)); } return $instance; }
/** * {@inheritdoc} */ protected function getPageBy(SiteInterface $site = null, $fieldName, $value) { if ('id' == $fieldName) { $id = $value; } elseif (isset($this->pageReferences[$fieldName][$value])) { $id = $this->pageReferences[$fieldName][$value]; } else { $id = null; } if (null === $id || !isset($this->pages[$id])) { $this->pages[$id] = false; $parameters = array($fieldName => $value); if ($site) { $parameters['site'] = $site->getId(); } $page = $this->pageManager->findOneBy($parameters); if (!$page) { throw new PageNotFoundException(sprintf('Unable to find the page : %s = %s', $fieldName, $value)); } $this->loadBlocks($page); $id = $page->getId(); if ($fieldName != 'id') { $this->pageReferences[$fieldName][$value] = $id; } $this->pages[$id] = $page; } return $this->pages[$id]; }
/** * {@inheritdoc} */ public function load(SnapshotInterface $snapshot) { $page = $this->pageManager->create(); $page->setRouteName($snapshot->getRouteName()); $page->setPageAlias($snapshot->getPageAlias()); $page->setType($snapshot->getType()); $page->setCustomUrl($snapshot->getUrl()); $page->setUrl($snapshot->getUrl()); $page->setPosition($snapshot->getPosition()); $page->setDecorate($snapshot->getDecorate()); $page->setSite($snapshot->getSite()); $page->setEnabled($snapshot->getEnabled()); $content = $this->fixPageContent($snapshot->getContent()); $page->setId($content['id']); $page->setJavascript($content['javascript']); $page->setStylesheet($content['stylesheet']); $page->setRawHeaders($content['raw_headers']); $page->setTitle($content['title']); $page->setMetaDescription($content['meta_description']); $page->setMetaKeyword($content['meta_keyword']); $page->setName($content['name']); $page->setSlug($content['slug']); $page->setTemplateCode($content['template_code']); $page->setRequestMethod($content['request_method']); $createdAt = new \DateTime(); $createdAt->setTimestamp($content['created_at']); $page->setCreatedAt($createdAt); $updatedAt = new \DateTime(); $updatedAt->setTimestamp($content['updated_at']); $page->setUpdatedAt($updatedAt); return $page; }
/** * Retrieves page with id $id or throws an exception if it doesn't exist * * @param $id * * @return PageInterface * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function getPage($id) { $page = $this->pageManager->findOneBy(array('id' => $id)); if (null === $page) { throw new NotFoundHttpException(sprintf('Page (%d) not found', $id)); } return $page; }
/** * {@inheritdoc} */ public function process(ConsumerEvent $event) { $pageId = $event->getMessage()->getValue('pageId'); $page = $this->pageManager->findOneBy(array('id' => $pageId)); if (!$page) { return; } // start a transaction $this->snapshotManager->getConnection()->beginTransaction(); // creating snapshot $snapshot = $this->transformer->create($page); // update the page status $page->setEdited(false); $this->pageManager->save($page); // save the snapshot $this->snapshotManager->save($snapshot); $this->snapshotManager->enableSnapshots(array($snapshot)); // commit the changes $this->snapshotManager->getConnection()->commit(); }
public function testExecute() { $page1 = $this->getMock('Sonata\\PageBundle\\Model\\PageInterface'); $page2 = $this->getMock('Sonata\\PageBundle\\Model\\PageInterface'); $systemPage = $this->getMock('Sonata\\PageBundle\\Model\\PageInterface'); $this->pageManager->expects($this->at(0))->method('findBy')->with($this->equalTo(array('routeName' => Page::PAGE_ROUTE_CMS_NAME)))->will($this->returnValue(array($page1, $page2))); $this->pageManager->expects($this->at(1))->method('findBy')->with($this->equalTo(array('url' => null, 'parent' => null)))->will($this->returnValue(array($systemPage))); $block = new Block(); $blockContext = new BlockContext($block, array('mode' => 'public', 'title' => 'List Pages', 'template' => 'SonataPageBundle:Block:block_pagelist.html.twig')); $blockService = new PageListBlockService('block.service', $this->templating, $this->pageManager); $blockService->execute($blockContext); $this->assertSame('SonataPageBundle:Block:block_pagelist.html.twig', $this->templating->view); $this->assertSame($blockContext, $this->templating->parameters['context']); $this->assertInternalType('array', $this->templating->parameters['settings']); $this->assertInstanceOf('Sonata\\BlockBundle\\Model\\BlockInterface', $this->templating->parameters['block']); $this->assertCount(2, $this->templating->parameters['elements']); $this->assertContains($page1, $this->templating->parameters['elements']); $this->assertContains($page2, $this->templating->parameters['elements']); $this->assertCount(1, $this->templating->parameters['systemElements']); $this->assertContains($systemPage, $this->templating->parameters['systemElements']); }
/** * Write a page, this method is used by both POST and PUT action methods. * * @param Request $request Symfony request * @param int|null $id A page identifier * * @return FormInterface */ protected function handleWritePage($request, $id = null) { $page = $id ? $this->getPage($id) : null; $form = $this->formFactory->createNamed(null, 'sonata_page_api_form_page', $page, array('csrf_protection' => false)); $form->bind($request); if ($form->isValid()) { $page = $form->getData(); $this->pageManager->save($page); $view = FOSRestView::create($page); $serializationContext = SerializationContext::create(); $serializationContext->setGroups(array('sonata_api_read')); $serializationContext->enableMaxDepthChecks(); $view->setSerializationContext($serializationContext); return $view; } return $form; }
/** * Updates site page routes with all routes available in Symfony router service * * @param SiteInterface $site A page bundle site instance * @param OutputInterface $output A Symfony console output * * @return void */ public function update(SiteInterface $site, OutputInterface $output = null) { $message = sprintf(" > <info>Updating core routes for site</info> : <comment>%s - %s</comment>", $site->getName(), $site->getUrl()); $this->writeln($output, array(str_repeat('=', strlen($message)), "", $message, "", str_repeat('=', strlen($message)))); $knowRoutes = array(); $root = $this->pageManager->getPageByUrl($site, '/'); // no root url for the given website, create one if (!$root) { $root = $this->pageManager->create(array('routeName' => PageInterface::PAGE_ROUTE_CMS_NAME, 'name' => 'Homepage', 'url' => '/', 'site' => $site, 'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT', 'slug' => '/')); $this->pageManager->save($root); } // Iterate over declared routes from the routing mechanism foreach ($this->router->getRouteCollection()->all() as $name => $route) { $name = trim($name); $knowRoutes[] = $name; $page = $this->pageManager->findOneBy(array('routeName' => $name, 'site' => $site->getId())); $routeHostRegex = $route->compile()->getHostRegex(); if (!$this->decoratorStrategy->isRouteNameDecorable($name) || !$this->decoratorStrategy->isRouteUriDecorable($route->getPath()) || null !== $routeHostRegex && !preg_match($routeHostRegex, $site->getHost())) { if ($page) { $page->setEnabled(false); $this->writeln($output, sprintf(' <error>DISABLE</error> <error>% -50s</error> %s', $name, $route->getPath())); } else { continue; } } $update = true; if (!$page) { $update = false; $requirements = $route->getRequirements(); $page = $this->pageManager->create(array('routeName' => $name, 'name' => $name, 'url' => $route->getPath(), 'site' => $site, 'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT')); } if (!$page->getParent() && $page->getId() != $root->getId()) { $page->setParent($root); } $page->setSlug($route->getPath()); $page->setUrl($route->getPath()); $page->setRequestMethod(isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT'); $this->pageManager->save($page); $this->writeln($output, sprintf(' <info>%s</info> % -50s %s', $update ? 'UPDATE ' : 'CREATE ', $name, $route->getPath())); } // Iterate over error pages foreach ($this->exceptionListener->getHttpErrorCodes() as $name) { $name = trim($name); $knowRoutes[] = $name; $page = $this->pageManager->findOneBy(array('routeName' => $name, 'site' => $site->getId())); if (!$page) { $params = array('routeName' => $name, 'name' => $name, 'decorate' => false, 'site' => $site); $page = $this->pageManager->create($params); $this->writeln($output, sprintf(' <info>%s</info> % -50s %s', 'CREATE ', $name, '')); } // an internal page or an error page should not have any parent (no direct access) $page->setParent(null); $this->pageManager->save($page); } $has = false; foreach ($this->pageManager->getHybridPages($site) as $page) { if (!$page->isHybrid() || $page->isInternal()) { continue; } if (!in_array($page->getRouteName(), $knowRoutes)) { if (!$has) { $has = true; $this->writeln($output, array('', 'Some hybrid pages does not exist anymore', str_repeat('-', 80))); } $this->writeln($output, sprintf(' <error>ERROR</error> %s', $page->getRouteName())); } } if ($has) { $this->writeln($output, <<<MSG <error> *WARNING* : Pages has been updated however some pages do not exist anymore. You must remove them manually. </error> MSG ); } }
/** * {@inheritdoc} */ public function execute(BlockContextInterface $blockContext, Response $response = null) { $pageList = $this->pageManager->findBy(array('routeName' => Page::PAGE_ROUTE_CMS_NAME)); $systemElements = $this->pageManager->findBy(array('url' => null, 'parent' => null)); return $this->renderResponse($blockContext->getTemplate(), array('context' => $blockContext, 'block' => $blockContext->getBlock(), 'settings' => $blockContext->getSettings(), 'elements' => $pageList, 'systemElements' => $systemElements), $response); }