Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file is part of the Kdyby (http://www.kdyby.org)
- *
- * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
- *
- * @license http://www.kdyby.org/license
- */
- namespace Kdyby\CMS;
- use Kdyby;
- use Nette;
- use Nette\Application\Routers\Route;
- /**
- * @author Filip Procházka <[email protected]>
- */
- class NodeRouter extends Nette\Application\Routers\RouteList
- {
- /**
- * @var \Kdyby\CMS\Application
- */
- private $application;
- /**
- * @var ContentRepository\PathMap
- */
- private $pathMap;
- /**
- * @var string
- */
- private $nodePath;
- /**
- * @param ContentRepository\PathMap $pathMap
- */
- public function __construct(ContentRepository\PathMap $pathMap)
- {
- parent::__construct('CmsPackage');
- $this->pathMap = $pathMap;
- $this[] = new Route('[<language [a-z]{2,3}>/][<node>][.<format>]?action=<action>', array(
- 'presenter' => 'Node',
- 'action' => 'default',
- 'language' => array(
- Route::VALUE => NULL,
- Route::FILTER_IN => function ($lang) {
- return in_array($lang, array('cs', 'en')) ? $lang : NULL;
- },
- ),
- 'node' => array(
- Route::VALUE => NULL,
- Route::PATTERN => '.*',
- Route::FILTER_IN => callback($this, 'filterInNode'),
- Route::FILTER_OUT => callback($this, 'filterOutNode'),
- ),
- 'format' => NULL,
- ));
- }
- /**
- * @internal
- * @param \Kdyby\CMS\Application $application
- */
- public function setApplication(Nette\Application\Application $application)
- {
- $this->application = $application;
- }
- /**
- * @internal
- * @param string $nodePath
- * @return int|NULL
- */
- public function filterInNode($nodePath)
- {
- if (is_numeric($nodePath)) {
- return $nodePath;
- }
- $this->nodePath = trim($nodePath, '/');
- return $this->pathMap->pathId($this->nodePath);
- }
- /**
- * @internal
- * @param string $node
- * @return null|string
- */
- public function filterOutNode($node)
- {
- if (is_object($node)) {
- /** @var \Kdyby\CMS\Sitemap\AbstractNode $node */
- return $node->getPath() ?: NULL;
- } elseif (!is_numeric($node)) {
- return $node ?: NULL;
- }
- return $this->pathMap->absolutePath($node) ?: NULL;
- }
- /**
- * @param \Nette\Http\IRequest $httpRequest
- * @return \Nette\Application\Request|NULL
- */
- public function match(Nette\Http\IRequest $httpRequest)
- {
- if (!$this->application instanceof Application) {
- return NULL;
- }
- if (!$this->application->getSite()) {
- return NULL;
- }
- /** @var Route $route */
- $route = $this[0];
- if ($appRequest = $route->match($httpRequest)) {
- $presenter = $appRequest->getPresenterName();
- $appRequest->setPresenterName($this->getModule() . $presenter);
- $params = $appRequest->getParameters();
- if (!$this->nodePath && ($nodeId = $this->pathMap->pathId(""))) {
- $params['node'] = (int)$nodeId;
- }
- $params['site'] = $this->application->getSite()->getId();
- $params['nodePath'] = $this->nodePath;
- $this->nodePath = NULL;
- $appRequest->setParameters($params);
- return $appRequest;
- }
- return NULL;
- }
- /**
- * @param \Nette\Application\Request $appRequest
- * @param \Nette\Http\Url $refUrl
- * @return NULL|string
- */
- public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
- {
- if (!$this->application instanceof Application) {
- return NULL;
- }
- if (!$this->application->getSite()) {
- return NULL;
- }
- $params = $appRequest->getParameters();
- if (isset($params['nodePath']) && (!isset($params['node']) || $params['node'] === NULL)) {
- $params['node'] = $params['nodePath'];
- }
- unset($params['site'], $params['nodePath']);
- $appRequest->setParameters($params);
- return parent::constructUrl($appRequest, $refUrl);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment