From 7548913711e4f28d87c27226807f3226a1ae02ad Mon Sep 17 00:00:00 2001 From: Nathan Glasl Date: Tue, 5 Feb 2019 16:01:04 +1100 Subject: [PATCH] [FEATURE] There's now validation for vanity mappings. --- README.md | 2 +- code/dataobjects/LinkMapping.php | 4 --- .../SiteTreeMisdirectionExtension.php | 31 ++++++++++++++++--- composer.json | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ee76745..ccf9369 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [misdirection](https://packagist.org/packages/nglasl/silverstripe-misdirection) -_The current release is **2.2.26**_ +_The current release is **2.3.0**_ > A module for SilverStripe which will allow both simple and regular expression link redirections based on customisable mappings, either hooking into a page not found or replacing the default automated URL handling. diff --git a/code/dataobjects/LinkMapping.php b/code/dataobjects/LinkMapping.php index 8cf202d..0c7608f 100644 --- a/code/dataobjects/LinkMapping.php +++ b/code/dataobjects/LinkMapping.php @@ -272,10 +272,6 @@ public function getCMSFields() { return $fields; } - /** - * Confirm that the current link mapping is valid. - */ - public function validate() { $result = parent::validate(); diff --git a/code/extensions/SiteTreeMisdirectionExtension.php b/code/extensions/SiteTreeMisdirectionExtension.php index 4e69921..f3945ba 100644 --- a/code/extensions/SiteTreeMisdirectionExtension.php +++ b/code/extensions/SiteTreeMisdirectionExtension.php @@ -15,10 +15,6 @@ class SiteTreeMisdirectionExtension extends DataExtension { 'VanityMapping' => 'LinkMapping' ); - /** - * Display the vanity mapping fields. - */ - public function updateSettingsFields($fields) { $fields->addFieldToTab('Root.Misdirection', HeaderField::create( @@ -42,6 +38,33 @@ public function updateSettingsFields($fields) { $this->owner->extend('updateSiteTreeMisdirectionExtensionSettingsFields', $fields); } + public function validate(ValidationResult $result) { + + // Retrieve the vanity mapping URL, where this is only possible using the POST variable. + + $vanityURL = (!Controller::has_curr() || is_null($controller = Controller::curr()) || is_null($URL = $controller->getRequest()->postVar('VanityURL'))) ? $this->owner->VanityMapping()->MappedLink : $URL; + + // Determine whether another vanity mapping already exists. + + $existing = LinkMapping::get()->filter(array( + 'MappedLink' => $vanityURL, + 'RedirectType' => 'Page', + 'RedirectPageID:not' => array( + 0, + $this->owner->ID + ) + ))->first(); + if($result->valid() && $existing && ($page = $existing->getRedirectPage())) { + $link = Controller::join_links(CMSPageSettingsController::singleton()->Link('show'), $page->ID); + $result->error('Vanity URL already exists!'); + } + + // Allow extension. + + $this->owner->extend('validateSiteTreeMisdirectionExtension', $result); + return $result; + } + /** * Update the corresponding vanity mapping. */ diff --git a/composer.json b/composer.json index 1ac3664..4cf25d0 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "extra": { "installer-name": "misdirection", "branch-alias": { - "dev-master": "2.2.x-dev" + "dev-master": "2.3.x-dev" }, "screenshots": [ "https://raw.githubusercontent.com/nglasl/silverstripe-misdirection/master/images/misdirection-testing.png"