HosipLan

Untitled

Mar 12th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Kdyby (http://www.kdyby.org)
  5.  *
  6.  * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
  7.  *
  8.  * @license http://www.kdyby.org/license
  9.  */
  10.  
  11. namespace Kdyby\CMS;
  12.  
  13. use Kdyby;
  14. use Nette;
  15. use Nette\Application\Routers\Route;
  16.  
  17.  
  18.  
  19. /**
  20.  * @author Filip Procházka <[email protected]>
  21.  */
  22. class NodeRouter extends Nette\Application\Routers\RouteList
  23. {
  24.     /**
  25.      * @var \Kdyby\CMS\Application
  26.      */
  27.     private $application;
  28.  
  29.     /**
  30.      * @var ContentRepository\PathMap
  31.      */
  32.     private $pathMap;
  33.  
  34.     /**
  35.      * @var string
  36.      */
  37.     private $nodePath;
  38.  
  39.  
  40.  
  41.     /**
  42.      * @param ContentRepository\PathMap $pathMap
  43.      */
  44.     public function __construct(ContentRepository\PathMap $pathMap)
  45.     {
  46.         parent::__construct('CmsPackage');
  47.         $this->pathMap = $pathMap;
  48.  
  49.         $this[] = new Route('[<language [a-z]{2,3}>/][<node>][.<format>]?action=<action>', array(
  50.             'presenter' => 'Node',
  51.             'action' => 'default',
  52.             'language' => array(
  53.                 Route::VALUE => NULL,
  54.                 Route::FILTER_IN => function ($lang) {
  55.                     return in_array($lang, array('cs', 'en')) ? $lang : NULL;
  56.                 },
  57.             ),
  58.             'node' => array(
  59.                 Route::VALUE => NULL,
  60.                 Route::PATTERN => '.*',
  61.                 Route::FILTER_IN => callback($this, 'filterInNode'),
  62.                 Route::FILTER_OUT => callback($this, 'filterOutNode'),
  63.             ),
  64.             'format' => NULL,
  65.         ));
  66.     }
  67.  
  68.  
  69.  
  70.     /**
  71.      * @internal
  72.      * @param \Kdyby\CMS\Application $application
  73.      */
  74.     public function setApplication(Nette\Application\Application $application)
  75.     {
  76.         $this->application = $application;
  77.     }
  78.  
  79.  
  80.  
  81.     /**
  82.      * @internal
  83.      * @param string $nodePath
  84.      * @return int|NULL
  85.      */
  86.     public function filterInNode($nodePath)
  87.     {
  88.         if (is_numeric($nodePath)) {
  89.             return $nodePath;
  90.         }
  91.  
  92.         $this->nodePath = trim($nodePath, '/');
  93.         return $this->pathMap->pathId($this->nodePath);
  94.     }
  95.  
  96.  
  97.  
  98.     /**
  99.      * @internal
  100.      * @param string $node
  101.      * @return null|string
  102.      */
  103.     public function filterOutNode($node)
  104.     {
  105.         if (is_object($node)) {
  106.             /** @var \Kdyby\CMS\Sitemap\AbstractNode $node */
  107.             return $node->getPath() ?: NULL;
  108.  
  109.         } elseif (!is_numeric($node)) {
  110.             return $node ?: NULL;
  111.         }
  112.  
  113.         return $this->pathMap->absolutePath($node) ?: NULL;
  114.     }
  115.  
  116.  
  117.  
  118.     /**
  119.      * @param \Nette\Http\IRequest $httpRequest
  120.      * @return \Nette\Application\Request|NULL
  121.      */
  122.     public function match(Nette\Http\IRequest $httpRequest)
  123.     {
  124.         if (!$this->application instanceof Application) {
  125.             return NULL;
  126.         }
  127.  
  128.         if (!$this->application->getSite()) {
  129.             return NULL;
  130.         }
  131.  
  132.         /** @var Route $route */
  133.         $route = $this[0];
  134.         if ($appRequest = $route->match($httpRequest)) {
  135.             $presenter = $appRequest->getPresenterName();
  136.             $appRequest->setPresenterName($this->getModule() . $presenter);
  137.             $params = $appRequest->getParameters();
  138.  
  139.             if (!$this->nodePath && ($nodeId = $this->pathMap->pathId(""))) {
  140.                 $params['node'] = (int)$nodeId;
  141.             }
  142.  
  143.             $params['site'] = $this->application->getSite()->getId();
  144.             $params['nodePath'] = $this->nodePath;
  145.             $this->nodePath = NULL;
  146.             $appRequest->setParameters($params);
  147.  
  148.             return $appRequest;
  149.         }
  150.  
  151.         return NULL;
  152.     }
  153.  
  154.  
  155.  
  156.     /**
  157.      * @param \Nette\Application\Request $appRequest
  158.      * @param \Nette\Http\Url $refUrl
  159.      * @return NULL|string
  160.      */
  161.     public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
  162.     {
  163.         if (!$this->application instanceof Application) {
  164.             return NULL;
  165.         }
  166.  
  167.         if (!$this->application->getSite()) {
  168.             return NULL;
  169.         }
  170.  
  171.         $params = $appRequest->getParameters();
  172.         if (isset($params['nodePath']) && (!isset($params['node']) || $params['node'] === NULL)) {
  173.             $params['node'] = $params['nodePath'];
  174.         }
  175.         unset($params['site'], $params['nodePath']);
  176.         $appRequest->setParameters($params);
  177.  
  178.         return parent::constructUrl($appRequest, $refUrl);
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment