Added UrlGenerator smarty plugin, to allow templates and url

manipulation in templates.
This commit is contained in:
franck
2013-07-17 11:36:28 +02:00
parent 0dfee8331b
commit 21068f4c1f
4 changed files with 79 additions and 7 deletions

View File

@@ -27,6 +27,7 @@
<forms> <forms>
<form name="thelia.customer.creation" class="Thelia\Form\CustomerCreation"/> <form name="thelia.customer.creation" class="Thelia\Form\CustomerCreation"/>
<form name="thelia.customer.modification" class="Thelia\Form\CustomerModification"/> <form name="thelia.customer.modification" class="Thelia\Form\CustomerModification"/>
<form name="thelia.customer.login" class="Thelia\Form\CustomerLogin"/>
<form name="thelia.admin.login" class="Thelia\Form\AdminLogin"/> <form name="thelia.admin.login" class="Thelia\Form\AdminLogin"/>
</forms> </forms>
@@ -120,8 +121,9 @@
<tag name="thelia.parser.register_plugin"/> <tag name="thelia.parser.register_plugin"/>
</service> </service>
<service id="smarty.url.module" class="Thelia\Core\Template\Smarty\Plugins\UrlGenerator" > <service id="smarty.url.module" class="Thelia\Core\Template\Smarty\Plugins\UrlGenerator" scope="request">
<tag name="thelia.parser.register_plugin"/> <tag name="thelia.parser.register_plugin"/>
<argument type="service" id="request"/>
</service> </service>
<service id="smarty.plugin.security" class="Thelia\Core\Template\Smarty\Plugins\Security" scope="request"> <service id="smarty.plugin.security" class="Thelia\Core\Template\Smarty\Plugins\Security" scope="request">

View File

@@ -69,11 +69,17 @@ class Security implements SmartyPluginInterface
$permissions = $this->_explode($params['permissions']); $permissions = $this->_explode($params['permissions']);
if (! $this->securityContext->isGranted($roles, $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'.", sprintf("User not granted for roles '%s', permissions '%s' in context '%s'.",
implode(',', $roles), implode(',', $permissions), $context implode(',', $roles), implode(',', $permissions), $context
) )
); );
if (! empty($params['login_tpl'])) {
$ex->setLoginTemplate($params['login_tpl']);
}
throw $ex;
} }
return ''; return '';

View File

@@ -1,5 +1,4 @@
<?php <?php
use Thelia\Tools\URL;
/*************************************************************************************/ /*************************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
@@ -27,10 +26,18 @@ namespace Thelia\Core\Template\Smarty\Plugins;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Tools\URL; use Thelia\Tools\URL;
use Thelia\Core\HttpFoundation\Request;
class UrlGenerator implements SmartyPluginInterface class UrlGenerator implements SmartyPluginInterface
{ {
/** protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Process url generator function * Process url generator function
* *
* @param array $params * @param array $params
@@ -42,7 +49,49 @@ class UrlGenerator implements SmartyPluginInterface
// the path to process // the path to process
$path =trim($params['path']); $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() public function getPluginDescriptors()
{ {
return array( 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')
); );
} }
} }

View File

@@ -32,7 +32,6 @@ class URL
* *
* @param string $path the relative path * @param string $path the relative path
* @param mixed $parameters An array of parameters * @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 * @return string The generated URL
*/ */
@@ -54,4 +53,18 @@ class URL
return $base . $queryString; 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);
}
} }