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

BUG Fix controller bypassing init #42

Merged
Merged
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
51 changes: 38 additions & 13 deletions code/controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,50 @@ public function preview(SS_HTTPRequest $request) {
return $this->errorPage();
}

$page = $shareToken->Page();
$page = Versioned::get_one_by_stage(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases this was returning a $page with ID = 0, so change this to check for stage version explicitly.

'SiteTree',
'Stage',
sprintf('"SiteTree"."ID" = \'%d\'', $shareToken->PageID)
);

$latest = $page->Versions(null, 'Version DESC')->first();
$latest = Versioned::get_latest_version('SiteTree', $shareToken->PageID);

$controller = $this->getControllerFor($latest);
$controller = $this->getControllerFor($page);

if(!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) {
Requirements::css(SHAREDRAFTCONTENT_DIR . '/css/top-bar.css');

$rendered = $controller->render();

$data = new ArrayData(array(
'Page' => $page,
'Latest' => $latest,
));

$include = (string) $data->renderWith('Includes/TopBar');

return str_replace('</body>', $include . '</body>', (string) $rendered);
// Temporarily un-secure the draft site and switch to draft
$oldSecured = Session::get('unsecuredDraftSite');
$oldMode = Versioned::get_reading_mode();
$restore = function() use ($oldSecured, $oldMode) {
Session::set('unsecuredDraftSite', $oldSecured);
Versioned::set_reading_mode($oldMode);
};

// Process page inside an unsecured draft container
try {
Session::set('unsecuredDraftSite', true);
Versioned::reading_stage('Stage');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without setting to stage, the menu would not show other pages in the stage-only.


// Create mock request; Simplify request to single top level reqest
$pageRequest = new SS_HTTPRequest('GET', $page->URLSegment);
$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
$rendered = $controller->handleRequest($pageRequest, $this->model);

// Render draft heading
$data = new ArrayData(array(
'Page' => $page,
'Latest' => $latest,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure separate $latest and $page is needed, but I've left in $latest anyway.

));
$include = (string) $data->renderWith('Includes/TopBar');
} catch(Exception $ex) {
$restore();
throw $ex;
}
$restore();

return str_replace('</body>', $include . '</body>', (string) $rendered->getBody());
} else {
return $this->errorPage();
}
Expand Down