From 7215891d259616721a8fb9969c0addb57daf16e3 Mon Sep 17 00:00:00 2001 From: Darren-Lee Joseph Date: Fri, 16 Oct 2015 21:25:24 +0100 Subject: [PATCH] Ensure a static error page is created from latest Utility Page content on dev/build --- code/UtilityPage.php | 73 ++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/code/UtilityPage.php b/code/UtilityPage.php index 8079930..aa1f853 100644 --- a/code/UtilityPage.php +++ b/code/UtilityPage.php @@ -48,26 +48,61 @@ public function requireDefaultRecords() { // Skip creation of default records if(!self::config()->create_default_pages) return; + // Ensure that an assets path exists before we do any error page creation + if(!file_exists(ASSETS_PATH)) { + mkdir(ASSETS_PATH); + } + + $code = self::$defaults['ErrorCode']; + $pagePath = self::get_filepath_for_errorcode($code); + $page = UtilityPage::get()->first(); + $pageExists = ($page && $page->exists()); + //Only create a UtilityPage on dev/build if one does not already exist. - if(!UtilityPage::get()->exists()) { - - $page = UtilityPage::create(array( - 'Title' => _t('MaintenanceMode.TITLE', 'Undergoing Scheduled Maintenance'), - 'URLSegment' => _t('MaintenanceMode.URLSEGMENT', 'offline'), - 'MenuTitle' => _t('MaintenanceMode.MENUTITLE', 'Utility Page'), - 'Content' => _t('MaintenanceMode.CONTENT', '

We’ll be back soon!

' - .'

Sorry for the inconvenience but ' - .'our site is currently down for scheduled maintenance. ' - .'If you need to you can always contact us, ' - .'otherwise we’ll be back online shortly!

' - .'

— The Team

'), - 'ParentID' => 0, - 'Status' => 'Published' - )); - $page->write(); - $page->publish('Stage', 'Live'); - - DB::alteration_message('Utility Page created', 'created'); + if(!$pageExists or !file_exists($pagePath)) { + + if(!$pageExists) { + $page = UtilityPage::create(array( + 'Title' => _t('MaintenanceMode.TITLE', 'Undergoing Scheduled Maintenance'), + 'URLSegment' => _t('MaintenanceMode.URLSEGMENT', 'offline'), + 'MenuTitle' => _t('MaintenanceMode.MENUTITLE', 'Utility Page'), + 'Content' => _t('MaintenanceMode.CONTENT', '

We’ll be back soon!

' + . '

Sorry for the inconvenience but ' + . 'our site is currently down for scheduled maintenance. ' + . 'If you need to you can always contact us, ' + . 'otherwise we’ll be back online shortly!

' + . '

— The Team

'), + 'ParentID' => 0, + 'Status' => 'Published' + )); + $page->write(); + $page->publish('Stage', 'Live'); + } + + // Ensure a static error page is created from latest Utility Page content + $response = Director::test(Director::makeRelative($page->Link())); + $written = null; + if($fh = fopen($pagePath, 'w')) { + $written = fwrite($fh, $response->getBody()); + fclose($fh); + } + + if($written) { + DB::alteration_message( + sprintf('%s error Utility Page created', $code), + 'created' + ); + } else { + DB::alteration_message( + sprintf( + '%s error Utility page could not be created at %s. Please check permissions', + $code, + $pagePath + ), + 'error' + ); + } + } }