url global substitution

This commit is contained in:
Etienne Roudeix
2013-08-30 14:30:33 +02:00
parent 4ba16dc290
commit 3c71aa613f
15 changed files with 126 additions and 166 deletions

View File

@@ -90,7 +90,8 @@ class FeatureValue extends BaseLoop
$this->request->getSession()->getLocale(),
array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'),
FeatureAvTableMap::TABLE_NAME,
'FEATURE_AV_ID'
'FEATURE_AV_ID',
true
);
$feature = $this->getFeature();

View File

@@ -40,11 +40,7 @@ class ParserContext implements \IteratorAggregate
public function __construct(Request $request)
{
// Setup basic variables
$this
->set('BASE_URL' , ConfigQuery::read('base_url', '/'))
->set('INDEX_PAGE' , URL::getIndexPage())
->set('RETURN_TO_URL' , URL::absoluteUrl($request->getSession()->getReturnToUrl()))
->set('THELIA_VERSION' , ConfigQuery::read('thelia_version', 'undefined'))
$this->set('THELIA_VERSION' , ConfigQuery::read('thelia_version', 'undefined'))
;
}

View File

@@ -47,14 +47,15 @@ class UrlGenerator extends AbstractSmartyPlugin
public function generateUrlFunction($params, &$smarty)
{
// the path to process
$path = $this->getParam($params, 'path');
$path = $this->getParam($params, 'path');
$target = $this->getParam($params, 'target', null);
$target = $this->getParam($params, 'target', null);
$url = URL::absoluteUrl($path, $this->getArgsFromParam($params, array('path', 'target')));
$url = URL::absoluteUrl($path, $this->getArgsFromParam($params, array('path', 'target')));
if ($target != null) $url .= '#'.$target;
return $url;
if ($target != null) $url .= '#'.$target;
return $url;
}
/**
@@ -81,6 +82,15 @@ class UrlGenerator extends AbstractSmartyPlugin
return $this->generateViewUrlFunction($params, true);
}
public function navigateToUrlFunction($params, &$smarty)
{
$to = $this->getParam($params, 'to', null);
$toMethod = $this->getNavigateToMethod($to);
return $this->$toMethod();
}
protected function generateViewUrlFunction($params, $forAdmin)
{
// the view name (without .html)
@@ -125,7 +135,50 @@ class UrlGenerator extends AbstractSmartyPlugin
return array(
new SmartyPluginDescriptor('function', 'url', $this, 'generateUrlFunction'),
new SmartyPluginDescriptor('function', 'viewurl', $this, 'generateFrontViewUrlFunction'),
new SmartyPluginDescriptor('function', 'admin_viewurl', $this, 'generateAdminViewUrlFunction')
new SmartyPluginDescriptor('function', 'admin_viewurl', $this, 'generateAdminViewUrlFunction'),
new SmartyPluginDescriptor('function', 'navigate', $this, 'navigateToUrlFunction'),
);
}
/**
* @return array sur le format "to_value" => "method_name"
*/
protected function getNavigateToValues()
{
return array(
"current" => "getCurrentUrl",
"return_to" => "getReturnToUrl",
"index" => "getIndexUrl",
);
}
protected function getNavigateToMethod($to)
{
if($to === null) {
throw new \InvalidArgumentException("Missing 'to' parameter in `navigate` substitution.");
}
$navigateToValues = $this->getNavigateToValues();
if(!array_key_exists($to, $navigateToValues)) {
throw new \InvalidArgumentException("Incorrect value for parameter `to` in `navigate` substitution.");
}
return $navigateToValues[$to];
}
protected function getCurrentUrl()
{
return URL::retrieveCurrent($this->request);
}
protected function getReturnToUrl()
{
return URL::absoluteUrl($this->request->getSession()->getReturnToUrl());
}
protected function getIndexUrl()
{
return Url::getIndexPage();
}
}

View File

