/**
  * {@inheritdoc}
  */
 public function process(ConsumerEvent $event)
 {
     $page = $this->pageManager->findOneBy(array('id' => $event->getMessage()->getValue('pageId')));
     if (!$page) {
         return;
     }
     // start a transaction
     $this->snapshotManager->getConnection()->beginTransaction();
     // cleanup snapshots
     $this->snapshotManager->cleanup($page, $event->getMessage()->getValue('keepSnapshots'));
     // commit the changes
     $this->snapshotManager->getConnection()->commit();
 }
 /**
  * {@inheritdoc}
  */
 protected function getPageBy(SiteInterface $site = null, $fieldName, $value)
 {
     if ('id' == $fieldName) {
         $fieldName = 'pageId';
         $id = $value;
     } elseif (isset($this->pageReferences[$fieldName][$value])) {
         $id = $this->pageReferences[$fieldName][$value];
     } else {
         $id = null;
     }
     if (null === $id || !isset($this->pages[$id])) {
         $parameters = array($fieldName => $value);
         if ($site) {
             $parameters['site'] = $site->getId();
         }
         $snapshot = $this->snapshotManager->findEnableSnapshot($parameters);
         if (!$snapshot) {
             throw new PageNotFoundException();
         }
         $page = new SnapshotPageProxy($this->snapshotManager, $this->transformer, $snapshot);
         $this->pages[$id] = false;
         if ($page) {
             $this->loadBlocks($page);
             $id = $page->getId();
             if ($fieldName != 'id') {
                 $this->pageReferences[$fieldName][$value] = $id;
             }
             $this->pages[$id] = $page;
         }
     }
     return $this->pages[$id];
 }
 /**
  * Retrieves Snapshot with id $id or throws an exception if it doesn't exist.
  *
  * @param $id
  *
  * @return SnapshotInterface
  *
  * @throws NotFoundHttpException
  */
 protected function getSnapshot($id)
 {
     $snapshot = $this->snapshotManager->findOneBy(array('id' => $id));
     if (null === $snapshot) {
         throw new NotFoundHttpException(sprintf('Snapshot (%d) not found', $id));
     }
     return $snapshot;
 }
 /**
  * {@inheritdoc}
  */
 public function process(ConsumerEvent $event)
 {
     $pageId = $event->getMessage()->getValue('pageId');
     $page = $this->pageManager->findOneBy(array('id' => $pageId));
     if (!$page) {
         return;
     }
     // start a transaction
     $this->snapshotManager->getConnection()->beginTransaction();
     // creating snapshot
     $snapshot = $this->transformer->create($page);
     // update the page status
     $page->setEdited(false);
     $this->pageManager->save($page);
     // save the snapshot
     $this->snapshotManager->save($snapshot);
     $this->snapshotManager->enableSnapshots(array($snapshot));
     // commit the changes
     $this->snapshotManager->getConnection()->commit();
 }
 /**
  * get the tree of the page
  *
  * @return array of Application\Sonata\PageBundle\Entity\Page
  */
 public function getParents()
 {
     if (!$this->parents) {
         $parents = array();
         $snapshot = $this->snapshot;
         while ($snapshot) {
             $content = json_decode($snapshot->getContent(), true);
             $parentId = $content['parent_id'];
             $snapshot = $parentId ? $this->manager->getSnapshotByPageId($parentId) : null;
             if ($snapshot) {
                 $parents[] = new SnapshotPageProxy($this->manager, $snapshot);
             }
         }
         $this->setParents(array_reverse($parents));
     }
     return $this->parents;
 }
 /**
  * {@inheritdoc}
  */
 public function getChildren(PageInterface $parent)
 {
     if (!isset($this->children[$parent->getId()])) {
         $date = new \Datetime();
         $parameters = array('publicationDateStart' => $date, 'publicationDateEnd' => $date, 'parentId' => $parent->getId());
         $manager = $this->registry->getManagerForClass($this->snapshotManager->getClass());
         if (!$manager instanceof EntityManagerInterface) {
             throw new \RuntimeException('Invalid entity manager type');
         }
         $snapshots = $manager->createQueryBuilder()->select('s')->from($this->snapshotManager->getClass(), 's')->where('s.parentId = :parentId and s.enabled = 1')->andWhere('s.publicationDateStart <= :publicationDateStart AND ( s.publicationDateEnd IS NULL OR s.publicationDateEnd >= :publicationDateEnd )')->orderBy('s.position')->setParameters($parameters)->getQuery()->execute();
         $pages = array();
         foreach ($snapshots as $snapshot) {
             $page = new SnapshotPageProxy($this->snapshotManager, $this, $snapshot);
             $pages[$page->getId()] = $page;
         }
         $this->children[$parent->getId()] = new \Doctrine\Common\Collections\ArrayCollection($pages);
     }
     return $this->children[$parent->getId()];
 }
 /**
  * {@inheritdoc}
  */
 public function getParents()
 {
     if (!$this->parents) {
         $parents = array();
         $snapshot = $this->snapshot;
         while ($snapshot) {
             $content = $snapshot->getContent();
             if (!$content['parent_id']) {
                 break;
             }
             $snapshot = $this->manager->findEnableSnapshot(array('pageId' => $content['parent_id']));
             if (!$snapshot) {
                 break;
             }
             $parents[] = new SnapshotPageProxy($this->manager, $this->transformer, $snapshot);
         }
         $this->setParents(array_reverse($parents));
     }
     return $this->parents;
 }