-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,25 +42,50 @@ public function preview(SS_HTTPRequest $request) { | |
return $this->errorPage(); | ||
} | ||
|
||
$page = $shareToken->Page(); | ||
$page = Versioned::get_one_by_stage( | ||
'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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
There was a problem hiding this comment.
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.