From d88d15f6e6e2b2d196136ce259fca7650e2eac6c Mon Sep 17 00:00:00 2001 From: Jinzhe Li Date: Wed, 26 Apr 2017 16:08:20 +0930 Subject: [PATCH 1/4] Add locale support for YMCA --- controllers/RetourController.php | 2 +- services/RetourService.php | 4 ++-- templates/_edit.twig | 22 ++++++++++++++++++++++ templates/redirects.twig | 12 ++++++++---- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/controllers/RetourController.php b/controllers/RetourController.php index 22b8999..2bb62a9 100644 --- a/controllers/RetourController.php +++ b/controllers/RetourController.php @@ -155,7 +155,7 @@ public function actionSaveRedirect(array $variables = array()) /* -- 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 !="")) diff --git a/services/RetourService.php b/services/RetourService.php index bdb3775..09a40a1 100644 --- a/services/RetourService.php +++ b/services/RetourService.php @@ -180,7 +180,7 @@ public function lookupRedirect($url, $redirects) /* -- Do a straight up match */ case "exactmatch": - if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) + if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0 && CRAFT_LOCALE == $redirect['locale']) { $error = $this->incrementRedirectHitCount($redirect); RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false); @@ -193,7 +193,7 @@ public function lookupRedirect($url, $redirects) case "regexmatch": $matchRegEx = "`" . $redirect['redirectSrcUrlParsed'] . "`i"; - if (preg_match($matchRegEx, $url) === 1) + if (preg_match($matchRegEx, $url) === 1 && CRAFT_LOCALE == $redirect['locale']) { $error = $this->incrementRedirectHitCount($redirect); RetourPlugin::log($redirectMatchType . " result: " . print_r($error, true), LogLevel::Info, false); diff --git a/templates/_edit.twig b/templates/_edit.twig index 257659e..718340c 100755 --- a/templates/_edit.twig +++ b/templates/_edit.twig @@ -124,6 +124,28 @@ }) }} +
+
+ +

{{ "Select the locale for this redirect." |t |raw}}

