Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Add locale check logic for the redirect #71

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion controllers/RetourController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ public function actionSaveRedirect(array $variables = array())
$record = new Retour_StaticRedirectsRecord;
}


// Set the record attributes, defaulting to the existing values for whatever is missing from the post data
$record->locale = craft()->language;

$record->locale = craft()->request->getPost('redirectLocale', craft()->language);
$record->redirectMatchType = craft()->request->getPost('redirectMatchType', $record->redirectMatchType);
$record->redirectSrcUrl = craft()->request->getPost('redirectSrcUrl', $record->redirectSrcUrl);
if (($record->redirectMatchType == "exactmatch") && ($record->redirectSrcUrl != "")) {
Expand Down
48 changes: 34 additions & 14 deletions services/RetourService.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,28 +203,30 @@ public function lookupRedirect($url, $redirects)
// Do a straight up match
case "exactmatch":
if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
$error = $this->incrementRedirectHitCount($redirect);
RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false);
$this->saveRedirectToCache($url, $redirect);

return $redirect;
if (($this->shouldMatchLocale() && $this->getLocale() == $redirect['locale']) || !$this->shouldMatchLocale()) {
$error = $this->incrementRedirectHitCount($redirect);
RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false);
$this->saveRedirectToCache($url, $redirect);
return $redirect;
}
}
break;

// Do a regex match
case "regexmatch":
$matchRegEx = "`" . $redirect['redirectSrcUrlParsed'] . "`i";
if (preg_match($matchRegEx, $url) === 1) {
$error = $this->incrementRedirectHitCount($redirect);
RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false);
if (($this->shouldMatchLocale() && $this->getLocale() == $redirect['locale']) || !$this->shouldMatchLocale()) {
$error = $this->incrementRedirectHitCount($redirect);
RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false);

// If we're not associated with an EntryID, handle capture group replacement
if ($redirect['associatedElementId'] == 0) {
$redirect['redirectDestUrl'] = preg_replace($matchRegEx, $redirect['redirectDestUrl'], $url);
}
$this->saveRedirectToCache($url, $redirect);
// If we're not associated with an EntryID, handle capture group replacement
if ($redirect['associatedElementId'] == 0) {
$redirect['redirectDestUrl'] = preg_replace($matchRegEx, $redirect['redirectDestUrl'], $url);
}
$this->saveRedirectToCache($url, $redirect);

return $redirect;
return $redirect;
}
}
break;

Expand Down Expand Up @@ -578,5 +580,23 @@ public function getMatchesList()
}

return $result;

}

/**
* @return Whether locale should be compared against
*/
function shouldMatchLocale()
{
return defined("CRAFT_LOCALE");
} /* -- shouldMatchLocale */

/**
* @return Get locale
*/
function getLocale()
{
return CRAFT_LOCALE;
} /* -- getLocale */

}
24 changes: 24 additions & 0 deletions templates/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@
value: values.redirectHttpCode,
}) }}
</div>

{% set locales = craft.i18n.getSiteLocales %}
{% if locales|length %}
{% set localeOptions = {} %}
{% for locale in locales %}
{% set localeId = locale.id %}
{% set localeName = locale.name %}
{% set localeOptions = localeOptions | merge({ (localeId) : localeName}) %}
{% endfor %}
<div class="field">
<div class="heading">
<label for= "redirectLocale">{{ "Redirect Local"|t}}</label>
<div class="instructions"><p>{{ "Select the locale for this redirect." |t |raw}}</p></div>
</div>
{{ forms.selectField({
fieldClass: 'nomarginfield',
class: 'selectField',
id: "redirectLocale",
options: localeOptions,
name: "redirectLocale",
value: values.locale,
}) }}
</div>
{% endif %}

<!-- Needed for for Craft < 2.5 -->

