2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A redirector page redirects when the page is visited.
|
2009-04-29 01:55:53 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class RedirectorPage extends Page {
|
2009-04-29 01:55:53 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
static $add_action = "Redirector to another page";
|
2009-04-29 01:55:53 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $icon = array("cms/images/treeicons/page-shortcut","file");
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"RedirectionType" => "Enum('Internal,External','Internal')",
|
|
|
|
"ExternalURL" => "Varchar(255)",
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"LinkTo" => "SiteTree",
|
|
|
|
);
|
|
|
|
|
|
|
|
static $many_many = array(
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this page if the redirect is external, otherwise
|
|
|
|
* returns the target page.
|
|
|
|
* @return SiteTree
|
|
|
|
*/
|
|
|
|
function ContentSource() {
|
|
|
|
if($this->RedirectionType == 'Internal') {
|
|
|
|
return $this->LinkTo();
|
|
|
|
} else {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 03:46:17 +02:00
|
|
|
/**
|
|
|
|
* Return the the link that should be used for this redirector page, in navigation, etc.
|
|
|
|
* If the redirectorpage has been appropriately configured, then it will return the redirection
|
|
|
|
* destination, to prevent unnecessary 30x redirections. However, if it's misconfigured, then
|
|
|
|
* it will return a link to itself, which will then display an error message.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Link() {
|
2008-10-09 03:46:17 +02:00
|
|
|
if($link = $this->redirectionLink()) return $link;
|
|
|
|
else return $this->regularLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the normal link directly to this page. Once you visit this link, a 30x redirection
|
|
|
|
* will take you to your final destination.
|
|
|
|
*/
|
|
|
|
function regularLink() {
|
|
|
|
return parent::Link();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the link that we should redirect to.
|
|
|
|
* Only return a value if there is a legal redirection destination.
|
|
|
|
*/
|
|
|
|
function redirectionLink() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->RedirectionType == 'External') {
|
2008-10-09 03:46:17 +02:00
|
|
|
if($this->ExternalURL) {
|
2009-05-25 08:59:21 +02:00
|
|
|
return $this->ExternalURL;
|
2008-10-09 03:46:17 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2007-10-02 06:36:13 +02:00
|
|
|
$linkTo = $this->LinkToID ? DataObject::get_by_id("SiteTree", $this->LinkToID) : null;
|
2008-10-09 03:46:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($linkTo) {
|
2008-10-09 03:46:17 +02:00
|
|
|
// We shouldn't point to ourselves - that would create an infinite loop! Return null since we have a
|
|
|
|
// bad configuration
|
|
|
|
if($this->ID == $linkTo->ID) {
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// If we're linking to another redirectorpage then just return the URLSegment, to prevent a cycle of redirector
|
|
|
|
// pages from causing an infinite loop. Instead, they will cause a 30x redirection loop in the browser, but
|
|
|
|
// this can be handled sufficiently gracefully by the browser.
|
|
|
|
} elseif($linkTo instanceof RedirectorPage) {
|
|
|
|
return $linkTo->regularLink();
|
|
|
|
|
|
|
|
// For all other pages, just return the link of the page.
|
|
|
|
} else {
|
|
|
|
return $linkTo->Link();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCMSFields() {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/RedirectorPage.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-01-19 01:33:43 +01:00
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
$fields->removeByName('Content', true);
|
|
|
|
$fields->addFieldsToTab('Root.Content.Main',
|
|
|
|
array(
|
|
|
|
new HeaderField('RedirectorDescHeader',_t('RedirectorPage.HEADER', "This page will redirect users to another page")),
|
|
|
|
new OptionsetField(
|
|
|
|
"RedirectionType",
|
|
|
|
_t('RedirectorPage.REDIRECTTO', "Redirect to"),
|
|
|
|
array(
|
|
|
|
"Internal" => _t('RedirectorPage.REDIRECTTOPAGE', "A page on your website"),
|
|
|
|
"External" => _t('RedirectorPage.REDIRECTTOEXTERNAL', "Another website"),
|
|
|
|
),
|
|
|
|
"Internal"
|
2007-07-19 12:40:28 +02:00
|
|
|
),
|
2009-01-19 01:33:43 +01:00
|
|
|
new TreeDropdownField(
|
|
|
|
"LinkToID",
|
|
|
|
_t('RedirectorPage.YOURPAGE', "Page on your website"),
|
|
|
|
"SiteTree"
|
|
|
|
),
|
|
|
|
new TextField("ExternalURL", _t('RedirectorPage.OTHERURL', "Other website URL"))
|
2007-07-19 12:40:28 +02:00
|
|
|
)
|
|
|
|
);
|
2007-10-25 04:47:45 +02:00
|
|
|
|
|
|
|
return $fields;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-12-15 02:30:41 +01:00
|
|
|
|
|
|
|
function subPagesToCache() {
|
|
|
|
$urls = parent::subPagesToCache();
|
|
|
|
$urls[] = $this->URLSegment . '/';
|
|
|
|
return $urls;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Controller for the {@link RedirectorPage}.
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class RedirectorPage_Controller extends Page_Controller {
|
|
|
|
function init() {
|
2008-10-09 03:46:17 +02:00
|
|
|
if($link = $this->redirectionLink()) {
|
|
|
|
Director::redirect($link, 301);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-09 03:46:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::init();
|
|
|
|
}
|
2008-10-09 00:35:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If we ever get this far, it means that the redirection failed.
|
|
|
|
*/
|
|
|
|
function index() {
|
2008-10-09 03:46:17 +02:00
|
|
|
return array(
|
|
|
|
"Content" => "<p>" .
|
2008-10-09 00:35:22 +02:00
|
|
|
_t('RedirectorPage.HASBEENSETUP', 'A redirector page has been set up without anywhere to redirect to.') .
|
2008-10-09 03:46:17 +02:00
|
|
|
"</p>"
|
|
|
|
);
|
2008-10-09 00:35:22 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-12-15 02:30:41 +01:00
|
|
|
?>
|