silverstripe-subsites/src/Model/SubsiteDomain.php

228 lines
6.2 KiB
PHP
Raw Normal View History

BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
<?php
2017-05-24 12:32:05 +02:00
namespace SilverStripe\Subsites\Model;
2017-05-29 13:42:42 +02:00
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
2016-09-22 16:38:29 +02:00
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
2017-05-29 13:42:42 +02:00
use SilverStripe\Forms\OptionsetField;
2016-09-22 16:38:29 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\Subsites\Forms\WildcardDomainField;
2016-09-22 16:38:29 +02:00
/**
2017-05-29 13:42:42 +02:00
* @property string $Domain domain name of this subsite. Can include wildcards. Do not include the URL scheme here
* @property string $Protocol Required protocol (http or https) if only one is supported. 'automatic' implies
* that any links to this subsite should use the current protocol, and that both are supported.
* @property string $SubstitutedDomain Domain name with all wildcards filled in
* @property string $FullProtocol Full protocol including ://
* @property bool $IsPrimary Is this the primary subdomain?
*/
2017-05-24 15:26:28 +02:00
class SubsiteDomain extends DataObject
{
private static $table_name = 'SubsiteDomain';
2017-05-24 14:31:56 +02:00
/**
2017-05-24 15:26:28 +02:00
*
* @var string
*/
private static $default_sort = '"IsPrimary" DESC';
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +02:00
/** *
* @var array
*/
private static $db = [
'Domain' => 'Varchar(255)',
'Protocol' => "Enum('http,https,automatic','automatic')",
'IsPrimary' => 'Boolean',
];
2017-05-29 13:42:42 +02:00
/**
* Specifies that this subsite is http only
2017-05-29 13:42:42 +02:00
*/
const PROTOCOL_HTTP = 'http';
2017-05-24 15:26:28 +02:00
/**
2017-05-29 13:42:42 +02:00
* Specifies that this subsite is https only
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
const PROTOCOL_HTTPS = 'https';
2017-05-24 15:26:28 +02:00
/**
2017-05-29 13:42:42 +02:00
* Specifies that this subsite supports both http and https
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
const PROTOCOL_AUTOMATIC = 'automatic';
2017-05-24 15:26:28 +02:00
/**
2017-05-29 13:42:42 +02:00
* Get the descriptive title for this domain
2017-05-24 15:26:28 +02:00
*
2017-05-29 13:42:42 +02:00
* @return string
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function getTitle()
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
return $this->Domain;
2017-05-24 15:26:28 +02:00
}
/**
*
* @var array
*/
private static $has_one = [
'Subsite' => Subsite::class,
];
2017-05-29 13:42:42 +02:00
/**
* @config
* @var array
*/
private static $summary_fields = [
'Domain',
'IsPrimary',
];
2017-05-29 13:42:42 +02:00
/*** @config
2017-05-29 13:42:42 +02:00
* @var array
2017-05-24 15:26:28 +02:00
*/
private static $casting = [
2017-05-29 13:42:42 +02:00
'SubstitutedDomain' => 'Varchar',
'FullProtocol' => 'Varchar',
'AbsoluteLink' => 'Varchar',
];
/**
* Whenever a Subsite Domain is written, rewrite the hostmap
*
* @return void
*/
public function onAfterWrite()
{
Subsite::writeHostMap();
2017-12-13 11:03:00 +01:00
parent::onAfterWrite();
}
/**
*
* @return FieldList
*/
public function getCMSFields()
{
$protocols = [
self::PROTOCOL_HTTP => _t(__CLASS__ . '.PROTOCOL_HTTP', 'http://'),
self::PROTOCOL_HTTPS => _t(__CLASS__ . '.PROTOCOL_HTTPS', 'https://'),
self::PROTOCOL_AUTOMATIC => _t(__CLASS__ . '.PROTOCOL_AUTOMATIC', 'Automatic')
];
$fields = FieldList::create(
WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255)
->setDescription(_t(
__CLASS__ . '.DOMAIN_DESCRIPTION',
2017-05-29 13:42:42 +02:00
'Hostname of this subsite (exclude protocol). Allows wildcards (*).'
)),
OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols)
->setValue($this->Protocol ?: self::PROTOCOL_AUTOMATIC)
2017-05-29 13:42:42 +02:00
->setDescription(_t(
__CLASS__ . '.PROTOCOL_DESCRIPTION',
2017-05-29 13:42:42 +02:00
'When generating links to this subsite, use the selected protocol. <br />' .
'Selecting \'Automatic\' means subsite links will default to the current protocol.'
)),
CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary'))
->setDescription(_t(
__CLASS__ . '.ISPRIMARY_DESCRIPTION',
'Mark this as the default domain for this subsite'
))
2015-11-23 04:53:45 +01:00
);
2017-05-24 15:26:28 +02:00
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
*
* @param bool $includerelations
* @return array
*/
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);
$labels['Domain'] = _t(__CLASS__ . '.DOMAIN', 'Domain');
$labels['Protocol'] = _t(__CLASS__ . '.Protocol', 'Protocol');
$labels['IsPrimary'] = _t(__CLASS__ . '.IS_PRIMARY', 'Is Primary Domain?');
2017-05-24 15:26:28 +02:00
return $labels;
}
2017-05-29 13:42:42 +02:00
/**
* Get the link to this subsite
*
* @return string
*/
public function Link()
{
return $this->getFullProtocol() . $this->Domain;
2017-05-24 15:26:28 +02:00
}
2017-05-29 13:42:42 +02:00
/**
2017-05-29 13:42:42 +02:00
* Gets the full protocol (including ://) for this domain
*
* @return string
*/
public function getFullProtocol()
{
switch ($this->Protocol) {
case self::PROTOCOL_HTTPS:
2017-05-29 13:42:42 +02:00
return 'https://';
case self::PROTOCOL_HTTP:
2017-05-29 13:42:42 +02:00
return 'http://';
default:
2017-05-29 13:42:42 +02:00
return Director::protocol();
}
}
/**
* Retrieves domain name with wildcards substituted with actual values
*
* @todo Refactor domains into separate wildcards / primary domains
*
* @return string
*/
public function getSubstitutedDomain()
{
$currentHost = Director::host();
2017-05-29 13:42:42 +02:00
// If there are wildcards in the primary domain (not recommended), make some
// educated guesses about what to replace them with:
2022-04-13 03:49:48 +02:00
$domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain ?? '');
2017-05-29 13:42:42 +02:00
// Default to "subsite." prefix for first wildcard
// TODO Whats the significance of "subsite" in this context?!
2022-04-13 03:49:48 +02:00
$domain = preg_replace('/^\*\./', "subsite.", $domain ?? '');
2017-05-29 13:42:42 +02:00
// *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com'
2022-04-13 03:49:48 +02:00
$domain = str_replace('.www.', '.', $domain ?? '');
2017-05-29 13:42:42 +02:00
return $domain;
}
/**
* Get absolute link for this domain
*
* @return string
*/
public function getAbsoluteLink()
{
return $this->getFullProtocol() . $this->getSubstitutedDomain();
}
/**
* Get absolute baseURL for this domain
*
* @return string
*/
public function absoluteBaseURL()
{
return Controller::join_links(
$this->getAbsoluteLink(),
Director::baseURL()
);
}
2013-04-30 07:53:09 +02:00
}