/**
  * Replace root contentset for a page and its descendants.
  *
  * @param \BackBee\CoreDomain\NestedNode\Page            $page
  * @param \BackBee\CoreDomain\ClassContent\ContentSet    $oldContentSet
  * @param \BackBee\CoreDomain\ClassContent\ContentSet    $newContentSet
  * @param \BackBee\Security\Token\BBUserToken $userToken
  */
 public function updateRootContentSetByPage(Page $page, ContentSet $oldContentSet, ContentSet $newContentSet, BBUserToken $userToken)
 {
     $em = $this->_em;
     $q = $this->createQueryBuilder('c');
     $results = $q->leftJoin('c._pages', 'p')->leftJoin('p._section', 'sp')->leftJoin('c._subcontent', 'subcontent')->where('subcontent = :contentToReplace')->andWhere('sp._root = :cpageRoot')->andWhere('sp._leftnode > :cpageLeftnode')->andWhere('sp._rightnode < :cpageRightnode')->setParameters(['contentToReplace' => $oldContentSet, 'cpageRoot' => $page->getSection()->getRoot(), 'cpageLeftnode' => $page->getLeftnode(), 'cpageRightnode' => $page->getRightnode()])->getQuery()->getResult();
     if ($results) {
         foreach ($results as $parentContentSet) {
             /* create draft for the main container */
             $draft = $em->getRepository('BackBee\\CoreDomain\\ClassContent\\Revision')->getDraft($parentContentSet, $userToken, true);
             if (null !== $draft) {
                 $parentContentSet->setDraft($draft);
             }
             /* Replace the old ContentSet by the new one */
             $parentContentSet->replaceChildBy($oldContentSet, $newContentSet);
             $em->persist($parentContentSet);
         }
     }
 }
 /**
  * Set a page to filter the query on a nested portion.
  *
  * @param  BackBee\CoreDomain\NestedNode\Page $page
  */
 public function addPageFilter(Page $page)
 {
     if ($page && !$page->isRoot()) {
         $this->leftJoin('cc._mainnode', 'p')->leftJoin('p._section', 'sp')->andWhere('sp._root = :selectedPageRoot')->andWhere('sp._leftnode >= :selectedPageLeftnode')->andWhere('sp._rightnode <= :selectedPageRightnode')->setParameters(['selectedPageRoot' => $page->getSection()->getRoot(), 'selectedPageLeftnode' => $page->getLeftnode(), 'selectedPageRightnode' => $page->getRightnode()]);
     }
 }