Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Commit

Permalink
[FEATURE] There's now validation for vanity mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Feb 11, 2019
1 parent 9ce82e5 commit 7548913
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 0 additions & 4 deletions code/dataobjects/LinkMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ public function getCMSFields() {
return $fields;
}

/**
* Confirm that the current link mapping is valid.
*/

public function validate() {

$result = parent::validate();
Expand Down
31 changes: 27 additions & 4 deletions code/extensions/SiteTreeMisdirectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7548913

Please sign in to comment.