silverstripe-subsites/code/extensions/ErrorPageSubsite.php

49 lines
1.6 KiB
PHP
Raw Normal View History

2009-10-05 23:01:01 +02:00
<?php
2016-09-22 16:38:29 +02:00
2017-05-24 12:32:05 +02:00
namespace SilverStripe\Subsites\Extensions;
2016-09-22 16:38:29 +02:00
use SilverStripe\ORM\DataExtension;
2017-05-24 15:26:28 +02:00
use SilverStripe\ORM\DataObject;
2017-05-24 12:32:05 +02:00
use SilverStripe\Subsites\Model\Subsite;
2017-05-24 13:36:04 +02:00
2017-05-24 12:32:05 +02:00
2017-05-24 15:26:28 +02:00
class ErrorPageSubsite extends DataExtension
{
/**
* Alter file path to generated a static (static) error page file to handle error page template on different sub-sites
*
* {@see Error::get_error_filename()}
*
* FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including main site) between
* opening ErrorPage in the CMS and publish ErrorPage causes static error page to get generated incorrectly.
*
* @param string $name Filename to write to
* @param int $statusCode Integer error code
*/
public function updateErrorFilename(&$name, $statusCode)
{
// Try to get current subsite from session
$subsite = Subsite::currentSubsite(false);
// since this function is called from Page class before the controller is created, we have to get subsite from domain instead
if (!$subsite) {
$subsiteID = Subsite::getSubsiteIDForDomain();
if ($subsiteID != 0) {
$subsite = DataObject::get_by_id(Subsite::class, $subsiteID);
}
}
// Without subsite, don't rewrite
if ($subsite) {
// Add subdomain to end of filename, just before .html
// This should preserve translatable locale in the filename as well
$subdomain = $subsite->domain();
$name = substr($name, 0, -5) . "-{$subdomain}.html";
}
}
2017-05-24 12:32:05 +02:00
}