/**
  * @param NodeTranslation $value
  *
  * @return string
  */
 public function transform($value)
 {
     if (null === $value) {
         return "";
     }
     if (false === $value instanceof NodeTranslation) {
         throw new TransformationFailedException("Value must be instance of NodeTranslation");
     }
     return $this->router->generate('_slug', ['_locale' => $value->getLang(), 'url' => $value->getUrl()]);
 }
 /**
  * @inheritDoc
  * @todo: remove after PR #1108 is merged
  *
  * @see https://github.com/Kunstmaan/KunstmaanBundlesCMS/pull/1108
  */
 protected function renderCustomSearchView(NodeTranslation $nodeTranslation, SearchViewTemplateInterface $page, EngineInterface $renderer)
 {
     $view = $page->getSearchView();
     $renderContext = new RenderContext(['locale' => $nodeTranslation->getLang(), 'page' => $page, 'indexMode' => true, 'nodetranslation' => $nodeTranslation]);
     if ($page instanceof PageInterface) {
         $request = $this->container->get('request_stack')->getCurrentRequest();
         $page->service($this->container, $request, $renderContext);
     }
     $content = $this->removeHtml($renderer->render($view, $renderContext->getArrayCopy()));
     return $content;
 }
 /**
  * Build iterator
  *
  * NOTE : The submission fields are added as export fields as well ...
  */
 public function buildIterator()
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select('fs')->from('KunstmaanFormBundle:FormSubmission', 'fs')->innerJoin('fs.node', 'n', 'WITH', 'fs.node = n.id')->andWhere('n.id = :node')->andWhere('fs.lang = :lang')->setParameter('node', $this->nodeTranslation->getNode()->getId())->setParameter('lang', $this->nodeTranslation->getLang())->addOrderBy('fs.created', 'DESC');
     $iterableResult = $qb->getQuery()->iterate();
     $isHeaderWritten = false;
     $collection = new ArrayCollection();
     foreach ($iterableResult as $row) {
         /* @var FormSubmission $submission */
         $submission = $row[0];
         // Write row data
         $data = array('id' => $submission->getId(), 'date' => $submission->getCreated()->format('d/m/Y H:i:s'), 'language' => $submission->getLang());
         foreach ($submission->getFields() as $field) {
             $header = $this->translator->trans($field->getLabel());
             if (!$isHeaderWritten) {
                 $this->addExportField($header, $header);
             }
             $data[$header] = $field->__toString();
         }
         $isHeaderWritten = true;
         $collection->add(array($data));
     }
     $this->iterator = $collection->getIterator();
 }
 private function ensureUniqueUrl(NodeTranslation $translation, HasNodeInterface $page)
 {
     if ($page instanceof StructureNode) {
         $translation->setSlug('');
         $translation->setUrl($translation->getFullSlug());
         return $translation;
     }
     $translation->setUrl($translation->getFullSlug());
     // Find all translations with this new URL, whose nodes are not deleted.
     $translationWithSameUrl = $this->nodeTranslationRepo->getNodeTranslationForUrl($translation->getUrl(), $translation->getLang(), false, $translation);
     if ($translationWithSameUrl instanceof NodeTranslation) {
         $translation->setSlug($this->slugifier->slugify($this->IncrementString($translation->getSlug())));
         $this->ensureUniqueUrl($translation, $page);
     }
     return $translation;
 }
 public function getParentNodeTranslation(NodeTranslation $nodeTranslation)
 {
     $parent = $nodeTranslation->getNode()->getParent();
     if (is_null($parent)) {
         return null;
     }
     $qb = $this->createQueryBuilder('nt')->select('nt,n')->innerJoin('nt.publicNodeVersion', 'nv')->innerJoin('nt.node', 'n')->where('nt.node = :parent')->andWhere('n.deleted = 0')->andWhere('nt.lang = :lang')->setParameter('parent', $parent)->setParameter('lang', $nodeTranslation->getLang());
     return $qb->getQuery()->getOneOrNullResult();
 }
 /**
  * Add nodeTranslation
  *
  * @param NodeTranslation $nodeTranslation
  *
  * @return Node
  */
 public function addNodeTranslation(NodeTranslation $nodeTranslation)
 {
     $this->nodeTranslations[] = $nodeTranslation;
     $nodeTranslation->setNode($this);
     return $this;
 }
 /**
  * @param string $lang   The locale
  * @param string $title  The title
  * @param string $slug   The slug
  * @param int    $nodeId The node id
  *
  * @return Node
  */
 private function getNodeWithTranslation($lang, $title, $slug, $nodeId = null)
 {
     $node = new Node();
     if (!is_null($nodeId)) {
         $node->setId($nodeId);
     }
     $nodeTranslation = new NodeTranslation();
     $nodeTranslation->setLang($lang)->setTitle($title)->setSlug($slug);
     $node->addNodeTranslation($nodeTranslation);
     return $node;
 }
 /**
  * @param NodeTranslation $nodeTranslation
  *
  * @return null|object
  */
 public function getPageByNodeTranslation(NodeTranslation $nodeTranslation)
 {
     return $nodeTranslation->getRef($this->em);
 }
 /**
  * @covers Kunstmaan\NodeBundle\Entity\Node::addNodeTranslation
  * @covers Kunstmaan\NodeBundle\Entity\Node::getNodeTranslation
  */
 public function testGetNodeTranslation()
 {
     $translation1 = new NodeTranslation();
     $translation1->setLang('nl');
     $translation1->setOnline(true);
     $this->object->addNodeTranslation($translation1);
     $translation2 = new NodeTranslation();
     $translation2->setLang('fr');
     $translation2->setOnline(true);
     $this->object->addNodeTranslation($translation2);
     $this->assertEquals($translation1, $this->object->getNodeTranslation('nl'));
     $this->assertEquals($translation2, $this->object->getNodeTranslation('fr'));
     $this->assertNotEquals($translation1, $this->object->getNodeTranslation('fr'));
     $this->assertNotEquals($translation2, $this->object->getNodeTranslation('nl'));
     $this->assertNull($this->object->getNodeTranslation('en'));
 }
 /**
  * Make some modifications to the default created query builder
  *
  * @param QueryBuilder $queryBuilder The query builder
  * @param array        $params       The parameters
  */
 public function adaptQueryBuilder(QueryBuilder $queryBuilder, array $params = array())
 {
     parent::adaptQueryBuilder($queryBuilder);
     $queryBuilder->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id')->andWhere('n.id = :node')->andWhere('b.lang = :lang')->setParameter('node', $this->nodeTranslation->getNode()->getId())->setParameter('lang', $this->nodeTranslation->getLang())->addOrderBy('b.created', 'DESC');
 }
 private function nodeTranslationToCategory(NodeTranslation $nt)
 {
     return new Category($nt->getTitle(), $nt->getNode()->getId(), $nt->getUrl());
 }
 /**
  * @covers Kunstmaan\NodeBundle\Helper\Menu\ActionsMenuBuilder::createActionsMenu
  */
 public function testShouldShowDeleteButtonWhenTheNodeHasAParent()
 {
     $nodeTranslation = new NodeTranslation();
     $node = new Node();
     $node->setParent(new Node());
     $nodeTranslation->setNode($node);
     $nodeVersion = new NodeVersion();
     $nodeVersion->setType('public');
     $nodeVersion->setNodeTranslation($nodeTranslation);
     $this->builder->setActiveNodeVersion($nodeVersion);
     $menu = $this->builder->createActionsMenu();
     $this->assertNotNull($menu->getChild('action.delete'));
     $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
 }
 /**
  * @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;
 }
 /**
  * @covers Kunstmaan\NodeBundle\Helper\Menu\ActionsMenuBuilder::createActionsMenu
  */
 public function testShouldShowRecopyButtonWhenTheNodeHasTranslations()
 {
     $node = new Node();
     $nodeTranslation = new NodeTranslation();
     $nodeTranslation->setLang('en');
     $node->addNodeTranslation($nodeTranslation);
     $nodeVersion = new NodeVersion();
     $nodeVersion->setType("public");
     $nodeVersion->setNodeTranslation($nodeTranslation);
     $this->builder->setActiveNodeVersion($nodeVersion);
     $nodeTranslation = new NodeTranslation();
     $nodeTranslation->setLang('nl');
     $node->addNodeTranslation($nodeTranslation);
     $nodeVersion = new NodeVersion();
     $nodeVersion->setType("public");
     $nodeVersion->setNodeTranslation($nodeTranslation);
     $this->builder->setActiveNodeVersion($nodeVersion);
     $menu = $this->builder->createActionsMenu();
     $this->assertNotNull($menu->getChild('action.recopyfromlanguage'));
     $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
 }
 /**
  * @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;
 }
 /**
  * Render default search view (all indexable pageparts in the main context
  * of the page)
  *
  * @param NodeTranslation       $nodeTranslation
  * @param HasPagePartsInterface $page
  * @param EngineInterface       $renderer
  *
  * @return string
  */
 protected function renderDefaultSearchView(NodeTranslation $nodeTranslation, HasPagePartsInterface $page, EngineInterface $renderer)
 {
     $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page);
     $view = 'KunstmaanNodeSearchBundle:PagePart:view.html.twig';
     $content = $this->removeHtml($renderer->render($view, array('locale' => $nodeTranslation->getLang(), 'page' => $page, 'pageparts' => $pageparts, 'indexMode' => true)));
     return $content;
 }
 /**
  * @param NodeTranslation $nodeTranslation The NodeTranslation
  * @param \DateTime       $date            The date to unpublish
  *
  * @throws AccessDeniedException
  */
 public function unPublishLater(NodeTranslation $nodeTranslation, \DateTime $date)
 {
     $node = $nodeTranslation->getNode();
     if (false === $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_UNPUBLISH, $node)) {
         throw new AccessDeniedException();
     }
     //remove existing first
     $this->unSchedulePublish($nodeTranslation);
     $user = $this->tokenStorage->getToken()->getUser();
     $queuedNodeTranslationAction = new QueuedNodeTranslationAction();
     $queuedNodeTranslationAction->setNodeTranslation($nodeTranslation)->setAction(QueuedNodeTranslationAction::ACTION_UNPUBLISH)->setUser($user)->setDate($date);
     $this->em->persist($queuedNodeTranslationAction);
     $this->em->flush();
 }