Skip to content

Commit

Permalink
Provide support for page redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
camspiers committed Sep 8, 2014
1 parent 0f9fb1c commit 8927698
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
framework
/framework/
/cms/
119 changes: 115 additions & 4 deletions code/RedirectUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ class RedirectUrl extends DataObject implements PermissionProvider
'To' => 'Varchar(2560)'
];

/**
* @var array
*/
private static $has_one = [
'FromRelation' => 'SiteTree',
'ToRelation' => 'SiteTree'
];

/**
* @var array
*/
private static $summary_fields = [
'From',
'To'
'FromLink',
'ToLink'
];

/**
Expand All @@ -42,11 +50,90 @@ public function setDataSource(CachedDataSource $dataSource)
}

/**
* @return RequiredFields
* @return FieldList
*/
public function getCMSFields()
{
$fields = new FieldList();

$fields->push($manual = new ToggleCompositeField(
'TextLinks',
'Enter urls',
[
new TextField('From', 'From url (e.g. "/my-page/")'),
new TextField('To', 'To url (e.g. "/my-page/", "http://google.com/")')
]
));

$fields->push($page = new ToggleCompositeField(
'SiteTree',
'Select pages from list',
[
new TreeDropdownField('FromRelationID', 'From', 'SiteTree'),
new TreeDropdownField('ToRelationID', 'To', 'SiteTree')
]
));

if ($this->getField('From') || $this->getField('To')) {
$manual->setStartClosed(false);
}

if ($this->getField('FromRelationID') || $this->getField('ToRelationID')) {
$page->setStartClosed(false);
}

return $fields;
}

/**
* @return string|bool
*/
public function getFromLink()
{
return $this->getLink('From');
}

/**
* @return string|bool
*/
public function getToLink()
{
return $this->getLink('To');
}

/**
* @param string $type
* @return string|bool
*/
protected function getLink($type)
{
if (!$relation = $this->getLinkRelation($type)) {
return $this->getField($type);
}

return sprintf(
"/%s",
ltrim($relation->RelativeLink(), '/')
);
}

/**
* @param string $type
* @return bool|SiteTree
*/
protected function getLinkRelation($type)
{
$relation = $this->getComponent(sprintf("%sRelation", $type));

return $relation->exists() ? $relation : false;
}

/**
* @return RedirectUrlValidator
*/
public function getCMSValidator()
{
return new RequiredFields('From', 'To');
return new RedirectUrlValidator();
}

/**
Expand Down Expand Up @@ -104,6 +191,30 @@ protected function hasPermission($member = null)
return Permission::checkMember($member, self::PERMISSION);
}

/**
* Clear out from and to manual links if we have a relation
*/
protected function onBeforeWrite()
{
parent::onBeforeWrite();

if ($this->isChanged('FromRelationID') && $this->getLinkRelation('From')) {
$this->setField('From', '');
}

if ($this->isChanged('ToRelationID') && $this->getLinkRelation('To')) {
$this->setField('To', '');
}

if ($this->isChanged('From') && $this->getField('From')) {
$this->setField('FromRelationID', 0);
}

if ($this->isChanged('To') && $this->getField('To')) {
$this->setField('ToRelationID', 0);
}
}

/**
*
*/
Expand Down
33 changes: 33 additions & 0 deletions code/RedirectUrlValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class RedirectUrlValidator extends Validator
{
public function php($data)
{
$valid = true;

if (empty($data['From']) && empty($data['FromRelationID'])) {
foreach (['From', 'FromRelationID'] as $error) {
$this->validationError(
$error,
"A 'From' url must be specified",
"required"
);
}
$valid = false;
}

if (empty($data['To']) && empty($data['ToRelationID'])) {
foreach (['To', 'ToRelationID'] as $error) {
$this->validationError(
$error,
"A 'To' url must be specified",
"required"
);
}
$valid = false;
}

return $valid;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"require-dev": {
"silverstripe/framework": "3.1.*@dev",
"silverstripe/cms": "3.1.*@dev",
"symfony/yaml": "~2.5"
},
"suggest": {
Expand Down
52 changes: 50 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/RequestFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(Redirector $redirector)
*/
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
{
if ($response = $this->redirector->getResponse($request)) {
if (!\Director::is_cli() && $response = $this->redirector->getResponse($request)) {
$response->output();
exit;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/RedirectUrlTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RedirectUrlTransformer implements TransformerInterface
public function transform($item)
{
if ($item instanceof RedirectUrl) {
return new Redirect($item->getField('From'), $item->getField('To'));
return new Redirect($item->getFromLink(), $item->getToLink());
} else {
throw new \InvalidArgumentException(sprintf(
"Instance provided to %s::%s must be an instance of RedirectUrl '%s' given",
Expand Down

0 comments on commit 8927698

Please sign in to comment.