1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Subsites\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\FileNameFilter; |
6
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\ORM\DataExtension; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
11
|
|
|
|
12
|
|
|
class ErrorPageSubsite extends DataExtension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Alter file path to generated a static (static) error page file to handle error page template |
16
|
|
|
* on different sub-sites |
17
|
|
|
* |
18
|
|
|
* @see ErrorPage::get_error_filename() |
19
|
|
|
* |
20
|
|
|
* FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including |
21
|
|
|
* main site) between opening ErrorPage in the CMS and publish ErrorPage causes static error page to get |
22
|
|
|
* generated incorrectly. |
23
|
|
|
* |
24
|
|
|
* @param string $name |
25
|
|
|
* @param int $statusCode |
26
|
|
|
*/ |
27
|
|
|
public function updateErrorFilename(&$name, &$statusCode) |
28
|
|
|
{ |
29
|
|
|
$static_filepath = Config::inst()->get($this->owner->ClassName, 'static_filepath'); |
30
|
|
|
$subdomainPart = ''; |
31
|
|
|
|
32
|
|
|
// Try to get current subsite from session |
33
|
|
|
$subsite = Subsite::currentSubsite(); |
34
|
|
|
|
35
|
|
|
// since this function is called from Page class before the controller is created, we have |
36
|
|
|
// to get subsite from domain instead |
37
|
|
|
if (!$subsite) { |
38
|
|
|
$subsiteID = Subsite::getSubsiteIDForDomain(); |
39
|
|
|
if ($subsiteID != 0) { |
40
|
|
|
$subsite = DataObject::get_by_id(Subsite::class, $subsiteID); |
41
|
|
|
} else { |
42
|
|
|
$subsite = null; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($subsite) { |
47
|
|
|
$subdomain = $subsite->domain(); |
48
|
|
|
$subdomainPart = "-{$subdomain}"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// @todo implement Translatable namespace |
52
|
|
|
if (singleton(SiteTree::class)->hasExtension('Translatable') |
53
|
|
|
&& $locale |
|
|
|
|
54
|
|
|
&& $locale != Translatable::default_locale() |
|
|
|
|
55
|
|
|
) { |
56
|
|
|
$fileName = "error-{$statusCode}-{$locale}{$subdomainPart}.html"; |
57
|
|
|
} else { |
58
|
|
|
$fileName= "error-{$statusCode}{$subdomainPart}.html"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$fileName = FileNameFilter::create()->filter($fileName); |
62
|
|
|
|
63
|
|
|
$name = implode('/', [$static_filepath, $fileName]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|