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

FIX versioned class not found and preview session bugfix #67

Merged
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions src/Controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace SilverStripe\ShareDraftContent\Controllers;

use BadMethodCallException;
use Exception;
use PageController;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ShareDraftContent\Models\ShareToken;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;

Expand Down Expand Up @@ -48,7 +50,12 @@ public function preview(HTTPRequest $request)
{
$key = $request->param('Key');
$token = $request->param('Token');

try {
$session = $this->getRequest()->getSession();
} catch (BadMethodCallException $e) {
// Create a new session
$session = $this->getRequest()->setSession(Injector::inst()->create(Session::class));
}
/**
* @var ShareToken $shareToken
*/
Expand All @@ -72,16 +79,16 @@ public function preview(HTTPRequest $request)
Requirements::css('silverstripe/sharedraftcontent:css/top-bar.css');

// Temporarily un-secure the draft site and switch to draft
$oldSecured = Session::get('unsecuredDraftSite');
$oldSecured = $session->get('unsecuredDraftSite');
$oldMode = Versioned::get_reading_mode();
$restore = function () use ($oldSecured, $oldMode) {
Session::set('unsecuredDraftSite', $oldSecured);
$restore = function () use ($oldSecured, $oldMode, $session) {
$session->set('unsecuredDraftSite', $oldSecured);
Versioned::set_reading_mode($oldMode);
};

// Process page inside an unsecured draft container
try {
Session::set('unsecuredDraftSite', true);
$session->set('unsecuredDraftSite', true);
Versioned::set_stage('Stage');

// Hack to get around ContentController::init() redirecting on home page
Expand All @@ -90,6 +97,7 @@ public function preview(HTTPRequest $request)
// Create mock request; Simplify request to single top level request
$pageRequest = new HTTPRequest('GET', $page->URLSegment);
$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
$pageRequest->setSession($session);
$rendered = $controller->handleRequest($pageRequest, $this->model);

// Render draft heading
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/ShareDraftContentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function getNewShareToken()
))->first();
}

$config = Config::inst()->forClass(__CLASS__);
$config = Config::forClass(__CLASS__);

$validForDays = $config->valid_for_days;

Expand Down