diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index c420e1550..b235df3ee 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -36,4 +36,8 @@ Thelia\Controller\Front\CartController::changeItem cart + + + Thelia\Controller\Front\UrlRewritingController::check + diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 1a610768d..4b245a9e4 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -148,9 +148,9 @@ class BaseController extends ContainerAware * redirect request to specify url * @param string $url */ - public function redirect($url) + public function redirect($url, $status = 302) { - Redirect::exec($url); + Redirect::exec($url, $status); } /** diff --git a/core/lib/Thelia/Controller/Front/DefaultController.php b/core/lib/Thelia/Controller/Front/DefaultController.php index 235e1bb64..75588ca31 100755 --- a/core/lib/Thelia/Controller/Front/DefaultController.php +++ b/core/lib/Thelia/Controller/Front/DefaultController.php @@ -23,6 +23,9 @@ namespace Thelia\Controller\Front; use Symfony\Component\HttpFoundation\Request; +use Thelia\Model\ConfigQuery; +use Thelia\Tools\Redirect; +use Thelia\Tools\URL; /** * @@ -43,6 +46,16 @@ class DefaultController extends BaseFrontController */ public function noAction(Request $request) { + if(ConfigQuery::isRewritingEnable()) { + + /* Does the query GET parameters match a rewritten URL ? */ + $rewrittenUrl = URL::retrieveCurrent($request, true); + if($rewrittenUrl !== null) { + /* 301 redirection to rewritten URL */ + $this->redirect($rewrittenUrl, 301); + } + } + if (! $view = $request->query->get('view')) { $view = "index"; if ($request->request->has('view')) { diff --git a/core/lib/Thelia/Controller/Front/UrlRewritingController.php b/core/lib/Thelia/Controller/Front/UrlRewritingController.php new file mode 100755 index 000000000..ffdc95832 --- /dev/null +++ b/core/lib/Thelia/Controller/Front/UrlRewritingController.php @@ -0,0 +1,84 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Controller\Front; + +//use Propel\Runtime\Exception\PropelException; +//use Thelia\Form\Exception\FormValidationException; +//use Thelia\Core\Event\CartEvent; +//use Thelia\Core\Event\TheliaEvents; +//use Symfony\Component\HttpFoundation\Request; +//use Thelia\Form\CartAdd; + +use Thelia\Core\HttpFoundation\Request; +use Thelia\Exception\UrlRewritingException; +use Thelia\Model\ConfigQuery; +use Thelia\Tools\URL; + +class UrlRewritingController extends BaseFrontController +{ + public function check(Request $request) + { + if(ConfigQuery::isRewritingEnable()) { + try { + $rewrittentUrlData = URL::resolveCurrent($request); + } catch(UrlRewritingException $e) { + $code = $e->getCode(); + switch($e->getCode()) { + case UrlRewritingException::URL_NOT_FOUND : + /* TODO : redirect 404 */ + throw $e; + break; + default: + throw $e; + } + } + + /* define GET arguments in request */ + + if(null !== $rewrittentUrlData->view) { + $request->query->set('view', $rewrittentUrlData->view); + if(null !== $rewrittentUrlData->viewId) { + $request->query->set($rewrittentUrlData->view . '_id', $rewrittentUrlData->viewId); + } + } + if(null !== $rewrittentUrlData->locale) { + $request->query->set('locale', $rewrittentUrlData->locale); + } + + foreach($rewrittentUrlData->otherParameters as $parameter => $value) { + $request->query->set($parameter, $value); + } + } + + if (! $view = $request->query->get('view')) { + $view = "index"; + if ($request->request->has('view')) { + $view = $request->request->get('view'); + } + } + + $request->attributes->set('_view', $view); + + } + +} diff --git a/core/lib/Thelia/Exception/UrlRewritingException.php b/core/lib/Thelia/Exception/UrlRewritingException.php new file mode 100755 index 000000000..507058ce4 --- /dev/null +++ b/core/lib/Thelia/Exception/UrlRewritingException.php @@ -0,0 +1,40 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Exception; + +use Thelia\Log\Tlog; + +class UrlRewritingException extends \Exception +{ + const UNKNOWN_EXCEPTION = 0; + + const URL_NOT_FOUND = 404; + + public function __construct($message, $code = null, $previous = null) { + if($code === null) { + $code = self::UNKNOWN_EXCEPTION; + } + parent::__construct($message, $code, $previous); + } +} diff --git a/core/lib/Thelia/Rewriting/RewritingResolver.php b/core/lib/Thelia/Rewriting/RewritingResolver.php index 818dbec32..83d9840d6 100755 --- a/core/lib/Thelia/Rewriting/RewritingResolver.php +++ b/core/lib/Thelia/Rewriting/RewritingResolver.php @@ -22,6 +22,11 @@ /*************************************************************************************/ namespace Thelia\Rewriting; +use Propel\Runtime\ActiveQuery\Criteria; +use Thelia\Exception\RewritingUrlException; +use Thelia\Exception\UrlRewritingException; +use Thelia\Model\RewritingArgumentQuery; + /** * Class RewritingResolver * @package Thelia\Rewriting @@ -31,5 +36,47 @@ namespace Thelia\Rewriting; */ class RewritingResolver { + public $view; + public $viewId; + public $locale; + public $otherParameters; + public function __construct($url = null) + { + if($url !== null) { + $this->load($url); + } + } + + public function load($rewrittenUrl) + { + $search = RewritingArgumentQuery::create() + //->filterByUrl($rewrittenUrl) + ->joinRewritingUrl('ru', Criteria::RIGHT_JOIN) + ->where('`ru`.URL = ?', $rewrittenUrl, \PDO::PARAM_STR) + ->withColumn('`ru`.URL', 'ru_url') + ->withColumn('`ru`.VIEW', 'ru_view') + ->withColumn('`ru`.VIEW_LOCALE', 'ru_locale') + ->withColumn('`ru`.VIEW_ID', 'ru_viewId') + ->find(); + + if($search->count() == 0) { + throw new UrlRewritingException('URL NOT FOUND', UrlRewritingException::URL_NOT_FOUND); + } + + $otherParameters = array(); + foreach($search as $result) { + $parameter = $result->getParameter(); + $value = $result->getValue(); + + if(null !== $parameter) { + $otherParameters[$parameter] = $value; + } + } + + $this->view = $search->getFirst()->getVirtualColumn('ru_view'); + $this->viewId = $search->getFirst()->getVirtualColumn('ru_viewId'); + $this->locale = $search->getFirst()->getVirtualColumn('ru_locale'); + $this->otherParameters = $otherParameters; + } } diff --git a/core/lib/Thelia/Rewriting/RewritingRetriever.php b/core/lib/Thelia/Rewriting/RewritingRetriever.php index 0deb7de45..c47fb2a84 100755 --- a/core/lib/Thelia/Rewriting/RewritingRetriever.php +++ b/core/lib/Thelia/Rewriting/RewritingRetriever.php @@ -79,6 +79,10 @@ class RewritingRetriever */ public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array()) { + if(empty($viewOtherParameters)) { + return $this->getViewUrl($view, $viewLocale, $viewId); + } + $urlQuery = RewritingUrlQuery::create() ->joinRewritingArgument('ra', Criteria::LEFT_JOIN) ->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID') diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index 1bf0e566c..e51ed5abb 100755 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -25,6 +25,7 @@ namespace Thelia\Tools; use Symfony\Component\HttpFoundation\Request; use Thelia\Model\ConfigQuery; +use Thelia\Rewriting\RewritingResolver; use Thelia\Rewriting\RewritingRetriever; class URL @@ -126,7 +127,7 @@ class URL return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl; } - public static function retrieveCurrent(Request $request) + public static function retrieveCurrent(Request $request, $rewrittenUrlOnly = false) { $rewrittenUrl = null; if(ConfigQuery::isRewritingEnable()) { @@ -151,6 +152,25 @@ class URL $rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters); } - return $rewrittenUrl === null ? self::viewUrl($view, $allParametersWithoutView) : $rewrittenUrl; + if($rewrittenUrlOnly) { + return $rewrittenUrl !== null ? $rewrittenUrl : null; + } else { + return $rewrittenUrl !== null ? $rewrittenUrl : self::viewUrl($view, $allParametersWithoutView); + } + } + + public static function resolve($url) + { + $resolver = new RewritingResolver($url); + return $resolver; + } + + public static function resolveCurrent(Request $request) + { + if(ConfigQuery::isRewritingEnable()) { + return self::resolve($request->get('rewritten_url')); + } + + return null; } }