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 // 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()) { 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 // When access /admin/, we should try a redirect to another part of the admin rather than be locked out
$menu = $this->MainMenu(); $menu = $this->MainMenu();
@ -175,7 +175,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
&& $candidate->MenuItem->controller && $candidate->MenuItem->controller
&& singleton($candidate->MenuItem->controller)->canView() && 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. // Don't continue if there's already been a redirection request.
if(Director::redirected_to()) return; if($this->redirectedTo()) return;
// Audit logging hook // Audit logging hook
if(empty($_REQUEST['executeForm']) && !$this->request->isAjax()) $this->extend('accessedCMS'); 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. * Redirect to the given URL.
* It is generally recommended to call Director::redirect() rather than calling this function directly.
*/ */
function redirect($url, $code=302) { function redirect($url, $code=302) {
if(!$this->response) $this->response = new SS_HTTPResponse(); if(!$this->response) $this->response = new SS_HTTPResponse();

View File

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

View File

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

View File

@ -175,11 +175,12 @@ class FileIFrameField extends FileField {
|| ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile'])) || ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile']))
) { ) {
$form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required'); $form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
Director::redirectBack(); $form->getController()->redirectBack();
return; return;
} }
$desiredClass = $this->dataClass(); $desiredClass = $this->dataClass();
$controller = $this->form->getController();
// upload a new file // upload a new file
if($data['FileSource'] == 'new') { if($data['FileSource'] == 'new') {
@ -189,12 +190,12 @@ class FileIFrameField extends FileField {
$this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName); $this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName);
} catch (Exception $e){ } catch (Exception $e){
$form->sessionMessage(_t('FileIFrameField.DISALLOWEDFILETYPE', 'This filetype is not allowed to be uploaded'), 'bad'); $form->sessionMessage(_t('FileIFrameField.DISALLOWEDFILETYPE', 'This filetype is not allowed to be uploaded'), 'bad');
Director::redirectBack(); $controller->redirectBack();
return; return;
} }
if($this->upload->isError()) { if($this->upload->isError()) {
Director::redirectBack(); $controller->redirectBack();
return; return;
} }
@ -209,7 +210,7 @@ class FileIFrameField extends FileField {
// dont allow the user to attach a folder by default // dont allow the user to attach a folder by default
if(!$fileObject || ($fileObject instanceof Folder && $desiredClass != 'Folder')) { if(!$fileObject || ($fileObject instanceof Folder && $desiredClass != 'Folder')) {
Director::redirectBack(); $controller->redirectBack();
return; return;
} }
@ -222,7 +223,7 @@ class FileIFrameField extends FileField {
} }
$this->form->getRecord()->write(); $this->form->getRecord()->write();
Director::redirectBack(); $controller->redirectBack();
} }
/** /**
@ -260,7 +261,7 @@ class FileIFrameField extends FileField {
$this->form->getRecord()->{$this->getName() . 'ID'} = 0; $this->form->getRecord()->{$this->getName() . 'ID'} = 0;
$this->form->getRecord()->write(); $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->hasMethod($funcName)
&& !$this->controller->checkAccessAction($funcName) && !$this->controller->checkAccessAction($funcName)
// If a button exists, allow it on the controller // If a button exists, allow it on the controller
&& !$this->Actions()->fieldByName('action_' . $funcName) && !$this->actions->fieldByName('action_' . $funcName)
) { ) {
return $this->httpError( return $this->httpError(
403, 403,
@ -291,7 +291,7 @@ class Form extends RequestHandler {
) { ) {
return $this->httpError( return $this->httpError(
403, 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) // TODO : Once we switch to a stricter policy regarding allowed_actions (meaning actions must be set explicitly in allowed_actions in order to run)
@ -877,7 +877,7 @@ class Form extends RequestHandler {
*/ */
public function FormName() { public function FormName() {
if($this->htmlID) return $this->htmlID; 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() { public function Controller() {
return $this->getController();
}
/**
* Get the controller.
* @return Controller
*/
public function getController() {
return $this->controller; return $this->controller;
} }
/**
* Set the controller.
* @param Controller $controller
* @return Form
*/
public function setController($controller) {
$this->controller = $controller;
return $this;
}
/** /**
* @return string * @return string
*/ */
public function Name() { 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; 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. * 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. * That method will return the field itself.

View File

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

View File

@ -284,7 +284,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
$controller = Controller::curr(); $controller = Controller::curr();
$noActionURL = $controller->removeAction($_REQUEST['url']); $noActionURL = $controller->removeAction($_REQUEST['url']);
$controller->getResponse()->removeHeader('Location'); //clear the existing redirect $controller->getResponse()->removeHeader('Location'); //clear the existing redirect
return Director::redirect($noActionURL, 302); return $controller->redirect($noActionURL, 302);
} }
$actions = new FieldList(); $actions = new FieldList();
@ -406,7 +406,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
$controller = Controller::curr(); $controller = Controller::curr();
$noActionURL = $controller->removeAction($data['url']); $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>"; echo "<p>Setting up the database; you will be returned to your site shortly....</p>";
$this->doBuild(true); $this->doBuild(true);
echo "<p>Done!</p>"; echo "<p>Done!</p>";
Director::redirect($_GET['returnURL']); $this->redirect($_GET['returnURL']);
} else { } else {
$this->doBuild(isset($_REQUEST['quiet']) || isset($_REQUEST['from_installer']), !isset($_REQUEST['dont_populate'])); $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"), _t('Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"),
"bad" "bad"
); );
Director::redirectBack(); $this->controller->redirectBack();
return; return;
} }
} }
@ -80,7 +80,7 @@ class ChangePasswordForm extends Form {
// The user is not logged in and no valid auto login hash is available // The user is not logged in and no valid auto login hash is available
if(!$member) { if(!$member) {
Session::clear('AutoLoginHash'); Session::clear('AutoLoginHash');
Director::redirect('loginpage'); $this->controller->redirect('loginpage');
return; return;
} }
} }
@ -91,7 +91,7 @@ class ChangePasswordForm extends Form {
$this->sessionMessage( $this->sessionMessage(
_t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"), _t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"),
"bad"); "bad");
Director::redirectBack(); $this->controller->redirectBack();
return; return;
} }
else if($data['NewPassword1'] == $data['NewPassword2']) { else if($data['NewPassword1'] == $data['NewPassword2']) {
@ -107,12 +107,12 @@ class ChangePasswordForm extends Form {
// absolute redirection URLs may cause spoofing // absolute redirection URLs may cause spoofing
&& Director::is_site_url($_REQUEST['BackURL']) && Director::is_site_url($_REQUEST['BackURL'])
) { ) {
Director::redirect($_REQUEST['BackURL']); $this->controller->redirect($_REQUEST['BackURL']);
} }
else { else {
// Redirect to default location - the login form saying "You are logged in as..." // Redirect to default location - the login form saying "You are logged in as..."
$redirectURL = HTTP::setGetVar('BackURL', Director::absoluteBaseURL(), $this->controller->Link('login')); $redirectURL = HTTP::setGetVar('BackURL', Director::absoluteBaseURL(), $this->controller->Link('login'));
Director::redirect($redirectURL); $this->controller->redirect($redirectURL);
} }
} else { } else {
$this->clearMessage(); $this->clearMessage();
@ -124,7 +124,7 @@ class ChangePasswordForm extends Form {
), ),
"bad" "bad"
); );
Director::redirectBack(); $this->controller->redirectBack();
} }
} else { } else {
@ -132,7 +132,7 @@ class ChangePasswordForm extends Form {
$this->sessionMessage( $this->sessionMessage(
_t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"), _t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"),
"bad"); "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 // TODO AccessLogEntry needs an extension to handle permission denied errors
// Audit logging hook // 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; 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 // Event handler for pre-login, with an option to let it break you out of the login form
$eventResults = $this->extend('onBeforeSecurityLogin'); $eventResults = $this->extend('onBeforeSecurityLogin');
// If there was a redirection, return // 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 // If there was an SS_HTTPResponse object returned, then return that
else if($eventResults) { else if($eventResults) {
foreach($eventResults as $result) { foreach($eventResults as $result) {