App object refactor

This commit is contained in:
Damian Mooyman 2017-06-08 18:02:18 +12:00
parent d14e000e66
commit 21e204cc59
7 changed files with 55 additions and 78 deletions

View File

@ -1576,7 +1576,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
public function getNewItem($id, $setID = true)
{
$parentClass = $this->stat('tree_class');
list($dummy, $className, $parentID, $suffix) = array_pad(explode('-', $id), 4, null);
list(, $className, $parentID) = array_pad(explode('-', $id), 3, null);
if (!is_a($className, $parentClass, true)) {
$response = Security::permissionFailure($this);
@ -1588,18 +1588,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
/** @var SiteTree $newItem */
$newItem = Injector::inst()->create($className);
if (!$suffix) {
$sessionTag = "NewItems." . $parentID . "." . $className;
if (Session::get($sessionTag)) {
$suffix = '-' . Session::get($sessionTag);
Session::set($sessionTag, Session::get($sessionTag) + 1);
} else {
Session::set($sessionTag, 1);
}
$id = $id . $suffix;
}
$newItem->Title = _t(
'SilverStripe\\CMS\\Controllers\\CMSMain.NEWPAGE',
"New {pagetype}",
@ -1612,10 +1600,14 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
// DataObject::fieldExists only checks the current class, not the hierarchy
// This allows the CMS to set the correct sort value
if ($newItem->castingHelper('Sort')) {
$newItem->Sort = DB::prepared_query('SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?', array($parentID))->value() + 1;
$maxSort = DB::prepared_query(
'SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?',
array($parentID)
)->value();
$newItem->Sort = (int)$maxSort + 1;
}
if ($setID) {
if ($setID && $id) {
$newItem->ID = $id;
}

View File

@ -180,8 +180,6 @@ class CMSPageAddController extends CMSPageEditController
$className = isset($data['PageType']) ? $data['PageType'] : "Page";
$parentID = isset($data['ParentID']) ? (int)$data['ParentID'] : 0;
$suffix = isset($data['Suffix']) ? "-" . $data['Suffix'] : null;
if (!$parentID && isset($data['Parent'])) {
$page = SiteTree::get_by_link($data['Parent']);
if ($page) {
@ -203,18 +201,19 @@ class CMSPageAddController extends CMSPageEditController
return Security::permissionFailure($this);
}
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
$record = $this->getNewItem("new-$className-$parentID", false);
$this->extend('updateDoAdd', $record, $form);
$record->write();
$editController = CMSPageEditController::singleton();
$editController->setCurrentPageID($record->ID);
Session::set(
$session = $this->getRequest()->getSession();
$session->set(
"FormInfo.Form_EditForm.formError.message",
_t('SilverStripe\\CMS\\Controllers\\CMSMain.PageAdded', 'Successfully created page')
);
Session::set("FormInfo.Form_EditForm.formError.type", 'good');
$session->set("FormInfo.Form_EditForm.formError.type", 'good');
return $this->redirect(Controller::join_links($editController->Link('show'), $record->ID));
}

View File

@ -5,24 +5,22 @@ namespace SilverStripe\CMS\Controllers;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Convert;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\ORM\SS_List;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
@ -169,16 +167,14 @@ class ContentController extends Controller
* fall over to a child controller in order to provide functionality for nested URLs.
*
* @param HTTPRequest $request
* @param DataModel $model
* @return HTTPResponse
* @throws HTTPResponse_Exception
*/
public function handleRequest(HTTPRequest $request, DataModel $model)
public function handleRequest(HTTPRequest $request)
{
/** @var SiteTree $child */
$child = null;
$action = $request->param('Action');
$this->setDataModel($model);
// If nested URLs are enabled, and there is no action handler for the current request then attempt to pass
// control to a child controller. This allows for the creation of chains of controllers which correspond to a
@ -203,7 +199,7 @@ class ContentController extends Controller
$request->shiftAllParams();
$request->shift();
$response = ModelAsController::controller_for($child)->handleRequest($request, $model);
$response = ModelAsController::controller_for($child)->handleRequest($request);
} else {
// If a specific locale is requested, and it doesn't match the page found by URLSegment,
// look for a translation and redirect (see #5001). Only happens on the last child in
@ -227,7 +223,7 @@ class ContentController extends Controller
Director::set_current_page($this->data());
try {
$response = parent::handleRequest($request, $model);
$response = parent::handleRequest($request);
Director::set_current_page(null);
} catch (HTTPResponse_Exception $e) {
@ -474,8 +470,8 @@ HTML;
global $project;
$data = new ArrayData(array(
'Project' => Convert::raw2xml($project),
'Username' => Convert::raw2xml(Session::get('username')),
'Password' => Convert::raw2xml(Session::get('password')),
'Username' => Convert::raw2xml($this->getRequest()->getSession()->get('username')),
'Password' => Convert::raw2xml($this->getRequest()->getSession()->get('password')),
));
return array(
@ -518,8 +514,8 @@ HTML;
}
$data = new ArrayData(array(
'Username' => Convert::raw2xml(Session::get('username')),
'Password' => Convert::raw2xml(Session::get('password')),
'Username' => Convert::raw2xml($this->getRequest()->getSession()->get('username')),
'Password' => Convert::raw2xml($this->getRequest()->getSession()->get('password')),
'UnsuccessfulFiles' => $unsuccessful
));
$content->setValue($data->renderWith([

View File

@ -2,22 +2,20 @@
namespace SilverStripe\CMS\Controllers;
use Exception;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\NestedController;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\NestedController;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use Exception;
use Translatable;
/**
@ -26,8 +24,9 @@ use Translatable;
*/
class ModelAsController extends Controller implements NestedController
{
private static $extensions = array('SilverStripe\\CMS\\Controllers\\OldPageRedirector');
private static $extensions = [
OldPageRedirector::class,
];
/**
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
@ -54,9 +53,9 @@ class ModelAsController extends Controller implements NestedController
parent::init();
}
protected function beforeHandleRequest(HTTPRequest $request, DataModel $model)
protected function beforeHandleRequest(HTTPRequest $request)
{
parent::beforeHandleRequest($request, $model);
parent::beforeHandleRequest($request);
// If the database has not yet been created, redirect to the build page.
/** @skipUpgrade */
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
@ -73,12 +72,11 @@ class ModelAsController extends Controller implements NestedController
/**
* @uses ModelAsController::getNestedController()
* @param HTTPRequest $request
* @param DataModel $model
* @return HTTPResponse
*/
public function handleRequest(HTTPRequest $request, DataModel $model)
public function handleRequest(HTTPRequest $request)
{
$this->beforeHandleRequest($request, $model);
$this->beforeHandleRequest($request);
// If we had a redirection or something, halt processing.
if ($this->getResponse()->isFinished()) {
@ -99,7 +97,7 @@ class ModelAsController extends Controller implements NestedController
$result = $this->getNestedController();
if ($result instanceof RequestHandler) {
$result = $result->handleRequest($this->getRequest(), $model);
$result = $result->handleRequest($this->getRequest());
} elseif (!($result instanceof HTTPResponse)) {
user_error("ModelAsController::getNestedController() returned bad object type '" .
get_class($result)."'", E_USER_WARNING);
@ -131,11 +129,11 @@ class ModelAsController extends Controller implements NestedController
// Select child page
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment));
if (SiteTree::config()->nested_urls) {
if (SiteTree::config()->get('nested_urls')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0);
}
/** @var SiteTree $sitetree */
$sitetree = DataObject::get_one('SilverStripe\\CMS\\Model\\SiteTree', $conditions);
$sitetree = DataObject::get_one(SiteTree::class, $conditions);
// Check translation module
// @todo Refactor out module specific code

View File

@ -10,8 +10,6 @@ use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Resettable;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\DB;
use Translatable;
@ -103,9 +101,9 @@ class RootURLController extends Controller implements Resettable
self::$cached_homepage_link = null;
}
protected function beforeHandleRequest(HTTPRequest $request, DataModel $model)
protected function beforeHandleRequest(HTTPRequest $request)
{
parent::beforeHandleRequest($request, $model);
parent::beforeHandleRequest($request);
self::$is_at_root = true;
@ -123,13 +121,12 @@ class RootURLController extends Controller implements Resettable
/**
* @param HTTPRequest $request
* @param DataModel|null $model
* @return HTTPResponse
*/
public function handleRequest(HTTPRequest $request, DataModel $model = null)
public function handleRequest(HTTPRequest $request)
{
self::$is_at_root = true;
$this->beforeHandleRequest($request, $model);
$this->beforeHandleRequest($request);
if (!$this->getResponse()->isFinished()) {
/** @skipUpgrade */
@ -142,7 +139,7 @@ class RootURLController extends Controller implements Resettable
$request->match('$URLSegment//$Action', true);
$controller = new ModelAsController();
$response = $controller->handleRequest($request, $model);
$response = $controller->handleRequest($request);
$this->prepareResponse($response);
}

View File

@ -2,21 +2,21 @@
namespace SilverStripe\CMS\Model;
use Page;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Storage\GeneratedAssetHandler;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataModel;
use SilverStripe\Versioned\Versioned;
use SilverStripe\ORM\DB;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\View\Requirements;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Assets\File;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use Page;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
/**
* ErrorPage holds the content for the page of an error response.
@ -32,7 +32,6 @@ use Page;
*/
class ErrorPage extends Page
{
private static $db = array(
"ErrorCode" => "Int",
);
@ -97,10 +96,7 @@ class ErrorPage extends Page
Requirements::clear_combined_files();
return ModelAsController::controller_for($errorPage)
->handleRequest(
new HTTPRequest('GET', ''),
DataModel::inst()
);
->handleRequest(new HTTPRequest('GET', ''));
}
// then fall back on a cached version
@ -279,7 +275,7 @@ class ErrorPage extends Page
// Run the page (reset the theme, it might've been disabled by LeftAndMain::init())
Config::nest();
Config::inst()->update('SilverStripe\\View\\SSViewer', 'theme_enabled', true);
SSViewer::config()->set('theme_enabled', true);
$response = Director::test(Director::makeRelative($this->Link()));
Config::unnest();
$errorContent = $response->getBody();

View File

@ -1,17 +1,15 @@
<?php
namespace SilverStripe\CMS\Model;
use SilverStripe\ORM\DataModel;
use PageController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use PageController;
/**
* Controller for ErrorPages.
*/
class ErrorPageController extends PageController
{
/**
* Overload the provided {@link Controller::handleRequest()} to append the
* correct status code post request since otherwise permission related error
@ -19,13 +17,14 @@ class ErrorPageController extends PageController
* {@link HTTPResponse::isFinished() ignoring the response body.
*
* @param HTTPRequest $request
* @param DataModel $model
* @return HTTPResponse
*/
public function handleRequest(HTTPRequest $request, DataModel $model = null)
public function handleRequest(HTTPRequest $request)
{
$response = parent::handleRequest($request, $model);
$response->setStatusCode($this->ErrorCode);
/** @var ErrorPage $page */
$page = $this->data();
$response = parent::handleRequest($request);
$response->setStatusCode($page->ErrorCode);
return $response;
}
}