2011-03-18 04:01:06 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A redirector page redirects when the page is visited.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
|
|
|
class RedirectorPage extends Page {
|
2012-06-13 09:46:43 +02:00
|
|
|
|
2011-04-24 01:03:51 +02:00
|
|
|
static $description = 'Redirects to a different internal page';
|
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
static $db = array(
|
|
|
|
"RedirectionType" => "Enum('Internal,External','Internal')",
|
|
|
|
"ExternalURL" => "Varchar(2083)" // 2083 is the maximum length of a URL in Internet Explorer.
|
|
|
|
);
|
|
|
|
|
|
|
|
static $defaults = array(
|
|
|
|
"RedirectionType" => "Internal"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"LinkTo" => "SiteTree",
|
|
|
|
);
|
|
|
|
|
|
|
|
static $many_many = array(
|
|
|
|
);
|
|
|
|
|
2012-03-19 02:05:09 +01:00
|
|
|
static $allowed_children = array();
|
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function Link() {
|
|
|
|
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($action = null) {
|
|
|
|
return parent::Link($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the link that we should redirect to.
|
|
|
|
* Only return a value if there is a legal redirection destination.
|
|
|
|
*/
|
|
|
|
function redirectionLink() {
|
|
|
|
if($this->RedirectionType == 'External') {
|
|
|
|
if($this->ExternalURL) {
|
|
|
|
return $this->ExternalURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$linkTo = $this->LinkToID ? DataObject::get_by_id("SiteTree", $this->LinkToID) : null;
|
|
|
|
|
|
|
|
if($linkTo) {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function syncLinkTracking() {
|
|
|
|
if ($this->RedirectionType == 'Internal') {
|
|
|
|
if($this->LinkToID) {
|
|
|
|
$this->HasBrokenLink = DataObject::get_by_id('SiteTree', $this->LinkToID) ? false : true;
|
|
|
|
} else {
|
|
|
|
// An incomplete redirector page definitely has a broken link
|
|
|
|
$this->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO implement checking of a remote site
|
|
|
|
$this->HasBrokenLink = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBeforeWrite() {
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
// Prefix the URL with "http://" if no prefix is found
|
|
|
|
if($this->ExternalURL && (strpos($this->ExternalURL, '://') === false)) {
|
|
|
|
$this->ExternalURL = 'http://' . $this->ExternalURL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 15:55:32 +02:00
|
|
|
function getCMSFields() {
|
2012-04-11 05:32:41 +02:00
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/RedirectorPage.js');
|
2011-03-18 04:01:06 +01:00
|
|
|
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
$fields->removeByName('Content', true);
|
|
|
|
|
|
|
|
// Remove all metadata fields, does not apply for redirector pages
|
|
|
|
$fields->removeByName('MetaTagsHeader');
|
|
|
|
$fields->removeByName('MetaTitle');
|
|
|
|
$fields->removeByName('MetaKeywords');
|
|
|
|
$fields->removeByName('MetaDescription');
|
|
|
|
$fields->removeByName('ExtraMeta');
|
|
|
|
|
2011-12-17 04:45:09 +01:00
|
|
|
$fields->addFieldsToTab('Root.Main',
|
2011-03-18 04:01:06 +01:00
|
|
|
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"
|
|
|
|
),
|
|
|
|
new TreeDropdownField(
|
|
|
|
"LinkToID",
|
|
|
|
_t('RedirectorPage.YOURPAGE', "Page on your website"),
|
|
|
|
"SiteTree"
|
|
|
|
),
|
|
|
|
new TextField("ExternalURL", _t('RedirectorPage.OTHERURL', "Other website URL"))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't cache RedirectorPages
|
|
|
|
function subPagesToCache() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for the {@link RedirectorPage}.
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
|
|
|
class RedirectorPage_Controller extends Page_Controller {
|
2012-05-23 11:45:16 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
function init() {
|
2012-05-23 11:45:16 +02:00
|
|
|
parent::init();
|
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
if($link = $this->redirectionLink()) {
|
2012-05-23 11:45:16 +02:00
|
|
|
$this->redirect($link, 301);
|
|
|
|
return;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we ever get this far, it means that the redirection failed.
|
|
|
|
*/
|
|
|
|
function Content() {
|
|
|
|
return "<p class=\"message-setupWithoutRedirect\">" .
|
|
|
|
_t('RedirectorPage.HASBEENSETUP', 'A redirector page has been set up without anywhere to redirect to.') .
|
|
|
|
"</p>";
|
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|