/**
  * If there is a draft version it'll try to publish the draft first. Makse snese because if you want to publish the public version you don't publish but you save.
  *
  * @param NodeTranslation $nodeTranslation
  *
  * @throws AccessDeniedException
  */
 public function publish(NodeTranslation $nodeTranslation, $user = null)
 {
     if (false === $this->securityContext->isGranted(PermissionMap::PERMISSION_PUBLISH, $nodeTranslation->getNode())) {
         throw new AccessDeniedException();
     }
     if (is_null($user)) {
         $user = $this->securityContext->getToken()->getUser();
     }
     $node = $nodeTranslation->getNode();
     $nodeVersion = $nodeTranslation->getNodeVersion('draft');
     if (!is_null($nodeVersion) && $nodeTranslation->isOnline()) {
         $page = $nodeVersion->getRef($this->em);
         /** @var $nodeVersion NodeVersion */
         $nodeVersion = $this->createPublicVersion($page, $nodeTranslation, $nodeVersion, $user);
         $nodeTranslation = $nodeVersion->getNodeTranslation();
     } else {
         $nodeVersion = $nodeTranslation->getPublicNodeVersion();
     }
     $page = $nodeVersion->getRef($this->em);
     $this->eventDispatcher->dispatch(Events::PRE_PUBLISH, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
     $nodeTranslation->setOnline(true)->setPublicNodeVersion($nodeVersion)->setUpdated(new \DateTime());
     $this->em->persist($nodeTranslation);
     $this->em->flush();
     // Remove scheduled task
     $this->unSchedulePublish($nodeTranslation);
     $this->eventDispatcher->dispatch(Events::POST_PUBLISH, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
 }
 /**
  * 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)
 }
 /**
  * @covers Kunstmaan\NodeBundle\Entity\NodeTranslation::setOnline
  * @covers Kunstmaan\NodeBundle\Entity\NodeTranslation::isOnline
  */
 public function testSetIsOnline()
 {
     $this->object->setOnline(true);
     $this->assertTrue($this->object->isOnline());
 }