public function getCurrentSite(Request $request) { $currentSite = null; $siteId = $request->get('site'); if (!$siteId && $this->session->has(self::SESSION_NAME)) { $currentSiteId = $this->session->get(self::SESSION_NAME); $currentSite = $this->siteManager->find($currentSiteId); if (!$currentSite) { $sites = $this->getSites(); if (count($sites) > 0) { $currentSite = $this->getSites()[0]; } } } else { foreach ($this->getSites() as $site) { if ($siteId && $site->getId() == $siteId) { $currentSite = $site; } elseif (!$siteId && $site->getIsDefault()) { $currentSite = $site; } } if (!$currentSite && count($this->sites) > 0) { $currentSite = $this->sites[0]; } } if ($currentSite) { $this->session->set(self::SESSION_NAME, $currentSite->getId()); } return $currentSite; }
/** * Creates snapshots of all pages. * * @ApiDoc( * statusCodes={ * 200="Returned when snapshots are successfully queued for creation", * 400="Returned when an error has occurred while snapshots creation", * } * ) * * @return \FOS\RestBundle\View\View * * @throws NotFoundHttpException */ public function postPagesSnapshotsAction() { $sites = $this->siteManager->findAll(); foreach ($sites as $site) { $this->backend->createAndPublish('sonata.page.create_snapshot', array('siteId' => $site->getId())); } return array('queued' => true); }
/** * Retrieves Site with id $id or throws an exception if it doesn't exist * * @param $id * * @return SiteInterface * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function getSite($id) { $site = $this->siteManager->findOneBy(array('id' => $id)); if (null === $site) { throw new NotFoundHttpException(sprintf('Site (%d) not found', $id)); } return $site; }
/** * Write a site, this method is used by both POST and PUT action methods. * * @param Request $request Symfony request * @param int|null $id A post identifier * * @return FormInterface */ protected function handleWriteSite($request, $id = null) { $site = $id ? $this->getSite($id) : null; $form = $this->formFactory->createNamed(null, 'sonata_page_api_form_site', $site, array('csrf_protection' => false)); $form->bind($request); if ($form->isValid()) { $site = $form->getData(); $this->siteManager->save($site); $view = FOSRestView::create($site); $serializationContext = SerializationContext::create(); $serializationContext->setGroups(array('sonata_api_read')); $serializationContext->enableMaxDepthChecks(); $view->setSerializationContext($serializationContext); return $view; } return $form; }
/** * @return SiteInterface[] */ public function getSites() { return $this->siteManager->findBy(array()); }
/** * @param Request $request * * @return SiteInterface[] */ protected function getSites(Request $request) { // sort by isDefault DESC in order to have default site in first position // which will be used if no site found for the current request return $this->siteManager->findBy(array('host' => array($request->getHost(), 'localhost'), 'enabled' => true), array('isDefault' => 'DESC')); }