Permalink
Browse files

Reduced code duplication in ErrorPage (no functional changes)

  • Loading branch information...
1 parent b15b983 commit 416178b6682878c5b094d8d21fa2a1660ebe4ce8 @chillu chillu committed Jan 29, 2013
Showing with 61 additions and 52 deletions.
  1. +61 −52 code/model/ErrorPage.php
View
@@ -72,62 +72,71 @@ public function requireDefaultRecords() {
if(!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}
+
+ $defaultPages = array(
+ array(
+ 'ErrorCode' => 404,
+ 'Title' => _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'),
+ 'Content' => _t(
+ 'ErrorPage.DEFAULTERRORPAGECONTENT',
+ '<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p>'
+ . '<p>Please check the spelling of the URL you were trying to access and try again.</p>'
+ )
+ ),
+ array(
+ 'ErrorCode' => 500,
+ 'Title' => _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'),
+ 'Content' => _t(
+ 'ErrorPage.DEFAULTSERVERERRORPAGECONTENT',
+ '<p>Sorry, there was a problem with handling your request.</p>'
+ )
+ )
+ );
- $pageNotFoundErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'");
- $pageNotFoundErrorPageExists = ($pageNotFoundErrorPage && $pageNotFoundErrorPage->exists()) ? true : false;
- $pageNotFoundErrorPagePath = self::get_filepath_for_errorcode(404);
- if(!($pageNotFoundErrorPageExists && file_exists($pageNotFoundErrorPagePath))) {
- if(!$pageNotFoundErrorPageExists) {
- $pageNotFoundErrorPage = new ErrorPage();
- $pageNotFoundErrorPage->ErrorCode = 404;
- $pageNotFoundErrorPage->Title = _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found');
- $pageNotFoundErrorPage->Content = _t('ErrorPage.DEFAULTERRORPAGECONTENT', '<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>');
- $pageNotFoundErrorPage->write();
- $pageNotFoundErrorPage->publish('Stage', 'Live');
- }
-
- // Ensure a static error page is created from latest error page content
- $response = Director::test(Director::makeRelative($pageNotFoundErrorPage->Link()));
- $written = null;
- if($fh = fopen($pageNotFoundErrorPagePath, 'w')) {
- $written = fwrite($fh, $response->getBody());
- fclose($fh);
- }
-
- if($written) {
- DB::alteration_message('404 error page created', 'created');
- } else {
- DB::alteration_message(sprintf('404 error page could not be created at %s. Please check permissions', $pageNotFoundErrorPagePath), 'error');
- }
- }
-
- $serverErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '500'");
- $serverErrorPageExists = ($serverErrorPage && $serverErrorPage->exists()) ? true : false;
- $serverErrorPagePath = self::get_filepath_for_errorcode(500);
- if(!($serverErrorPageExists && file_exists($serverErrorPagePath))) {
- if(!$serverErrorPageExists) {
- $serverErrorPage = new ErrorPage();
- $serverErrorPage->ErrorCode = 500;
- $serverErrorPage->Title = _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error');
- $serverErrorPage->Content = _t('ErrorPage.DEFAULTSERVERERRORPAGECONTENT', '<p>Sorry, there was a problem with handling your request.</p>');
- $serverErrorPage->write();
- $serverErrorPage->publish('Stage', 'Live');
- }
-
- // Ensure a static error page is created from latest error page content
- $response = Director::test(Director::makeRelative($serverErrorPage->Link()));
- $written = null;
- if($fh = fopen($serverErrorPagePath, 'w')) {
- $written = fwrite($fh, $response->getBody());
- fclose($fh);
- }
+ foreach($defaultPages as $defaultData) {
+ $code = $defaultData['ErrorCode'];
+ $page = DataObject::get_one(
+ 'ErrorPage',
+ sprintf("\"ErrorCode\" = '%s'", $code)
+ );
+ $pageExists = ($page && $page->exists());
+ $pagePath = self::get_filepath_for_errorcode($code);
+ if(!($pageExists && file_exists($pagePath))) {
+ if(!$pageExists) {
+ $page = new ErrorPage();
+ $page->ErrorCode = $code;
+ $page->Title = $defaultData['Title'];
+ $page->Content = $defaultData['Title'];
+ $page->write();
+ $page->publish('Stage', 'Live');
+ }
- if($written) {
- DB::alteration_message('500 error page created', 'created');
- } else {
- DB::alteration_message(sprintf('500 error page could not be created at %s. Please check permissions', $serverErrorPagePath), 'error');
+ // Ensure a static error page is created from latest error 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 page created', $code),
+ 'created'
+ );
+ } else {
+ DB::alteration_message(
+ sprintf(
+ '%s error page could not be created at %s. Please check permissions',
+ $code,
+ $pagePath
+ ),
+ 'error'
+ );
+ }
}
}
+
}
}

0 comments on commit 416178b

Please sign in to comment.