@@ -162,9 +162,9 @@ class RewritingUrlTableMap extends TableMap
// columns
$this->addPrimaryKey('ID', 'Id', 'INTEGER', true, null, null);
$this->addColumn('URL', 'Url', 'VARCHAR', true, 255, null);
$this->addColumn('VIEW', 'View', 'VARCHAR', false, 255, null);
$this->addColumn('VIEW', 'View', 'VARCHAR', true, 255, null);
$this->addColumn('VIEW_ID', 'ViewId', 'VARCHAR', false, 255, null);
$this->addColumn('VIEW_LOCALE', 'ViewLocale', 'VARCHAR', false, 255, null);
$this->addColumn('VIEW_LOCALE', 'ViewLocale', 'VARCHAR', true, 255, null);
$this->addForeignKey('REDIRECTED', 'Redirected', 'INTEGER', 'rewriting_url', 'ID', false, null, null);
$this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null);
$this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null);

View File

@@ -23,7 +23,7 @@ class ModelCriteriaTools
* @param null $foreignTable
* @param string $foreignKey
*/
public static function getFrontEndI18n(ModelCriteria &$search, $defaultLangWithoutTranslation, $askedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID')
public static function getFrontEndI18n(ModelCriteria &$search, $defaultLangWithoutTranslation, $askedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID', $forceReturn = false)
{
if($foreignTable === null) {
$foreignTable = $search->getTableMap()->getName();
@@ -32,13 +32,13 @@ class ModelCriteriaTools
$aliasPrefix = $foreignTable . '_';
}
$askedLocaleI18nAlias = 'asked_locale_i18n';
$defaultLocaleI18nAlias = 'default_locale_i18n';
$askedLocaleI18nAlias = $aliasPrefix . 'asked_locale_i18n';
$defaultLocaleI18nAlias = $aliasPrefix . 'default_locale_i18n';
if($defaultLangWithoutTranslation == 0) {
$askedLocaleJoin = new Join();
$askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias);
$askedLocaleJoin->setJoinType(Criteria::INNER_JOIN);
$askedLocaleJoin->setJoinType($forceReturn === false ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN);
$search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias)
->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR);
@@ -67,7 +67,9 @@ class ModelCriteriaTools
$search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
$search->where('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)');
if(!$forceReturn) {
$search->where('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)');
}
foreach($columns as $column) {
$search->withColumn('CASE WHEN NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID) THEN `' . $askedLocaleI18nAlias . '`.`' . $column . '` ELSE `' . $defaultLocaleI18nAlias . '`.`' . $column . '` END', $aliasPrefix . 'i18n_' . $column);
@@ -84,7 +86,7 @@ class ModelCriteriaTools
$aliasPrefix = $foreignTable . '_';
}
$askedLocaleI18nAlias = 'asked_locale_i18n';
$askedLocaleI18nAlias = $aliasPrefix . 'asked_locale_i18n';
$askedLocaleJoin = new Join();
$askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias);

View File

@@ -35,7 +35,7 @@ use Thelia\Model\Map\RewritingUrlTableMap;
*/
class RewritingRetriever
{
public function getViewUrl($view, $viewId, $viewLocale)
public function getViewUrl($view, $viewLocale, $viewId)
{
$url = $this->getViewUrlQuery($view, $viewId, $viewLocale);
@@ -48,22 +48,22 @@ class RewritingRetriever
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
->where('ISNULL(`ra`.REWRITING_URL_ID)')
->filterByView($view)
->filterByViewId($viewId)
->filterByViewLocale($viewLocale)
->filterByViewId($viewId)
->filterByRedirected(null)
->orderByUpdatedAt(Criteria::DESC)
->findOne();
}
public function getSpecificUrl($view = null, $viewId = null, $viewLocale = null, $viewOtherParameters = array())
public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array())
{
$urlQuery = RewritingUrlQuery::create()
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
->withColumn('`ra`.PARAMETER', 'ra_parameter')
->withColumn('`ra`.VALUE', 'ra_value')
->filterByView($view)
->filterByViewId($viewId)
->filterByViewLocale($viewLocale)
->filterByViewId($viewId)
->filterByRedirected(null)
->orderByUpdatedAt(Criteria::DESC);
@@ -86,10 +86,7 @@ class RewritingRetriever
->combine(array('count_condition', 'parameter_full_condition'), Criteria::LOGICAL_AND, 'full_having_condition');
$urlQuery
->having(array('full_having_condition'))
//->having('COUNT(' . RewritingUrlTableMap::ID . ') = ?', $otherParametersCount, \PDO::PARAM_INT)
;
$urlQuery->having(array('full_having_condition'));
} else {
$urlQuery->where('ISNULL(`ra`.REWRITING_URL_ID)');
}

