From 21068f4c1fa280ffed0469d4d863de1689ac3df7 Mon Sep 17 00:00:00 2001 From: franck Date: Wed, 17 Jul 2013 11:36:28 +0200 Subject: [PATCH] Added UrlGenerator smarty plugin, to allow templates and url manipulation in templates. --- core/lib/Thelia/Config/Resources/config.xml | 4 +- .../Core/Template/Smarty/Plugins/Security.php | 8 ++- .../Template/Smarty/Plugins/UrlGenerator.php | 59 +++++++++++++++++-- core/lib/Thelia/Tools/URL.php | 15 ++++- 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index bb680aeac..b91428f6c 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -27,6 +27,7 @@
+ @@ -120,8 +121,9 @@ - + + diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php index 28fd8e4cf..cd19576b1 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php @@ -69,11 +69,17 @@ class Security implements SmartyPluginInterface $permissions = $this->_explode($params['permissions']); if (! $this->securityContext->isGranted($roles, $permissions)) { - throw new AuthenticationException( + $ex = new AuthenticationException( sprintf("User not granted for roles '%s', permissions '%s' in context '%s'.", implode(',', $roles), implode(',', $permissions), $context ) ); + + if (! empty($params['login_tpl'])) { + $ex->setLoginTemplate($params['login_tpl']); + } + + throw $ex; } return ''; diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php index adcee9579..cef25cc33 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php @@ -1,5 +1,4 @@ request = $request; + } + + /** * Process url generator function * * @param array $params @@ -42,7 +49,49 @@ class UrlGenerator implements SmartyPluginInterface // the path to process $path =trim($params['path']); - return URL::absoluteUrl($path); + return URL::absoluteUrl($path, $this->getArgsFromParam($params)); + } + + /** + * Process view url generator function + * + * @param array $params + * @param unknown $smarty + * @return string no text is returned. + */ + public function generateViewUrlFunction($params, &$smarty) + { + // the path to process + $view = trim($params['view']); + + return URL::viewUrl($view, $this->getArgsFromParam($params)); + } + + /** + * Get URL parameters array from a comma separated list or arguments in the + * parameters. + * + * @param array $params Smarty function params + * @return array the parameters array (either emply, of valued) + */ + private function getArgsFromParam($params) { + + if (isset($params['args'])) + return explode($params['args'], ','); + + return array(); + } + + /** + * Process view url generator function + * + * @param array $params + * @param unknown $smarty + * @return string no text is returned. + */ + public function generateReturnToUrl($params, &$smarty) + { + return URL::absoluteUrl($this->request->getSession()->getReturnToUrl()); } /** @@ -53,7 +102,9 @@ class UrlGenerator implements SmartyPluginInterface public function getPluginDescriptors() { return array( - new SmartyPluginDescriptor('function', 'url', $this, 'generateUrlFunction') + new SmartyPluginDescriptor('function', 'url', $this, 'generateUrlFunction'), + new SmartyPluginDescriptor('function', 'viewurl', $this, 'generateViewUrlFunction'), + new SmartyPluginDescriptor('function', 'return_to_url', $this, 'generateReturnToUrl') ); } } diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index b1c8964d4..ae30d7280 100644 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -32,7 +32,6 @@ class URL * * @param string $path the relative path * @param mixed $parameters An array of parameters - * @param Boolean|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface) * * @return string The generated URL */ @@ -54,4 +53,18 @@ class URL return $base . $queryString; } + + /** + * Returns the Absolute URL to a view + * + * @param string $viewName the view name (e.g. login for login.html) + * @param mixed $parameters An array of parameters + * + * @return string The generated URL + */ + public static function viewUrl($viewName, array $parameters = array()) { + $path = sprintf("%s?view=%s", ConfigQuery::read('base_url', '/'), $viewName); + + return self::absoluteUrl($path, $parameters); + } }