mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API CHANGE Moved RequestHandler->isAjax() to SS_HTTPRequest->isAjax()
This commit is contained in:
parent
6f89fe0703
commit
a44b67bae2
@ -68,7 +68,7 @@ class CMSBatchActionHandler extends RequestHandler {
|
|||||||
|
|
||||||
function handleAction($request) {
|
function handleAction($request) {
|
||||||
// This method can't be called without ajax.
|
// This method can't be called without ajax.
|
||||||
if(!$this->parentController->isAjax()) {
|
if(!$request->isAjax()) {
|
||||||
$this->parentController->redirectBack();
|
$this->parentController->redirectBack();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -193,7 +193,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
|||||||
if(Director::redirected_to()) return;
|
if(Director::redirected_to()) return;
|
||||||
|
|
||||||
// Audit logging hook
|
// Audit logging hook
|
||||||
if(empty($_REQUEST['executeForm']) && !$this->isAjax()) $this->extend('accessedCMS');
|
if(empty($_REQUEST['executeForm']) && !$this->request->isAjax()) $this->extend('accessedCMS');
|
||||||
|
|
||||||
// Set the members html editor config
|
// Set the members html editor config
|
||||||
HtmlEditorConfig::set_active(Member::currentUser()->getHtmlEditorConfigForCMS());
|
HtmlEditorConfig::set_active(Member::currentUser()->getHtmlEditorConfigForCMS());
|
||||||
@ -341,7 +341,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function index($request) {
|
function index($request) {
|
||||||
return ($this->isAjax()) ? $this->show($request) : $this->getViewer('index')->process($this);
|
return ($request->isAjax()) ? $this->show($request) : $this->getViewer('index')->process($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -535,17 +535,6 @@ class Controller extends RequestHandler implements TemplateGlobalProvider {
|
|||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if this controller is processing an ajax request
|
|
||||||
* @return boolean True if this controller is processing an ajax request
|
|
||||||
*/
|
|
||||||
function isAjax() {
|
|
||||||
return (
|
|
||||||
isset($this->requestParams['ajax']) || isset($_REQUEST['ajax']) ||
|
|
||||||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joins two or more link segments together, putting a slash between them if necessary.
|
* Joins two or more link segments together, putting a slash between them if necessary.
|
||||||
* Use this for building the results of {@link Link()} methods.
|
* Use this for building the results of {@link Link()} methods.
|
||||||
|
@ -682,7 +682,7 @@ class Director implements TemplateGlobalProvider {
|
|||||||
*/
|
*/
|
||||||
static function is_ajax() {
|
static function is_ajax() {
|
||||||
if(Controller::has_curr()) {
|
if(Controller::has_curr()) {
|
||||||
return Controller::curr()->isAjax();
|
return Controller::curr()->getRequest()->isAjax();
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
isset($_REQUEST['ajax']) ||
|
isset($_REQUEST['ajax']) ||
|
||||||
|
@ -233,6 +233,20 @@ class SS_HTTPRequest implements ArrayAccess {
|
|||||||
return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;
|
return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this request an ajax request,
|
||||||
|
* based on custom HTTP ajax added by common JavaScript libraries,
|
||||||
|
* or based on an explicit "ajax" request parameter.
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
function isAjax() {
|
||||||
|
return (
|
||||||
|
$this->requestVar('ajax') ||
|
||||||
|
$this->getHeader('X-Requested-With') && $this->getHeader('X-Requested-With') == "XMLHttpRequest"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the existence of a key-value pair in the request to be checked using
|
* Enables the existence of a key-value pair in the request to be checked using
|
||||||
* array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title']
|
* array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title']
|
||||||
|
@ -339,6 +339,59 @@ class RequestHandler extends ViewableData {
|
|||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 3.0 Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())
|
||||||
|
*/
|
||||||
|
function isAjax() {
|
||||||
|
Deprecation::notice('3.0', 'Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())');
|
||||||
|
return $this->request->isAjax();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the X-Get-Fragment header that AJAX responses may provide, returning the
|
||||||
|
* fragment, or, in the case of non-AJAX form submissions, redirecting back to the submitter.
|
||||||
|
*
|
||||||
|
* X-Get-Fragment ensures that users won't end up seeing the unstyled form HTML in their browser
|
||||||
|
* If a JS error prevents the Ajax overriding of form submissions from happening. It also provides
|
||||||
|
* better non-JS operation.
|
||||||
|
*
|
||||||
|
* Out of the box, the handler "CurrentForm" value, which will return the rendered form. Non-Ajax
|
||||||
|
* calls will redirect back.
|
||||||
|
*
|
||||||
|
* To extend its responses, pass a map to the $options argument. Each key is the value of X-Get-Fragment
|
||||||
|
* that will work, and the value is a PHP 'callable' value that will return the response for that
|
||||||
|
* value.
|
||||||
|
*
|
||||||
|
* If you specify $options['default'], this will be used as the non-ajax response.
|
||||||
|
*
|
||||||
|
* Note that if you use handleFragmentResponse, then any Ajax requests will have to include X-Get-Fragment
|
||||||
|
* or an error will be thrown.
|
||||||
|
*/
|
||||||
|
function handleFragmentResponse($form, $options = array()) {
|
||||||
|
// Prepare the default options and combine with the others
|
||||||
|
$lOptions = array(
|
||||||
|
'currentform' => array($form, 'forTemplate'),
|
||||||
|
'default' => array('Director', 'redirectBack'),
|
||||||
|
);
|
||||||
|
if($options) foreach($options as $k => $v) {
|
||||||
|
$lOptions[strtolower($k)] = $v;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($fragment = $this->request->getHeader('X-Get-Fragment')) {
|
||||||
|
$fragment = strtolower($fragment);
|
||||||
|
if(isset($lOptions[$fragment])) {
|
||||||
|
return call_user_func($lOptions[$fragment]);
|
||||||
|
} else {
|
||||||
|
throw new SS_HTTPResponse_Exception("X-Get-Fragment = '$fragment' not supported for this URL.", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if($this->isAjax()) throw new SS_HTTPResponse_Exception("Ajax requests to this URL require an X-Get-Fragment header.", 400);
|
||||||
|
return call_user_func($lOptions['default']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the SS_HTTPRequest object that this controller is using.
|
* Returns the SS_HTTPRequest object that this controller is using.
|
||||||
* Returns a placeholder {@link NullHTTPRequest} object unless
|
* Returns a placeholder {@link NullHTTPRequest} object unless
|
||||||
|
@ -196,7 +196,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
|
|||||||
'ItemEditForm' => $form,
|
'ItemEditForm' => $form,
|
||||||
))->renderWith($this->template);
|
))->renderWith($this->template);
|
||||||
|
|
||||||
if($controller->isAjax()) {
|
if($request->isAjax()) {
|
||||||
return $return;
|
return $return;
|
||||||
} else {
|
} else {
|
||||||
// If not requested by ajax, we need to render it within the controller context+template
|
// If not requested by ajax, we need to render it within the controller context+template
|
||||||
|
@ -230,4 +230,16 @@ class HTTPRequestTest extends SapphireTest {
|
|||||||
'Nested GET parameters should supplement POST parameters'
|
'Nested GET parameters should supplement POST parameters'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testIsAjax() {
|
||||||
|
$req = new SS_HTTPRequest('GET', '/', array('ajax' => 0));
|
||||||
|
$this->assertFalse($req->isAjax());
|
||||||
|
|
||||||
|
$req = new SS_HTTPRequest('GET', '/', array('ajax' => 1));
|
||||||
|
$this->assertTrue($req->isAjax());
|
||||||
|
|
||||||
|
$req = new SS_HTTPRequest('GET', '/');
|
||||||
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
|
$this->assertTrue($req->isAjax());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user