Expand Down
12 changes: 8 additions & 4 deletions templates/redirects.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
<thead>
<th style="width: 25%" scope="col"><span class="sort-label">{{ "Legacy URL Pattern"|t }}</span></th>
<th style="width: 25%" scope="col"><span class="sort-label">{{ "Redirect To"|t }}</span></th>
<th style="width: 15%" scope="col"><span class="sort-label">{{ "Pattern Match Type"|t }}</span></th>
<th style="width: 5%" scope="col"><span class="sort-label">{{ "Locale"|t }}</span></th>
<th style="width: 10%" scope="col"><span class="sort-label">{{ "Pattern Match Type"|t }}</span></th>
<th style="width: 10%" scope="col"><span class="sort-label">{{ "Redirect Type"|t }}</span></th>
<th style="width: 5%" scope="col"><span class="sort-label">{{ "Hits"|t }}</span></th>
<th style="width: 16%" scope="col"><span class="sort-label">{{ "Last Hit"|t }}</span></th>
Expand All @@ -70,6 +71,7 @@
<tr data-id="{{ redir.id }}" data-name="{{ redir.redirectSrcUrl }}">
<td><a class="go" href="{{ url("retour/edit/" ~ redir.id) }}">{{ redir.redirectSrcUrl }}</a></td>
<td>{{ redir.redirectDestUrl }}</td>
<td>{{ redir.locale }}</td>
<td>{{ matchesList[redir.redirectMatchType] }}</td>
<td>{{ redir.redirectHttpCode }}</td>
<td>{{ redir.hitCount }}</td>
Expand Down Expand Up @@ -98,7 +100,8 @@
<thead>
<th style="width: 25%" scope="col"><span class="sort-label">{{ "Legacy URL Pattern"|t }}</span></th>
<th style="width: 25%" scope="col"><span class="sort-label">{{ "Redirect To"|t }}</span></th>
<th style="width: 15%" scope="col"><span class="sort-label">{{ "Pattern Match Type"|t }}</span></th>
<th style="width: 5%" scope="col"><span class="sort-label">{{ "Locale"|t }}</span></th>
<th style="width: 10%" scope="col"><span class="sort-label">{{ "Pattern Match Type"|t }}</span></th>
<th style="width: 10%" scope="col"><span class="sort-label">{{ "Redirect Type"|t }}</span></th>
<th style="width: 5%" scope="col"><span class="sort-label">{{ "Hits"|t }}</span></th>
<th style="width: 20%" scope="col"><span class="sort-label">{{ "Last Hit"|t }}</span></th>
Expand All @@ -111,6 +114,7 @@
<tr data-id="{{ redir.id }}" data-name="{{ redir.redirectSrcUrl }}">
<td><a class="go" href="{{ associatedEntry.cpEditUrl }}">{{ redir.redirectSrcUrl }}</a></td>
<td>{{ redir.redirectDestUrl }}</td>
<td>{{ redir.locale }}</td>
<td>{{ matchesList[redir.redirectMatchType] }}</td>
<td>{{ redir.redirectHttpCode }}</td>
<td>{{ redir.hitCount }}</td>
Expand Down Expand Up @@ -144,7 +148,7 @@ $('#static-redirects').dataTable({
"sDom": '<"top"ilpf<"clear">>rt<"bottom"ilp<"clear">>',
"sPaginationType": "full_numbers",
"aaSorting": [[ 4, "desc" ]],
"aoColumns": [ null, null, null, { "sType": "num" }, { "sType": "num" }, { "sType": "date" }, null],
"aoColumns": [ null, null, null, null, { "sType": "num" }, { "sType": "num" }, { "sType": "date" }, null],
"bLengthChange": false,
"iDisplayLength": 20,
"bInfo": true,
Expand Down Expand Up @@ -173,7 +177,7 @@ $('#entry-redirects').dataTable({
"sDom": '<"top"ilpf<"clear">>rt<"bottom"ilp<"clear">>',
"sPaginationType": "full_numbers",
"aaSorting": [[ 4, "desc" ]],
"aoColumns": [ null, null, null, { "sType": "num" }, { "sType": "num" }, { "sType": "date" }],
"aoColumns": [ null, null, null, null, { "sType": "num" }, { "sType": "num" }, { "sType": "date" }],
"bLengthChange": false,
"iDisplayLength": 20,
"bInfo": true,
Expand Down