/**
  * Index a node translation
  *
  * @param NodeTranslation $nodeTranslation
  * @param bool            $add Add node immediately to index?
  *
  * @return bool Return true if the document has been indexed
  */
 public function indexNodeTranslation(NodeTranslation $nodeTranslation, $add = false)
 {
     // Retrieve the public NodeVersion
     $publicNodeVersion = $nodeTranslation->getPublicNodeVersion();
     if (is_null($publicNodeVersion)) {
         return false;
     }
     // Retrieve the referenced entity from the public NodeVersion
     $page = $publicNodeVersion->getRef($this->em);
     if ($page->isStructureNode()) {
         return true;
     }
     // Only index online NodeTranslations
     if (!$nodeTranslation->isOnline()) {
         return false;
     }
     $node = $nodeTranslation->getNode();
     if ($this->isIndexable($page)) {
         $this->addPageToIndex($nodeTranslation, $node, $publicNodeVersion, $page);
         if ($add) {
             $this->searchProvider->addDocuments($this->documents);
             $this->documents = array();
         }
     }
     return true;
     // return true even if the page itself should not be indexed. This makes sure its children are being processed (i.e. structured nodes)
 }
 /**
  * @param NodeTranslation $translation The node translation
  * @param EntityManager   $em          The entity manager
  * @param array           $flashes     Flashes
  *
  * A function that checks the URL and sees if it's unique.
  * It's allowed to be the same when the node is a StructureNode.
  * When a node is deleted it needs to be ignored in the check.
  * Offline nodes need to be included as well.
  *
  * It sluggifies the slug, updates the URL
  * and checks all existing NodeTranslations ([1]), excluding itself. If a URL existsthat has the same url.
  * If an existing one is found the slug is modified, the URL is updated and the check is repeated
  * until no prior urls exist.
  *
  * NOTE: We need a way to tell if the slug has been modified or not.
  * NOTE: Would be cool if we could increment a number after the slug. Like check if it matches -v#
  *       and increment the number.
  *
  * [1] For all languages for now. The issue is that we need a way to know if a node's URL is prepended with the
  * language or not. For now both scenarios are possible so we check for all languages.
  *
  * @param NodeTranslation &$translation Reference to the NodeTranslation. This is modified in place.
  * @param EntityManager   $em           The entity manager
  * @param array           $flashes      The flash messages array
  *
  * @return bool
  *
  * @return boolean
  */
 private function ensureUniqueUrl(NodeTranslation &$translation, EntityManager $em, $flashes = array())
 {
     // Can't use GetRef here yet since the NodeVersions aren't loaded yet for some reason.
     $pnv = $translation->getPublicNodeVersion();
     $page = $em->getRepository($pnv->getRefEntityName())->find($pnv->getRefId());
     $isStructureNode = $page->isStructureNode();
     // If it's a StructureNode the slug and url should be empty.
     if ($isStructureNode) {
         $translation->setSlug('');
         $translation->setUrl($translation->getFullSlug());
         return true;
     }
     /* @var Kunstmaan\NodeBundle\Entity\NodeTranslation $nodeTranslationRepository */
     $nodeTranslationRepository = $em->getRepository('KunstmaanNodeBundle:NodeTranslation');
     if ($translation->getUrl() == $translation->getFullSlug()) {
         $this->logger->addDebug('Evaluating URL for NT ' . $translation->getId() . ' getUrl: \'' . $translation->getUrl() . '\' getFullSlug: \'' . $translation->getFullSlug() . '\'');
         return false;
     }
     // Adjust the URL.
     $translation->setUrl($translation->getFullSlug());
     // Find all translations with this new URL, whose nodes are not deleted.
     $translations = $nodeTranslationRepository->getNodeTranslationForUrl($translation->getUrl(), $translation->getLang(), false, $translation);
     $this->logger->addDebug('Found ' . count($translations) . ' node(s) that match url \'' . $translation->getUrl() . '\'');
     if (count($translations) > 0) {
         $oldUrl = $translation->getFullSlug();
         $translation->setSlug($this->slugifier->slugify($this->IncrementString($translation->getSlug())));
         $newUrl = $translation->getFullSlug();
         $message = 'The URL of the page has been changed from ' . $oldUrl . ' to ' . $newUrl . ' since another page already uses this URL.';
         $this->logger->addInfo($message);
         $flashes[] = $message;
         $this->ensureUniqueUrl($translation, $em, $flashes);
     } elseif (count($flashes) > 0) {
         // No translations found so we're certain we can show this message.
         $flash = end($flashes);
         $flash = current(array_slice($flashes, -1));
         $this->session->getFlashBag()->add('warning', $flash);
     }
     return true;
 }
 /**
  * @param NodeTranslation $nodeTranslation
  *
  * @throws AccessDeniedException
  */
 public function unPublish(NodeTranslation $nodeTranslation)
 {
     if (false === $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_UNPUBLISH, $nodeTranslation->getNode())) {
         throw new AccessDeniedException();
     }
     $node = $nodeTranslation->getNode();
     $nodeVersion = $nodeTranslation->getPublicNodeVersion();
     $page = $nodeVersion->getRef($this->em);
     $this->eventDispatcher->dispatch(Events::PRE_UNPUBLISH, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
     $nodeTranslation->setOnline(false);
     $this->em->persist($nodeTranslation);
     $this->em->flush();
     // Remove scheduled task
     $this->unSchedulePublish($nodeTranslation);
     $this->eventDispatcher->dispatch(Events::POST_UNPUBLISH, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
 }
 /**
  * @covers Kunstmaan\NodeBundle\Entity\NodeTranslation::setPublicNodeVersion
  * @covers Kunstmaan\NodeBundle\Entity\NodeTranslation::getPublicNodeVersion
  */
 public function testSetGetPublicNodeVersion()
 {
     $nodeVersion = new NodeVersion();
     $this->object->setPublicNodeVersion($nodeVersion);
     $this->assertEquals($nodeVersion, $this->object->getPublicNodeVersion());
 }
 /**
  * @param Request                $request
  * @param boolean                $preview
  * @param EntityManagerInterface $em
  * @param NodeTranslation        $nodeTranslation
  *
  * @return \Kunstmaan\NodeBundle\Entity\HasNodeInterface
  */
 private function getPageEntity(Request $request, $preview, EntityManagerInterface $em, NodeTranslation $nodeTranslation)
 {
     /* @var HasNodeInterface $entity */
     $entity = null;
     if ($preview) {
         $version = $request->get('version');
         if (!empty($version) && is_numeric($version)) {
             $nodeVersion = $em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
             if (!is_null($nodeVersion)) {
                 $entity = $nodeVersion->getRef($em);
             }
         }
     }
     if (is_null($entity)) {
         $entity = $nodeTranslation->getPublicNodeVersion()->getRef($em);
         return $entity;
     }
     return $entity;
 }