Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 69 additions & 64 deletions code/UnderConstruction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,52 @@
* @author Frank Mullenger <[email protected]>
* @package underconstruction
*/
class UnderConstruction_Decorator extends DataExtension {
class UnderConstruction_Decorator extends DataExtension
{

/**
/**
* Create an {@link ErrorPage} for status code 503
*
* @see UnderConstruction_Extension::onBeforeInit()
* @see DataObjectDecorator::requireDefaultRecords()
* @return Void
*/
function requireDefaultRecords() {
public function requireDefaultRecords()
{

// Ensure that an assets path exists before we do any error page creation
if(!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}
if (!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}

$pageUnderConstructionErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '503'");
$pageUnderConstructionErrorPageExists = ($pageUnderConstructionErrorPage && $pageUnderConstructionErrorPage->exists()) ? true : false;
$pageUnderConstructionErrorPagePath = ErrorPage::get_filepath_for_errorcode(503);
if(!($pageUnderConstructionErrorPageExists && file_exists($pageUnderConstructionErrorPagePath))) {
if(!$pageUnderConstructionErrorPageExists) {
$pageUnderConstructionErrorPage = new ErrorPage();
$pageUnderConstructionErrorPage->ErrorCode = 503;
$pageUnderConstructionErrorPage->Title = _t('UnderConstruction.TITLE', 'Under Construction');
$pageUnderConstructionErrorPage->Content = _t('UnderConstruction.CONTENT', '<p>Sorry, this site is currently under construction.</p>');
$pageUnderConstructionErrorPage->Status = 'New page';
$pageUnderConstructionErrorPage->write();
$pageUnderConstructionErrorPage->publish('Stage', 'Live');
}
$pageUnderConstructionErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '503'");
$pageUnderConstructionErrorPageExists = ($pageUnderConstructionErrorPage && $pageUnderConstructionErrorPage->exists()) ? true : false;
$pageUnderConstructionErrorPagePath = ErrorPage::get_filepath_for_errorcode(503);
if (!($pageUnderConstructionErrorPageExists && file_exists($pageUnderConstructionErrorPagePath))) {
if (!$pageUnderConstructionErrorPageExists) {
$pageUnderConstructionErrorPage = new ErrorPage();
$pageUnderConstructionErrorPage->ErrorCode = 503;
$pageUnderConstructionErrorPage->Title = _t('UnderConstruction.TITLE', 'Under Construction');
$pageUnderConstructionErrorPage->Content = _t('UnderConstruction.CONTENT', '<p>Sorry, this site is currently under construction.</p>');
$pageUnderConstructionErrorPage->Status = 'New page';
$pageUnderConstructionErrorPage->write();
$pageUnderConstructionErrorPage->publish('Stage', 'Live');
}

// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($pageUnderConstructionErrorPage->Link()));
if($fh = fopen($pageUnderConstructionErrorPagePath, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($pageUnderConstructionErrorPage->Link()));
if ($fh = fopen($pageUnderConstructionErrorPagePath, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}

if($written) {
DB::alteration_message('503 error page created', 'created');
} else {
DB::alteration_message(sprintf('503 error page could not be created at %s. Please check permissions', $pageUnderConstructionErrorPagePath), 'error');
}
}
}
if ($written) {
DB::alteration_message('503 error page created', 'created');
} else {
DB::alteration_message(sprintf('503 error page could not be created at %s. Please check permissions', $pageUnderConstructionErrorPagePath), 'error');
}
}
}
}

/**
Expand All @@ -58,34 +60,35 @@ function requireDefaultRecords() {
* @author Frank Mullenger <[email protected]>
* @package underconstruction
*/
class UnderConstruction_Extension extends Extension {
class UnderConstruction_Extension extends Extension
{

/**
/**
* If current logged in member is not an admin and not trying to log in to the admin
* or run a /dev/build then display an {@link ErrorPage}.
*
* @see UnderConstruction_Decorator::requireDefaultRecords()
* @return Void
*/
public function onBeforeInit() {

$siteConfig = SiteConfig::current_site_config();
$siteUnderConstruction = $siteConfig->UnderConstruction;
public function onBeforeInit()
{
$siteConfig = SiteConfig::current_site_config();
$siteUnderConstruction = $siteConfig->UnderConstruction;

if ($siteUnderConstruction) {
if ($siteUnderConstruction) {

//Check to see if running /dev/build
$runningDevBuild = $this->owner && $this->owner->data() instanceof ErrorPage;

if (!Permission::check('ADMIN')
&& strpos($_SERVER['REQUEST_URI'], '/admin') === false
&& strpos($_SERVER['REQUEST_URI'], '/Security') === false
&& !Director::isDev()
if (!Permission::check('ADMIN')
&& strpos($_SERVER['REQUEST_URI'], '/admin') === false
&& strpos($_SERVER['REQUEST_URI'], '/Security') === false
&& !Director::isDev()
&& !$runningDevBuild) {
Debug::friendlyError(503);
exit;
Debug::friendlyError(503);
exit;
}
}
}
}
}

Expand All @@ -96,27 +99,29 @@ public function onBeforeInit() {
* @author Frank Mullenger <[email protected]>
* @package underconstruction
*/
class UnderConstruction_Settings extends DataExtension {
class UnderConstruction_Settings extends DataExtension
{

// Add database field for flag to either display or hide under construction pages.
static $db = array(
// Add database field for flag to either display or hide under construction pages.
public static $db = array(
'UnderConstruction' => 'Boolean'
);
);

/**
* Adding field to allow CMS users to turn off under construction pages.
*
* @see DataExtension::updateCMSFields()
*/
function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Access', new HeaderField(
'UnderConstructionHeading',
_t('UnderConstruction.SETTINGSHEADING', 'Is this site under construction?'),
/**
* Adding field to allow CMS users to turn off under construction pages.
*
* @see DataExtension::updateCMSFields()
*/
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.Access', new HeaderField(
'UnderConstructionHeading',
_t('UnderConstruction.SETTINGSHEADING', 'Is this site under construction?'),
2
));
$fields->addFieldToTab('Root.Access', new CheckboxField(
'UnderConstruction',
_t('UnderConstruction.SETTINGSCHECKBOXLABEL', '&nbsp; Display an under construction page?')
$fields->addFieldToTab('Root.Access', new CheckboxField(
'UnderConstruction',
_t('UnderConstruction.SETTINGSCHECKBOXLABEL', '&nbsp; Display an under construction page?')
));
}
}
}
}