Skip to content

Commit

Permalink
Merge pull request #42 from tractorcow/pulls/fix-controller-init
Browse files Browse the repository at this point in the history
BUG Fix controller bypassing init
  • Loading branch information
assertchris committed Aug 26, 2015
2 parents 8eaa1f8 + 5191479 commit ab5efe3
Showing 1 changed file with 38 additions and 13 deletions.
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(
'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');

// 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,
));
$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

0 comments on commit ab5efe3

Please sign in to comment.