View File

@@ -53,7 +53,11 @@ class URL
// Already absolute ?
if (substr($path, 0, 4) != 'http') {
$root = $path_only == self::PATH_TO_FILE ? ConfigQuery::read('base_url', '/') : self::getIndexPage();
/**
* @etienne : can't be done here for it's already done in ::viewUrl / ::adminViewUrl
*/
//$root = $path_only == self::PATH_TO_FILE ? ConfigQuery::read('base_url', '/') : self::getIndexPage();
$root = $path_only == self::PATH_TO_FILE ? ConfigQuery::read('base_url', '/') : '';
$base = rtrim($root, '/') . '/' . ltrim($path, '/');
} else
@@ -116,7 +120,7 @@ class URL
$rewrittenUrl = null;
if(ConfigQuery::isRewritingEnable()) {
$retriever = new RewritingRetriever();
$rewrittenUrl = $retriever->getViewUrl($view, $viewId, $viewLocale);
$rewrittenUrl = $retriever->getViewUrl($view, $viewLocale, $viewId);
}
return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl;
@@ -127,24 +131,26 @@ class URL
$rewrittenUrl = null;
if(ConfigQuery::isRewritingEnable()) {
$view = $request->query->get('view', null);
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
$viewLocale = $request->query->get('locale', null);
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
$allOtherParameters = $request->query->all();
$allParameters = $request->query->all();
$allParametersWithoutView = $allParameters;
if($view !== null) {
unset($allOtherParameters['view']);
unset($allParametersWithoutView['view']);
}
$allOtherParameters = $allParametersWithoutView;
if($viewLocale !== null) {
unset($allOtherParameters['locale']);
}
if($viewId !== null) {
unset($allOtherParameters[$view . '_id']);
}
if($viewLocale !== null) {
unset($allOtherParameters['locale']);
}
$retriever = new RewritingRetriever();
$rewrittenUrl = $retriever->getSpecificUrl($view, $viewId, $viewLocale, $allOtherParameters);
$rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
}
return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl;
return $rewrittenUrl === null ? self::viewUrl($view, $allParametersWithoutView) : $rewrittenUrl;
}
}

View File

@@ -1443,9 +1443,9 @@ CREATE TABLE `rewriting_url`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`url` VARCHAR(255) NOT NULL,
`view` VARCHAR(255),
`view` VARCHAR(255) NOT NULL,
`view_id` VARCHAR(255),
`view_locale` VARCHAR(255),
`view_locale` VARCHAR(255) NOT NULL,
`redirected` INTEGER,
`created_at` DATETIME,
`updated_at` DATETIME,

View File

@@ -1096,9 +1096,9 @@
<table name="rewriting_url" namespace="Thelia\Model">
<column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER" />
<column name="url" required="true" size="255" type="VARCHAR" />
<column name="view" size="255" type="VARCHAR" />
<column name="view" required="true" size="255" type="VARCHAR" />
<column name="view_id" size="255" type="VARCHAR" />
<column name="view_locale" size="255" type="VARCHAR" />
<column name="view_locale" required="true" size="255" type="VARCHAR" />
<column name="redirected" type="INTEGER" />
<foreign-key foreignTable="rewriting_url" name="fk_rewriting_url_redirected" onDelete="RESTRICT" onUpdate="RESTRICT">
<reference foreign="id" local="redirected" />

View File

