Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API CHANGE Moved RequestHandler->isAjax() to SS_HTTPRequest->isAjax()

  • Loading branch information...
commit a44b67bae2229acbcd374847615e1d1338c99f6c 1 parent 6f89fe0
@chillu chillu authored
View
2  admin/code/CMSBatchActionHandler.php
@@ -68,7 +68,7 @@ function Link() {
function handleAction($request) {
// This method can't be called without ajax.
- if(!$this->parentController->isAjax()) {
+ if(!$request->isAjax()) {
$this->parentController->redirectBack();
return;
}
View
4 admin/code/LeftAndMain.php
@@ -193,7 +193,7 @@ function init() {
if(Director::redirected_to()) return;
// 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
HtmlEditorConfig::set_active(Member::currentUser()->getHtmlEditorConfigForCMS());
@@ -341,7 +341,7 @@ function handleRequest(SS_HTTPRequest $request, DataModel $model = null) {
}
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);
}
View
11 control/Controller.php
@@ -536,17 +536,6 @@ function setSession(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.
* Use this for building the results of {@link Link()} methods.
* If either of the links have query strings,
View
2  control/Director.php
@@ -682,7 +682,7 @@ static function forceWWW() {
*/
static function is_ajax() {
if(Controller::has_curr()) {
- return Controller::curr()->isAjax();
+ return Controller::curr()->getRequest()->isAjax();
} else {
return (
isset($_REQUEST['ajax']) ||
View
14 control/HTTPRequest.php
@@ -232,6 +232,20 @@ function removeHeader($header) {
function getURL() {
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
View
53 control/RequestHandler.php
@@ -338,6 +338,59 @@ public function httpError($errorCode, $errorMessage = null) {
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.
View
2  forms/gridfield/GridFieldDetailForm.php
@@ -196,7 +196,7 @@ function edit($request) {
'ItemEditForm' => $form,
))->renderWith($this->template);
- if($controller->isAjax()) {
+ if($request->isAjax()) {
return $return;
} else {
// If not requested by ajax, we need to render it within the controller context+template
View
12 tests/control/HTTPRequestTest.php
@@ -230,4 +230,16 @@ public function testRequestVars() {
'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());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.