+
+ {% set locales = craft.i18n.getSiteLocales %} + {% set localeOptions = {} %} + {% for locale in locales %} + {% set localeId = locale.id %} + {% set localeName = locale.name %} + {% set localeOptions = localeOptions | merge({ (localeId) : localeName}) %} + {% endfor %} + {{ forms.selectField({ + fieldClass: 'nomarginfield', + class: 'selectField', + id: "redirectLocale", + options: localeOptions, + name: "redirectLocale", + value: values.locale, + }) }} +
+ {% if craft.app.version < 2.5 %} diff --git a/templates/redirects.twig b/templates/redirects.twig index 52cbfc5..569c97d 100755 --- a/templates/redirects.twig +++ b/templates/redirects.twig @@ -58,7 +58,8 @@ {{ "Legacy URL Pattern"|t }} {{ "Redirect To"|t }} - {{ "Pattern Match Type"|t }} + {{ "Locale"|t }} + {{ "Pattern Match Type"|t }} {{ "Redirect Type"|t }} {{ "Hits"|t }} {{ "Last Hit"|t }} @@ -70,6 +71,7 @@ {{ redir.redirectSrcUrl }} {{ redir.redirectDestUrl }} + {{ redir.locale }} {{ matchesList[redir.redirectMatchType] }} {{ redir.redirectHttpCode }} {{ redir.hitCount }} @@ -98,7 +100,8 @@ {{ "Legacy URL Pattern"|t }} {{ "Redirect To"|t }} - {{ "Pattern Match Type"|t }} + {{ "Locale"|t }} + {{ "Pattern Match Type"|t }} {{ "Redirect Type"|t }} {{ "Hits"|t }} {{ "Last Hit"|t }} @@ -111,6 +114,7 @@ {{ redir.redirectSrcUrl }} {{ redir.redirectDestUrl }} + {{ redir.locale }} {{ matchesList[redir.redirectMatchType] }} {{ redir.redirectHttpCode }} {{ redir.hitCount }} @@ -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, @@ -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, From 7cc34966f2171f196ccb1d1c1a9b8719ef915887 Mon Sep 17 00:00:00 2001 From: Jinzhe Li Date: Wed, 26 Apr 2017 16:39:28 +0930 Subject: [PATCH 2/4] Check for existence of locale to avoid error --- services/RetourService.php | 50 +++++++++++++++++++++++++++----------- templates/_edit.twig | 18 ++++++++------ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/services/RetourService.php b/services/RetourService.php index 09a40a1..e27cb9c 100644 --- a/services/RetourService.php +++ b/services/RetourService.php @@ -180,12 +180,15 @@ public function lookupRedirect($url, $redirects) /* -- Do a straight up match */ case "exactmatch": - if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0 && CRAFT_LOCALE == $redirect['locale']) + 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; @@ -193,19 +196,22 @@ public function lookupRedirect($url, $redirects) case "regexmatch": $matchRegEx = "`" . $redirect['redirectSrcUrlParsed'] . "`i"; - if (preg_match($matchRegEx, $url) === 1 && CRAFT_LOCALE == $redirect['locale']) + 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 we're not associated with an EntryID, handle capture group replacement */ - if ($redirect['associatedElementId'] == 0) - { - $redirect['redirectDestUrl'] = preg_replace($matchRegEx, $redirect['redirectDestUrl'], $url); + if ($redirect['associatedElementId'] == 0) + { + $redirect['redirectDestUrl'] = preg_replace($matchRegEx, $redirect['redirectDestUrl'], $url); + } + $this->saveRedirectToCache($url, $redirect); + return $redirect; } - $this->saveRedirectToCache($url, $redirect); - return $redirect; } break; @@ -579,4 +585,20 @@ public function getMatchesList() return $result; } /* -- getMatchesList */ +/** + * @return Whether locale should be compared against + */ + function shouldMatchLocale() + { + return defined(CRAFT_LOCALE); + } /* -- shouldMatchLocale */ + +/** + * @return Get locale + */ + function getLocale() + { + return CRAFT_LOCALE; + } /* -- getLocale */ + } diff --git a/templates/_edit.twig b/templates/_edit.twig index 718340c..679bfd6 100755 --- a/templates/_edit.twig +++ b/templates/_edit.twig @@ -123,19 +123,20 @@ value: values.redirectHttpCode, }) }} - + + {% 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 %}

{{ "Select the locale for this redirect." |t |raw}}

- {% set locales = craft.i18n.getSiteLocales %} - {% set localeOptions = {} %} - {% for locale in locales %} - {% set localeId = locale.id %} - {% set localeName = locale.name %} - {% set localeOptions = localeOptions | merge({ (localeId) : localeName}) %} - {% endfor %} {{ forms.selectField({ fieldClass: 'nomarginfield', class: 'selectField', @@ -145,6 +146,7 @@ value: values.locale, }) }}
+ {% endif %} From 5f272f57564240ee6e5d35a9c40b1326bdfc7a7b Mon Sep 17 00:00:00 2001 From: Jinzhe Li Date: Wed, 26 Apr 2017 17:23:31 +0930 Subject: [PATCH 3/4] Fix a bug --- services/RetourService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/RetourService.php b/services/RetourService.php index e27cb9c..82981ea 100644 --- a/services/RetourService.php +++ b/services/RetourService.php @@ -590,7 +590,7 @@ public function getMatchesList() */ function shouldMatchLocale() { - return defined(CRAFT_LOCALE); + return defined("CRAFT_LOCALE"); } /* -- shouldMatchLocale */ /** From 8aed994ad073a9eeea882aa470c8933c6a233e47 Mon Sep 17 00:00:00 2001 From: Jinzhe Li Date: Thu, 27 Apr 2017 10:42:55 +0930 Subject: [PATCH 4/4] Make comment style consistent --- services/RetourService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/RetourService.php b/services/RetourService.php index 82981ea..e3459aa 100644 --- a/services/RetourService.php +++ b/services/RetourService.php @@ -203,7 +203,7 @@ public function lookupRedirect($url, $redirects) $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 we're not associated with an EntryID, handle capture group replacement */ if ($redirect['associatedElementId'] == 0) {