/** * @param EventArgs $args * @return mixed|void */ public function onFlush(EventArgs $args) { $em = $args->getEntityManager(); $unitOfWork = $em->getUnitOfWork(); foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) { if ($entity instanceof PageInterface) { if ($contentRoute = $entity->getContentRoute()) { $contentRoute->setPath(PageHelper::getPageRoutePath($entity->getPath())); $em->persist($contentRoute); $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute); foreach ($entity->getAllChildren() as $child) { $contentRoute = $child->getContentRoute(); $contentRoute->setPath(PageHelper::getPageRoutePath($child->getPath())); $em->persist($contentRoute); $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute); if ($entity->getStatus() == Page::STATUS_PUBLISHED) { if ($childSnapshot = $child->getSnapshot()) { $snapshotRoute = $childSnapshot->getContentRoute(); $newPath = PageHelper::getPageRoutePath($child->getPath()); $snapshotRoute->setPath($newPath); $childSnapshot->setPath($newPath); $em->persist($childSnapshot); $em->persist($snapshotRoute); $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($childSnapshot)), $childSnapshot); $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($snapshotRoute)), $snapshotRoute); } } } } } } }
/** * Returns the corresponding route of the given URL for the locale supplied * If none is found it returns the original route object * * @param $oldUrl * @param $locale * @return array|\Networking\InitCmsBundle\Component\Routing\Route * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function getTranslationRoute($oldUrl, $locale) { $cookies = $this->request->cookies ? $this->request->cookies->all() : array(); $oldRequest = Request::create($oldUrl, 'GET', array(), $cookies); if ($this->request->getSession()) { $oldRequest->setSession($this->request->getSession()); } try { $request = $this->pageHelper->matchContentRouteRequest($oldRequest); } catch (ResourceNotFoundException $e) { $request = $oldRequest; } if (!($content = $request->get('_content', false))) { try { $route = $this->router->matchRequest(Request::create($oldUrl)); } catch (ResourceNotFoundException $e) { if ($route = $this->router->matchRequest(Request::create('/404'))) { return $route; } throw new NotFoundHttpException(sprintf('Could not find a translation to "%s" for this request"', $locale)); } if (!array_key_exists('_content', $route)) { return $route; } if (!($content = $route['_content'])) { return $route; } } if ($content instanceof PageInterface) { $translation = $content->getAllTranslations()->get($locale); if (is_null($translation)) { //@todo does this make sense, or should we throw an exception return array('_route' => 'networking_init_cms_home'); } //return a contentRoute object $contentRoute = $translation->getContentRoute()->setContent($translation); return ContentRouteManager::generateRoute($contentRoute, $contentRoute->getPath(), ''); } if ($content instanceof PageSnapshotInterface) { $content = $this->pageHelper->unserializePageSnapshotData($content); $translation = $content->getAllTranslations()->get($locale); if ($translation && ($snapshotId = $translation->getId())) { /** @var $snapshot PageSnapshotInterface */ $snapshot = $this->om->getRepository($content->getSnapshotClassType())->findOneBy(array('resourceId' => $snapshotId)); if ($snapshot) { $contentRoute = $snapshot->getRoute(); return ContentRouteManager::generateRoute($contentRoute, $contentRoute->getPath(), ''); } } } if ($this->fallbackRoute) { return $this->fallbackRoute; } if ($route = $this->router->matchRequest(Request::create('/404'))) { return $route; } //no valid translation found throw new NotFoundHttpException(sprintf('Could not find a translation to "%s" for content "%s"', $locale, $content->__toString())); }
/** * @param null $node * @param bool $direct * @param null $sortByField * @param string $direction * @param bool $includeNode * @param string $viewStatus * @return array */ public function getChildrenByStatus($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false, $viewStatus = Page::STATUS_PUBLISHED) { $qb = $this->childrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode); $aliases = $qb->getRootAliases(); if ($viewStatus == Page::STATUS_PUBLISHED) { $qb->addSelect('ps.id AS ps_id'); $qb->addSelect('ps.versionedData AS ps_versionedData'); $qb->leftJoin('\\Networking\\InitCmsBundle\\Entity\\PageSnapshot', 'ps', Expr\Join::WITH, sprintf('%s.page = ps.page ', $aliases[0])); $qb->leftJoin('ps.contentRoute', 'cr'); } else { $qb->leftJoin(sprintf('%s.page', $aliases[0]), 'p'); $qb->leftJoin('p.contentRoute', 'cr'); } $qb->addSelect('cr.path AS path'); $results = $qb->getQuery()->getResult(); $menuItems = array(); foreach ($results as $item) { $menuItem = $item[0]; if (!$menuItem->getRedirectUrl() && !$menuItem->getInternalUrl()) { if ($viewStatus == Page::STATUS_PUBLISHED) { if (!$item['path'] || !$item['ps_id']) { continue; } else { if (!$this->pageHelper->jsonPageIsActive($item['ps_versionedData'])) { continue; } $menuItem->setPath($item['path']); } } else { if (!$item['path']) { continue; } else { $menuItem->setPath($item['path']); } } } $menuItems[] = $menuItem; } return $menuItems; }
/** * @covers PageHelper::getPageRoutePath() */ public function testGetPageRoutePath() { $this->assertEquals('/', PageHelper::getPageRoutePath(''), 'empty route is "/"'); $this->assertEquals('/hallo', PageHelper::getPageRoutePath('hallo'), 'slash at the beginning'); $this->assertEquals('/some/tree/', PageHelper::getPageRoutePath('/some-2/tree-20/'), 'remove -numbers in path'); }