@@ -67,7 +67,7 @@
</form>
{/loop}
<div class="view-shop"><a href="{$INDEX_PAGE}" title="{intl l='View site'}" target="_blank"><i class="icon-white icon-eye-open"></i> {intl l="View shop"}</a></div>
<div class="view-shop"><a href="{navigate to="index"}" title="{intl l='View site'}" target="_blank"><i class="icon-white icon-eye-open"></i> {intl l="View shop"}</a></div>
</div>
</div>

View File

@@ -8,7 +8,7 @@
</ul>
{form name="thelia.cart.add" }
{* We use $INDEX_PAGE as form action to avoid mixing post and get data *}
{* We use {navigate to="index"} as form action to avoid mixing post and get data *}
<form action="{url path="/cart/add" }" method="post" {form_enctype form=$form}>
{*

View File

@@ -22,34 +22,10 @@
{loop name="product" type="product" category="#ID"}
<div style="border: dashed 2px red; padding: 20px; margin: 10px;">
<h3><a name="#REF" href="#URL">PRODUCT #ID : #REF (#LOOP_COUNT / #LOOP_TOTAL)</a></h3>
<h3><a href="#URL">PRODUCT #ID : #REF (#LOOP_COUNT / #LOOP_TOTAL)</a></h3>
<h4>#TITLE</h4>
<p>#DESCRIPTION</p>
{ifloop rel="acc"}
<h5>Accessories</h5>
<ul>
{loop name="acc" type="accessory" product="#ID" order="accessory"}
<li><a href="##REF">#REF</a></li>
{/loop}
</ul>
{/ifloop}
{elseloop rel="acc"}
<h5>No accessory</h5>
{/elseloop}
{ifloop rel="prod_ass_cont"}
<h5>Associated Content</h5>
<ul>
{loop name="prod_ass_cont" type="associated_content" product="#ID" order="associated_content"}
<li>#TITLE</li>
{/loop}
</ul>
{/ifloop}
{elseloop rel="prod_ass_cont"}
<h5>No associated content</h5>
{/elseloop}
{ifloop rel="ft"}
<h5>Features</h5>
<ul>
@@ -67,29 +43,6 @@
{elseloop rel="ft"}
<h5>No feature</h5>
{/elseloop}
<h5>Product sale elements</h5>
{assign var=current_product value=#ID}
{loop name="pse" type="product_sale_elements" product="#ID"}
<div style="border: solid 2px darkorange; padding: 5px; margin: 5px;">
{loop name="combi" type="attribute_combination" product_sale_elements="#ID"}
#ATTRIBUTE_TITLE = #ATTRIBUTE_AVAILABILITY_TITLE<br />
{/loop}
<br />#WEIGHT g
<br /><strong>{if #IS_PROMO == 1} #PROMO_PRICE € (instead of #PRICE) {else} #PRICE € {/if}</strong>
<br /><br />
Add
<select>
{for $will=1 to #QUANTITY}
<option>{$will}</option>
{/for}
</select>
to my cart
</ul>
</div>
{/loop}
</div>
{/loop}
{loop name="catgory1" type="category" parent="#ID"}
@@ -110,75 +63,29 @@
{loop name="product" type="product" category="#ID"}
<div style="border: solid 1px green; padding: 20px; margin: 10px;">
<h3><a name="#REF" href="#URL">PRODUCT #ID : #REF (#LOOP_COUNT / #LOOP_TOTAL)</a></h3>
<h4>#TITLE</h4>
<p>#DESCRIPTION</p>
<div style="border: solid 1px green; padding: 20px; margin: 10px;">
<h3><a href="#URL">PRODUCT #ID : #REF (#LOOP_COUNT / #LOOP_TOTAL)</a></h3>
<h4>#TITLE</h4>
<p>#DESCRIPTION</p>
{ifloop rel="acc"}
<h5>Accessories</h5>
<ul>
{loop name="acc" type="accessory" product="#ID" order="accessory"}
<li><a href="##REF">#REF</a></li>
{/loop}
</ul>
{/ifloop}
{elseloop rel="acc"}
<h5>No accessory</h5>
{/elseloop}
{ifloop rel="prod_ass_cont"}
<h5>Associated Content</h5>
<ul>
{loop name="prod_ass_cont" type="associated_content" product="#ID" order="associated_content"}
<li>#TITLE</li>
{/loop}
</ul>
{/ifloop}
{elseloop rel="prod_ass_cont"}
<h5>No associated content</h5>
{/elseloop}
{ifloop rel="ft"}
<h5>Features</h5>
<ul>
{loop name="ft" type="feature" order="manual" product="#ID"}
<li>
<strong>#TITLE</strong> :
{loop name="ft_v" type="feature_value" product="{$current_product}" feature="#ID"}
#TITLE / #PERSONAL_VALUE
{ifloop rel="ft"}
<h5>Features</h5>
<ul>
{assign var=current_product value=#ID}
{loop name="ft" type="feature" order="manual" product="#ID"}
<li>
<strong>#TITLE</strong> :
{loop name="ft_v" type="feature_value" product="{$current_product}" feature="#ID"}
#TITLE / #PERSONAL_VALUE
{/loop}
</li>
{/loop}
</li>
{/loop}
</ul>
{/ifloop}
{elseloop rel="ft"}
<h5>No feature</h5>
{/elseloop}
<h5>Product sale elements</h5>
{assign var=current_product value=#ID}
{loop name="pse" type="product_sale_elements" product="#ID"}
<div style="border: solid 2px darkorange; padding: 5px; margin: 5px;">
{loop name="combi" type="attribute_combination" product_sale_elements="#ID"}
#ATTRIBUTE_TITLE = #ATTRIBUTE_AVAILABILITY_TITLE<br />
{/loop}
<br />#WEIGHT g
<br /><strong>{if #IS_PROMO == 1} #PROMO_PRICE € (instead of #PRICE) {else} #PRICE € {/if}</strong>
<br /><br />
Add
<select>
{for $will=1 to #QUANTITY}
<option>{$will}</option>
{/for}
</select>
to my cart
</ul>
</div>
{/loop}
</div>
</ul>
{/ifloop}
{elseloop rel="ft"}
<h5>No feature</h5>
{/elseloop}
</div>
{/loop}
</div>
{/loop}

View File

@@ -1,7 +1,7 @@
{include file="includes/header.html"}
{form name="thelia.customer.creation"}
{* We use $INDEX_PAGE as form action to avoid mixing post and get data *}
{* We use {navigate to="index"} as form action to avoid mixing post and get data *}
<form action="{url path="/customer/create" }" method="post" {form_enctype form=$form}>
{*
The two fields below are not par of the form, they are here to defines
@@ -16,7 +16,7 @@
*}
{form_field form=$form field='success_url'}
<input type="hidden" name="{$name}" value="{$RETURN_TO_URL}" /> {* the url the user is redirected to on login success *}
<input type="hidden" name="{$name}" value="{navigate to="return_to"}" /> {* the url the user is redirected to on login success *}
{/form_field}
{form_field form=$form field='auto_login'}

View File

@@ -3,8 +3,8 @@
<h1>{intl l='Please login'}</h1>
{form name="thelia.customer.login" }
{* We use $INDEX_PAGE as form action to avoid mixing post and get data *}
<form action="{$INDEX_PAGE}" method="post" {form_enctype form=$form}>
{* We use {navigate to="index"} as form action to avoid mixing post and get data *}
<form action="{navigate to="index"}" method="post" {form_enctype form=$form}>
{*
The two fields below are not par of the Login form, they are here to defines
@@ -20,7 +20,7 @@
*}
{form_field form=$form field='success_url'}
<input type="hidden" name="{$name}" value="{$RETURN_TO_URL}" /> {* the url the user is redirected to on login success *}
<input type="hidden" name="{$name}" value="{navigate to="return_to"}" /> {* the url the user is redirected to on login success *}
{/form_field}
{*

View File

@@ -21,8 +21,6 @@ if ( false === in_array($request->getClientIp(), $trustIp)) {
$thelia = new Thelia("dev", true);
\Thelia\Tools\URL::retrieveCurrent($request);
$response = $thelia->handle($request)->prepare($request)->send();
$thelia->terminate($request, $response);