Skip to content

Commit

Permalink
Minor change in code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbitaleio committed Aug 6, 2015
1 parent 3dd061b commit d7b2e36
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,67 @@
use Orbitale\Bundle\CmsBundle\Entity\Page;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class PageController extends AbstractCmsController
{

/**
* @var Request
*/
protected $request;

/**
* @Route("/{slugs}", name="orbitale_cms_page", requirements={"slugs": "([a-zA-Z0-9_-]+\/?)*"}, defaults={"slugs": ""})
*
* @param Request $request
* @param string $slugs
* @param string|null $_locale
*
* @return \Symfony\Component\HttpFoundation\Response
* @return Response
*/
public function indexAction(Request $request, $slugs = '', $_locale = null)
{
if (preg_match('~/$~', $slugs)) {
return $this->redirect($this->generateUrl('orbitale_cms_page', array('slugs' => rtrim($slugs, '/'))));
}

$this->request = $request;
$this->request->setLocale($_locale ?: $this->request->getLocale());

$slugsArray = preg_split('~/~', $slugs, -1, PREG_SPLIT_NO_EMPTY);

$pages = $this->getPages($slugsArray);

$currentPage = $this->getCurrentPage($pages, $slugsArray);

if ($currentPage->isHomepage() && strlen($slugs)) {
return $this->redirect($this->generateUrl('orbitale_cms_page', array('slugs' => '', '_locale' => $this->request->getLocale())));
}

return $currentPage instanceof Response
? $currentPage
: $this->render('OrbitaleCmsBundle:Front:index.html.twig', array(
'pages' => $pages,
'page' => $currentPage,
))
;
}

/**
* Retrieves the page list based on slugs.
* Also checks the hierarchy of the different pages.
*
* @param array $slugsArray
*
* @return Page[]
*/
protected function getPages(array $slugsArray = array())
{
/** @var Page[] $pages */
$pages = $this->getDoctrine()->getManager()
->getRepository('OrbitaleCmsBundle:Page')
->findFrontPages($slugsArray, $request->getHost(), $_locale ?: $request->getLocale())
->findFrontPages($slugsArray, $this->request->getHost(), $this->request->getLocale())
;

if (!count($pages) || (count($slugsArray) && count($pages) !== count($slugsArray))) {
Expand All @@ -49,20 +86,25 @@ public function indexAction(Request $request, $slugs = '', $_locale = null)
throw $this->createNotFoundException($msg);
}

return $pages;
}

/**
* Retrieves the current page based on page list and entered slugs
*
* @param Page[] $pages
* @param array $slugsArray
*
* @return Page
*/
protected function getCurrentPage(array $pages, array $slugsArray)
{
if (count($pages) === count($slugsArray)) {
/** @var Page $currentPage */
$currentPage = $this->getFinalTreeElement($slugsArray, $pages);
} else {
$currentPage = current($pages);
}

if ($currentPage->isHomepage() && strlen($slugs)) {
return $this->redirect($this->generateUrl('orbitale_cms_page', array('slugs' => '', '_locale' => $_locale)));
}

return $this->render('OrbitaleCmsBundle:Front:index.html.twig', array(
'pages' => $pages,
'page' => $currentPage,
));
return $currentPage;
}
}

0 comments on commit d7b2e36

Please sign in to comment.