Merge pull request #485 from halkyon/deprecate_director_statics_for_controller

Deprecate director controller static functions
This commit is contained in:
Sean Harvey 2012-05-23 15:54:57 -07:00
commit 1ed5e3c9be
11 changed files with 84 additions and 38 deletions

View File

@ -164,7 +164,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
);
// Allow customisation of the access check by a extension
// Also all the canView() check to execute Director::redirect()
// Also all the canView() check to execute Controller::redirect()
if(!$this->canView() && !$this->response->isFinished()) {
// When access /admin/, we should try a redirect to another part of the admin rather than be locked out
$menu = $this->MainMenu();
@ -175,7 +175,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
&& $candidate->MenuItem->controller
&& singleton($candidate->MenuItem->controller)->canView()
) {
return Director::redirect($candidate->Link);
return $this->redirect($candidate->Link);
}
}
@ -194,7 +194,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
}
// Don't continue if there's already been a redirection request.
if(Director::redirected_to()) return;
if($this->redirectedTo()) return;
// Audit logging hook
if(empty($_REQUEST['executeForm']) && !$this->request->isAjax()) $this->extend('accessedCMS');

View File

@ -434,8 +434,7 @@ class Controller extends RequestHandler implements TemplateGlobalProvider {
}
/**
* Redirct to the given URL.
* It is generally recommended to call Director::redirect() rather than calling this function directly.
* Redirect to the given URL.
*/
function redirect($url, $code=302) {
if(!$this->response) $this->response = new SS_HTTPResponse();

View File

@ -397,33 +397,41 @@ class Director implements TemplateGlobalProvider {
/**
* Redirect to another page.
* @deprecated 2.5 Use Controller->redirect()
* - $url can be an absolute URL
* - or it can be a URL relative to the "site base"
* - if it is just a word without an slashes, then it redirects to another action on the current controller.
*/
static function redirect($url, $code=302) {
Deprecation::notice('2.5', 'Use Controller->redirect() instead.');
Controller::curr()->redirect($url, $code);
}
/**
* Tests whether a redirection has been requested.
* @deprecated 2.5 Use Controller->redirectedTo() instead
* @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will return null;
*/
static function redirected_to() {
Deprecation::notice('2.5', 'Use Controller->redirectedTo() instead.');
return Controller::curr()->redirectedTo();
}
/**
* Sets the HTTP status code
* @deprecated 2.5 Use Controller->getResponse()->setStatusCode() instead
*/
static function set_status_code($code) {
Deprecation::notice('2.5', 'Use Controller->getResponse()->setStatusCode() instead');
return Controller::curr()->getResponse()->setStatusCode($code);
}
/**
* Returns the current HTTP status code
* @deprecated 2.5 Use Controller->getResponse()->getStatusCode() instead
*/
static function get_status_code() {
Deprecation::notice('2.5', 'Use Controller->getResponse()->getStatusCode() instead');
return Controller::curr()->getResponse()->getStatusCode();
}

View File

@ -180,6 +180,6 @@ class DevelopmentAdmin extends Controller {
}
function errors() {
Director::redirect("Debug_");
$this->redirect("Debug_");
}
}

View File

@ -1,4 +1,4 @@
<?php
<?php
/**
* A field that allows you to attach a file to a DataObject without submitting the form it is part of, through the use
* of an iframe.
@ -175,12 +175,13 @@ class FileIFrameField extends FileField {
|| ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile']))
) {
$form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
Director::redirectBack();
$form->getController()->redirectBack();
return;
}
$desiredClass = $this->dataClass();
$controller = $this->form->getController();
// upload a new file
if($data['FileSource'] == 'new') {
$fileObject = Object::create($desiredClass);
@ -189,12 +190,12 @@ class FileIFrameField extends FileField {
$this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName);
} catch (Exception $e){
$form->sessionMessage(_t('FileIFrameField.DISALLOWEDFILETYPE', 'This filetype is not allowed to be uploaded'), 'bad');
Director::redirectBack();
$controller->redirectBack();
return;
}
if($this->upload->isError()) {
Director::redirectBack();
$controller->redirectBack();
return;
}
@ -209,7 +210,7 @@ class FileIFrameField extends FileField {
// dont allow the user to attach a folder by default
if(!$fileObject || ($fileObject instanceof Folder && $desiredClass != 'Folder')) {
Director::redirectBack();
$controller->redirectBack();
return;
}
@ -222,7 +223,7 @@ class FileIFrameField extends FileField {
}
$this->form->getRecord()->write();
Director::redirectBack();
$controller->redirectBack();
}
/**
@ -255,12 +256,12 @@ class FileIFrameField extends FileField {
$file->delete();
}
}
// then un-attach file from this record
$this->form->getRecord()->{$this->getName() . 'ID'} = 0;
$this->form->getRecord()->write();
Director::redirectBack();
$this->form->getController()->redirectBack();
}
/**

View File

@ -277,7 +277,7 @@ class Form extends RequestHandler {
$this->controller->hasMethod($funcName)
&& !$this->controller->checkAccessAction($funcName)
// If a button exists, allow it on the controller
&& !$this->Actions()->fieldByName('action_' . $funcName)
&& !$this->actions->fieldByName('action_' . $funcName)
) {
return $this->httpError(
403,
@ -291,7 +291,7 @@ class Form extends RequestHandler {
) {
return $this->httpError(
403,
sprintf('Action "%s" not allowed on form (Name: "%s")', $funcName, $this->Name())
sprintf('Action "%s" not allowed on form (Name: "%s")', $funcName, $this->name)
);
}
// TODO : Once we switch to a stricter policy regarding allowed_actions (meaning actions must be set explicitly in allowed_actions in order to run)
@ -871,13 +871,13 @@ class Form extends RequestHandler {
* @ignore
*/
private $htmlID = null;
/**
* Returns the name of the form
*/
public function FormName() {
if($this->htmlID) return $this->htmlID;
else return $this->class . '_' . str_replace(array('.','/'),'',$this->name);
else return $this->class . '_' . str_replace(array('.', '/'), '', $this->name);
}
/**
@ -888,19 +888,57 @@ class Form extends RequestHandler {
}
/**
* Returns this form's controller
* Returns this form's controller.
* This is used in the templates.
*/
public function Controller() {
return $this->getController();
}
/**
* Get the controller.
* @return Controller
*/
public function getController() {
return $this->controller;
}
/**
* Set the controller.
* @param Controller $controller
* @return Form
*/
public function setController($controller) {
$this->controller = $controller;
return $this;
}
/**
* @return string
*/
public function Name() {
Deprecation::notice('3.0', 'Use getName() instead.');
return $this->getName();
}
/**
* Get the name of the form.
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Set the name of the form.
* @param string $name
* @return Form
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Returns an object where there is a method with the same name as each data field on the form.
* That method will return the field itself.

View File

@ -53,7 +53,7 @@
* $file->loadUploaded($_FILES['FileTypeID']);
*
* // Redirect to a page thanking people for registering
* Director::redirect('thanks-for-your-submission/');
* $this->redirect('thanks-for-your-submission/');
* }
* </code>
*

View File

@ -284,7 +284,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
$controller = Controller::curr();
$noActionURL = $controller->removeAction($_REQUEST['url']);
$controller->getResponse()->removeHeader('Location'); //clear the existing redirect
return Director::redirect($noActionURL, 302);
return $controller->redirect($noActionURL, 302);
}
$actions = new FieldList();
@ -406,7 +406,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
$controller = Controller::curr();
$noActionURL = $controller->removeAction($data['url']);
return Director::redirect($noActionURL, 302); //redirect back to admin section
return $controller->redirect($noActionURL, 302); //redirect back to admin section
}
/**

View File

@ -97,7 +97,7 @@ class DatabaseAdmin extends Controller {
echo "<p>Setting up the database; you will be returned to your site shortly....</p>";
$this->doBuild(true);
echo "<p>Done!</p>";
Director::redirect($_GET['returnURL']);
$this->redirect($_GET['returnURL']);
} else {
$this->doBuild(isset($_REQUEST['quiet']) || isset($_REQUEST['from_installer']), !isset($_REQUEST['dont_populate']));
}

View File

@ -67,7 +67,7 @@ class ChangePasswordForm extends Form {
_t('Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"),
"bad"
);
Director::redirectBack();
$this->controller->redirectBack();
return;
}
}
@ -80,7 +80,7 @@ class ChangePasswordForm extends Form {
// The user is not logged in and no valid auto login hash is available
if(!$member) {
Session::clear('AutoLoginHash');
Director::redirect('loginpage');
$this->controller->redirect('loginpage');
return;
}
}
@ -91,7 +91,7 @@ class ChangePasswordForm extends Form {
$this->sessionMessage(
_t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"),
"bad");
Director::redirectBack();
$this->controller->redirectBack();
return;
}
else if($data['NewPassword1'] == $data['NewPassword2']) {
@ -107,12 +107,12 @@ class ChangePasswordForm extends Form {
// absolute redirection URLs may cause spoofing
&& Director::is_site_url($_REQUEST['BackURL'])
) {
Director::redirect($_REQUEST['BackURL']);
$this->controller->redirect($_REQUEST['BackURL']);
}
else {
// Redirect to default location - the login form saying "You are logged in as..."
$redirectURL = HTTP::setGetVar('BackURL', Director::absoluteBaseURL(), $this->controller->Link('login'));
Director::redirect($redirectURL);
$this->controller->redirect($redirectURL);
}
} else {
$this->clearMessage();
@ -124,7 +124,7 @@ class ChangePasswordForm extends Form {
),
"bad"
);
Director::redirectBack();
$this->controller->redirectBack();
}
} else {
@ -132,7 +132,7 @@ class ChangePasswordForm extends Form {
$this->sessionMessage(
_t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"),
"bad");
Director::redirectBack();
$this->controller->redirectBack();
}
}

View File

@ -230,9 +230,9 @@ class Security extends Controller {
// TODO AccessLogEntry needs an extension to handle permission denied errors
// Audit logging hook
if($controller) $controller->extend('permissionDenied', $member);
$controller->extend('permissionDenied', $member);
Director::redirect("Security/login?BackURL=" . urlencode($_SERVER['REQUEST_URI']));
$controller->redirect("Security/login?BackURL=" . urlencode($_SERVER['REQUEST_URI']));
}
return;
}
@ -319,7 +319,7 @@ class Security extends Controller {
// Event handler for pre-login, with an option to let it break you out of the login form
$eventResults = $this->extend('onBeforeSecurityLogin');
// If there was a redirection, return
if(Director::redirected_to()) return;
if($this->redirectedTo()) return;
// If there was an SS_HTTPResponse object returned, then return that
else if($eventResults) {
foreach($eventResults as $result) {