/**
  * {@inheritdoc}
  */
 public function loadSnapshots(SiteInterface $site)
 {
     $now = new \Datetime();
     $query = $this->getEntityManager()->createQuery(sprintf('
           SELECT s,p
           FROM %s s
           INNER JOIN s.page p
           WHERE
             s.enabled = 1
               AND
             s.site = %d
               AND
             s.publicationDateStart <= \'%s\'
               AND
             ( s.publicationDateEnd IS NULL OR s.publicationDateEnd >= \'%s\' )
           ORDER BY s.position ASC
           ', $this->class, $site->getId(), $now->format('Y-m-d H:i:s'), $now->format('Y-m-d H:i:s')));
     $query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_INNER_JOIN, true);
     $query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
     $query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $site->getLocale());
     $snapshots = array();
     foreach ($query->execute() as $snap) {
         $snapshots[$snap->getPage()->getId()] = $snap;
     }
     $this->snapshots = $snapshots;
 }
 /**
  * {@inheritdoc}
  */
 public function loadPages(SiteInterface $site)
 {
     $query = $this->getEntityManager()->createQuery(sprintf('SELECT p FROM %s p INDEX BY p.id WHERE p.site = %d ORDER BY p.position ASC', $this->class, $site->getId()));
     $query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
     $query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $site->getLocale());
     $pages = $query->execute();
     foreach ($pages as $page) {
         $parent = $page->getParent();
         $page->disableChildrenLazyLoading();
         if (!$parent) {
             continue;
         }
         $pages[$parent->getId()]->disableChildrenLazyLoading();
         //$pages[$parent->getId()]->addChildren($page);
     }
     return $pages;
 }
 /**
  * Returns TRUE whether the given site matches the given request
  *
  * @param SiteInterface $site    A site instance
  * @param Request       $request A request instance
  *
  * @return string|boolean FALSE whether the site does not match
  */
 protected function matchRequest(SiteInterface $site, Request $request)
 {
     $results = array();
     // we read the value from the attribute to handle fragment support
     $requestPathInfo = $request->get('pathInfo', $request->getPathInfo());
     foreach ($site->getLanguageVersions() as $lv) {
         if (preg_match(sprintf('@^(%s)(/.*|$)@', $lv->getRelativePath()), $requestPathInfo, $results)) {
             $site->setHost($lv->getHost());
             $site->setLocale($lv->getLocale());
             $site->setRelativePath($lv->getRelativePath());
             $site->setTitle($lv->getTitle());
             $site->setMetaDescription($lv->getMetaDescription());
             $site->setMetaKeywords($lv->getMetaKeywords());
             $this->site = $site;
             $request->setLocale($lv->getLocale());
             $request->attributes->set('_locale', $lv->getLocale());
             return $results[2];
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public final function onKernelRequest(GetResponseEvent $event)
 {
     if (!$this->decoratorStrategy->isRouteUriDecorable($event->getRequest()->getPathInfo())) {
         return;
     }
     $this->handleKernelRequest($event);
     if ($this->site) {
         if ($this->site->getTitle()) {
             $this->seoPage->setTitle($this->site->getTitle());
         }
         if ($this->site->getMetaDescription()) {
             $this->seoPage->addMeta('name', 'description', $this->site->getMetaDescription());
         }
         if ($this->site->getMetaKeywords()) {
             $this->seoPage->addMeta('name', 'keywords', $this->site->getMetaKeywords());
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function getPageBy(SiteInterface $site = null, $fieldName, $value)
 {
     if ('id' === $fieldName) {
         $fieldName = 'pageId';
         $id = $value;
     } else {
         $id = null;
     }
     if (null === $id || !$site || $site && !isset($this->pages[$site->getId()][$id])) {
         $parameters = array($fieldName => $value);
         if ($site) {
             $parameters['site'] = $site;
         }
         $snapshot = $this->snapshotManager->findEnableSnapshot($parameters);
         if (!$snapshot) {
             throw new PageNotFoundException();
         }
         $page = new SnapshotPageProxy($this->snapshotManager, $this->transformer, $snapshot);
         $site = $page->getSite();
         if ($page) {
             $this->loadBlocks($page);
             $id = $page->getId();
             $this->pages[$site->getId()][$id] = $page;
         }
     }
     return $this->pages[$site->getId()][$id];
 }
 /**
  * @param SiteInterface $site
  *
  * @return mixed
  */
 public function getHybridPages(SiteInterface $site)
 {
     return $this->getEntityManager()->createQueryBuilder()->select('p')->from($this->class, 'p')->where('p.routeName <> :routeName and p.site = :site')->setParameters(array('routeName' => PageInterface::PAGE_ROUTE_CMS_NAME, 'site' => $site->getId()))->getQuery()->execute();
 }
 /**
  * {@inheritdoc}
  */
 protected function getPageBy(SiteInterface $site = null, $fieldName, $value)
 {
     if ('id' == $fieldName) {
         $id = $value;
     } elseif (isset($this->pageReferences[$fieldName][$value])) {
         $id = $this->pageReferences[$fieldName][$value];
     } else {
         $id = null;
     }
     if (null === $id || !isset($this->pages[$id])) {
         $this->pages[$id] = false;
         $parameters = array($fieldName => $value);
         if ($site) {
             $parameters['site'] = $site->getId();
         }
         $page = $this->pageManager->findOneBy($parameters);
         if (!$page) {
             throw new PageNotFoundException(sprintf('Unable to find the page : %s = %s', $fieldName, $value));
         }
         $this->loadBlocks($page);
         $id = $page->getId();
         if ($fieldName != 'id') {
             $this->pageReferences[$fieldName][$value] = $id;
         }
         $this->pages[$id] = $page;
     }
     return $this->pages[$id];
 }
    /**
     * Updates site page routes with all routes available in Symfony router service
     *
     * @param SiteInterface   $site   A page bundle site instance
     * @param OutputInterface $output A Symfony console output
     *
     * @return void
     */
    public function update(SiteInterface $site, OutputInterface $output = null)
    {
        $message = sprintf(" > <info>Updating core routes for site</info> : <comment>%s - %s</comment>", $site->getName(), $site->getUrl());
        $this->writeln($output, array(str_repeat('=', strlen($message)), "", $message, "", str_repeat('=', strlen($message))));
        $knowRoutes = array();
        $root = $this->pageManager->getPageByUrl($site, '/');
        // no root url for the given website, create one
        if (!$root) {
            $root = $this->pageManager->create(array('routeName' => PageInterface::PAGE_ROUTE_CMS_NAME, 'name' => 'Homepage', 'url' => '/', 'site' => $site, 'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT', 'slug' => '/'));
            $this->pageManager->save($root);
        }
        // Iterate over declared routes from the routing mechanism
        foreach ($this->router->getRouteCollection()->all() as $name => $route) {
            $name = trim($name);
            $knowRoutes[] = $name;
            $page = $this->pageManager->findOneBy(array('routeName' => $name, 'site' => $site->getId()));
            $routeHostRegex = $route->compile()->getHostRegex();
            if (!$this->decoratorStrategy->isRouteNameDecorable($name) || !$this->decoratorStrategy->isRouteUriDecorable($route->getPath()) || null !== $routeHostRegex && !preg_match($routeHostRegex, $site->getHost())) {
                if ($page) {
                    $page->setEnabled(false);
                    $this->writeln($output, sprintf('  <error>DISABLE</error> <error>% -50s</error> %s', $name, $route->getPath()));
                } else {
                    continue;
                }
            }
            $update = true;
            if (!$page) {
                $update = false;
                $requirements = $route->getRequirements();
                $page = $this->pageManager->create(array('routeName' => $name, 'name' => $name, 'url' => $route->getPath(), 'site' => $site, 'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT'));
            }
            if (!$page->getParent() && $page->getId() != $root->getId()) {
                $page->setParent($root);
            }
            $page->setSlug($route->getPath());
            $page->setUrl($route->getPath());
            $page->setRequestMethod(isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT');
            $this->pageManager->save($page);
            $this->writeln($output, sprintf('  <info>%s</info> % -50s %s', $update ? 'UPDATE ' : 'CREATE ', $name, $route->getPath()));
        }
        // Iterate over error pages
        foreach ($this->exceptionListener->getHttpErrorCodes() as $name) {
            $name = trim($name);
            $knowRoutes[] = $name;
            $page = $this->pageManager->findOneBy(array('routeName' => $name, 'site' => $site->getId()));
            if (!$page) {
                $params = array('routeName' => $name, 'name' => $name, 'decorate' => false, 'site' => $site);
                $page = $this->pageManager->create($params);
                $this->writeln($output, sprintf('  <info>%s</info> % -50s %s', 'CREATE ', $name, ''));
            }
            // an internal page or an error page should not have any parent (no direct access)
            $page->setParent(null);
            $this->pageManager->save($page);
        }
        $has = false;
        foreach ($this->pageManager->getHybridPages($site) as $page) {
            if (!$page->isHybrid() || $page->isInternal()) {
                continue;
            }
            if (!in_array($page->getRouteName(), $knowRoutes)) {
                if (!$has) {
                    $has = true;
                    $this->writeln($output, array('', 'Some hybrid pages does not exist anymore', str_repeat('-', 80)));
                }
                $this->writeln($output, sprintf('  <error>ERROR</error>   %s', $page->getRouteName()));
            }
        }
        if ($has) {
            $this->writeln($output, <<<MSG
<error>
  *WARNING* : Pages has been updated however some pages do not exist anymore.
              You must remove them manually.
</error>
MSG
);
        }
    }
 /**
  * Returns TRUE whether the given site matches the given request
  *
  * @param SiteInterface $site    A site instance
  * @param Request       $request A request instance
  *
  * @return string|boolean FALSE whether the site does not match
  */
 protected function matchRequest(SiteInterface $site, Request $request)
 {
     $results = array();
     // we read the value from the attribute to handle fragment support
     $requestPathInfo = $request->get('pathInfo', $request->getPathInfo());
     if (!preg_match(sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()), $requestPathInfo, $results)) {
         return false;
     }
     return $results[2];
 }
 /**
  * @param SiteInterface $site
  * @return BasePage[]
  */
 public function loadPagesEnabled(SiteInterface $site)
 {
     if ($this->pages == null) {
         $this->pages = $this->getEntityManager()->createQuery(sprintf('SELECT p FROM %s p INDEX BY p.id WHERE p.site = %d and p.published=1 ORDER BY p.position ASC', $this->class, $site->getId()))->useResultCache(true, 300)->execute();
         /* @var $Pages PageInterface[] */
         foreach ($this->pages as $page) {
             /* @var $page Page */
             $parent = $page->getParent();
             /* @var $parent Page */
             $page->disableChildrenLazyLoading();
             if (!$parent) {
                 continue;
             }
             if (isset($this->pages[$parent->getId()])) {
                 $this->pages[$parent->getId()]->disableChildrenLazyLoading();
                 $this->pages[$parent->getId()]->addChildren($page);
             }
         }
     }
     return $this->pages;
 }